rubocop-minitest 0.27.0 → 0.28.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.yml +6 -0
- data/lib/rubocop/cop/generator.rb +1 -1
- data/lib/rubocop/cop/minitest/assert_match.rb +4 -1
- data/lib/rubocop/cop/minitest/lifecycle_hooks_order.rb +100 -0
- data/lib/rubocop/cop/minitest/refute_match.rb +4 -1
- data/lib/rubocop/cop/minitest_cops.rb +1 -0
- data/lib/rubocop/cop/mixin/minitest_cop_rule.rb +15 -4
- data/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +4 -2
- data/lib/rubocop/minitest/version.rb +1 -1
- metadata +6 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4d321b9b8d16e6c5c3cf304835e7c48fde65c173202925d3e072c779c90bb86
|
4
|
+
data.tar.gz: 40856b7d83975172736776deae22bb7f62d5d9b913674bb354a6c7cbf7011882
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54a62f0f9510ef6d00dedccc0f59672446f70fea23d12e63cab8915423932aff0921fde8b52e44ce6b93543a0c43a9726867aeb50a84fae1d8e79a3e0ec40f38
|
7
|
+
data.tar.gz: 45a933d110a001cb8ada63094fadfb918c814b85c2d68154fc7f1180488f9937a5858a8db2abca0ba7a80323be9ea11e5940df21736ce69166aaf2947388c276
|
data/config/default.yml
CHANGED
@@ -158,6 +158,12 @@ Minitest/GlobalExpectations:
|
|
158
158
|
VersionAdded: '0.7'
|
159
159
|
VersionChanged: '0.26'
|
160
160
|
|
161
|
+
Minitest/LifecycleHooksOrder:
|
162
|
+
Description: 'Checks that lifecycle hooks are declared in the order in which they will be executed.'
|
163
|
+
StyleGuide: 'https://minitest.rubystyle.guide/#hooks-ordering'
|
164
|
+
Enabled: pending
|
165
|
+
VersionAdded: '0.28'
|
166
|
+
|
161
167
|
Minitest/LiteralAsActualArgument:
|
162
168
|
Description: 'This cop enforces correct order of `expected` and `actual` arguments for `assert_equal`.'
|
163
169
|
StyleGuide: 'https://minitest.rubystyle.guide/#assert-equal-arguments-order'
|
@@ -9,6 +9,8 @@ module RuboCop
|
|
9
9
|
# @example
|
10
10
|
# # bad
|
11
11
|
# assert(matcher.match(string))
|
12
|
+
# assert(matcher.match?(string))
|
13
|
+
# assert(matcher =~ string)
|
12
14
|
# assert(matcher.match(string), 'message')
|
13
15
|
#
|
14
16
|
# # good
|
@@ -18,7 +20,8 @@ module RuboCop
|
|
18
20
|
class AssertMatch < Base
|
19
21
|
extend MinitestCopRule
|
20
22
|
|
21
|
-
define_rule :assert, target_method:
|
23
|
+
define_rule :assert, target_method: %i[match match? =~],
|
24
|
+
preferred_method: :assert_match, inverse: 'regexp_type?'
|
22
25
|
end
|
23
26
|
end
|
24
27
|
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Minitest
|
6
|
+
# Checks that lifecycle hooks are declared in the order in which they will be executed.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# class FooTest < Minitest::Test
|
11
|
+
# def teardown; end
|
12
|
+
# def setup; end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # good
|
16
|
+
# class FooTest < Minitest::Test
|
17
|
+
# def setup; end
|
18
|
+
# def teardown; end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# # bad (after test cases)
|
22
|
+
# class FooTest < Minitest::Test
|
23
|
+
# def test_something
|
24
|
+
# assert foo
|
25
|
+
# end
|
26
|
+
# def setup; end
|
27
|
+
# def teardown; end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# # good
|
31
|
+
# class FooTest < Minitest::Test
|
32
|
+
# def setup; end
|
33
|
+
# def teardown; end
|
34
|
+
# def test_something
|
35
|
+
# assert foo
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# # good (after non test case methods)
|
40
|
+
# class FooTest < Minitest::Test
|
41
|
+
# def do_something; end
|
42
|
+
# def setup; end
|
43
|
+
# def teardown; end
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
class LifecycleHooksOrder < Base
|
47
|
+
include MinitestExplorationHelpers
|
48
|
+
include RangeHelp
|
49
|
+
extend AutoCorrector
|
50
|
+
|
51
|
+
MSG = '`%<current>s` is supposed to appear before `%<previous>s`.'
|
52
|
+
|
53
|
+
# Regular method's position should be last.
|
54
|
+
REGULAR_METHOD_POSITION = LIFECYCLE_HOOK_METHODS_IN_ORDER.size + 1
|
55
|
+
HOOKS_ORDER_MAP = Hash.new do |hash, hook|
|
56
|
+
hash[hook] = LIFECYCLE_HOOK_METHODS_IN_ORDER.index(hook) || REGULAR_METHOD_POSITION
|
57
|
+
end
|
58
|
+
|
59
|
+
# rubocop:disable Metrics/MethodLength
|
60
|
+
def on_class(class_node)
|
61
|
+
return unless test_class?(class_node)
|
62
|
+
|
63
|
+
previous_index = -1
|
64
|
+
previous_hook_node = nil
|
65
|
+
|
66
|
+
hooks_and_test_cases(class_node).each do |node|
|
67
|
+
hook = node.method_name
|
68
|
+
index = HOOKS_ORDER_MAP[hook]
|
69
|
+
|
70
|
+
if index < previous_index
|
71
|
+
message = format(MSG, current: hook, previous: previous_hook_node.method_name)
|
72
|
+
add_offense(node, message: message) do |corrector|
|
73
|
+
autocorrect(corrector, previous_hook_node, node)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
previous_index = index
|
77
|
+
previous_hook_node = node
|
78
|
+
end
|
79
|
+
end
|
80
|
+
# rubocop:enable Metrics/MethodLength
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def hooks_and_test_cases(class_node)
|
85
|
+
class_def_nodes(class_node).select do |node|
|
86
|
+
lifecycle_hook_method?(node) || test_case?(node)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def autocorrect(corrector, previous_node, node)
|
91
|
+
previous_node_range = range_with_comments_and_lines(previous_node)
|
92
|
+
node_range = range_with_comments_and_lines(node)
|
93
|
+
|
94
|
+
corrector.insert_before(previous_node_range, node_range.source)
|
95
|
+
corrector.remove(node_range)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -9,6 +9,8 @@ module RuboCop
|
|
9
9
|
# @example
|
10
10
|
# # bad
|
11
11
|
# refute(matcher.match(string))
|
12
|
+
# refute(matcher.match?(string))
|
13
|
+
# refute(matcher =~ string)
|
12
14
|
# refute(matcher.match(string), 'message')
|
13
15
|
#
|
14
16
|
# # good
|
@@ -18,7 +20,8 @@ module RuboCop
|
|
18
20
|
class RefuteMatch < Base
|
19
21
|
extend MinitestCopRule
|
20
22
|
|
21
|
-
define_rule :refute, target_method:
|
23
|
+
define_rule :refute, target_method: %i[match match? =~],
|
24
|
+
preferred_method: :refute_match, inverse: 'regexp_type?'
|
22
25
|
end
|
23
26
|
end
|
24
27
|
end
|
@@ -30,6 +30,7 @@ require_relative 'minitest/duplicate_test_run'
|
|
30
30
|
require_relative 'minitest/empty_line_before_assertion_methods'
|
31
31
|
require_relative 'minitest/test_file_name'
|
32
32
|
require_relative 'minitest/global_expectations'
|
33
|
+
require_relative 'minitest/lifecycle_hooks_order'
|
33
34
|
require_relative 'minitest/literal_as_actual_argument'
|
34
35
|
require_relative 'minitest/multiple_assertions'
|
35
36
|
require_relative 'minitest/no_assertions'
|
@@ -13,9 +13,14 @@ module RuboCop
|
|
13
13
|
# define_rule :assert, target_method: :include?, preferred_method: :assert_includes
|
14
14
|
# define_rule :assert, target_method: :instance_of?, inverse: true
|
15
15
|
#
|
16
|
+
# @example Multiple target methods
|
17
|
+
# # `preferred_method` is required
|
18
|
+
# define_rule :assert, target_method: %i[match match? =~],
|
19
|
+
# preferred_method: :assert_match, inverse: 'regexp_type?'
|
20
|
+
#
|
16
21
|
# @param assertion_method [Symbol] Assertion method like `assert` or `refute`.
|
17
|
-
# @param target_method [Symbol] Method name offensed by assertion method arguments.
|
18
|
-
# @param preferred_method [Symbol]
|
22
|
+
# @param target_method [Symbol, Array<Symbol>] Method name(s) offensed by assertion method arguments.
|
23
|
+
# @param preferred_method [Symbol] Is required if passing multiple target methods. Custom method name replaced by
|
19
24
|
# autocorrection. The preferred method name that connects
|
20
25
|
# `assertion_method` and `target_method` with `_` is
|
21
26
|
# the default name.
|
@@ -24,7 +29,12 @@ module RuboCop
|
|
24
29
|
# @api private
|
25
30
|
#
|
26
31
|
def define_rule(assertion_method, target_method:, preferred_method: nil, inverse: false)
|
27
|
-
|
32
|
+
target_methods = Array(target_method)
|
33
|
+
if target_methods.size > 1 && preferred_method.nil?
|
34
|
+
raise ArgumentError, '`:preferred_method` keyword argument must be used if using more than one target method.'
|
35
|
+
end
|
36
|
+
|
37
|
+
preferred_method = "#{assertion_method}_#{target_methods.first.to_s.delete('?')}" if preferred_method.nil?
|
28
38
|
|
29
39
|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
30
40
|
include ArgumentRangeHelper
|
@@ -37,7 +47,8 @@ module RuboCop
|
|
37
47
|
return unless node.method?(:#{assertion_method})
|
38
48
|
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
|
39
49
|
return unless arguments.first&.call_type?
|
40
|
-
return if arguments.first.arguments.empty? ||
|
50
|
+
return if arguments.first.arguments.empty? ||
|
51
|
+
#{target_methods}.none? { |target_method| arguments.first.method?(target_method) }
|
41
52
|
|
42
53
|
add_offense(node, message: offense_message(arguments)) do |corrector|
|
43
54
|
autocorrect(corrector, node, arguments)
|
@@ -12,14 +12,16 @@ module RuboCop
|
|
12
12
|
|
13
13
|
ASSERTION_PREFIXES = %w[assert refute].freeze
|
14
14
|
|
15
|
-
|
15
|
+
LIFECYCLE_HOOK_METHODS_IN_ORDER = %i[
|
16
16
|
before_setup
|
17
17
|
setup
|
18
18
|
after_setup
|
19
19
|
before_teardown
|
20
20
|
teardown
|
21
21
|
after_teardown
|
22
|
-
].
|
22
|
+
].freeze
|
23
|
+
|
24
|
+
LIFECYCLE_HOOK_METHODS = LIFECYCLE_HOOK_METHODS_IN_ORDER.to_set.freeze
|
23
25
|
|
24
26
|
private
|
25
27
|
|
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.
|
4
|
+
version: 0.28.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-
|
13
|
+
date: 2023-02-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
21
|
+
version: '1.39'
|
22
22
|
- - "<"
|
23
23
|
- !ruby/object:Gem::Version
|
24
24
|
version: '2.0'
|
@@ -28,24 +28,10 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - ">="
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
version: '
|
31
|
+
version: '1.39'
|
32
32
|
- - "<"
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '2.0'
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: minitest
|
37
|
-
requirement: !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - "~>"
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
version: '5.11'
|
42
|
-
type: :development
|
43
|
-
prerelease: false
|
44
|
-
version_requirements: !ruby/object:Gem::Requirement
|
45
|
-
requirements:
|
46
|
-
- - "~>"
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version: '5.11'
|
49
35
|
description: |
|
50
36
|
Automatic Minitest code style checking tool.
|
51
37
|
A RuboCop extension focused on enforcing Minitest best practices and coding conventions.
|
@@ -82,6 +68,7 @@ files:
|
|
82
68
|
- lib/rubocop/cop/minitest/duplicate_test_run.rb
|
83
69
|
- lib/rubocop/cop/minitest/empty_line_before_assertion_methods.rb
|
84
70
|
- lib/rubocop/cop/minitest/global_expectations.rb
|
71
|
+
- lib/rubocop/cop/minitest/lifecycle_hooks_order.rb
|
85
72
|
- lib/rubocop/cop/minitest/literal_as_actual_argument.rb
|
86
73
|
- lib/rubocop/cop/minitest/multiple_assertions.rb
|
87
74
|
- lib/rubocop/cop/minitest/no_assertions.rb
|
@@ -125,7 +112,7 @@ metadata:
|
|
125
112
|
homepage_uri: https://docs.rubocop.org/rubocop-minitest/
|
126
113
|
changelog_uri: https://github.com/rubocop/rubocop-minitest/blob/master/CHANGELOG.md
|
127
114
|
source_code_uri: https://github.com/rubocop/rubocop-minitest
|
128
|
-
documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.
|
115
|
+
documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.28
|
129
116
|
bug_tracker_uri: https://github.com/rubocop/rubocop-minitest/issues
|
130
117
|
rubygems_mfa_required: 'true'
|
131
118
|
post_install_message:
|