rubocop-minitest 0.11.0 → 0.14.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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +0 -3
  3. data/.rubocop.yml +14 -1
  4. data/CHANGELOG.md +34 -0
  5. data/Gemfile +1 -1
  6. data/config/default.yml +11 -0
  7. data/docs/antora.yml +1 -1
  8. data/docs/modules/ROOT/pages/cops.adoc +2 -0
  9. data/docs/modules/ROOT/pages/cops_minitest.adoc +68 -1
  10. data/lib/rubocop/cop/minitest/assert_empty.rb +1 -1
  11. data/lib/rubocop/cop/minitest/assert_empty_literal.rb +4 -9
  12. data/lib/rubocop/cop/minitest/assert_equal.rb +1 -1
  13. data/lib/rubocop/cop/minitest/assert_in_delta.rb +2 -1
  14. data/lib/rubocop/cop/minitest/assert_includes.rb +1 -1
  15. data/lib/rubocop/cop/minitest/assert_instance_of.rb +1 -1
  16. data/lib/rubocop/cop/minitest/assert_kind_of.rb +1 -1
  17. data/lib/rubocop/cop/minitest/assert_match.rb +1 -1
  18. data/lib/rubocop/cop/minitest/assert_nil.rb +4 -11
  19. data/lib/rubocop/cop/minitest/assert_output.rb +1 -1
  20. data/lib/rubocop/cop/minitest/assert_path_exists.rb +5 -12
  21. data/lib/rubocop/cop/minitest/assert_respond_to.rb +1 -1
  22. data/lib/rubocop/cop/minitest/assert_silent.rb +8 -6
  23. data/lib/rubocop/cop/minitest/assert_truthy.rb +4 -11
  24. data/lib/rubocop/cop/minitest/assert_with_expected_argument.rb +4 -1
  25. data/lib/rubocop/cop/minitest/assertion_in_lifecycle_hook.rb +1 -1
  26. data/lib/rubocop/cop/minitest/global_expectations.rb +4 -7
  27. data/lib/rubocop/cop/minitest/literal_as_actual_argument.rb +14 -14
  28. data/lib/rubocop/cop/minitest/multiple_assertions.rb +2 -2
  29. data/lib/rubocop/cop/minitest/no_assertions.rb +48 -0
  30. data/lib/rubocop/cop/minitest/refute_empty.rb +1 -1
  31. data/lib/rubocop/cop/minitest/refute_equal.rb +5 -7
  32. data/lib/rubocop/cop/minitest/refute_false.rb +16 -26
  33. data/lib/rubocop/cop/minitest/refute_in_delta.rb +2 -1
  34. data/lib/rubocop/cop/minitest/refute_includes.rb +1 -1
  35. data/lib/rubocop/cop/minitest/refute_instance_of.rb +1 -1
  36. data/lib/rubocop/cop/minitest/refute_kind_of.rb +1 -1
  37. data/lib/rubocop/cop/minitest/refute_match.rb +1 -1
  38. data/lib/rubocop/cop/minitest/refute_nil.rb +3 -8
  39. data/lib/rubocop/cop/minitest/refute_path_exists.rb +5 -12
  40. data/lib/rubocop/cop/minitest/refute_respond_to.rb +1 -1
  41. data/lib/rubocop/cop/minitest/test_method_name.rb +8 -7
  42. data/lib/rubocop/cop/minitest/unreachable_assertion.rb +42 -0
  43. data/lib/rubocop/cop/minitest/unspecified_exception.rb +1 -1
  44. data/lib/rubocop/cop/minitest_cops.rb +2 -0
  45. data/lib/rubocop/cop/mixin/in_delta_mixin.rb +5 -15
  46. data/lib/rubocop/cop/mixin/minitest_cop_rule.rb +11 -12
  47. data/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +14 -4
  48. data/lib/rubocop/minitest/version.rb +1 -1
  49. data/relnotes/v0.11.1.md +5 -0
  50. data/relnotes/v0.12.0.md +10 -0
  51. data/relnotes/v0.12.1.md +5 -0
  52. data/relnotes/v0.13.0.md +5 -0
  53. data/relnotes/v0.14.0.md +5 -0
  54. data/rubocop-minitest.gemspec +1 -1
  55. data/tasks/cops_documentation.rake +11 -14
  56. metadata +12 -5
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop checks for `assert_raises` has an assertion method at
7
+ # the bottom of block because the assertion will be never reached.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # assert_raises FooError do
13
+ # obj.occur_error
14
+ # assert_equal('foo', obj.bar) # Never asserted.
15
+ # end
16
+ #
17
+ # # good
18
+ # assert_raises FooError do
19
+ # obj.occur_error
20
+ # end
21
+ # assert_equal('foo', obj.bar)
22
+ #
23
+ class UnreachableAssertion < Base
24
+ include MinitestExplorationHelpers
25
+
26
+ MSG = 'Unreachable `%<assertion_method>s` detected.'
27
+
28
+ def on_block(node)
29
+ return unless node.method?(:assert_raises) && (body = node.body)
30
+
31
+ last_node = body.begin_type? ? body.children.last : body
32
+ return unless last_node.send_type?
33
+
34
+ method_name = last_node.method_name
35
+ return unless assertion_method?(method_name)
36
+
37
+ add_offense(last_node, message: format(MSG, assertion_method: method_name))
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -14,7 +14,7 @@ module RuboCop
14
14
  # assert_raises(FooException) { raise FooException }
