rubocop-minitest 0.28.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4d321b9b8d16e6c5c3cf304835e7c48fde65c173202925d3e072c779c90bb86
4
- data.tar.gz: 40856b7d83975172736776deae22bb7f62d5d9b913674bb354a6c7cbf7011882
3
+ metadata.gz: 04d404efc9a07a82ed435c474b1c4370afa1d36e92f11ec1176911dbe677577c
4
+ data.tar.gz: '079e6fe08096eae97e74790ffda49a120fd5d1b06d15968666d83e046ec821a7'
5
5
  SHA512:
6
- metadata.gz: 54a62f0f9510ef6d00dedccc0f59672446f70fea23d12e63cab8915423932aff0921fde8b52e44ce6b93543a0c43a9726867aeb50a84fae1d8e79a3e0ec40f38
7
- data.tar.gz: 45a933d110a001cb8ada63094fadfb918c814b85c2d68154fc7f1180488f9937a5858a8db2abca0ba7a80323be9ea11e5940df21736ce69166aaf2947388c276
6
+ metadata.gz: adf9211bbf6d285987b8bea806bafd15961a8307ea8f6baec4b7ab8d248a571782883ba619bb3542fb440a7ed218daf73b8166e09c33537b52042bcd306aa81e
7
+ data.tar.gz: 94b7021203ca9992df0278ca2d753a7e6c03a54b8a5f49ca3231caf7b8f9b1dd40db791d3504f08bce49bee9e84e1a658cf741785ad74daa03df661dd68ac704
data/README.md CHANGED
@@ -10,8 +10,8 @@ The library is based on the guidelines outlined in the community [Minitest Style
10
10
 
11
11
  Just install the `rubocop-minitest` gem
12
12
 
13
- ```bash
14
- gem install rubocop-minitest
13
+ ```sh
14
+ $ gem install rubocop-minitest
15
15
  ```
16
16
 
17
17
  or if you use bundler put this in your `Gemfile`
@@ -46,8 +46,8 @@ cops together with the standard cops.
46
46
 
47
47
  ### Command line
48
48
 
49
- ```bash
50
- rubocop --require rubocop-minitest
49
+ ```sh
50
+ $ rubocop --require rubocop-minitest
51
51
  ```
52
52
 
53
53
  ### Rake task
data/config/default.yml CHANGED
@@ -181,6 +181,11 @@ Minitest/NoAssertions:
181
181
  Enabled: false
182
182
  VersionAdded: '0.12'
183
183
 
184
+ Minitest/NoTestCases:
185
+ Description: 'Checks if test class contains any test cases.'
186
+ Enabled: false
187
+ VersionAdded: '0.30'
188
+
184
189
  Minitest/NonPublicTestMethod:
185
190
  Description: 'Detects non `public` (marked as `private` or `protected`) test methods.'
186
191
  Enabled: pending
@@ -11,14 +11,30 @@ module RuboCop
11
11
  # assert(object.instance_of?(Class))
12
12
  # assert(object.instance_of?(Class), 'message')
13
13
  #
14
+ # # bad
15
+ # assert_equal(Class, object.class)
16
+ # assert_equal(Class, object.class, 'message')
17
+ #
14
18
  # # good
15
19
  # assert_instance_of(Class, object)
16
20
  # assert_instance_of(Class, object, 'message')
17
21
  #
18
22
  class AssertInstanceOf < Base
19
- extend MinitestCopRule
23
+ include InstanceOfAssertionHandleable
24
+ extend AutoCorrector
25
+
26
+ RESTRICT_ON_SEND = %i[assert assert_equal].freeze
27
+
28
+ def_node_matcher :instance_of_assertion?, <<~PATTERN
29
+ {
30
+ (send nil? :assert (send $_ :instance_of? $const) $_?)
31
+ (send nil? :assert_equal $const (send $_ :class) $_?)
32
+ }
33
+ PATTERN
20
34
 
21
- define_rule :assert, target_method: :instance_of?, inverse: true
35
+ def on_send(node)
36
+ investigate(node, :assert)
37
+ end
22
38
  end
23
39
  end
24
40
  end
@@ -36,8 +36,7 @@ module RuboCop
36
36
  private
37
37
 
38
38
  def find_test_case(node)
39
- def_ancestor = node.each_ancestor(:def).first
40
- def_ancestor if test_case?(def_ancestor)
39
+ node.each_ancestor.find { |ancestor| test_case?(ancestor) }
41
40
  end
42
41
 
43
42
  def references_gvar?(assertion, gvar_name)
@@ -96,7 +96,9 @@ module RuboCop
96
96
  wont_be_kind_of wont_match wont_be_nil wont_be wont_respond_to wont_be_same_as
97
97
  ].freeze
