rubocop-minitest 0.26.1 → 0.27.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30d74ad78ccdb578c6cc6e19c201f1b14869fea35d42f20c951e2a3ccd9da72d
4
- data.tar.gz: 4ebac2d4e8a5efc3b0238388769986db1ab0625812cc378f0acd907428a180df
3
+ metadata.gz: 7e4abf180bb13a43bdb76c4c4e376c9d156dc4e48f3bce9b4171a2432bceda9b
4
+ data.tar.gz: '083ae2d51b7dafed513e7372eab667f9deea384676e0748af92d49c629ca9efe'
5
5
  SHA512:
6
- metadata.gz: a6b65afa72586fd58d096ce07858b0373cb65d9ba637807d557e7043347856e3ad69bd0fba61e1ef80fb69fb2928a4f6234200e40454ac068b85ec6579f03c36
7
- data.tar.gz: c5d62abb9019d7b89c7c4e5b2e50cbf696e1ef6fd31323c2674648faa1f535f50e96f45778f3b17078126a74a4e324125f3ec34b054a3f44ec91288d9046e221
6
+ metadata.gz: 562f5efee32059aa022c9a742745a89a752ad7d74f136f17b54f84a21f9d6a0030e905c8a00ab09782274f1c1544134bc6d8d6cb0d03e7e2de70b65dcb9309a3
7
+ data.tar.gz: 32d58f40bb69b87ed83ff3c342e255de7afaba760330f345d09b987ce352161577a9ee8f16677a2172cc59b48006efecb6272e71e90318281143fffe3155654b
data/config/default.yml CHANGED
@@ -111,9 +111,9 @@ Minitest/AssertTruthy:
111
111
  Description: 'This cop enforces the test to use `assert(actual)` instead of using `assert_equal(true, actual)`.'
112
112
  StyleGuide: 'https://minitest.rubystyle.guide#assert-truthy'
113
113
  Enabled: true
114
- SafeAutoCorrect: false
114
+ Safe: false
115
115
  VersionAdded: '0.2'
116
- VersionChanged: '0.26'
116
+ VersionChanged: '0.27'
117
117
 
118
118
  Minitest/AssertWithExpectedArgument:
119
119
  Description: 'This cop tries to detect when a user accidentally used `assert` when they meant to use `assert_equal`.'
@@ -175,6 +175,12 @@ Minitest/NoAssertions:
175
175
  Enabled: false
176
176
  VersionAdded: '0.12'
177
177
 
178
+ Minitest/NonPublicTestMethod:
179
+ Description: 'Detects non `public` (marked as `private` or `protected`) test methods.'
180
+ Enabled: pending
181
+ Severity: warning
182
+ VersionAdded: '0.27'
183
+
178
184
  Minitest/RefuteEmpty:
179
185
  Description: 'This cop enforces to use `refute_empty` instead of using `refute(object.empty?)`.'
180
186
  StyleGuide: 'https://minitest.rubystyle.guide#refute-empty'
@@ -191,7 +197,9 @@ Minitest/RefuteFalse:
191
197
  Description: 'Check if your test uses `refute(actual)` instead of `assert_equal(false, actual)`.'
192
198
  StyleGuide: 'https://minitest.rubystyle.guide#refute-false'
193
199
  Enabled: true
200
+ Safe: false
194
201
  VersionAdded: '0.3'
202
+ VersionChanged: '0.27'
195
203
 
196
204
  Minitest/RefuteInDelta:
197
205
  Description: 'This cop enforces the test to use `refute_in_delta` instead of using `refute_equal` to compare floats.'
@@ -6,7 +6,14 @@ module RuboCop
6
6
  # Enforces the test to use `assert(actual)` instead of using `assert_equal(true, actual)`.
7
7
  #
8
8
  # @safety
9
- # This cop's autocorrection is unsafe because true might be expected instead of truthy.
9
+ # This cop is unsafe because true might be expected instead of truthy.
10
+ # False positives cannot be prevented when this is a variable or method return value.
11
+ #
12
+ # [source,ruby]
13
+ # ----
14
+ # assert_equal(true, 'truthy') # failure
15
+ # assert('truthy') # success
16
+ # ----
10
17
  #
11
18
  # @example
12
19
  # # bad
@@ -49,12 +49,6 @@ module RuboCop
49
49
 
50
50
  private
51
51
 
52
- def assertions_count(node)
53
- node.each_descendant(:send).count do |send_node|
54
- assertion_method?(send_node)
55
- end
56
- end
57
-
58
52
  def max_assertions
