rubocop-rspec 2.9.0 → 2.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/config/default.yml +16 -1
- data/lib/rubocop/cop/rspec/be_nil.rb +41 -7
- data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +4 -0
- data/lib/rubocop/cop/rspec/empty_example_group.rb +6 -6
- data/lib/rubocop/cop/rspec/verified_double_reference.rb +111 -0
- data/lib/rubocop/cop/rspec_cops.rb +1 -0
- data/lib/rubocop/rspec/version.rb +1 -1
- data/lib/rubocop-rspec.rb +14 -9
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d0bedaa464141123a49b02ad1a31ed8a63b9960304014c0ef5e52f96fb78176
|
4
|
+
data.tar.gz: e310236f596a19629629c70d9f94fcb46a39183c43908b5864860efb2abd7602
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cd82a374f1cd9362070fe6561f9c04927a2ac33f5e1b5ee97a80bc443b0a4c3c90a75d93778e7bf6f439ff759b60857cdeccc2a7b51bfdc8feebccc519e36c8
|
7
|
+
data.tar.gz: 19bbbdd2d38ef4802be62f44a18e95e83caaf6899c654dff281fe7058fc9bdf6f776cf0491b825b274f67450388a7aef292c2ee51033f6ae96a9d8671829c5e4
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,13 @@
|
|
2
2
|
|
3
3
|
## Master (Unreleased)
|
4
4
|
|
5
|
+
## 2.10.0 (2022-04-19)
|
6
|
+
|
7
|
+
* Fix a false positive for `RSpec/EmptyExampleGroup` when expectations in case statement. ([@ydah][])
|
8
|
+
* Add `RSpec/VerifiedDoubleReference` cop. ([@t3h2mas][])
|
9
|
+
* Make `RSpec/BeNil` cop configurable with a `be_nil` style and a `be` style. ([@bquorning][])
|
10
|
+
* Fix `Capybara/CurrentPathExpectation` autocorrect incompatible with `Style/TrailingCommaInArguments` autocorrect. ([@ydah][])
|
11
|
+
|
5
12
|
## 2.9.0 (2022-02-28)
|
6
13
|
|
7
14
|
* Add new `RSpec/BeNil` cop. ([@bquorning][])
|
@@ -672,3 +679,5 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
|
|
672
679
|
[@leoarnold]: https://github.com/leoarnold
|
673
680
|
[@harry-graham]: https://github.com/harry-graham
|
674
681
|
[@oshiro3]: https://github.com/oshiro3
|
682
|
+
[@ydah]: https://github.com/ydah
|
683
|
+
[@t3h2mas]: https://github.com/t3h2mas
|
data/config/default.yml
CHANGED
@@ -155,9 +155,14 @@ RSpec/BeEql:
|
|
155
155
|
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeEql
|
156
156
|
|
157
157
|
RSpec/BeNil:
|
158
|
-
Description:
|
158
|
+
Description: Ensures a consistent style is used when matching `nil`.
|
159
159
|
Enabled: pending
|
160
|
+
EnforcedStyle: be_nil
|
161
|
+
SupportedStyles:
|
162
|
+
- be
|
163
|
+
- be_nil
|
160
164
|
VersionAdded: 2.9.0
|
165
|
+
VersionChanged: 2.10.0
|
161
166
|
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeNil
|
162
167
|
|
163
168
|
RSpec/BeforeAfterAll:
|
@@ -761,6 +766,16 @@ RSpec/VariableName:
|
|
761
766
|
VersionChanged: '1.43'
|
762
767
|
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VariableName
|
763
768
|
|
769
|
+
RSpec/VerifiedDoubleReference:
|
770
|
+
Description: Checks for consistent verified double reference style.
|
771
|
+
Enabled: pending
|
772
|
+
EnforcedStyle: constant
|
773
|
+
SupportedStyles:
|
774
|
+
- constant
|
775
|
+
- string
|
776
|
+
VersionAdded: 2.10.0
|
777
|
+
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubleReference
|
778
|
+
|
764
779
|
RSpec/VerifiedDoubles:
|
765
780
|
Description: Prefer using verifying doubles over normal doubles.
|
766
781
|
Enabled: true
|
@@ -3,24 +3,39 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module RSpec
|
6
|
-
#
|
6
|
+
# Ensures a consistent style is used when matching `nil`.
|
7
7
|
#
|
8
|
-
#
|
9
|
-
#
|
8
|
+
# You can either use the more specific `be_nil` matcher, or the more
|
9
|
+
# generic `be` matcher with a `nil` argument.
|
10
10
|
#
|
11
|
-
#
|
11
|
+
# This cop can be configured using the `EnforcedStyle` option
|
12
12
|
#
|
13
|
+
# @example `EnforcedStyle: be_nil` (default)
|
13
14
|
# # bad
|
14
15
|
# expect(foo).to be(nil)
|
15
16
|
#
|
16
17
|
# # good
|
17
18
|
# expect(foo).to be_nil
|
18
19
|
#
|
20
|
+
# @example `EnforcedStyle: be`
|
21
|
+
# # bad
|
22
|
+
# expect(foo).to be_nil
|
23
|
+
#
|
24
|
+
# # good
|
25
|
+
# expect(foo).to be(nil)
|
26
|
+
#
|
19
27
|
class BeNil < Base
|
20
28
|
extend AutoCorrector
|
29
|
+
include ConfigurableEnforcedStyle
|
21
30
|
|
22
|
-
|
23
|
-
|
31
|
+
BE_MSG = 'Prefer `be(nil)` over `be_nil`.'
|
32
|
+
BE_NIL_MSG = 'Prefer `be_nil` over `be(nil)`.'
|
33
|
+
RESTRICT_ON_SEND = %i[be be_nil].freeze
|
34
|
+
|
35
|
+
# @!method be_nil_matcher?(node)
|
36
|
+
def_node_matcher :be_nil_matcher?, <<-PATTERN
|
37
|
+
(send nil? :be_nil)
|
38
|
+
PATTERN
|
24
39
|
|
25
40
|
# @!method nil_value_expectation?(node)
|
26
41
|
def_node_matcher :nil_value_expectation?, <<-PATTERN
|
@@ -28,9 +43,28 @@ module RuboCop
|
|
28
43
|
PATTERN
|
29
44
|
|
30
45
|
def on_send(node)
|
46
|
+
case style
|
47
|
+
when :be
|
48
|
+
check_be_style(node)
|
49
|
+
when :be_nil
|
50
|
+
check_be_nil_style(node)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def check_be_style(node)
|
57
|
+
return unless be_nil_matcher?(node)
|
58
|
+
|
59
|
+
add_offense(node, message: BE_MSG) do |corrector|
|
60
|
+
corrector.replace(node.loc.expression, 'be(nil)')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def check_be_nil_style(node)
|
31
65
|
return unless nil_value_expectation?(node)
|
32
66
|
|
33
|
-
add_offense(node) do |corrector|
|
67
|
+
add_offense(node, message: BE_NIL_MSG) do |corrector|
|
34
68
|
corrector.replace(node.loc.expression, 'be_nil')
|
35
69
|
end
|
36
70
|
end
|
@@ -52,6 +52,10 @@ module RuboCop
|
|
52
52
|
$(send nil? :match (str $_)))
|
53
53
|
PATTERN
|
54
54
|
|
55
|
+
def self.autocorrect_incompatible_with
|
56
|
+
[Style::TrailingCommaInArguments]
|
57
|
+
end
|
58
|
+
|
55
59
|
def on_send(node)
|
56
60
|
expectation_set_on_current_path(node) do
|
57
61
|
add_offense(node.loc.selector) do |corrector|
|
@@ -145,7 +145,7 @@ module RuboCop
|
|
145
145
|
return true unless body
|
146
146
|
return false if conditionals_with_examples?(body)
|
147
147
|
|
148
|
-
if body.if_type?
|
148
|
+
if body.if_type? || body.case_type?
|
149
149
|
!examples_in_branches?(body)
|
150
150
|
else
|
151
151
|
!examples?(body)
|
@@ -153,15 +153,15 @@ module RuboCop
|
|
153
153
|
end
|
154
154
|
|
155
155
|
def conditionals_with_examples?(body)
|
156
|
-
return unless body.begin_type?
|
156
|
+
return unless body.begin_type? || body.case_type?
|
157
157
|
|
158
|
-
body.each_descendant(:if).any? do |
|
159
|
-
examples_in_branches?(
|
158
|
+
body.each_descendant(:if, :case).any? do |condition_node|
|
159
|
+
examples_in_branches?(condition_node)
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
163
|
-
def examples_in_branches?(
|
164
|
-
|
163
|
+
def examples_in_branches?(condition_node)
|
164
|
+
condition_node.branches.any? { |branch| examples?(branch) }
|
165
165
|
end
|
166
166
|
end
|
167
167
|
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
# Checks for consistent verified double reference style.
|
7
|
+
#
|
8
|
+
# Only investigates references that are one of the supported styles.
|
9
|
+
#
|
10
|
+
# @see https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles
|
11
|
+
#
|
12
|
+
# This cop can be configured in your configuration using the
|
13
|
+
# `EnforcedStyle` option and supports `--auto-gen-config`.
|
14
|
+
#
|
15
|
+
# @example `EnforcedStyle: constant` (default)
|
16
|
+
# # bad
|
17
|
+
# let(:foo) do
|
18
|
+
# instance_double('ClassName', method_name: 'returned_value')
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# # good
|
22
|
+
# let(:foo) do
|
23
|
+
# instance_double(ClassName, method_name: 'returned_value')
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @example `EnforcedStyle: string`
|
27
|
+
# # bad
|
28
|
+
# let(:foo) do
|
29
|
+
# instance_double(ClassName, method_name: 'returned_value')
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# # good
|
33
|
+
# let(:foo) do
|
34
|
+
# instance_double('ClassName', method_name: 'returned_value')
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @example Reference is not in the supported style list. No enforcement
|
38
|
+
#
|
39
|
+
# # good
|
40
|
+
# let(:foo) do
|
41
|
+
# instance_double(@klass, method_name: 'returned_value')
|
42
|
+
# end
|
43
|
+
class VerifiedDoubleReference < Base
|
44
|
+
extend AutoCorrector
|
45
|
+
include ConfigurableEnforcedStyle
|
46
|
+
|
47
|
+
MSG = 'Use a %<style>s class reference for verified doubles.'
|
48
|
+
|
49
|
+
RESTRICT_ON_SEND = Set[
|
50
|
+
:class_double,
|
51
|
+
:class_spy,
|
52
|
+
:instance_double,
|
53
|
+
:instance_spy,
|
54
|
+
:mock_model,
|
55
|
+
:object_double,
|
56
|
+
:object_spy,
|
57
|
+
:stub_model
|
58
|
+
].freeze
|
59
|
+
|
60
|
+
REFERENCE_TYPE_STYLES = {
|
61
|
+
str: :string,
|
62
|
+
const: :constant
|
63
|
+
}.freeze
|
64
|
+
|
65
|
+
# @!method verified_double(node)
|
66
|
+
def_node_matcher :verified_double, <<~PATTERN
|
67
|
+
(send
|
68
|
+
nil?
|
69
|
+
RESTRICT_ON_SEND
|
70
|
+
$_class_reference
|
71
|
+
...)
|
72
|
+
PATTERN
|
73
|
+
|
74
|
+
def on_send(node)
|
75
|
+
verified_double(node) do |class_reference|
|
76
|
+
break correct_style_detected unless opposing_style?(class_reference)
|
77
|
+
|
78
|
+
message = format(MSG, style: style)
|
79
|
+
expression = class_reference.loc.expression
|
80
|
+
|
81
|
+
add_offense(expression, message: message) do |corrector|
|
82
|
+
violation = class_reference.children.last.to_s
|
83
|
+
corrector.replace(expression, correct_style(violation))
|
84
|
+
|
85
|
+
opposite_style_detected
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def opposing_style?(class_reference)
|
93
|
+
class_reference_style = REFERENCE_TYPE_STYLES[class_reference.type]
|
94
|
+
|
95
|
+
# Only enforce supported styles
|
96
|
+
return false unless class_reference_style
|
97
|
+
|
98
|
+
class_reference_style != style
|
99
|
+
end
|
100
|
+
|
101
|
+
def correct_style(violation)
|
102
|
+
if style == :string
|
103
|
+
"'#{violation}'"
|
104
|
+
else
|
105
|
+
violation
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -97,6 +97,7 @@ require_relative 'rspec/subject_stub'
|
|
97
97
|
require_relative 'rspec/unspecified_exception'
|
98
98
|
require_relative 'rspec/variable_definition'
|
99
99
|
require_relative 'rspec/variable_name'
|
100
|
+
require_relative 'rspec/verified_double_reference'
|
100
101
|
require_relative 'rspec/verified_doubles'
|
101
102
|
require_relative 'rspec/void_expect'
|
102
103
|
require_relative 'rspec/yield'
|
data/lib/rubocop-rspec.rb
CHANGED
@@ -37,16 +37,21 @@ require_relative 'rubocop/cop/rspec_cops'
|
|
37
37
|
# We have to register our autocorrect incompatibilities in RuboCop's cops
|
38
38
|
# as well so we do not hit infinite loops
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
[RSpec::AlignLeftLetBrace, RSpec::AlignRightLetBrace]
|
46
|
-
end
|
47
|
-
end
|
40
|
+
RuboCop::Cop::Layout::ExtraSpacing.singleton_class.prepend(
|
41
|
+
Module.new do
|
42
|
+
def autocorrect_incompatible_with
|
43
|
+
super.push(RuboCop::Cop::RSpec::AlignLeftLetBrace)
|
44
|
+
.push(RuboCop::Cop::RSpec::AlignRightLetBrace)
|
48
45
|
end
|
49
46
|
end
|
50
|
-
|
47
|
+
)
|
48
|
+
|
49
|
+
RuboCop::Cop::Style::TrailingCommaInArguments.singleton_class.prepend(
|
50
|
+
Module.new do
|
51
|
+
def autocorrect_incompatible_with
|
52
|
+
super.push(RuboCop::Cop::RSpec::Capybara::CurrentPathExpectation)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
)
|
51
56
|
|
52
57
|
RuboCop::AST::Node.include(RuboCop::RSpec::Node)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Backus
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2022-
|
13
|
+
date: 2022-04-19 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rubocop
|
@@ -226,6 +226,7 @@ files:
|
|
226
226
|
- lib/rubocop/cop/rspec/unspecified_exception.rb
|
227
227
|
- lib/rubocop/cop/rspec/variable_definition.rb
|
228
228
|
- lib/rubocop/cop/rspec/variable_name.rb
|
229
|
+
- lib/rubocop/cop/rspec/verified_double_reference.rb
|
229
230
|
- lib/rubocop/cop/rspec/verified_doubles.rb
|
230
231
|
- lib/rubocop/cop/rspec/void_expect.rb
|
231
232
|
- lib/rubocop/cop/rspec/yield.rb
|