98
98
 
99
- BLOCK_MATCHERS = %i[must_output must_raise must_be_silent must_throw].freeze
99
+ BLOCK_MATCHERS = %i[
100
+ must_output must_pattern_match must_raise must_be_silent must_throw wont_pattern_match
101
+ ].freeze
100
102
 
101
103
  RESTRICT_ON_SEND = VALUE_MATCHERS + BLOCK_MATCHERS
102
104
 
@@ -32,7 +32,7 @@ module RuboCop
32
32
 
33
33
  next if assertions_count.positive?
34
34
 
35
- add_offense(node.block_type? ? node.loc.expression : node.loc.name)
35
+ add_offense(node.block_type? ? node.source_range : node.loc.name)
36
36
  end
37
37
  end
38
38
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Checks if test class contains any test cases.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # class FooTest < Minitest::Test
11
+ # def do_something
12
+ # end
13
+ # end
14
+ #
15
+ # # good
16
+ # class FooTest < Minitest::Test
17
+ # def test_something
18
+ # assert true
19
+ # end
20
+ # end
21
+ #
22
+ class NoTestCases < Base
23
+ include MinitestExplorationHelpers
24
+
25
+ MSG = 'Test class should have test cases.'
26
+
27
+ def on_class(node)
28
+ return unless test_class?(node)
29
+
30
+ add_offense(node) if test_cases(node).empty?
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -36,7 +36,7 @@ module RuboCop
36
36
  corrector.replace(node.loc.selector, 'refute_equal')
37
37
 
38
38
  replacement = [expected, actual].map(&:source).join(', ')
39
- corrector.replace(first_argument_range(node), replacement)
39
+ corrector.replace(node.first_argument, replacement)
40
40
  end
41
41
  end
42
42
  end
@@ -66,7 +66,7 @@ module RuboCop
66
66
  if node.method?(:assert_equal)
67
67
  corrector.replace(first_and_second_arguments_range(node), actual.source)
68
68
  else
69
- corrector.replace(first_argument_range(node), actual.source)
69
+ corrector.replace(node.first_argument, actual.source)
70
70
  end
71
71
  end
72
72
  end
@@ -11,14 +11,30 @@ module RuboCop
11
11
  # refute(object.instance_of?(Class))
12
12
  # refute(object.instance_of?(Class), 'message')
13
13
  #
14
+ # # bad
15
+ # refute_equal(Class, object.class)
16
+ # refute_equal(Class, object.class, 'message')
17
+ #
14
18
  # # good
15
19
  # refute_instance_of(Class, object)
16
20
  # refute_instance_of(Class, object, 'message')
17
21
  #
18
22
  class RefuteInstanceOf < Base
19
- extend MinitestCopRule
23
+ include InstanceOfAssertionHandleable
24
+ extend AutoCorrector
25
+
26
+ RESTRICT_ON_SEND = %i[refute refute_equal].freeze
27
+
28
+ def_node_matcher :instance_of_assertion?, <<~PATTERN
29
+ {
30
+ (send nil? :refute (send $_ :instance_of? $const) $_?)
31
+ (send nil? :refute_equal $const (send $_ :class) $_?)
32
+ }
33
+ PATTERN
20
34
 
21
- define_rule :refute, target_method: :instance_of?, inverse: true
35
+ def on_send(node)
36
+ investigate(node, :refute)
37
+ end
22
38
  end
23
39
  end
24
40
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'mixin/argument_range_helper'
4
4
  require_relative 'mixin/in_delta_mixin'
