rubocop-minitest 0.30.0 → 0.31.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: 04d404efc9a07a82ed435c474b1c4370afa1d36e92f11ec1176911dbe677577c
4
- data.tar.gz: '079e6fe08096eae97e74790ffda49a120fd5d1b06d15968666d83e046ec821a7'
3
+ metadata.gz: a6ade83745f0a777cea6d2e37dfc8f00e7bc0857be549dd5b2d9544df4a201eb
4
+ data.tar.gz: 2afc383a877712b659efc1771fa6d44093ce4d11618ff37b23aa561dd1a986f6
5
5
  SHA512:
6
- metadata.gz: adf9211bbf6d285987b8bea806bafd15961a8307ea8f6baec4b7ab8d248a571782883ba619bb3542fb440a7ed218daf73b8166e09c33537b52042bcd306aa81e
7
- data.tar.gz: 94b7021203ca9992df0278ca2d753a7e6c03a54b8a5f49ca3231caf7b8f9b1dd40db791d3504f08bce49bee9e84e1a658cf741785ad74daa03df661dd68ac704
6
+ metadata.gz: fb1ecc48cb4f9563903aefa53ff3667f5509aa4f785bdb6fb1ab6ddfa240e5f034f0eae80ce991bde1ff19a925e426fdb19f78d3c41928c426da0068ad686d78
7
+ data.tar.gz: 16fbf8fe16331074e1ed953c96b45a327986701fd62a78d8b14325ffeaea96a619a2773f37665c5231fcf5fb90c40ca145fe1d24ae78924199dbb9b223ecab68
data/config/default.yml CHANGED
@@ -272,6 +272,12 @@ Minitest/RefuteSame:
272
272
  Enabled: pending
273
273
  VersionAdded: '0.26'
274
274
 
275
+ Minitest/ReturnInTestMethod:
276
+ Description: 'Enforces the use of `skip` instead of `return` in test methods.'
277
+ StyleGuide: 'https://minitest.rubystyle.guide/#skipping-runnable-methods'
278
+ Enabled: pending
279
+ VersionAdded: '0.31'
280
+
275
281
  Minitest/SkipEnsure:
276
282
  Description: 'Checks that `ensure` call even if `skip`.'
277
283
  Enabled: pending
@@ -3,7 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
- # Checks if test cases contain too many assertion calls.
6
+ # Checks if test cases contain too many assertion calls. If conditional code with assertions
7
+ # is used, the branch with maximum assertions is counted.
7
8
  # The maximum allowed assertion calls is configurable.
8
9
  #
9
10
  # @example Max: 1
@@ -36,7 +37,7 @@ module RuboCop
36
37
  return unless test_class?(class_node)
37
38
 
38
39
  test_cases(class_node).each do |node|
39
- assertions_count = assertions_count(node)
40
+ assertions_count = assertions_count(node.body)
40
41
 
41
42
  next unless assertions_count > max_assertions
42
43
 
@@ -49,6 +50,29 @@ module RuboCop
49
50
 
50
51
  private
51
52
 
53
+ def assertions_count(node)
54
+ return 0 unless node.is_a?(RuboCop::AST::Node)
55
+
56
+ assertions =
57
+ case node.type
58
+ when :if, :case, :case_match
59
+ assertions_count_in_branches(node.branches)
60
+ when :rescue
61
+ assertions_count(node.body) + assertions_count_in_branches(node.branches)
62
+ when :block, :numblock
63
+ assertions_count(node.body)
64
+ else
65
+ node.each_child_node.sum { |child| assertions_count(child) }
66
+ end
67
+
68
+ assertions += 1 if assertion_method?(node)
69
+ assertions
70
+ end
71
+
72
+ def assertions_count_in_branches(branches)
73
+ branches.map { |branch| assertions_count(branch) }.max
74
+ end
75
+
52
76
  def max_assertions
53
77
  Integer(cop_config.fetch('Max', 3))