15
15
  # assert_raises(FooException, 'This should have raised') { raise FooException }
16
16
  #
17
- class UnspecifiedException < Cop
17
+ class UnspecifiedException < Base
18
18
  MSG = 'Specify the exception being captured.'
19
19
 
20
20
  def on_block(block_node)
@@ -23,6 +23,7 @@ require_relative 'minitest/assert_truthy'
23
23
  require_relative 'minitest/global_expectations'
24
24
  require_relative 'minitest/literal_as_actual_argument'
25
25
  require_relative 'minitest/multiple_assertions'
26
+ require_relative 'minitest/no_assertions'
26
27
  require_relative 'minitest/refute_empty'
27
28
  require_relative 'minitest/refute_false'
28
29
  require_relative 'minitest/refute_equal'
@@ -35,4 +36,5 @@ require_relative 'minitest/refute_instance_of'
35
36
  require_relative 'minitest/refute_path_exists'
36
37
  require_relative 'minitest/refute_respond_to'
37
38
  require_relative 'minitest/test_method_name'
39
+ require_relative 'minitest/unreachable_assertion'
38
40
  require_relative 'minitest/unspecified_exception'
@@ -9,24 +9,14 @@ module RuboCop
9
9
  def on_send(node)
10
10
  equal_floats_call(node) do |expected, actual, message|
11
11
  message = message.first
12
+ good_method = build_good_method(expected, actual, message)
12
13
 
13
14
  if expected.float_type? || actual.float_type?
14
- message = format(MSG,
15
- good_method: build_good_method(expected, actual, message),
16
- bad_method: node.source)
15
+ message = format(MSG, good_method: good_method, bad_method: node.source)
17
16
 
18
- add_offense(node, message: message)
19
- end
20
- end
21
- end
22
-
23
- def autocorrect(node)
24
- equal_floats_call(node) do |expected, actual, message|
25
- message = message.first
26
- replacement = build_good_method(expected, actual, message)
27
-
28
- lambda do |corrector|
29
- corrector.replace(node, replacement)
17
+ add_offense(node, message: message) do |corrector|
18
+ corrector.replace(node, good_method)
19
+ end
30
20
  end
31
21
  end
32
22
  end
@@ -26,6 +26,7 @@ module RuboCop
26
26
 
27
27
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
28
28
  include ArgumentRangeHelper
29
+ extend AutoCorrector
29
30
 
30
31
  MSG = 'Prefer using `#{preferred_method}(%<new_arguments>s)` over ' \
31
32
  '`#{assertion_method}(%<original_arguments>s)`.'
@@ -36,23 +37,21 @@ module RuboCop
36
37
  return unless (arguments = peel_redundant_parentheses_from(node.arguments))
37
38
  return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:#{target_method})
38
39
 
39
- add_offense(node, message: offense_message(arguments))
40
+ add_offense(node, message: offense_message(arguments)) do |corrector|
41
+ autocorrect(corrector, node, arguments)
42
+ end
40
43
  end
41
44
 
42
- def autocorrect(node)
43
- lambda do |corrector|
44
- corrector.replace(node.loc.selector, '#{preferred_method}')
45
-
46
- arguments = peel_redundant_parentheses_from(node.arguments)
45
+ def autocorrect(corrector, node, arguments)
46
+ corrector.replace(node.loc.selector, '#{preferred_method}')
47
47
 
48
- new_arguments = new_arguments(arguments).join(', ')
48
+ new_arguments = new_arguments(arguments).join(', ')
49
49
 
50
- if enclosed_in_redundant_parentheses?(node)
51
- new_arguments = '(' + new_arguments + ')'
52
- end
53
-
54
- corrector.replace(first_argument_range(node), new_arguments)
50
+ if enclosed_in_redundant_parentheses?(node)
51
+ new_arguments = '(' + new_arguments + ')'
55
52
  end
53
+
54
+ corrector.replace(first_argument_range(node), new_arguments)
56
55
  end
57
56
 