5
+ require_relative 'mixin/instance_of_assertion_handleable'
5
6
  require_relative 'mixin/minitest_cop_rule'
6
7
  require_relative 'mixin/minitest_exploration_helpers'
7
8
  require_relative 'mixin/nil_assertion_handleable'
@@ -34,6 +35,7 @@ require_relative 'minitest/lifecycle_hooks_order'
34
35
  require_relative 'minitest/literal_as_actual_argument'
35
36
  require_relative 'minitest/multiple_assertions'
36
37
  require_relative 'minitest/no_assertions'
38
+ require_relative 'minitest/no_test_cases'
37
39
  require_relative 'minitest/non_public_test_method'
38
40
  require_relative 'minitest/refute_empty'
39
41
  require_relative 'minitest/refute_false'
@@ -9,12 +9,6 @@ module RuboCop
9
9
 
10
10
  private
11
11
 
12
- def first_argument_range(node)
13
- first_argument = node.first_argument
14
-
15
- range_between(first_argument.source_range.begin_pos, first_argument.source_range.end_pos)
16
- end
17
-
18
12
  def first_and_second_arguments_range(node)
19
13
  first_argument = node.first_argument
20
14
  second_argument = node.arguments[1]
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Common functionality for `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` cops.
7
+ # @api private
8
+ module InstanceOfAssertionHandleable
9
+ include ArgumentRangeHelper
10
+
11
+ MSG = 'Prefer using `%<prefer>s`.'
12
+
13
+ private
14
+
15
+ def investigate(node, assertion_type)
16
+ return unless (first_capture, second_capture, message = instance_of_assertion?(node))
17
+
18
+ required_arguments = build_required_arguments(node, assertion_type, first_capture, second_capture)
19
+ full_arguments = [required_arguments, message.first&.source].compact.join(', ')
20
+ prefer = "#{assertion_type}_instance_of(#{full_arguments})"
21
+
22
+ add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
23
+ range = replacement_range(node, assertion_type)
24
+
25
+ corrector.replace(node.loc.selector, "#{assertion_type}_instance_of")
26
+ corrector.replace(range, required_arguments)
27
+ end
28
+ end
29
+
30
+ def build_required_arguments(node, method_name, first_capture, second_capture)
31
+ if node.method?(method_name)
32
+ [second_capture, first_capture]
33
+ else
34
+ [first_capture, second_capture]
35
+ end.map(&:source).join(', ')
36
+ end
37
+
38
+ def replacement_range(node, method_name)
39
+ if node.method?(method_name)
40
+ node.first_argument
41
+ else
42
+ first_and_second_arguments_range(node)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -64,7 +64,7 @@ module RuboCop
64
64
  new_arguments = '(' + new_arguments + ')'
65
65
  end
66
66
 
67
- corrector.replace(first_argument_range(node), new_arguments)
67
+ corrector.replace(node.first_argument, new_arguments)
68
68
  end
69
69
 
70
70
  private
@@ -30,22 +30,23 @@ module RuboCop
30
30
  end
31
31
 
32
32
  def test_case?(node)
33
- return false unless node&.def_type? && test_method?(node)
33
+ return false unless (node&.def_type? && test_method?(node)) ||
34
+ (node&.block_type? && test_block?(node))
34
35
 
35
36
  class_ancestor = node.each_ancestor(:class).first
36
37
  test_class?(class_ancestor)
37
38
  end
38
39
 
39
40
  def test_cases(class_node, visibility_check: true)
40
- test_cases = class_def_nodes(class_node).select do |def_node|
41
+ test_methods = class_def_nodes(class_node).select do |def_node|
41
42
  test_method?(def_node, visibility_check: visibility_check)
42
43
  end
43
44
 
44
45
  # Support Active Support's `test 'example' { ... }` method.
45
46
  # https://api.rubyonrails.org/classes/ActiveSupport/Testing/Declarative.html
46
- test_blocks = class_node.each_descendant(:block).select { |block| block.method?(:test) || block.method?(:it) }
47
+ test_blocks = class_node.each_descendant(:block).select { |block_node| test_block?(block_node) }
47
48
 