54
78
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Enforces the use of `skip` instead of `return` in test methods.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # def test_something
11
+ # return if condition?
12
+ # assert_equal(42, something)
13
+ # end
14
+ #
15
+ # # good
16
+ # def test_something
17
+ # skip if condition?
18
+ # assert_equal(42, something)
19
+ # end
20
+ #
21
+ class ReturnInTestMethod < Base
22
+ include MinitestExplorationHelpers
23
+ extend AutoCorrector
24
+
25
+ MSG = 'Use `skip` instead of `return`.'
26
+
27
+ def on_return(node)
28
+ return unless node.ancestors.any? { |parent| test_case?(parent) }
29
+ return if inside_block?(node)
30
+
31
+ add_offense(node) do |corrector|
32
+ corrector.replace(node, 'skip')
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def inside_block?(node)
39
+ node.ancestors.any?(&:block_type?) || node.ancestors.any?(&:numblock_type?)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -29,6 +29,7 @@ require_relative 'minitest/assert_silent'
29
29
  require_relative 'minitest/assert_truthy'
30
30
  require_relative 'minitest/duplicate_test_run'
31
31
  require_relative 'minitest/empty_line_before_assertion_methods'
32
+ require_relative 'minitest/return_in_test_method'
32
33
  require_relative 'minitest/test_file_name'
33
34
  require_relative 'minitest/global_expectations'
34
35
  require_relative 'minitest/lifecycle_hooks_order'
@@ -34,7 +34,7 @@ module RuboCop
34
34
  (node&.block_type? && test_block?(node))
35
35
 
36
36
  class_ancestor = node.each_ancestor(:class).first
37
- test_class?(class_ancestor)
37
+ class_ancestor && test_class?(class_ancestor)
38
38
  end
39
39
 
40
40
  def test_cases(class_node, visibility_check: true)
@@ -100,7 +100,7 @@ module RuboCop
100
100
  end
101
101
 
102
102
  def assertion_method?(node)
103
- return false if !node.send_type? && !node.block_type?
103
+ return false if !node.send_type? && !node.block_type? && !node.numblock_type?
104
104
 
105
105
  ASSERTION_PREFIXES.any? do |prefix|
106
106
  method_name = node.method_name
@@ -216,7 +216,7 @@ module RuboCop
216
216
  end
217
217
 
218
218
  def ruby_version
219
- 2.6
219
+ RuboCop::TargetRuby::DEFAULT_VERSION
220
220
  end
221
221
  end
222
222
  # rubocop:enable Metrics/ModuleLength
@@ -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.30.0'
7
+ STRING = '0.31.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.30.0
4
+ version: 0.31.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-04-06 00:00:00.000000000 Z
13
+ date: 2023-05-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -87,6 +87,7 @@ files:
87
87
  - lib/rubocop/cop/minitest/refute_predicate.rb
88
88
  - lib/rubocop/cop/minitest/refute_respond_to.rb
89
89
  - lib/rubocop/cop/minitest/refute_same.rb
90
+ - lib/rubocop/cop/minitest/return_in_test_method.rb
90
91
  - lib/rubocop/cop/minitest/skip_ensure.rb
91
92
  - lib/rubocop/cop/minitest/skip_without_reason.rb
92
93
  - lib/rubocop/cop/minitest/test_file_name.rb
@@ -114,7 +115,7 @@ metadata:
114
115
  homepage_uri: https://docs.rubocop.org/rubocop-minitest/
115
116
  changelog_uri: https://github.com/rubocop/rubocop-minitest/blob/master/CHANGELOG.md
116
117
  source_code_uri: https://github.com/rubocop/rubocop-minitest
117
- documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.30
118
+ documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.31
118
119
  bug_tracker_uri: https://github.com/rubocop/rubocop-minitest/issues
119
120
  rubygems_mfa_required: 'true'
120
121
  post_install_message:
@@ -125,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
126
  requirements:
126
127
  - - ">="
127
128
  - !ruby/object:Gem::Version
128
- version: 2.6.0
129
+ version: 2.7.0
129
130
  required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  requirements:
131
132
  - - ">="