rubocop-minitest 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler'
4
+ require 'bundler/gem_tasks'
5
+
6
+ Dir['tasks/**/*.rake'].each { |t| load t }
7
+
8
+ begin
9
+ Bundler.setup(:default, :development)
10
+ rescue Bundler::BundlerError => e
11
+ warn e.message
12
+ warn 'Run `bundle install` to install missing gems'
13
+ exit e.status_code
14
+ end
15
+
16
+ require 'rubocop/rake_task'
17
+ require 'rake/testtask'
18
+
19
+ Rake::TestTask.new do |t|
20
+ t.libs << 'test'
21
+ t.libs << 'lib'
22
+ t.test_files = FileList['test/**/*_test.rb']
23
+ end
24
+
25
+ desc 'Run RuboCop over itself'
26
+ RuboCop::RakeTask.new(:internal_investigation).tap do |task|
27
+ if RUBY_ENGINE == 'ruby' &&
28
+ RbConfig::CONFIG['host_os'] !~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
29
+ task.options = %w[--parallel]
30
+ end
31
+ end
32
+
33
+ task default: %i[
34
+ documentation_syntax_check
35
+ generate_cops_documentation
36
+ test
37
+ internal_investigation
38
+ ]
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'rubocop/minitest'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ Minitest:
2
+ Include:
3
+ - '**/test/**/*'
4
+
5
+ Minitest/AssertNil:
6
+ Description: 'Check if your test uses `assert_nil` instead of `assert_equal(nil, something)`.'
7
+ StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#assert-nil'
8
+ Enabled: true
9
+ VersionAdded: '0.1'
10
+
11
+ Minitest/AssertEmpty:
12
+ Description: 'Check if your test uses `assert_empty` instead of `assert(actual.empty?)`.'
13
+ StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#assert-empty'
14
+ Enabled: true
15
+ VersionAdded: '0.2'
16
+
17
+ Minitest/AssertIncludes:
18
+ Description: 'Check if your test uses `assert_includes` instead of `assert(collection.includes?(actual))`.'
19
+ StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#assert-includes'
20
+ Enabled: true
21
+ VersionAdded: '0.2'
22
+
23
+ Minitest/AssertTruthy:
24
+ Description: 'Check if your test uses `assert(actual)` instead of `assert_equal(true, actual)`.'
25
+ StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#assert-truthy'
26
+ Enabled: true
27
+ VersionAdded: '0.2'
28
+
29
+ Minitest/RefuteNil:
30
+ Description: 'Check if your test uses `refute_nil` instead of `refute_equal(nil, something)`.'
31
+ StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#refute-nil'
32
+ Enabled: true
33
+ VersionAdded: '0.2'
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require 'yaml'
5
+
6
+ require 'rubocop'
7
+
8
+ require_relative 'rubocop/minitest'
9
+ require_relative 'rubocop/minitest/version'
10
+ require_relative 'rubocop/minitest/inject'
11
+
12
+ RuboCop::Minitest::Inject.defaults!
13
+
14
+ require_relative 'rubocop/cop/minitest_cops'
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Check if your test uses `assert_empty` instead of `assert(actual.empty?)`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # assert(actual.empty?)
11
+ # assert(actual.empty?, 'the message')
12
+ #
13
+ # # good
14
+ # assert_empty(actual)
15
+ # assert_empty(actual, 'the message')
16
+ #
17
+ class AssertEmpty < Cop
18
+ MSG = 'Prefer using `assert_empty(%<arguments>s)` over ' \
19
+ '`assert(%<receiver>s)`.'
20
+
21
+ def_node_matcher :assert_with_empty, <<~PATTERN
22
+ (send nil? :assert $(send $_ :empty?) $...)
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ assert_with_empty(node) do |first_receiver_arg, actual, rest_receiver_arg|
27
+ message = rest_receiver_arg.first
28
+
29
+ arguments = [actual.source, message&.source].compact.join(', ')
30
+ receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
31
+
32
+ offense_message = format(MSG, arguments: arguments, receiver: receiver)
33
+ add_offense(node, message: offense_message)
34
+ end
35
+ end
36
+
37
+ def autocorrect(node)
38
+ lambda do |corrector|
39
+ assert_with_empty(node) do |_first_receiver_arg, actual, rest_receiver_arg|
40
+ message = rest_receiver_arg.first
41
+
42
+ replacement = [actual.source, message&.source].compact.join(', ')
43
+ corrector.replace(node.loc.expression, "assert_empty(#{replacement})")
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Check if your test uses `assert_includes`
7
+ # instead of `assert(collection.includes?(actual))`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # assert(collection.includes?(actual))
12
+ # assert(collection.includes?(actual), 'the message')
13
+ #
14
+ # # good
15
+ # assert_includes(collection, actual)
16
+ # assert_includes(collection, actual, 'the message')
17
+ #
18
+ class AssertIncludes < Cop
19
+ MSG = 'Prefer using `assert_includes(%<arguments>s)` over ' \
20
+ '`assert(%<receiver>s)`.'
21
+
22
+ def_node_matcher :assert_with_includes, <<~PATTERN
23
+ (send nil? :assert $(send $_ :includes? $_) $...)
24
+ PATTERN
25
+
26
+ def on_send(node)
27
+ assert_with_includes(node) do
28
+ |first_receiver_arg, collection, actual, rest_receiver_arg|
29
+
30
+ message = rest_receiver_arg.first
31
+ arguments = node_arguments(collection, actual, message)
32
+ receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
33
+
34
+ offense_message = format(MSG, arguments: arguments, receiver: receiver)
35
+
36
+ add_offense(node, message: offense_message)
37
+ end
38
+ end
39
+
40
+ def autocorrect(node)
41
+ lambda do |corrector|
42
+ assert_with_includes(node) do
43
+ |_receiver, collection, actual, rest_receiver_arg|
44
+
45
+ message = rest_receiver_arg.first
46
+ replacement = node_arguments(collection, actual, message)
47
+ corrector.replace(node.loc.expression, "assert_includes(#{replacement})")
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def node_arguments(collection, actual, message)
55
+ [collection.source, actual.source, message&.source].compact.join(', ')
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Check if your test uses `assert_nil` instead of `assert_equal(nil, something)`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # assert_equal(nil, actual)
11
+ # assert_equal(nil, actual, 'the message')
12
+ #
13
+ # # good
14
+ # assert_nil(actual)
15
+ # assert_nil(actual, 'the message')
16
+ #
17
+ class AssertNil < Cop
18
+ MSG = 'Prefer using `assert_nil(%<arguments>s)` over ' \
19
+ '`assert_equal(nil, %<arguments>s)`.'
20
+
21
+ def_node_matcher :assert_equal_with_nil, <<~PATTERN
22
+ (send nil? :assert_equal nil $_ $...)
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ assert_equal_with_nil(node) do |actual, message|
27
+ message = message.first
28
+
29
+ arguments = [actual.source, message&.source].compact.join(', ')
30
+
31
+ add_offense(node, message: format(MSG, arguments: arguments))
32
+ end
33
+ end
34
+
35
+ def autocorrect(node)
36
+ lambda do |corrector|
37
+ arguments = node.arguments.reject(&:nil_type?)
38
+ replacement = arguments.map(&:source).join(', ')
39
+ corrector.replace(node.loc.expression, "assert_nil(#{replacement})")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Check if your test uses `assert(actual)`
7
+ # instead of `assert_equal(true, actual)`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # assert_equal(true, actual)
12
+ # assert_equal(true, actual, 'the message')
13
+ #
14
+ # # good
15
+ # assert(actual)
16
+ # assert(actual, 'the message')
17
+ #
18
+ class AssertTruthy < Cop
19
+ MSG = 'Prefer using `assert(%<arguments>s)` over ' \
20
+ '`assert_equal(true, %<arguments>s)`.'
21
+
22
+ def_node_matcher :assert_equal_with_truthy, <<~PATTERN
23
+ (send nil? :assert_equal true $_ $...)
24
+ PATTERN
25
+
26
+ def on_send(node)
27
+ assert_equal_with_truthy(node) do |actual, rest_receiver_arg|
28
+ message = rest_receiver_arg.first
29
+
30
+ arguments = [actual.source, message&.source].compact.join(', ')
31
+
32
+ add_offense(node, message: format(MSG, arguments: arguments))
33
+ end
34
+ end
35
+
36
+ def autocorrect(node)
37
+ lambda do |corrector|
38
+ arguments = node.arguments.reject(&:true_type?)
39
+ replacement = arguments.map(&:source).join(', ')
40
+ corrector.replace(node.loc.expression, "assert(#{replacement})")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Check if your test uses `refute_nil` instead of `refute_equal(nil, something)`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # refute_equal(nil, actual)
11
+ # refute_equal(nil, actual, 'the message')
12
+ #
13
+ # # good
14
+ # refute_nil(actual)
15
+ # refute_nil(actual, 'the message')
16
+ #
17
+ class RefuteNil < Cop
18
+ MSG = 'Prefer using `refute_nil(%<arguments>s)` over ' \
19
+ '`refute_equal(nil, %<arguments>s)`.'
20
+
21
+ def_node_matcher :refute_equal_with_nil, <<~PATTERN
22
+ (send nil? :refute_equal nil $_ $...)
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ refute_equal_with_nil(node) do |actual, message|
27
+ message = message.first
28
+
29
+ arguments = [actual.source, message&.source].compact.join(', ')
30
+
31
+ add_offense(node, message: format(MSG, arguments: arguments))
32
+ end
33
+ end
34
+
35
+ def autocorrect(node)
36
+ lambda do |corrector|
37
+ arguments = node.arguments.reject(&:nil_type?)
38
+ replacement = arguments.map(&:source).join(', ')
39
+ corrector.replace(node.loc.expression, "refute_nil(#{replacement})")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'minitest/assert_empty'
4
+ require_relative 'minitest/assert_nil'
5
+ require_relative 'minitest/assert_includes'
6
+ require_relative 'minitest/assert_truthy'
7
+ require_relative 'minitest/refute_nil'
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # RuboCop minitest project namespace
5
+ module Minitest
6
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
7
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
8
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
9
+
10
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Minitest
5
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
6
+ # bit of our configuration.
7
+ module Inject
8
+ def self.defaults!
9
+ path = CONFIG_DEFAULT.to_s
10
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
11
+ config = Config.new(hash, path)
12
+ puts "configuration from #{path}" if ConfigLoader.debug?
13
+ config = ConfigLoader.merge_with_default(config, path)
14
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Minitest
5
+ VERSION = '0.2.1'
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ <!-- START_COP_LIST -->
2
+ #### Department [Minitest](cops_minitest.md)
3
+
4
+ * [Minitest/AssertEmpty](cops_minitest.md#minitestassertempty)
5
+ * [Minitest/AssertIncludes](cops_minitest.md#minitestassertincludes)
6
+ * [Minitest/AssertNil](cops_minitest.md#minitestassertnil)
7
+ * [Minitest/AssertTruthy](cops_minitest.md#minitestasserttruthy)
8
+ * [Minitest/RefuteNil](cops_minitest.md#minitestrefutenil)
9
+
10
+ <!-- END_COP_LIST -->
@@ -0,0 +1,123 @@
1
+ # Minitest
2
+
3
+ ## Minitest/AssertEmpty
4
+
5
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
6
+ --- | --- | --- | --- | ---
7
+ Enabled | Yes | Yes | 0.2 | -
8
+
9
+ Check if your test uses `assert_empty` instead of `assert(actual.empty?)`.
10
+
11
+ ### Examples
12
+
13
+ ```ruby
14
+ # bad
15
+ assert(actual.empty?)
16
+ assert(actual.empty?, 'the message')
17
+
18
+ # good
19
+ assert_empty(actual)
20
+ assert_empty(actual, 'the message')
21
+ ```
22
+
23
+ ### References
24
+
25
+ * [https://github.com/rubocop-hq/minitest-style-guide#assert-empty](https://github.com/rubocop-hq/minitest-style-guide#assert-empty)
26
+
27
+ ## Minitest/AssertIncludes
28
+
29
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
30
+ --- | --- | --- | --- | ---
31
+ Enabled | Yes | Yes | 0.2 | -
32
+
33
+ Check if your test uses `assert_includes`
34
+ instead of `assert(collection.includes?(actual))`.
35
+
36
+ ### Examples
37
+
38
+ ```ruby
39
+ # bad
40
+ assert(collection.includes?(actual))
41
+ assert(collection.includes?(actual), 'the message')
42
+
43
+ # good
44
+ assert_includes(collection, actual)
45
+ assert_includes(collection, actual, 'the message')
46
+ ```
47
+
48
+ ### References
49
+
50
+ * [https://github.com/rubocop-hq/minitest-style-guide#assert-includes](https://github.com/rubocop-hq/minitest-style-guide#assert-includes)
51
+
52
+ ## Minitest/AssertNil
53
+
54
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
55
+ --- | --- | --- | --- | ---
56
+ Enabled | Yes | Yes | 0.1 | -
57
+
58
+ Check if your test uses `assert_nil` instead of `assert_equal(nil, something)`.
59
+
60
+ ### Examples
61
+
62
+ ```ruby
63
+ # bad
64
+ assert_equal(nil, actual)
65
+ assert_equal(nil, actual, 'the message')
66
+
67
+ # good
68
+ assert_nil(actual)
69
+ assert_nil(actual, 'the message')
70
+ ```
71
+
72
+ ### References
73
+
74
+ * [https://github.com/rubocop-hq/minitest-style-guide#assert-nil](https://github.com/rubocop-hq/minitest-style-guide#assert-nil)
75
+
76
+ ## Minitest/AssertTruthy
77
+
78
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
79
+ --- | --- | --- | --- | ---
80
+ Enabled | Yes | Yes | 0.2 | -
81
+
82
+ Check if your test uses `assert(actual)`
83
+ instead of `assert_equal(true, actual)`.
84
+
85
+ ### Examples
86
+
87
+ ```ruby
88
+ # bad
89
+ assert_equal(true, actual)
90
+ assert_equal(true, actual, 'the message')
91
+
92
+ # good
93
+ assert(actual)
94
+ assert(actual, 'the message')
95
+ ```
96
+
97
+ ### References
98
+
99
+ * [https://github.com/rubocop-hq/minitest-style-guide#assert-truthy](https://github.com/rubocop-hq/minitest-style-guide#assert-truthy)
100
+
101
+ ## Minitest/RefuteNil
102
+
103
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
104
+ --- | --- | --- | --- | ---
105
+ Enabled | Yes | Yes | 0.2 | -
106
+
107
+ Check if your test uses `refute_nil` instead of `refute_equal(nil, something)`.
108
+
109
+ ### Examples
110
+
111
+ ```ruby
112
+ # bad
113
+ refute_equal(nil, actual)
114
+ refute_equal(nil, actual, 'the message')
115
+
116
+ # good
117
+ refute_nil(actual)
118
+ refute_nil(actual, 'the message')
119
+ ```
120
+
121
+ ### References
122
+
123
+ * [https://github.com/rubocop-hq/minitest-style-guide#refute-nil](https://github.com/rubocop-hq/minitest-style-guide#refute-nil)