48
- test_cases + test_blocks
49
+ test_methods + test_blocks
49
50
  end
50
51
 
51
52
  def test_method?(def_node, visibility_check: true)
@@ -54,6 +55,10 @@ module RuboCop
54
55
  test_case_name?(def_node.method_name) && !def_node.arguments?
55
56
  end
56
57
 
58
+ def test_block?(block_node)
59
+ block_node.method?(:test) || block_node.method?(:it)
60
+ end
61
+
57
62
  def lifecycle_hooks(class_node)
58
63
  class_def_nodes(class_node)
59
64
  .select { |def_node| lifecycle_hook_method?(def_node) }
@@ -28,7 +28,7 @@ module RuboCop
28
28
 
29
29
  new_arguments = new_arguments(arguments).join(', ')
30
30
 
31
- corrector.replace(first_argument_range(node), new_arguments)
31
+ corrector.replace(node.first_argument, new_arguments)
32
32
  end
33
33
 
34
34
  private
@@ -79,7 +79,7 @@ module RuboCop
79
79
  cop_name = self.class.to_s.delete_suffix('Test')
80
80
  return unless RuboCop::Cop::Minitest.const_defined?(cop_name)
81
81
 
82
- @cop = RuboCop::Cop::Minitest.const_get(cop_name).new
82
+ @cop = RuboCop::Cop::Minitest.const_get(cop_name).new(configuration)
83
83
  end
84
84
 
85
85
  def format_offense(source, **replacements)
@@ -124,7 +124,7 @@ module RuboCop
124
124
  end
125
125
 
126
126
  def _investigate(cop, processed_source)
127
- team = RuboCop::Cop::Team.new([cop], nil, raise_error: true)
127
+ team = RuboCop::Cop::Team.new([cop], configuration, raise_error: true)
128
128
  report = team.investigate(processed_source)
129
129
  @last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source)
130
130
  report.offenses
@@ -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.28.0'
7
+ STRING = '0.30.0'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
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.28.0
4
+ version: 0.30.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: 2023-02-20 00:00:00.000000000 Z
13
+ date: 2023-04-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -72,6 +72,7 @@ files:
72
72
  - lib/rubocop/cop/minitest/literal_as_actual_argument.rb
73
73
  - lib/rubocop/cop/minitest/multiple_assertions.rb
74
74
  - lib/rubocop/cop/minitest/no_assertions.rb
75
+ - lib/rubocop/cop/minitest/no_test_cases.rb
75
76
  - lib/rubocop/cop/minitest/non_public_test_method.rb
76
77
  - lib/rubocop/cop/minitest/refute_empty.rb
77
78
  - lib/rubocop/cop/minitest/refute_equal.rb
@@ -96,6 +97,7 @@ files:
96
97
  - lib/rubocop/cop/minitest_cops.rb
97
98
  - lib/rubocop/cop/mixin/argument_range_helper.rb
98
99
  - lib/rubocop/cop/mixin/in_delta_mixin.rb
100
+ - lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
99
101
  - lib/rubocop/cop/mixin/minitest_cop_rule.rb
100
102
  - lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
101
103
  - lib/rubocop/cop/mixin/nil_assertion_handleable.rb
@@ -112,7 +114,7 @@ metadata:
112
114
  homepage_uri: https://docs.rubocop.org/rubocop-minitest/
113
115
  changelog_uri: https://github.com/rubocop/rubocop-minitest/blob/master/CHANGELOG.md
114
116
  source_code_uri: https://github.com/rubocop/rubocop-minitest
115
- documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.28
117
+ documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.30
116
118
  bug_tracker_uri: https://github.com/rubocop/rubocop-minitest/issues
117
119
  rubygems_mfa_required: 'true'
118
120
  post_install_message:
@@ -130,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
132
  - !ruby/object:Gem::Version
131
133
  version: '0'
132
134
  requirements: []
133
- rubygems_version: 3.4.1
135
+ rubygems_version: 3.4.6
134
136
  signing_key:
135
137
  specification_version: 4
136
138
  summary: Automatic Minitest code style checking tool.