rubocop-minitest 0.6.2 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +0 -3
  3. data/.gitattributes +1 -0
  4. data/.rubocop.yml +2 -1
  5. data/.rubocop_todo.yml +0 -7
  6. data/CHANGELOG.md +65 -0
  7. data/Gemfile +1 -1
  8. data/README.md +5 -1
  9. data/Rakefile +29 -0
  10. data/config/default.yml +103 -18
  11. data/docs/antora.yml +7 -0
  12. data/docs/modules/ROOT/nav.adoc +6 -0
  13. data/docs/modules/ROOT/pages/cops.adoc +37 -0
  14. data/docs/modules/ROOT/pages/cops_minitest.adoc +1014 -0
  15. data/docs/modules/ROOT/pages/index.adoc +5 -0
  16. data/docs/modules/ROOT/pages/installation.adoc +15 -0
  17. data/docs/modules/ROOT/pages/usage.adoc +32 -0
  18. data/{manual → legacy-docs}/cops.md +1 -0
  19. data/{manual → legacy-docs}/cops_minitest.md +64 -41
  20. data/{manual → legacy-docs}/index.md +0 -0
  21. data/{manual → legacy-docs}/installation.md +0 -0
  22. data/{manual → legacy-docs}/usage.md +0 -0
  23. data/lib/rubocop/cop/generator.rb +56 -0
  24. data/lib/rubocop/cop/minitest/assert_empty.rb +4 -30
  25. data/lib/rubocop/cop/minitest/assert_empty_literal.rb +15 -0
  26. data/lib/rubocop/cop/minitest/assert_equal.rb +2 -31
  27. data/lib/rubocop/cop/minitest/assert_in_delta.rb +27 -0
  28. data/lib/rubocop/cop/minitest/assert_includes.rb +4 -4
  29. data/lib/rubocop/cop/minitest/assert_instance_of.rb +4 -38
  30. data/lib/rubocop/cop/minitest/assert_kind_of.rb +25 -0
  31. data/lib/rubocop/cop/minitest/assert_match.rb +4 -39
  32. data/lib/rubocop/cop/minitest/assert_nil.rb +2 -2
  33. data/lib/rubocop/cop/minitest/assert_output.rb +49 -0
  34. data/lib/rubocop/cop/minitest/assert_path_exists.rb +58 -0
  35. data/lib/rubocop/cop/minitest/assert_respond_to.rb +10 -45
  36. data/lib/rubocop/cop/minitest/assert_silent.rb +45 -0
  37. data/lib/rubocop/cop/minitest/assert_truthy.rb +2 -2
  38. data/lib/rubocop/cop/minitest/assertion_in_lifecycle_hook.rb +43 -0
  39. data/lib/rubocop/cop/minitest/global_expectations.rb +95 -0
  40. data/lib/rubocop/cop/minitest/literal_as_actual_argument.rb +52 -0
  41. data/lib/rubocop/cop/minitest/multiple_assertions.rb +63 -0
  42. data/lib/rubocop/cop/minitest/refute_empty.rb +4 -30
  43. data/lib/rubocop/cop/minitest/refute_false.rb +3 -3
  44. data/lib/rubocop/cop/minitest/refute_in_delta.rb +27 -0
  45. data/lib/rubocop/cop/minitest/refute_includes.rb +4 -4
  46. data/lib/rubocop/cop/minitest/refute_instance_of.rb +4 -38
  47. data/lib/rubocop/cop/minitest/refute_kind_of.rb +25 -0
  48. data/lib/rubocop/cop/minitest/refute_match.rb +4 -39
  49. data/lib/rubocop/cop/minitest/refute_nil.rb +2 -2
  50. data/lib/rubocop/cop/minitest/refute_path_exists.rb +58 -0
  51. data/lib/rubocop/cop/minitest/refute_respond_to.rb +10 -45
  52. data/lib/rubocop/cop/minitest/test_method_name.rb +70 -0
  53. data/lib/rubocop/cop/minitest/unspecified_exception.rb +36 -0
  54. data/lib/rubocop/cop/minitest_cops.rb +17 -1
  55. data/lib/rubocop/cop/mixin/argument_range_helper.rb +10 -0
  56. data/lib/rubocop/cop/mixin/in_delta_mixin.rb +50 -0
  57. data/lib/rubocop/cop/mixin/minitest_cop_rule.rb +102 -0
  58. data/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +84 -0
  59. data/lib/rubocop/minitest/version.rb +1 -1
  60. data/mkdocs.yml +2 -2
  61. data/relnotes/v0.10.0.md +21 -0
  62. data/relnotes/v0.6.2.md +5 -0
  63. data/relnotes/v0.7.0.md +13 -0
  64. data/relnotes/v0.8.0.md +12 -0
  65. data/relnotes/v0.8.1.md +5 -0
  66. data/relnotes/v0.9.0.md +10 -0
  67. data/rubocop-minitest.gemspec +4 -4
  68. data/tasks/cops_documentation.rake +83 -52
  69. data/tasks/cut_release.rake +16 -0
  70. metadata +45 -15
  71. data/lib/rubocop/cop/mixin/includes_cop_rule.rb +0 -78
