rubocop-minitest 0.30.0 → 0.31.1

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: 121360d540ec5e0fcb507855d4601990650d4b785cb5b50f6260ac3d081a86df
4
+ data.tar.gz: fc9be860f55393d27a4cce401e6e2362d9460eaec3c79fd4bcec5eb67b5958f6
5
5
  SHA512:
6
- metadata.gz: adf9211bbf6d285987b8bea806bafd15961a8307ea8f6baec4b7ab8d248a571782883ba619bb3542fb440a7ed218daf73b8166e09c33537b52042bcd306aa81e
7
- data.tar.gz: 94b7021203ca9992df0278ca2d753a7e6c03a54b8a5f49ca3231caf7b8f9b1dd40db791d3504f08bce49bee9e84e1a658cf741785ad74daa03df661dd68ac704
6
+ metadata.gz: 06eb166163e30d215c9ae7916eae0fd36c997dd1b26154c53c69da62a3997eac57d3f8a7a0f5c9e4a0ec5340806d8d7e367c3c130f062432f810ba91144d2c9d
7
+ data.tar.gz: ac1c477e1e65bbfcdc5a3bd4096cf49bec8ea0e86839db10c35838015bdf92d8c6e613f383516929ea250a535dff72f69cd979f354bf5534e969681b9ad744ae
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/rubocop-minitest.svg)](https://badge.fury.io/rb/rubocop-minitest)
4
4
  [![CircleCI](https://circleci.com/gh/rubocop/rubocop-minitest.svg?style=svg)](https://circleci.com/gh/rubocop/rubocop-minitest)
5
5
 
6
- A [RuboCop](https://github.com/rubocop/rubocop) extension focused on enforcing Minitest best practices and coding conventions.
6
+ A [RuboCop](https://github.com/rubocop/rubocop) extension focused on enforcing [Minitest](https://github.com/minitest/minitest) best practices and coding conventions.
7
7
  The library is based on the guidelines outlined in the community [Minitest Style Guide](https://minitest.rubystyle.guide).
8
8
 
9
9
  ## Installation
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
@@ -9,7 +9,6 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert("rubocop-minitest" != actual)
12
- # assert(! "rubocop-minitest" == actual)
13
12
  #
14
13
  # # good
15
14
  # refute_equal("rubocop-minitest", actual)
@@ -22,7 +21,7 @@ module RuboCop
22
21
  RESTRICT_ON_SEND = %i[assert].freeze
23
22
 
24
23
  def_node_matcher :assert_not_equal, <<~PATTERN
25
- (send nil? :assert {(send $_ :!= $_) (send (send $_ :! ) :== $_) } $... )
24
+ (send nil? :assert (send $_ :!= $_) $... )
26
25
  PATTERN
27
26
 
28
27
  def on_send(node)
@@ -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,13 +100,12 @@ 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
107
107
 
108
- # TODO: Remove the fllowing `to_s` since Ruby 2.7 that supports `Symbol#start_with?`.
109
- method_name.to_s.start_with?(prefix) || node.method?(:flunk)
108
+ method_name.start_with?(prefix) || node.method?(:flunk)
110
109
  end
111
110
  end
112
111
 
@@ -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.1'
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.1
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-09-03 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,14 +126,14 @@ 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
  - - ">="
132
133
  - !ruby/object:Gem::Version
133
134
  version: '0'
134
135
  requirements: []
135
- rubygems_version: 3.4.6
136
+ rubygems_version: 3.4.10
136
137
  signing_key:
137
138
  specification_version: 4
138
139
  summary: Automatic Minitest code style checking tool.