59
53
  Integer(cop_config.fetch('Max', 3))
60
54
  end
@@ -35,13 +35,6 @@ module RuboCop
35
35
  add_offense(node.block_type? ? node.loc.expression : node.loc.name)
36
36
  end
37
37
  end
38
-
39
- private
40
-
41
- def assertions_count(node)
42
- base = assertion_method?(node) ? 1 : 0
43
- base + node.each_child_node.sum { |c| assertions_count(c) }
44
- end
45
38
  end
46
39
  end
47
40
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Detects non `public` (marked as `private` or `protected`) test methods.
7
+ # Minitest runs only test methods which are `public`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # class FooTest
12
+ # private # or protected
13
+ # def test_does_something
14
+ # assert_equal 42, do_something
15
+ # end
16
+ # end
17
+ #
18
+ # # good
19
+ # class FooTest
20
+ # def test_does_something
21
+ # assert_equal 42, do_something
22
+ # end
23
+ # end
24
+ #
25
+ # # good (not a test case name)
26
+ # class FooTest
27
+ # private # or protected
28
+ # def does_something
29
+ # assert_equal 42, do_something
30
+ # end
31
+ # end
32
+ #
33
+ # # good (no assertions)
34
+ # class FooTest
35
+ # private # or protected
36
+ # def test_does_something
37
+ # do_something
38
+ # end
39
+ # end
40
+ #
41
+ class NonPublicTestMethod < Base
42
+ include MinitestExplorationHelpers
43
+ include DefNode
44
+
45
+ MSG = 'Non `public` test method detected. Make it `public` for it to run.'
46
+
47
+ def on_class(node)
48
+ test_cases(node, visibility_check: false).each do |test_case|
49
+ add_offense(test_case) if non_public?(test_case) && assertions(test_case).any?
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -5,6 +5,16 @@ module RuboCop
5
5
  module Minitest
6
6
  # Enforces the use of `refute(object)` over `assert_equal(false, object)`.
7
7
  #
8
+ # @safety
9
+ # This cop is unsafe because it cannot detect failure when second argument is `nil`.
10
+ # False positives cannot be prevented when this is a variable or method return value.
11
+ #
12
+ # [source,ruby]
13
+ # ----
14
+ # assert_equal(false, nil) # failure
15
+ # refute(nil) # success
16
+ # ----
17
+ #
8
18
  # @example
9
19
  # # bad
10
20
  # assert_equal(false, actual)
@@ -37,7 +37,7 @@ module RuboCop
37
37
  def on_class(class_node)
38
38
  return unless test_class?(class_node)
39
39
 
40
- class_elements(class_node).each do |node|
40
+ class_def_nodes(class_node).each do |node|
41
41
  next unless offense?(node)
42
42
 
43
43
  test_method_name = node.loc.name
@@ -50,17 +50,6 @@ module RuboCop
50
50
 
51
51
  private
52
52
 
53
- def class_elements(class_node)
54
- class_def = class_node.body
55
- return [] unless class_def
56
-
57
- if class_def.def_type?
58
- [class_def]
59
- else
60
- class_def.each_child_node(:def).to_a
61
- end
62
- end
63
-
64
53
  def offense?(node)
65
54
  return false if assertions(node).none?
66
55
 
@@ -46,7 +46,7 @@ module RuboCop
46
46
 
47
47
  case node.method_name
48
48
  when *SINGLE_ASSERTION_ARGUMENT_METHODS
49
- actual.nil? && expected&.literal?
49
+ actual.nil? && expected&.literal? && !expected.xstr_type?
50
50
  when *TWO_ASSERTION_ARGUMENTS_METHODS
51
51
  return false unless expected || actual
52
52
  return false if expected.source != actual.source
@@ -33,6 +33,7 @@ require_relative 'minitest/global_expectations'
33
33
  require_relative 'minitest/literal_as_actual_argument'
34
34
  require_relative 'minitest/multiple_assertions'
35
35
  require_relative 'minitest/no_assertions'
36
+ require_relative 'minitest/non_public_test_method'
36
37
  require_relative 'minitest/refute_empty'
37
38
  require_relative 'minitest/refute_false'
38
39
  require_relative 'minitest/refute_equal'
@@ -7,6 +7,7 @@ module RuboCop
7
7
  # Helper methods for different explorations against test files and test cases.
8
8
  # @api private
9
9
  module MinitestExplorationHelpers
10
+ include DefNode
10
11
  extend NodePattern::Macros
11
12
 