58
57
  private
@@ -19,6 +19,8 @@ module RuboCop
19
19
  refute_respond_to refute_same
20
20
  ].freeze
21
21
 
22
+ FLUNK = 'flunk'
23
+
22
24
  LIFECYCLE_HOOK_METHODS = %i[
23
25
  before_setup
24
26
  setup
@@ -42,8 +44,13 @@ module RuboCop
42
44
  end
43
45
 
44
46
  def test_cases(class_node)
45
- class_def_nodes(class_node)
46
- .select { |def_node| test_case_name?(def_node.method_name) }
47
+ test_cases = class_def_nodes(class_node).select { |def_node| test_case_name?(def_node.method_name) }
48
+
49
+ # Support Active Support's `test 'example' { ... }` method.
50
+ # https://api.rubyonrails.org/classes/ActiveSupport/Testing/Declarative.html
51
+ test_blocks = class_node.each_descendant(:block).select { |block_node| block_node.method?(:test) }
52
+
53
+ test_cases + test_blocks
47
54
  end
48
55
 
49
56
  def lifecycle_hooks(class_node)
@@ -82,11 +89,14 @@ module RuboCop
82
89
 
83
90
  def assertion?(node)
84
91
  node.send_type? &&
85
- ASSERTION_PREFIXES.any? { |prefix| node.method_name.to_s.start_with?(prefix) }
92
+ ASSERTION_PREFIXES.any? do |prefix|
93
+ method_name = node.method_name.to_s
94
+ method_name == FLUNK || method_name.start_with?(prefix)
95
+ end
86
96
  end
87
97
 
88
98
  def assertion_method?(method_name)
89
- ASSERTION_METHODS.include?(method_name)
99
+ method_name == FLUNK || ASSERTION_METHODS.include?(method_name)
90
100
  end
91
101
 
92
102
  def lifecycle_hook_method?(node)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Minitest
5
5
  # This module holds the RuboCop Minitest version information.
6
6
  module Version
7
- STRING = '0.11.0'
7
+ STRING = '0.14.0'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
@@ -0,0 +1,5 @@
1
+ ### Changes
2
+
3
+ * [#126](https://github.com/rubocop/rubocop-minitest/issues/126): Mark `Minitest/AssertWithExpectedArgument` as unsafe. ([@koic][])
4
+
5
+ [@koic]: https://github.com/koic
@@ -0,0 +1,10 @@
1
+ ### New features
2
+
3
+ * [#124](https://github.com/rubocop/rubocop-minitest/pull/124): Add new `Minitest/NoAssertions` cop. ([@ghiculescu][])
4
+
5
+ ### Changes
6
+
7
+ * [#129](https://github.com/rubocop/rubocop-minitest/pull/129): Drop Ruby 2.4 support. ([@koic][])
8
+
9
+ [@ghiculescu]: https://github.com/ghiculescu
10
+ [@koic]: https://github.com/koic
@@ -0,0 +1,5 @@
1
+ ### Bug fixes
2
+
3
+ * [#131](https://github.com/rubocop/rubocop-minitest/issues/131): Fix an error for `Minitest/MultipleAssertions` and fixes a false positive for `test` block. ([@koic][])
4
+
5
+ [@koic]: https://github.com/koic
@@ -0,0 +1,5 @@
1
+ ### New features
2
+
3
+ * [#136](https://github.com/rubocop/rubocop-minitest/pull/136): Support Active Support's `test` method for `Minitest/MultipleAssertions` and `Minitest/NoAssertions` cops. ([@koic][])
4
+
5
+ [@koic]: https://github.com/koic
@@ -0,0 +1,5 @@
1
+ ### New features
2
+
3
+ * [#133](https://github.com/rubocop/rubocop-minitest/issues/133): Add new `Minitest/UnreachableAssertion` cop. ([@koic][])
4
+
5
+ [@koic]: https://github.com/koic
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
  DESCRIPTION
17
17
  spec.license = 'MIT'
18
18
 
19
- spec.required_ruby_version = '>= 2.4.0'
19
+ spec.required_ruby_version = '>= 2.5.0'
20
20
  spec.metadata = {
21
21
  'homepage_uri' => 'https://docs.rubocop.org/rubocop-minitest/',
22
22
  'changelog_uri' => 'https://github.com/rubocop/rubocop-minitest/blob/master/CHANGELOG.md',
@@ -25,15 +25,14 @@ task verify_cops_documentation: :generate_cops_documentation do
25
25
  # Output diff before raising error
26
26
  sh('GIT_PAGER=cat git diff docs')
27
27
 
28
- warn 'The docs directory is out of sync. ' \
29
- 'Run `rake generate_cops_documentation` and commit the results.'
28
+ warn 'The docs directory is out of sync. Run `rake generate_cops_documentation` and commit the results.'
30
29
  exit!
31
30
  end
32
31
  end
33
32
 
34
33
  desc 'Syntax check for the documentation comments'
35
34
  task documentation_syntax_check: :yard_for_generate_documentation do
36
- require 'parser/ruby25'
35
+ require 'parser/ruby30'
37
36
 
38
37
  ok = true
39
38
  YARD::Registry.load!
@@ -46,17 +45,15 @@ task documentation_syntax_check: :yard_for_generate_documentation do
46
45
  end
47
46
 
48
47
  examples.to_a.each do |example|
49
- begin
50
- buffer = Parser::Source::Buffer.new('<code>', 1)
51
- buffer.source = example.text
52
- parser = Parser::Ruby25.new(RuboCop::AST::Builder.new)
53
- parser.diagnostics.all_errors_are_fatal = true
54
- parser.parse(buffer)
55
- rescue Parser::SyntaxError => e
56
- path = example.object.file
57
- puts "#{path}: Syntax Error in an example. #{e}"
58
- ok = false
59
- end
48
+ buffer = Parser::Source::Buffer.new('<code>', 1)
49
+ buffer.source = example.text
50
+ parser = Parser::Ruby30.new(RuboCop::AST::Builder.new)
51
+ parser.diagnostics.all_errors_are_fatal = true
52
+ parser.parse(buffer)
53
+ rescue Parser::SyntaxError => e
54
+ path = example.object.file
55
+ puts "#{path}: Syntax Error in an example. #{e}"
56
+ ok = false
60
57
  end
61
58
  end
62
59
  abort unless ok
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-minitest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2021-03-21 00:00:00.000000000 Z
13
+ date: 2021-07-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -105,6 +105,7 @@ files:
105
105
  - lib/rubocop/cop/minitest/global_expectations.rb
106
106
  - lib/rubocop/cop/minitest/literal_as_actual_argument.rb
107
107
  - lib/rubocop/cop/minitest/multiple_assertions.rb
108
+ - lib/rubocop/cop/minitest/no_assertions.rb
108
109
  - lib/rubocop/cop/minitest/refute_empty.rb
109
110
  - lib/rubocop/cop/minitest/refute_equal.rb
110
111
  - lib/rubocop/cop/minitest/refute_false.rb
@@ -117,6 +118,7 @@ files:
117
118
  - lib/rubocop/cop/minitest/refute_path_exists.rb
118
119
  - lib/rubocop/cop/minitest/refute_respond_to.rb
119
120
  - lib/rubocop/cop/minitest/test_method_name.rb
121
+ - lib/rubocop/cop/minitest/unreachable_assertion.rb
120
122
  - lib/rubocop/cop/minitest/unspecified_exception.rb
121
123
  - lib/rubocop/cop/minitest_cops.rb
122
124
  - lib/rubocop/cop/mixin/argument_range_helper.rb
@@ -134,6 +136,11 @@ files:
134
136
  - relnotes/v0.10.2.md
135
137
  - relnotes/v0.10.3.md
136
138
  - relnotes/v0.11.0.md
139
+ - relnotes/v0.11.1.md
140
+ - relnotes/v0.12.0.md
141
+ - relnotes/v0.12.1.md
142
+ - relnotes/v0.13.0.md
143
+ - relnotes/v0.14.0.md
137
144
  - relnotes/v0.2.0.md
138
145
  - relnotes/v0.2.1.md
139
146
  - relnotes/v0.3.0.md
@@ -158,7 +165,7 @@ metadata:
158
165
  homepage_uri: https://docs.rubocop.org/rubocop-minitest/
159
166
  changelog_uri: https://github.com/rubocop/rubocop-minitest/blob/master/CHANGELOG.md
160
167
  source_code_uri: https://github.com/rubocop/rubocop-minitest
161
- documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.11
168
+ documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.14
162
169
  bug_tracker_uri: https://github.com/rubocop/rubocop-minitest/issues
163
170
  post_install_message:
164
171
  rdoc_options: []
@@ -168,14 +175,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
175
  requirements:
169
176
  - - ">="
170
177
  - !ruby/object:Gem::Version
171
- version: 2.4.0
178
+ version: 2.5.0
172
179
  required_rubygems_version: !ruby/object:Gem::Requirement
173
180
  requirements:
174
181
  - - ">="
175
182
  - !ruby/object:Gem::Version
176
183
  version: '0'
177
184
  requirements: []
178
- rubygems_version: 3.2.13
185
+ rubygems_version: 3.2.18
179
186
  signing_key:
180
187
  specification_version: 4
181
188
  summary: Automatic Minitest code style checking tool.