@@ -0,0 +1,5 @@
1
+ = RuboCop Minitest
2
+
3
+ A https://github.com/rubocop-hq/rubocop[RuboCop] extension focused on enforcing Minitest best practices and coding conventions.
4
+
5
+ It's based on the community-driven https://minitest.rubystyle.guide[Minitest style guide].
@@ -0,0 +1,15 @@
1
+ = Installation
2
+
3
+ Just install the `rubocop-minitest` gem
4
+
5
+ [source,sh]
6
+ ----
7
+ gem install rubocop-minitest
8
+ ----
9
+
10
+ or if you use bundler put this in your `Gemfile`
11
+
12
+ [source,ruby]
13
+ ----
14
+ gem 'rubocop-minitest'
15
+ ----
@@ -0,0 +1,32 @@
1
+ = Usage
2
+
3
+ You need to tell RuboCop to load the Minitest extension. There are three
4
+ ways to do this:
5
+
6
+ == RuboCop configuration file
7
+
8
+ Put this into your `.rubocop.yml`.
9
+
10
+ [source,yaml]
11
+ ----
12
+ require: rubocop-minitest
13
+ ----
14
+
15
+ Now you can run `rubocop` and it will automatically load the RuboCop Minitest
16
+ cops together with the standard cops.
17
+
18
+ == Command line
19
+
20
+ [source,sh]
21
+ ----
22
+ rubocop --require rubocop-minitest
23
+ ----
24
+
25
+ == Rake task
26
+
27
+ [source,ruby]
28
+ ----
29
+ RuboCop::RakeTask.new do |task|
30
+ task.requires << 'rubocop-minitest'
31
+ end
32
+ ----
@@ -10,6 +10,7 @@
10
10
  * [Minitest/AssertNil](cops_minitest.md#minitestassertnil)
11
11
  * [Minitest/AssertRespondTo](cops_minitest.md#minitestassertrespondto)
12
12
  * [Minitest/AssertTruthy](cops_minitest.md#minitestasserttruthy)
13
+ * [Minitest/GlobalExpectations](cops_minitest.md#minitestglobalexpectations)
13
14
  * [Minitest/RefuteEmpty](cops_minitest.md#minitestrefuteempty)
14
15
  * [Minitest/RefuteEqual](cops_minitest.md#minitestrefuteequal)
15
16
  * [Minitest/RefuteFalse](cops_minitest.md#minitestrefutefalse)
@@ -14,11 +14,11 @@ instead of using `assert(object.empty?)`.
14
14
  ```ruby
15
15
  # bad
16
16
  assert(object.empty?)
17
- assert(object.empty?, 'the message')
17
+ assert(object.empty?, 'message')
18
18
 
19
19
  # good
20
20
  assert_empty(object)
21
- assert_empty(object, 'the message')
21
+ assert_empty(object, 'message')
22
22
  ```
23
23
 
24
24
  ### References
@@ -82,11 +82,11 @@ instead of using `assert(collection.include?(object))`.
82
82
  ```ruby
83
83
  # bad
84
84
  assert(collection.include?(object))
85
- assert(collection.include?(object), 'the message')
85
+ assert(collection.include?(object), 'message')
86
86
 
87
87
  # good
88
88
  assert_includes(collection, object)
89
- assert_includes(collection, object, 'the message')
89
+ assert_includes(collection, object, 'message')
90
90
  ```
91
91
 
92
92
  ### References
@@ -107,11 +107,11 @@ over `assert(object.instance_of?(Class))`.
107
107
  ```ruby
108
108
  # bad
109
109
  assert(object.instance_of?(Class))
110
- assert(object.instance_of?(Class), 'the message')
110
+ assert(object.instance_of?(Class), 'message')
111
111
 
112
112
  # good
113
113
  assert_instance_of(Class, object)
114
- assert_instance_of(Class, object, 'the message')
114
+ assert_instance_of(Class, object, 'message')
115
115
  ```
116
116
 
117
117
  ### References
@@ -132,11 +132,11 @@ instead of using `assert(matcher.match(string))`.
132
132
  ```ruby
133
133
  # bad
134
134
  assert(matcher.match(string))
135
- assert(matcher.match(string), 'the message')
135
+ assert(matcher.match(string), 'message')
136
136
 
137
137
  # good
138
138
  assert_match(regex, string)
139
- assert_match(matcher, string, 'the message')
139
+ assert_match(matcher, string, 'message')
140
140
  ```
141
141
 
142
142
  ### References
@@ -157,11 +157,11 @@ instead of using `assert_equal(nil, something)`.
157
157
  ```ruby
158
158
  # bad
159
159
  assert_equal(nil, actual)
160
- assert_equal(nil, actual, 'the message')
160
+ assert_equal(nil, actual, 'message')
161
161
 
162
162
  # good
163
163
  assert_nil(actual)
164
- assert_nil(actual, 'the message')
164
+ assert_nil(actual, 'message')
165
165
  ```
166
166
 
167
167
  ### References
@@ -174,21 +174,21 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChan
174
174
  --- | --- | --- | --- | ---
175
175
  Enabled | Yes | Yes | 0.3 | -
176
176
 
177
- This cop enforces the use of `assert_respond_to(object, :some_method)`
178
- over `assert(object.respond_to?(:some_method))`.
177
+ This cop enforces the use of `assert_respond_to(object, :do_something)`
178
+ over `assert(object.respond_to?(:do_something))`.
179
179
 
180
180
  ### Examples
181
181
 
182
182
  ```ruby
183
183
  # bad
184
- assert(object.respond_to?(:some_method))
185
- assert(object.respond_to?(:some_method), 'the message')
186
- assert(respond_to?(:some_method))
184
+ assert(object.respond_to?(:do_something))
185
+ assert(object.respond_to?(:do_something), 'message')
186
+ assert(respond_to?(:do_something))
187
187
 
188
188
  # good
189
- assert_respond_to(object, :some_method)
190
- assert_respond_to(object, :some_method, 'the message')
191
- assert_respond_to(self, some_method)
189
+ assert_respond_to(object, :do_something)
190
+ assert_respond_to(object, :do_something, 'message')
191
+ assert_respond_to(self, :do_something)
192
192
  ```
193
193
 
194
194
  ### References
@@ -209,17 +209,40 @@ instead of using `assert_equal(true, actual)`.
209
209
  ```ruby
210
210
  # bad
211
211
  assert_equal(true, actual)
212
- assert_equal(true, actual, 'the message')
212
+ assert_equal(true, actual, 'message')
213
213
 
214
214
  # good
215
215
  assert(actual)
216
- assert(actual, 'the message')
216
+ assert(actual, 'message')
217
217
  ```
218
218
 
219
219
  ### References
220
220
 
221
221
  * [https://github.com/rubocop-hq/minitest-style-guide#assert-truthy](https://github.com/rubocop-hq/minitest-style-guide#assert-truthy)
222
222
 
223
+ ## Minitest/GlobalExpectations
224
+
225
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
226
+ --- | --- | --- | --- | ---
227
+ Enabled | Yes | Yes | 0.7 | -
228
+
229
+ This cop checks for deprecated global expectations
230
+ and autocorrects them to use expect format.
231
+
232
+ ### Examples
233
+
234
+ ```ruby
235
+ # bad
236
+ musts.must_equal expected_musts
237
+ wonts.wont_match expected_wonts
238
+ musts.must_raise TypeError
239
+
240
+ # good
241
+ _(musts).must_equal expected_musts
242
+ _(wonts).wont_match expected_wonts
243
+ _ { musts }.must_raise TypeError
244
+ ```
245
+
223
246
  ## Minitest/RefuteEmpty
224
247
 
225
248
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
@@ -234,11 +257,11 @@ using `refute(object.empty?)`.
234
257
  ```ruby
235
258
  # bad
236
259
  refute(object.empty?)
237
- refute(object.empty?, 'the message')
260
+ refute(object.empty?, 'message')
238
261
 
239
262
  # good
240
263
  refute_empty(object)
241
- refute_empty(object, 'the message')
264
+ refute_empty(object, 'message')
242
265
  ```
243
266
 
244
267
  ### References
@@ -283,14 +306,14 @@ over `assert_equal(false, object)`.
283
306
  ```ruby
284
307
  # bad
285
308
  assert_equal(false, actual)
286
- assert_equal(false, actual, 'the message')
309
+ assert_equal(false, actual, 'message')
287
310
 
288
311
  assert(!test)
289
- assert(!test, 'the message')
312
+ assert(!test, 'message')
290
313
 
291
314
  # good
292
315
  refute(actual)
293
- refute(actual, 'the message')
316
+ refute(actual, 'message')
294
317
  ```
295
318
 
296
319
  ### References
@@ -311,11 +334,11 @@ instead of using `refute(collection.include?(object))`.
311
334
  ```ruby
312
335
  # bad
313
336
  refute(collection.include?(object))
314
- refute(collection.include?(object), 'the message')
337
+ refute(collection.include?(object), 'message')
315
338
 
316
339
  # good
317
340
  refute_includes(collection, object)
318
- refute_includes(collection, object, 'the message')
341
+ refute_includes(collection, object, 'message')
319
342
  ```
320
343
 
321
344
  ### References
@@ -336,11 +359,11 @@ over `refute(object.instance_of?(Class))`.
336
359
  ```ruby
337
360
  # bad
338
361
  refute(object.instance_of?(Class))
339
- refute(object.instance_of?(Class), 'the message')
362
+ refute(object.instance_of?(Class), 'message')
340
363
 
341
364
  # good
342
365
  refute_instance_of(Class, object)
343
- refute_instance_of(Class, object, 'the message')
366
+ refute_instance_of(Class, object, 'message')
344
367
  ```
345
368
 
346
369
  ### References
@@ -361,11 +384,11 @@ instead of using `refute(matcher.match(string))`.
361
384
  ```ruby
362
385
  # bad
363
386
  refute(matcher.match(string))
364
- refute(matcher.match(string), 'the message')
387
+ refute(matcher.match(string), 'message')
365
388
 
366
389
  # good
367
390
  refute_match(matcher, string)
368
- refute_match(matcher, string, 'the message')
391
+ refute_match(matcher, string, 'message')
369
392
  ```
370
393
 
371
394
  ### References
@@ -386,11 +409,11 @@ instead of using `refute_equal(nil, something)`.
386
409
  ```ruby
387
410
  # bad
388
411
  refute_equal(nil, actual)
389
- refute_equal(nil, actual, 'the message')
412
+ refute_equal(nil, actual, 'message')
390
413
 
391
414
  # good
392
415
  refute_nil(actual)
393
- refute_nil(actual, 'the message')
416
+ refute_nil(actual, 'message')
394
417
  ```
395
418
 
396
419
  ### References
@@ -403,21 +426,21 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChan
403
426
  --- | --- | --- | --- | ---
404
427
  Enabled | Yes | Yes | 0.4 | -
405
428
 
406
- This cop enforces the test to use `refute_respond_to(object, :some_method)`
407
- over `refute(object.respond_to?(:some_method))`.
429
+ This cop enforces the test to use `refute_respond_to(object, :do_something)`
430
+ over `refute(object.respond_to?(:do_something))`.
408
431
 
409
432
  ### Examples
410
433
 
411
434
  ```ruby
412
435
  # bad
413
- refute(object.respond_to?(:some_method))
414
- refute(object.respond_to?(:some_method), 'the message')
415
- refute(respond_to?(:some_method))
436
+ refute(object.respond_to?(:do_something))
437
+ refute(object.respond_to?(:do_something), 'message')
438
+ refute(respond_to?(:do_something))
416
439
 
417
440
  # good
418
- refute_respond_to(object, :some_method)
419
- refute_respond_to(object, :some_method, 'the message')
420
- refute_respond_to(self, :some_method)
441
+ refute_respond_to(object, :do_something)
442
+ refute_respond_to(object, :do_something, 'message')
443
+ refute_respond_to(self, :do_something)
421
444
  ```
422
445
 
423
446
  ### References
File without changes
File without changes
File without changes
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Source and test generator for new cops
6
+ #
7
+ # This generator will take a cop name and generate a source file
8
+ # and test file when given a valid qualified cop name.
9
+ class Generator
10
+ TEST_TEMPLATE = <<~TEST
11
+ # frozen_string_literal: true
12
+
13
+ require 'test_helper'
14
+
15
+ class %<cop_name>sTest < Minitest::Test
16
+ def test_registers_offense_when_using_bad_method
17
+ assert_offense(<<~RUBY)
18
+ bad_method
19
+ ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`.
20
+ RUBY
21
+
22
+ assert_correction(<<~RUBY)
23
+ good_method
24
+ RUBY
25
+ end
26
+
27
+ def test_does_not_register_offense_when_using_good_method
28
+ assert_no_offenses(<<~RUBY)
29
+ good_method
30
+ RUBY
31
+ end
32
+ end
33
+ TEST
34
+
35
+ def write_test
36
+ write_unless_file_exists(test_path, generated_test)
37
+ end
38
+
39
+ private
40
+
41
+ def test_path
42
+ File.join(
43
+ 'test',
44
+ 'rubocop',
45
+ 'cop',
46
+ 'minitest',
47
+ "#{snake_case(badge.cop_name.to_s)}_test.rb"
48
+ )
49
+ end
50
+
51
+ def generated_test
52
+ generate(TEST_TEMPLATE)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -9,42 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert(object.empty?)
12
- # assert(object.empty?, 'the message')
12
+ # assert(object.empty?, 'message')
13
13
  #
14
14
  # # good
15
15
  # assert_empty(object)
16
- # assert_empty(object, 'the message')
16
+ # assert_empty(object, 'message')
17
17
  #
18
18
  class AssertEmpty < Cop
19
- include ArgumentRangeHelper
19
+ extend MinitestCopRule
20
20
 
21
- MSG = 'Prefer using `assert_empty(%<arguments>s)` over ' \
22
- '`assert(%<receiver>s)`.'
23
-
24
- def_node_matcher :assert_with_empty, <<~PATTERN
25
- (send nil? :assert $(send $_ :empty?) $...)
26
- PATTERN
27
-
28
- def on_send(node)
29
- assert_with_empty(node) do |first_receiver_arg, actual, rest_receiver_arg|
30
- message = rest_receiver_arg.first
31
-
32
- arguments = [actual.source, message&.source].compact.join(', ')
33
- receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
34
-
35
- offense_message = format(MSG, arguments: arguments, receiver: receiver)
36
- add_offense(node, message: offense_message)
37
- end
38
- end
39
-
40
- def autocorrect(node)
41
- lambda do |corrector|
42
- assert_with_empty(node) do |_, actual_arg|
43
- corrector.replace(node.loc.selector, 'assert_empty')
44
- corrector.replace(first_argument_range(node), actual_arg.source)
45
- end
46
- end
47
- end
21
+ define_rule :assert, target_method: :empty?
48
22
  end
49
23
  end
50
24
  end
@@ -15,6 +15,8 @@ module RuboCop
15
15
  # assert_empty(object)
16
16
  #
17
17
  class AssertEmptyLiteral < Cop
18
+ include ArgumentRangeHelper
19
+
18
20
  MSG = 'Prefer using `assert_empty(%<arguments>s)` over ' \
19
21
  '`assert(%<literal>s, %<arguments>s)`.'
20
22
 
@@ -24,12 +26,25 @@ module RuboCop
24
26
 
25
27
  def on_send(node)
26
28
  assert_with_empty_literal(node) do |literal, matchers|
29
+ return unless literal.values.empty?
30
+
27
31
  args = matchers.map(&:source).join(', ')
28
32
 
29
33
  message = format(MSG, literal: literal.source, arguments: args)
30
34
  add_offense(node, message: message)
31
35
  end
32
36
  end
37
+
38
+ def autocorrect(node)
39
+ assert_with_empty_literal(node) do |_literal, matchers|
40
+ object = matchers.first
41
+
42
+ lambda do |corrector|
43
+ corrector.replace(node.loc.selector, 'assert_empty')
44
+ corrector.replace(first_and_second_arguments_range(node), object.source)
45
+ end
46
+ end
47
+ end
33
48
  end
34
49
  end
35
50
  end