12
13
  ASSERTION_PREFIXES = %w[assert refute].freeze
@@ -27,14 +28,16 @@ module RuboCop
27
28
  end
28
29
 
29
30
  def test_case?(node)
30
- return false unless node&.def_type? && test_case_name?(node.method_name)
31
+ return false unless node&.def_type? && test_method?(node)
31
32
 
32
33
  class_ancestor = node.each_ancestor(:class).first
33
34
  test_class?(class_ancestor)
34
35
  end
35
36
 
36
- def test_cases(class_node)
37
- test_cases = class_def_nodes(class_node).select { |def_node| test_case_name?(def_node.method_name) }
37
+ def test_cases(class_node, visibility_check: true)
38
+ test_cases = class_def_nodes(class_node).select do |def_node|
39
+ test_method?(def_node, visibility_check: visibility_check)
40
+ end
38
41
 
39
42
  # Support Active Support's `test 'example' { ... }` method.
40
43
  # https://api.rubyonrails.org/classes/ActiveSupport/Testing/Declarative.html
@@ -43,6 +46,12 @@ module RuboCop
43
46
  test_cases + test_blocks
44
47
  end
45
48
 
49
+ def test_method?(def_node, visibility_check: true)
50
+ return false if visibility_check && non_public?(def_node)
51
+
52
+ test_case_name?(def_node.method_name) && !def_node.arguments?
53
+ end
54
+
46
55
  def lifecycle_hooks(class_node)
47
56
  class_def_nodes(class_node)
48
57
  .select { |def_node| lifecycle_hook_method?(def_node) }
@@ -77,6 +86,12 @@ module RuboCop
77
86
  send_nodes.select { |send_node| assertion_method?(send_node) }
78
87
  end
79
88
 
89
+ def assertions_count(node)
90
+ node.each_descendant(:send).count do |send_node|
91
+ assertion_method?(send_node)
92
+ end
93
+ end
94
+
80
95
  def assertion_method?(node)
81
96
  return false if !node.send_type? && !node.block_type?
82
97
 
@@ -10,7 +10,7 @@ module RuboCop
10
10
  #
11
11
  # This mixin makes it easier to specify strict offense assertions
12
12
  # in a declarative and visual fashion. Just type out the code that
13
- # should generate a offense, annotate code by writing '^'s
13
+ # should generate an offense, annotate code by writing '^'s
14
14
  # underneath each character that should be highlighted, and follow
15
15
  # the carets with a string (separated by a space) that is the
16
16
  # message of the offense. You can include multiple offenses in
@@ -77,6 +77,7 @@ module RuboCop
77
77
 
78
78
  def setup
79
79
  cop_name = self.class.to_s.delete_suffix('Test')
80
+ return unless RuboCop::Cop::Minitest.const_defined?(cop_name)
80
81
 
81
82
  @cop = RuboCop::Cop::Minitest.const_get(cop_name).new
82
83
  end
@@ -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.26.1'
7
+ STRING = '0.27.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.26.1
4
+ version: 0.27.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-01-17 00:00:00.000000000 Z
13
+ date: 2023-01-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -85,6 +85,7 @@ files:
85
85
  - lib/rubocop/cop/minitest/literal_as_actual_argument.rb
86
86
  - lib/rubocop/cop/minitest/multiple_assertions.rb
87
87
  - lib/rubocop/cop/minitest/no_assertions.rb
88
+ - lib/rubocop/cop/minitest/non_public_test_method.rb
88
89
  - lib/rubocop/cop/minitest/refute_empty.rb
89
90
  - lib/rubocop/cop/minitest/refute_equal.rb
90
91
  - lib/rubocop/cop/minitest/refute_false.rb
@@ -124,7 +125,7 @@ metadata:
124
125
  homepage_uri: https://docs.rubocop.org/rubocop-minitest/
125
126
  changelog_uri: https://github.com/rubocop/rubocop-minitest/blob/master/CHANGELOG.md
126
127
  source_code_uri: https://github.com/rubocop/rubocop-minitest
127
- documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.26
128
+ documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.27
128
129
  bug_tracker_uri: https://github.com/rubocop/rubocop-minitest/issues
129
130
  rubygems_mfa_required: 'true'
130
131
  post_install_message:
@@ -142,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  - !ruby/object:Gem::Version
143
144
  version: '0'
144
145
  requirements: []
145
- rubygems_version: 3.3.26
146
+ rubygems_version: 3.4.1
146
147
  signing_key:
147
148
  specification_version: 4
148
149
  summary: Automatic Minitest code style checking tool.