rubocop-vicenzo 0.10.0 → 0.12.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 +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +34 -12
- data/README.md +1 -1
- data/config/default.yml +32 -1
- data/config/style.yml +19 -1
- data/lib/rubocop/cop/vicenzo/naming/short_name.rb +27 -1
- data/lib/rubocop/cop/vicenzo/rspec/lambda_premise.rb +236 -0
- data/lib/rubocop/cop/vicenzo/rspec/nested_context_improper_start.rb +3 -4
- data/lib/rubocop/cop/vicenzo/rspec/scenario_describe.rb +184 -0
- data/lib/rubocop/cop/vicenzo/rspec/shared_premises.rb +120 -0
- data/lib/rubocop/cop/vicenzo/rspec/unnested_context_improper_start.rb +3 -3
- data/lib/rubocop/cop/vicenzo_cops.rb +3 -0
- data/lib/rubocop/vicenzo/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fdd24a07de899f160e3348295372bec2487ff29d33a9cd6db71478eec020bc8b
|
|
4
|
+
data.tar.gz: b360bb5a873d11650ebb79bd8c9ce0c2b7d4677f6d8d88bff5aa237298414295
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d7328c0cd9d8b76a2c59abbc5ce1a10d1e809c11321c134506733f8fe6b00084cb6eb466b241b7d2fd7e46badf2d8de672e6a1b492f25c555e42cc6cd88f94dc
|
|
7
|
+
data.tar.gz: 0c1e78145f6cf3c45b23a2d9e49ed418ebad1fdd57da123ec27683a6e16eb10d052004b85d3fe7adfe1e3982de986cbe923333da42fe4c4dba216ebbf3f1d5d3
|
data/CHANGELOG.md
CHANGED
|
@@ -1,50 +1,72 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.12.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.11.0...v0.12.0) (2026-12-02)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add Vicenzo/RSpec/LambdaPremise cop ([#61](https://github.com/bvicenzo/rubocop-vicenzo/pull/61)) ([87a11c0](https://github.com/bvicenzo/rubocop-vicenzo/commit/87a11c00f87c2bd299898952293987369e64194d))
|
|
9
|
+
* add Vicenzo/RSpec/SharedPremises cop ([#57](https://github.com/bvicenzo/rubocop-vicenzo/pull/57)) ([f6610f8](https://github.com/bvicenzo/rubocop-vicenzo/commit/f6610f88d66ad6d870802e55b2dad829f87fcbd8))
|
|
10
|
+
|
|
11
|
+
## [0.11.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.10.0...v0.11.0) (2026-11-18)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* add Vicenzo/RSpec/ScenarioDescribe cop ([#54](https://github.com/bvicenzo/rubocop-vicenzo/pull/54)) ([df050f7](https://github.com/bvicenzo/rubocop-vicenzo/commit/df050f7e013b029a009c7445b30de739132de4b4)), closes [#48](https://github.com/bvicenzo/rubocop-vicenzo/issues/48)
|
|
17
|
+
* allow whole short words in Vicenzo/Naming/ShortName via AllowedNames ([#51](https://github.com/bvicenzo/rubocop-vicenzo/pull/51)) ([972ff35](https://github.com/bvicenzo/rubocop-vicenzo/commit/972ff35e376b4364876b7638d2838d5593e69ea9)), closes [#47](https://github.com/bvicenzo/rubocop-vicenzo/issues/47)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* remove 'however' from the context prefix vocabulary ([#49](https://github.com/bvicenzo/rubocop-vicenzo/pull/49)) ([0c46397](https://github.com/bvicenzo/rubocop-vicenzo/commit/0c46397b7fd04c943ae0f974d26f48cb80de17f0)), closes [#46](https://github.com/bvicenzo/rubocop-vicenzo/issues/46)
|
|
23
|
+
* report nested contexts whose forbidden first word is followed by punctuation ([#52](https://github.com/bvicenzo/rubocop-vicenzo/pull/52)) ([e103531](https://github.com/bvicenzo/rubocop-vicenzo/commit/e1035314d769f9e3a68cb6ba3d5ce98eab588bf4)), closes [#50](https://github.com/bvicenzo/rubocop-vicenzo/issues/50)
|
|
24
|
+
|
|
3
25
|
## [0.10.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.9.0...v0.10.0) (2026-10-23)
|
|
4
26
|
|
|
5
27
|
|
|
6
28
|
### Features
|
|
7
29
|
|
|
8
|
-
* add Vicenzo/RSpec/LetAliasingSubject cop ([#44](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
9
|
-
* add Vicenzo/Style/PreferManyOverSizeComparison cop ([#38](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
30
|
+
* add Vicenzo/RSpec/LetAliasingSubject cop ([#44](https://github.com/bvicenzo/rubocop-vicenzo/pull/44)) ([a8cebd3](https://github.com/bvicenzo/rubocop-vicenzo/commit/a8cebd3c0237e8805a58ff9efa7ca34f6beb34b0)), closes [#42](https://github.com/bvicenzo/rubocop-vicenzo/issues/42)
|
|
31
|
+
* add Vicenzo/Style/PreferManyOverSizeComparison cop ([#38](https://github.com/bvicenzo/rubocop-vicenzo/pull/38)) ([910ea02](https://github.com/bvicenzo/rubocop-vicenzo/commit/910ea026f7e4c46f9de27db580e6330cf845bfe7))
|
|
10
32
|
|
|
11
33
|
|
|
12
34
|
### Bug Fixes
|
|
13
35
|
|
|
14
|
-
* report subjects that derive from an allowed method in SubjectIsMethodResult ([#41](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
15
|
-
* see subjects declared in shared groups in LetIsMethodResult and LetAliasingSubject ([#45](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
36
|
+
* report subjects that derive from an allowed method in SubjectIsMethodResult ([#41](https://github.com/bvicenzo/rubocop-vicenzo/pull/41)) ([a5efd33](https://github.com/bvicenzo/rubocop-vicenzo/commit/a5efd3330b32dd24b4291121a64e378091297a57)), closes [#40](https://github.com/bvicenzo/rubocop-vicenzo/issues/40)
|
|
37
|
+
* see subjects declared in shared groups in LetIsMethodResult and LetAliasingSubject ([#45](https://github.com/bvicenzo/rubocop-vicenzo/pull/45)) ([a98039e](https://github.com/bvicenzo/rubocop-vicenzo/commit/a98039e2cbea568267d8b7832b4717b763678d14)), closes [#43](https://github.com/bvicenzo/rubocop-vicenzo/issues/43)
|
|
16
38
|
|
|
17
39
|
## [0.9.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.8.0...v0.9.0) (2026-09-23)
|
|
18
40
|
|
|
19
41
|
|
|
20
42
|
### Features
|
|
21
43
|
|
|
22
|
-
* add Vicenzo/Naming/AnonymousUnusedName cop ([#33](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
23
|
-
* add Vicenzo/Naming/ShortName cop and an opinionated config/style.yml ([#35](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
24
|
-
* add Vicenzo/RSpec/DescribeInsideContext cop ([#37](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
25
|
-
* add Vicenzo/RSpec/UnnestedContextImproperStart cop ([#36](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
44
|
+
* add Vicenzo/Naming/AnonymousUnusedName cop ([#33](https://github.com/bvicenzo/rubocop-vicenzo/pull/33)) ([fabbad6](https://github.com/bvicenzo/rubocop-vicenzo/commit/fabbad655dac0c362924dd0aaf245abd2c7023e1))
|
|
45
|
+
* add Vicenzo/Naming/ShortName cop and an opinionated config/style.yml ([#35](https://github.com/bvicenzo/rubocop-vicenzo/pull/35)) ([1b67f46](https://github.com/bvicenzo/rubocop-vicenzo/commit/1b67f4643b4348f5768a8fbb3b88cea35046e5e2))
|
|
46
|
+
* add Vicenzo/RSpec/DescribeInsideContext cop ([#37](https://github.com/bvicenzo/rubocop-vicenzo/pull/37)) ([3f3d6ae](https://github.com/bvicenzo/rubocop-vicenzo/commit/3f3d6aeb95d087e32594e8ded3d1ce0e3715dee7))
|
|
47
|
+
* add Vicenzo/RSpec/UnnestedContextImproperStart cop ([#36](https://github.com/bvicenzo/rubocop-vicenzo/pull/36)) ([0a8fb0f](https://github.com/bvicenzo/rubocop-vicenzo/commit/0a8fb0fec62370ac331b9a3275b95a0b22998b1d))
|
|
26
48
|
|
|
27
49
|
## [0.8.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.7.0...v0.8.0) (2026-09-09)
|
|
28
50
|
|
|
29
51
|
|
|
30
52
|
### Features
|
|
31
53
|
|
|
32
|
-
* add RSpec cops against top-level declarations ([#31](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
54
|
+
* add RSpec cops against top-level declarations ([#31](https://github.com/bvicenzo/rubocop-vicenzo/pull/31)) ([ef5e207](https://github.com/bvicenzo/rubocop-vicenzo/commit/ef5e20728c0c4bc94dbb5eec54a48ff5197d1b25))
|
|
33
55
|
|
|
34
56
|
## [0.7.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.6.0...v0.7.0) (2026-08-31)
|
|
35
57
|
|
|
36
58
|
|
|
37
59
|
### Features
|
|
38
60
|
|
|
39
|
-
* add RSpec cops against subjects hidden in lets ([#30](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
40
|
-
* add Vicenzo/RSpec/SubjectIsMethodResult cop ([#28](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
61
|
+
* add RSpec cops against subjects hidden in lets ([#30](https://github.com/bvicenzo/rubocop-vicenzo/pull/30)) ([9daf259](https://github.com/bvicenzo/rubocop-vicenzo/commit/9daf25972252fdfdb92a51bba9cfe70e63446d17))
|
|
62
|
+
* add Vicenzo/RSpec/SubjectIsMethodResult cop ([#28](https://github.com/bvicenzo/rubocop-vicenzo/pull/28)) ([b69245a](https://github.com/bvicenzo/rubocop-vicenzo/commit/b69245af973f3341bbe62bca628333c5acb909d7))
|
|
41
63
|
|
|
42
64
|
## [0.6.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.5.0...v0.6.0) (2026-08-19)
|
|
43
65
|
|
|
44
66
|
|
|
45
67
|
### Features
|
|
46
68
|
|
|
47
|
-
* add RSpec cops against mutated and derived premises ([#26](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
69
|
+
* add RSpec cops against mutated and derived premises ([#26](https://github.com/bvicenzo/rubocop-vicenzo/pull/26)) ([b138dd0](https://github.com/bvicenzo/rubocop-vicenzo/commit/b138dd00a76e1bcd62e95c1f2c51dca5b4232319))
|
|
48
70
|
|
|
49
71
|
## [0.5.0] - 2026-06-29
|
|
50
72
|
|
data/README.md
CHANGED
|
@@ -11,7 +11,7 @@ Add it to the `development`/`test` group of your `Gemfile`:
|
|
|
11
11
|
<!-- x-release-please-start-version -->
|
|
12
12
|
```ruby
|
|
13
13
|
group :development, :test do
|
|
14
|
-
gem 'rubocop-vicenzo', '~> 0.
|
|
14
|
+
gem 'rubocop-vicenzo', '~> 0.12.0', require: false
|
|
15
15
|
end
|
|
16
16
|
```
|
|
17
17
|
<!-- x-release-please-end-version -->
|
data/config/default.yml
CHANGED
|
@@ -16,6 +16,7 @@ Vicenzo/Naming/ShortName:
|
|
|
16
16
|
Enabled: true
|
|
17
17
|
Severity: convention
|
|
18
18
|
MinNameLength: 3
|
|
19
|
+
AllowedNames: []
|
|
19
20
|
VersionAdded: '0.9.0'
|
|
20
21
|
|
|
21
22
|
Vicenzo/Rails/EnumInclusionOfValidation:
|
|
@@ -87,6 +88,17 @@ Vicenzo/RSpec/IterationInsideExample:
|
|
|
87
88
|
Severity: warning
|
|
88
89
|
VersionAdded: '0.4.0'
|
|
89
90
|
|
|
91
|
+
Vicenzo/RSpec/LambdaPremise:
|
|
92
|
+
Description: 'Do not store a callable in a premise and call it from the spec: a premise declares a value.'
|
|
93
|
+
Enabled: true
|
|
94
|
+
Severity: warning
|
|
95
|
+
Include:
|
|
96
|
+
- '**/spec/**/*_spec.rb'
|
|
97
|
+
Exclude:
|
|
98
|
+
- '**/spec/support/**/*'
|
|
99
|
+
- '**/spec/factories/**/*'
|
|
100
|
+
VersionAdded: '0.12.0'
|
|
101
|
+
|
|
90
102
|
Vicenzo/RSpec/MutatedPremises:
|
|
91
103
|
Description: 'Do not mutate the premises (let, let_it_be, subject) an example starts from.'
|
|
92
104
|
Enabled: true
|
|
@@ -150,6 +162,26 @@ Vicenzo/RSpec/LetIsMethodResult:
|
|
|
150
162
|
- '**/spec/factories/**/*'
|
|
151
163
|
VersionAdded: '0.7.0'
|
|
152
164
|
|
|
165
|
+
Vicenzo/RSpec/ScenarioDescribe:
|
|
166
|
+
Description: 'A describe exercising an entry point a sibling describe already names is a scenario of it, and belongs in a context inside that describe.'
|
|
167
|
+
Enabled: true
|
|
168
|
+
Severity: warning
|
|
169
|
+
Include:
|
|
170
|
+
- '**/spec/**/*_spec.rb'
|
|
171
|
+
Exclude:
|
|
172
|
+
- '**/spec/support/**/*'
|
|
173
|
+
- '**/spec/factories/**/*'
|
|
174
|
+
VersionAdded: '0.11.0'
|
|
175
|
+
|
|
176
|
+
Vicenzo/RSpec/SharedPremises:
|
|
177
|
+
Description: 'Do not share premises through a shared context; declare them in the scenario that reads them, and share only complete behaviour through shared examples.'
|
|
178
|
+
Enabled: true
|
|
179
|
+
Severity: warning
|
|
180
|
+
AllowedContexts: []
|
|
181
|
+
Include:
|
|
182
|
+
- '**/spec/**/*.rb'
|
|
183
|
+
VersionAdded: '0.12.0'
|
|
184
|
+
|
|
153
185
|
Vicenzo/RSpec/SubjectDefinedAsLet:
|
|
154
186
|
Description: 'A let the expectations assert on, in a group declaring no subject, must be declared as the subject.'
|
|
155
187
|
Enabled: true
|
|
@@ -236,7 +268,6 @@ Vicenzo/RSpec/UnnestedContextImproperStart:
|
|
|
236
268
|
ForbiddenPrefixes:
|
|
237
269
|
- and
|
|
238
270
|
- but
|
|
239
|
-
- however
|
|
240
271
|
Include:
|
|
241
272
|
- '**/spec/**/*_spec.rb'
|
|
242
273
|
Exclude:
|
data/config/style.yml
CHANGED
|
@@ -130,11 +130,29 @@ Naming/RescuedExceptionsVariableName:
|
|
|
130
130
|
Enabled: true
|
|
131
131
|
PreferredName: error
|
|
132
132
|
|
|
133
|
+
# Short names that are whole English words, such as `to` as the upper bound of a
|
|
134
|
+
# range. Only whole words belong here: the abbreviations in the core
|
|
135
|
+
# Naming/MethodParameterName default (`db`, `io`, `ip`, `os`, `pp`, `cc`) are left
|
|
136
|
+
# out on purpose, since they are exactly what the rule is meant to catch.
|
|
137
|
+
Vicenzo/Naming/ShortName:
|
|
138
|
+
AllowedNames:
|
|
139
|
+
- to
|
|
140
|
+
- at
|
|
141
|
+
- by
|
|
142
|
+
- of
|
|
143
|
+
- on
|
|
144
|
+
|
|
133
145
|
# Vicenzo/Naming/ShortName covers local variables. Block parameters are the core
|
|
134
146
|
# cop's job, but it ships with MinNameLength: 1 and therefore never fires — raise
|
|
135
|
-
# it so both halves of the rule agree.
|
|
147
|
+
# it, and allow the same whole words, so both halves of the rule agree.
|
|
136
148
|
Naming/BlockParameterName:
|
|
137
149
|
MinNameLength: 3
|
|
150
|
+
AllowedNames:
|
|
151
|
+
- to
|
|
152
|
+
- at
|
|
153
|
+
- by
|
|
154
|
+
- of
|
|
155
|
+
- on
|
|
138
156
|
|
|
139
157
|
###### METRICS #####
|
|
140
158
|
|
|
@@ -26,6 +26,12 @@ module RuboCop
|
|
|
26
26
|
# chosen, and it is that signature the parameter cops point at. The fix
|
|
27
27
|
# cascades from there.
|
|
28
28
|
#
|
|
29
|
+
# `AllowedNames` accepts short names that are whole words of the domain,
|
|
30
|
+
# such as `to` as the upper bound of a range. It is not a place for
|
|
31
|
+
# abbreviations (`db`, `io`): those are exactly what the cop is meant to
|
|
32
|
+
# catch. Leading underscores are ignored when matching, so `_to` is
|
|
33
|
+
# allowed along with `to`.
|
|
34
|
+
#
|
|
29
35
|
# @example
|
|
30
36
|
# # bad
|
|
31
37
|
# v = shelf.capacity
|
|
@@ -71,6 +77,14 @@ module RuboCop
|
|
|
71
77
|
#
|
|
72
78
|
# # good
|
|
73
79
|
# book_title = build
|
|
80
|
+
#
|
|
81
|
+
# @example AllowedNames: [to]
|
|
82
|
+
# # bad
|
|
83
|
+
# at = schedule.first
|
|
84
|
+
#
|
|
85
|
+
# # good
|
|
86
|
+
# from = options.fetch(:from)
|
|
87
|
+
# to = options.fetch(:to)
|
|
74
88
|
class ShortName < Base
|
|
75
89
|
MSG = 'Name `%<name>s` spells %<length>d letter(s); at least %<minimum>d are required. ' \
|
|
76
90
|
'A letter or an abbreviation carries no meaning to the next reader — write the ' \
|
|
@@ -81,11 +95,13 @@ module RuboCop
|
|
|
81
95
|
|
|
82
96
|
ANONYMOUS_NAME_PATTERN = /\A_+\z/
|
|
83
97
|
|
|
98
|
+
LEADING_UNDERSCORES_PATTERN = /\A_+/
|
|
99
|
+
|
|
84
100
|
SCOPE_TYPES = %i[def defs block numblock itblock].freeze
|
|
85
101
|
|
|
86
102
|
def on_lvasgn(node)
|
|
87
103
|
name = node.children.first
|
|
88
|
-
return if anonymous?(name) || rescued_error?(node) || forwarded_by_shorthand?(node, name)
|
|
104
|
+
return if anonymous?(name) || allowed?(name) || rescued_error?(node) || forwarded_by_shorthand?(node, name)
|
|
89
105
|
|
|
90
106
|
length = spelled_length(name)
|
|
91
107
|
return if length >= min_name_length
|
|
@@ -105,6 +121,12 @@ module RuboCop
|
|
|
105
121
|
ANONYMOUS_NAME_PATTERN.match?(name.to_s)
|
|
106
122
|
end
|
|
107
123
|
|
|
124
|
+
# Leading underscores are stripped before the lookup, as the core
|
|
125
|
+
# `Naming/MethodParameterName` does, so `_to` is allowed along with `to`.
|
|
126
|
+
def allowed?(name)
|
|
127
|
+
allowed_names.include?(name.to_s.sub(LEADING_UNDERSCORES_PATTERN, ''))
|
|
128
|
+
end
|
|
129
|
+
|
|
108
130
|
def rescued_error?(node)
|
|
109
131
|
parent = node.parent
|
|
110
132
|
parent&.resbody_type? && parent.exception_variable == node
|
|
@@ -126,6 +148,10 @@ module RuboCop
|
|
|
126
148
|
def min_name_length
|
|
127
149
|
cop_config.fetch('MinNameLength', DEFAULT_MIN_NAME_LENGTH)
|
|
128
150
|
end
|
|
151
|
+
|
|
152
|
+
def allowed_names
|
|
153
|
+
cop_config.fetch('AllowedNames', [])
|
|
154
|
+
end
|
|
129
155
|
end
|
|
130
156
|
end
|
|
131
157
|
end
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'mixin/premise_tracking'
|
|
4
|
+
|
|
5
|
+
module RuboCop
|
|
6
|
+
module Cop
|
|
7
|
+
module Vicenzo
|
|
8
|
+
module RSpec
|
|
9
|
+
# Do not store a callable in a premise and call it from the spec.
|
|
10
|
+
#
|
|
11
|
+
# A premise (`let`, `let!`, `let_it_be`, `subject`) declares the value an example starts from. A premise whose
|
|
12
|
+
# value is a lambda or a proc the spec itself calls is not a value: it is a method dressed as a premise.
|
|
13
|
+
#
|
|
14
|
+
# 1. It takes arguments, so the declaration does not say which records exist; the reader has to find every
|
|
15
|
+
# call site and replay it.
|
|
16
|
+
# 2. It hides creation behind an indirection exactly like a helper method would - the helper
|
|
17
|
+
# `Vicenzo/RSpec/LeakyDefinition` already keeps out of spec files.
|
|
18
|
+
# 3. It is memoized, so the callable is shared, which invites state captured in its closure.
|
|
19
|
+
#
|
|
20
|
+
# There is always a static alternative, and the one to reach for depends on what the callable does. Replacing
|
|
21
|
+
# it with a `def` in the spec, a `before` that stores it, or a hash of builders only moves the problem.
|
|
22
|
+
#
|
|
23
|
+
# Only callables written as a literal in the premise count - `-> {}`, `lambda {}`, `proc {}`, `Proc.new {}`,
|
|
24
|
+
# or a hash or array holding nothing but those - and only when the spec invokes them (`call`, `.()`, `[]`,
|
|
25
|
+
# `yield`, after a `curry` or after reading one element of the collection). A callable handed to the unit
|
|
26
|
+
# under test is data, like a string or a number: the spec never calls it, production code does. A callable
|
|
27
|
+
# that a method returns is the unit under test itself, and calling it is the test. A block that only hands its
|
|
28
|
+
# parameters over to the callable (`middleware.call(job) { execution.call }`) is the same as passing it with
|
|
29
|
+
# `&`, so the object receiving the block is the one calling it.
|
|
30
|
+
#
|
|
31
|
+
# @example
|
|
32
|
+
# # bad - the records differ by a parameter
|
|
33
|
+
#
|
|
34
|
+
# let(:warehouse_at) do
|
|
35
|
+
# ->(address_trait, name) { create(:warehouse, name:, address: create(:address, address_trait)) }
|
|
36
|
+
# end
|
|
37
|
+
# let(:stops) { [build(:stop, warehouse: warehouse_at.(:downtown, 'Central'))] }
|
|
38
|
+
#
|
|
39
|
+
# # good - the variation lives in the factory, the record is created where it is read
|
|
40
|
+
#
|
|
41
|
+
# let(:stops) { [build(:stop, warehouse: create(:warehouse, name: 'Central', at: :downtown))] }
|
|
42
|
+
#
|
|
43
|
+
# @example
|
|
44
|
+
# # bad - one premise in different flavours
|
|
45
|
+
#
|
|
46
|
+
# let(:order_builder) { ->(status:) { create(:order, status:) } }
|
|
47
|
+
# let(:shipped_order) { order_builder.call(status: :shipped) }
|
|
48
|
+
#
|
|
49
|
+
# # good - one trait per flavour, or one `let` per context
|
|
50
|
+
#
|
|
51
|
+
# let(:shipped_order) { create(:order, :shipped) }
|
|
52
|
+
#
|
|
53
|
+
# @example
|
|
54
|
+
# # bad - measuring something during the example
|
|
55
|
+
#
|
|
56
|
+
# let(:count_queries) { -> { QueryCounter.count { get orders_path } } }
|
|
57
|
+
#
|
|
58
|
+
# it { expect(count_queries.call).to be <= 3 }
|
|
59
|
+
#
|
|
60
|
+
# # good - a helper in `spec/support`, read explicitly by the example
|
|
61
|
+
#
|
|
62
|
+
# it { expect(query_count { get orders_path }).to be <= 3 }
|
|
63
|
+
#
|
|
64
|
+
# @example
|
|
65
|
+
# # bad - the subject defers a call to the unit under test
|
|
66
|
+
#
|
|
67
|
+
# subject(:query) { -> { described_class.call } }
|
|
68
|
+
#
|
|
69
|
+
# it { expect { query.call }.not_to raise_error }
|
|
70
|
+
#
|
|
71
|
+
# # good - the unit is the subject, and the example calls it
|
|
72
|
+
#
|
|
73
|
+
# subject(:query) { described_class.new }
|
|
74
|
+
#
|
|
75
|
+
# it { expect { query.call }.not_to raise_error }
|
|
76
|
+
#
|
|
77
|
+
# @example
|
|
78
|
+
# # good - the callable is handed to the unit under test, which is the one calling it
|
|
79
|
+
#
|
|
80
|
+
# subject(:report) { Report.new(rows:, formatter:) }
|
|
81
|
+
#
|
|
82
|
+
# let(:formatter) { ->(value) { value.to_s.upcase } }
|
|
83
|
+
class LambdaPremise < RuboCop::Cop::RSpec::Base
|
|
84
|
+
include PremiseTracking
|
|
85
|
+
|
|
86
|
+
MSG = '`%<name>s` is a method dressed as a premise: a premise declares a value, it does not build one on ' \
|
|
87
|
+
'demand. If the records it builds differ only by a parameter, move that variation into the factory ' \
|
|
88
|
+
'(a trait for the shared state, a transient for the parameter) and create each record where it is ' \
|
|
89
|
+
'read. If it builds one premise in different flavours, declare one `let` per context or one trait ' \
|
|
90
|
+
'per flavour. If it measures or computes something during the example, extract a helper to ' \
|
|
91
|
+
'`spec/support`. If it defers a call to the unit under test, make the unit the subject and call ' \
|
|
92
|
+
'the method in the example. Do not replace it with a `def` in the spec, a `before` that stores it, ' \
|
|
93
|
+
'or a hash of builders.'
|
|
94
|
+
|
|
95
|
+
# `.()` is `call` in the AST.
|
|
96
|
+
INVOKERS = %i[call [] yield].freeze
|
|
97
|
+
CURRY = Set[:curry].freeze
|
|
98
|
+
ELEMENT_READERS = Set[:[], :fetch, :dig, :first, :last].freeze
|
|
99
|
+
|
|
100
|
+
# @!method callable_literal?(node)
|
|
101
|
+
def_node_matcher :callable_literal?, <<~PATTERN
|
|
102
|
+
(any_block
|
|
103
|
+
{
|
|
104
|
+
(lambda)
|
|
105
|
+
(send nil? {:lambda :proc})
|
|
106
|
+
(send (const {nil? cbase} :Proc) :new)
|
|
107
|
+
}
|
|
108
|
+
...
|
|
109
|
+
)
|
|
110
|
+
PATTERN
|
|
111
|
+
|
|
112
|
+
# @!method callable_collection?(node)
|
|
113
|
+
# A hash or an array holding nothing but callable literals - at least one, and no splat.
|
|
114
|
+
def_node_matcher :callable_collection?, <<~PATTERN
|
|
115
|
+
{
|
|
116
|
+
(hash (pair _ #callable_literal?)+)
|
|
117
|
+
(array #callable_literal?+)
|
|
118
|
+
}
|
|
119
|
+
PATTERN
|
|
120
|
+
|
|
121
|
+
# @!method premise_reference?(node, name)
|
|
122
|
+
# A bare call to the premise. A local variable or a block parameter with the same name is a `lvar`, so it
|
|
123
|
+
# never passes for the premise.
|
|
124
|
+
def_node_matcher :premise_reference?, <<~PATTERN
|
|
125
|
+
(send nil? %1)
|
|
126
|
+
PATTERN
|
|
127
|
+
|
|
128
|
+
# @!method read_from_premise?(node, name, methods)
|
|
129
|
+
# A call on the premise that hands back something to invoke: `name.curry`, `name[:key]`, `name.first`.
|
|
130
|
+
def_node_matcher :read_from_premise?, <<~PATTERN
|
|
131
|
+
(call (send nil? %1) %2 ...)
|
|
132
|
+
PATTERN
|
|
133
|
+
|
|
134
|
+
def on_block(node)
|
|
135
|
+
return unless spec_group?(node)
|
|
136
|
+
|
|
137
|
+
each_child_block(node) do |child|
|
|
138
|
+
check_premise(child, node) if premise?(child)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
alias on_numblock on_block
|
|
143
|
+
|
|
144
|
+
private
|
|
145
|
+
|
|
146
|
+
def check_premise(premise, group)
|
|
147
|
+
name = declared_name(premise)
|
|
148
|
+
kind = callable_kind(value_of(premise))
|
|
149
|
+
|
|
150
|
+
return if name.nil? || kind.nil?
|
|
151
|
+
return unless invoked?(name, kind, visible_scopes(group))
|
|
152
|
+
|
|
153
|
+
add_offense(premise.send_node, message: format(MSG, name:))
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# An anonymous `subject!` is still read as `subject`.
|
|
157
|
+
def declared_name(premise)
|
|
158
|
+
name = premise_name(premise)&.to_sym
|
|
159
|
+
|
|
160
|
+
name == :subject! ? :subject : name
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# The value is the last expression of the premise body.
|
|
164
|
+
def value_of(premise)
|
|
165
|
+
body = premise.body
|
|
166
|
+
|
|
167
|
+
body&.begin_type? ? body.children.last : body
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def callable_kind(value)
|
|
171
|
+
return :callable if callable_literal?(value)
|
|
172
|
+
|
|
173
|
+
:collection if callable_collection?(value)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Where the premise can be invoked from: the group declaring it, with everything below it, and the hooks
|
|
177
|
+
# and premises of its ancestors, which run in its scope - the "abstract let" shape. The ancestors' examples
|
|
178
|
+
# do not: they never see a premise declared below them.
|
|
179
|
+
def visible_scopes(group)
|
|
180
|
+
ancestors = group.each_ancestor(:any_block).select { |ancestor| spec_group?(ancestor) }
|
|
181
|
+
|
|
182
|
+
[group, *ancestors.flat_map { |ancestor| inherited_definitions(ancestor) }]
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def inherited_definitions(group)
|
|
186
|
+
definitions = []
|
|
187
|
+
|
|
188
|
+
each_child_block(group) { |child| definitions << child if hook?(child) || premise?(child) }
|
|
189
|
+
|
|
190
|
+
definitions
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def invoked?(name, kind, scopes)
|
|
194
|
+
scopes.any? do |scope|
|
|
195
|
+
scope.each_node(:call).any? { |node| invocation?(node, name, kind) && !forwarded_block?(node) }
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def invocation?(node, name, kind)
|
|
200
|
+
return false unless INVOKERS.include?(node.method_name)
|
|
201
|
+
|
|
202
|
+
receiver = node.receiver
|
|
203
|
+
|
|
204
|
+
if kind == :callable
|
|
205
|
+
premise_reference?(receiver, name) || read_from_premise?(receiver, name, CURRY)
|
|
206
|
+
else
|
|
207
|
+
read_from_premise?(receiver, name, ELEMENT_READERS)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# `middleware.call(job) { execution.call }` and `rows.map { |row| builder.(row) }` hand the callable to
|
|
212
|
+
# another object, as `&execution` and `&builder` would: the spec does not call it, the object does. A block
|
|
213
|
+
# handed to a receiverless call - a helper such as `query_count { report.call }`, or `expect { ... }` -
|
|
214
|
+
# still has the spec calling it.
|
|
215
|
+
def forwarded_block?(invocation)
|
|
216
|
+
block = invocation.parent
|
|
217
|
+
|
|
218
|
+
return false unless block.block_type? && block.body.equal?(invocation) && block.receiver
|
|
219
|
+
|
|
220
|
+
forwards_parameters?(block, invocation.arguments)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def forwards_parameters?(block, arguments)
|
|
224
|
+
parameters = block.arguments.children
|
|
225
|
+
|
|
226
|
+
return false unless parameters.all?(&:arg_type?) && parameters.size == arguments.size
|
|
227
|
+
|
|
228
|
+
parameters.zip(arguments).all? do |parameter, argument|
|
|
229
|
+
argument.lvar_type? && argument.children.first == parameter.name
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
@@ -5,7 +5,7 @@ module RuboCop
|
|
|
5
5
|
module Vicenzo
|
|
6
6
|
module RSpec
|
|
7
7
|
# Checks for nested `context` blocks where the inner `context`
|
|
8
|
-
# starts with "when", "with", or "without". It suggests replacing it with "and"
|
|
8
|
+
# starts with "when", "with", or "without". It suggests replacing it with "and" or "but".
|
|
9
9
|
#
|
|
10
10
|
# @example
|
|
11
11
|
# # bad
|
|
@@ -22,7 +22,7 @@ module RuboCop
|
|
|
22
22
|
# end
|
|
23
23
|
# end
|
|
24
24
|
class NestedContextImproperStart < RuboCop::Cop::RSpec::Base
|
|
25
|
-
MSG = 'Nested `context` should start with `and
|
|
25
|
+
MSG = 'Nested `context` should start with `and` or `but`, not `when`, `with`, or `without`.'
|
|
26
26
|
|
|
27
27
|
FORBIDDEN_PREFIXES = %w[when with without].freeze
|
|
28
28
|
|
|
@@ -59,8 +59,7 @@ module RuboCop
|
|
|
59
59
|
|
|
60
60
|
return unless description_node&.str_type?
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
first_word = text.split.first&.downcase
|
|
62
|
+
first_word = description_node.value.lstrip[/\A[[:alpha:]]+/]&.downcase
|
|
64
63
|
|
|
65
64
|
return unless FORBIDDEN_PREFIXES.include?(first_word)
|
|
66
65
|
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'describe_inside_context'
|
|
4
|
+
require_relative 'mixin/described_method'
|
|
5
|
+
require_relative 'mixin/premise_tracking'
|
|
6
|
+
require_relative 'mixin/subject_reference'
|
|
7
|
+
|
|
8
|
+
module RuboCop
|
|
9
|
+
module Cop
|
|
10
|
+
module Vicenzo
|
|
11
|
+
module RSpec
|
|
12
|
+
# A describe that exercises an entry point a sibling describe already names is a scenario of that entry
|
|
13
|
+
# point, escaped into a group of its own. Make it a context inside the describe naming the entry point.
|
|
14
|
+
#
|
|
15
|
+
# A describe names *what* is exercised, never *when*. A scenario usually escapes because the premises of
|
|
16
|
+
# the entry point's describe - a temporary directory, a `before` writing a file - did not fit it. Those
|
|
17
|
+
# premises belong in the contexts that read them: move them down, and the scenario fits back in. Left
|
|
18
|
+
# outside, the tree claims the entry point has fewer cases than it has, and the escaped one never states
|
|
19
|
+
# its premise as a context sentence.
|
|
20
|
+
#
|
|
21
|
+
# Only describes that name nothing are checked: a describe naming a class, an instance method
|
|
22
|
+
# (`'#name'`) or a class method (`'.name'`) states what it exercises, even when it calls another entry
|
|
23
|
+
# point to build its premises. Groups such as `describe 'GET /users'`, `describe 'enums'` or
|
|
24
|
+
# `describe 'callbacks'` are reported only when a sibling names the entry point they call.
|
|
25
|
+
#
|
|
26
|
+
# An entry point is reached by `described_class.name` (or `described_class.(...)` for `.call`) for a
|
|
27
|
+
# class method, and by `subject.name`, the named subject or `described_class.new(...).name` for an
|
|
28
|
+
# instance method. Calls inside a nested describe that names a class or an entry point belong to that
|
|
29
|
+
# describe, so they do not count.
|
|
30
|
+
#
|
|
31
|
+
# @example
|
|
32
|
+
# # bad - the reference file is a scenario of `.from_file`, outside its describe
|
|
33
|
+
# RSpec.describe ImageLoader do
|
|
34
|
+
# describe '.from_file' do
|
|
35
|
+
# let(:directory) { Dir.mktmpdir }
|
|
36
|
+
#
|
|
37
|
+
# after { FileUtils.remove_entry(directory) }
|
|
38
|
+
#
|
|
39
|
+
# context 'when the file does not exist' do
|
|
40
|
+
# it { expect { described_class.from_file(File.join(directory, 'missing.bin')) }.to raise_error(Errno::ENOENT) }
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
# describe 'loading the real reference file', :local_file do
|
|
45
|
+
# it { expect(described_class.from_file(reference_path)).to have_attributes(size: 393_232) }
|
|
46
|
+
# end
|
|
47
|
+
# end
|
|
48
|
+
#
|
|
49
|
+
# # good - every scenario of `.from_file` is a context inside it, carrying its own premises
|
|
50
|
+
# RSpec.describe ImageLoader do
|
|
51
|
+
# describe '.from_file' do
|
|
52
|
+
# context 'when the file does not exist' do
|
|
53
|
+
# let(:directory) { Dir.mktmpdir }
|
|
54
|
+
#
|
|
55
|
+
# after { FileUtils.remove_entry(directory) }
|
|
56
|
+
#
|
|
57
|
+
# it { expect { described_class.from_file(File.join(directory, 'missing.bin')) }.to raise_error(Errno::ENOENT) }
|
|
58
|
+
# end
|
|
59
|
+
#
|
|
60
|
+
# context 'when the file is the real reference file', :local_file do
|
|
61
|
+
# it { expect(described_class.from_file(reference_path)).to have_attributes(size: 393_232) }
|
|
62
|
+
# end
|
|
63
|
+
# end
|
|
64
|
+
# end
|
|
65
|
+
class ScenarioDescribe < RuboCop::Cop::RSpec::Base
|
|
66
|
+
include PremiseTracking
|
|
67
|
+
include SubjectReference
|
|
68
|
+
|
|
69
|
+
MSG = 'This `%<group>s` exercises `%<entry_point>s`, which the sibling `%<sibling>s` already names: ' \
|
|
70
|
+
'make it a `context` inside that group.'
|
|
71
|
+
|
|
72
|
+
CLASS_METHOD = '.'
|
|
73
|
+
INSTANCE_METHOD = '#'
|
|
74
|
+
|
|
75
|
+
# @!method describe_definition?(node)
|
|
76
|
+
def_node_matcher :describe_definition?, <<~PATTERN
|
|
77
|
+
(any_block (send #rspec? {#{DescribeInsideContext::DESCRIBES.map(&:inspect).join(' ')}} ...) ...)
|
|
78
|
+
PATTERN
|
|
79
|
+
|
|
80
|
+
# @!method described_class_reference?(node)
|
|
81
|
+
def_node_matcher :described_class_reference?, '(send nil? :described_class)'
|
|
82
|
+
|
|
83
|
+
# @!method new_instance?(node)
|
|
84
|
+
def_node_matcher :new_instance?, '(send (send nil? :described_class) :new ...)'
|
|
85
|
+
|
|
86
|
+
def on_block(node)
|
|
87
|
+
return unless spec_group?(node)
|
|
88
|
+
|
|
89
|
+
describes = child_describes(node)
|
|
90
|
+
named = named_entry_points(describes)
|
|
91
|
+
|
|
92
|
+
return if named.empty?
|
|
93
|
+
|
|
94
|
+
describes.each { |describe| check_candidate(describe, named) }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
alias on_numblock on_block
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
def child_describes(group)
|
|
102
|
+
describes = []
|
|
103
|
+
each_child_block(group) { |child| describes << child if describe_definition?(child) }
|
|
104
|
+
describes
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The first sibling naming each entry point, keyed by the entry point as written: `.name` or `#name`.
|
|
108
|
+
def named_entry_points(describes)
|
|
109
|
+
describes.each_with_object({}) do |describe, named|
|
|
110
|
+
entry_point = entry_point_named_by(describe)
|
|
111
|
+
named[entry_point] ||= describe if entry_point
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def check_candidate(describe, named)
|
|
116
|
+
return if names_something?(describe)
|
|
117
|
+
|
|
118
|
+
entry_point = first_reached_entry_point(describe, named)
|
|
119
|
+
return unless entry_point
|
|
120
|
+
|
|
121
|
+
add_offense(describe.send_node, message: message(describe, entry_point, named[entry_point]))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# `'.from_file'`, `'#size'` and the forms carrying a signature. A bare name, such as `'call'`, could be
|
|
125
|
+
# either, so it names nothing.
|
|
126
|
+
def entry_point_named_by(describe)
|
|
127
|
+
description = describe.send_node.first_argument
|
|
128
|
+
return unless description&.str_type?
|
|
129
|
+
|
|
130
|
+
match = DescribedMethod::PREFIXED_METHOD_DESCRIPTION.match(description.value)
|
|
131
|
+
"#{description.value[0]}#{match[:name]}" if match
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# A class changes what `described_class` is below it, and an entry point is what the group exercises:
|
|
135
|
+
# either way, the group states what it is about.
|
|
136
|
+
def names_something?(describe)
|
|
137
|
+
describe.send_node.first_argument&.const_type? || !entry_point_named_by(describe).nil?
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Walks the group in source order, leaving out the nested describes that name something: what is
|
|
141
|
+
# called there belongs to them.
|
|
142
|
+
def first_reached_entry_point(node, named)
|
|
143
|
+
node.each_child_node do |child|
|
|
144
|
+
next if describe_definition?(child) && names_something?(child)
|
|
145
|
+
|
|
146
|
+
entry_point = reached_entry_point(child, named) || first_reached_entry_point(child, named)
|
|
147
|
+
return entry_point if entry_point
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
nil
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def reached_entry_point(node, named)
|
|
154
|
+
return unless node.call_type? && node.receiver
|
|
155
|
+
|
|
156
|
+
class_method = "#{CLASS_METHOD}#{node.method_name}"
|
|
157
|
+
instance_method = "#{INSTANCE_METHOD}#{node.method_name}"
|
|
158
|
+
|
|
159
|
+
if named.has_key?(class_method) && described_class_reference?(node.receiver)
|
|
160
|
+
class_method
|
|
161
|
+
elsif named.has_key?(instance_method) && instance_receiver?(node)
|
|
162
|
+
instance_method
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def instance_receiver?(node)
|
|
167
|
+
new_instance?(node.receiver) || subject_reference?(node.receiver, subject_names(node))
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def message(describe, entry_point, sibling)
|
|
171
|
+
sibling_send = sibling.send_node
|
|
172
|
+
|
|
173
|
+
format(
|
|
174
|
+
MSG,
|
|
175
|
+
group: describe.method_name,
|
|
176
|
+
entry_point:,
|
|
177
|
+
sibling: "#{sibling_send.method_name} #{sibling_send.first_argument.source}"
|
|
178
|
+
)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Vicenzo
|
|
6
|
+
module RSpec
|
|
7
|
+
# Do not share premises through a shared context. Declare them in the scenario that reads them, and
|
|
8
|
+
# share only a complete behaviour, the same for every group, through shared examples.
|
|
9
|
+
#
|
|
10
|
+
# A shared context starts as a handful of premises a few groups happen to need. Then one group
|
|
11
|
+
# needs a premise slightly different, and instead of declaring its own, it bends the shared one: a
|
|
12
|
+
# parameter, an `if`, a `case`, a `let` overridden from the outside. Every bend is a scenario
|
|
13
|
+
# hidden inside the shared context, coupling specs that have nothing to do with each other, and
|
|
14
|
+
# the reader of a group can no longer see what the group starts from without opening another file
|
|
15
|
+
# and replaying its branches.
|
|
16
|
+
#
|
|
17
|
+
# Shared examples are a different thing when they assert a whole behaviour that holds the same for
|
|
18
|
+
# every group including them, such as being timestamped or auditable. That is sharing a contract,
|
|
19
|
+
# not premises.
|
|
20
|
+
#
|
|
21
|
+
# Tests do not have to be DRY; the code does. Repeating a premise in each scenario that reads it
|
|
22
|
+
# keeps every scenario whole and free to change on its own.
|
|
23
|
+
#
|
|
24
|
+
# Both the definition (`shared_context`) and every inclusion (`include_context`) are reported:
|
|
25
|
+
# the definition is the one to remove, and each inclusion is where its premises have to land.
|
|
26
|
+
#
|
|
27
|
+
# @example
|
|
28
|
+
# # bad - premises shared across scenarios
|
|
29
|
+
# RSpec.shared_context 'with an active subscription' do
|
|
30
|
+
# let(:plan) { create(:plan, :monthly) }
|
|
31
|
+
# let(:subscription) { create(:subscription, plan:, status: :active) }
|
|
32
|
+
# end
|
|
33
|
+
#
|
|
34
|
+
# RSpec.describe Invoice do
|
|
35
|
+
# describe '#total' do
|
|
36
|
+
# context 'when the subscription is active' do
|
|
37
|
+
# include_context 'with an active subscription'
|
|
38
|
+
#
|
|
39
|
+
# it { expect(invoice.total).to eq(100) }
|
|
40
|
+
# end
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
# # good - the scenario declares what it starts from
|
|
45
|
+
# RSpec.describe Invoice do
|
|
46
|
+
# describe '#total' do
|
|
47
|
+
# context 'when the subscription is active' do
|
|
48
|
+
# let(:subscription) { create(:subscription, plan: create(:plan, :monthly), status: :active) }
|
|
49
|
+
#
|
|
50
|
+
# it { expect(invoice.total).to eq(100) }
|
|
51
|
+
# end
|
|
52
|
+
# end
|
|
53
|
+
# end
|
|
54
|
+
#
|
|
55
|
+
# # good - a complete behaviour, the same for every group including it
|
|
56
|
+
# RSpec.shared_examples 'a timestamped record' do
|
|
57
|
+
# it { is_expected.to respond_to(:created_at) }
|
|
58
|
+
# it { is_expected.to respond_to(:updated_at) }
|
|
59
|
+
# end
|
|
60
|
+
#
|
|
61
|
+
# @example AllowedContexts: ['with a frozen clock']
|
|
62
|
+
# # good - an infrastructure context allowed by name
|
|
63
|
+
# RSpec.shared_context 'with a frozen clock' do
|
|
64
|
+
# around { |example| travel_to(Time.zone.local(2026, 1, 1)) { example.run } }
|
|
65
|
+
# end
|
|
66
|
+
#
|
|
67
|
+
# RSpec.describe Invoice do
|
|
68
|
+
# include_context 'with a frozen clock'
|
|
69
|
+
# end
|
|
70
|
+
class SharedPremises < RuboCop::Cop::RSpec::Base
|
|
71
|
+
DEFINITION_MSG = 'Shared context `%<name>s` shares premises across scenarios. Declare them in each ' \
|
|
72
|
+
'scenario that reads them; share only a complete behaviour, the same for every ' \
|
|
73
|
+
'group, through shared examples. Tests do not have to be DRY; the code does.'
|
|
74
|
+
|
|
75
|
+
INCLUSION_MSG = '`%<method>s` pulls premises from shared context `%<name>s`. Declare them in this ' \
|
|
76
|
+
'scenario, so it reads whole and changes without branching the shared context. ' \
|
|
77
|
+
'Tests do not have to be DRY; the code does.'
|
|
78
|
+
|
|
79
|
+
# @!method shared_context_definition(node)
|
|
80
|
+
def_node_matcher :shared_context_definition, <<~PATTERN
|
|
81
|
+
(any_block $(send #rspec? #SharedGroups.context $_ ...) ...)
|
|
82
|
+
PATTERN
|
|
83
|
+
|
|
84
|
+
# @!method shared_context_inclusion(node)
|
|
85
|
+
def_node_matcher :shared_context_inclusion, <<~PATTERN
|
|
86
|
+
(send nil? #Includes.context $_ ...)
|
|
87
|
+
PATTERN
|
|
88
|
+
|
|
89
|
+
def on_block(node)
|
|
90
|
+
shared_context_definition(node) do |send_node, name|
|
|
91
|
+
next if allowed_context?(name)
|
|
92
|
+
|
|
93
|
+
add_offense(send_node, message: format(DEFINITION_MSG, name: context_name(name)))
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
alias on_numblock on_block
|
|
97
|
+
|
|
98
|
+
def on_send(node)
|
|
99
|
+
shared_context_inclusion(node) do |name|
|
|
100
|
+
next if allowed_context?(name)
|
|
101
|
+
|
|
102
|
+
add_offense(node, message: format(INCLUSION_MSG, method: node.method_name, name: context_name(name)))
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
alias on_csend on_send
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
def allowed_context?(name) = allowed_contexts.include?(context_name(name))
|
|
110
|
+
|
|
111
|
+
def context_name(name) = name.respond_to?(:value) ? name.value.to_s : name.source
|
|
112
|
+
|
|
113
|
+
def allowed_contexts
|
|
114
|
+
cop_config.fetch('AllowedContexts', [])
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
|
4
4
|
module Cop
|
|
5
5
|
module Vicenzo
|
|
6
6
|
module RSpec
|
|
7
|
-
# Checks for `context` blocks with no parent scenario that start with "and"
|
|
7
|
+
# Checks for `context` blocks with no parent scenario that start with "and" or "but".
|
|
8
8
|
# It suggests starting them with "when", "with", or "without" instead.
|
|
9
9
|
#
|
|
10
10
|
# A conjunction continues the scenario of the enclosing context. When the closest example group is
|
|
@@ -34,7 +34,7 @@ module RuboCop
|
|
|
34
34
|
# end
|
|
35
35
|
# end
|
|
36
36
|
#
|
|
37
|
-
# @example ForbiddenPrefixes: ['and', 'but', '
|
|
37
|
+
# @example ForbiddenPrefixes: ['and', 'but', 'also']
|
|
38
38
|
# # bad
|
|
39
39
|
# describe '#available_colors' do
|
|
40
40
|
# context 'also when the color pink is not available' do
|
|
@@ -45,7 +45,7 @@ module RuboCop
|
|
|
45
45
|
MSG = 'Unnested `context` should start with `when`, `with`, or `without`, not `%<prefix>s`.'
|
|
46
46
|
|
|
47
47
|
CONTEXTS = %i[context fcontext xcontext].freeze
|
|
48
|
-
DEFAULT_FORBIDDEN_PREFIXES = %w[and but
|
|
48
|
+
DEFAULT_FORBIDDEN_PREFIXES = %w[and but].freeze
|
|
49
49
|
|
|
50
50
|
# @!method context_definition?(node)
|
|
51
51
|
def_node_matcher :context_definition?, <<~PATTERN
|
|
@@ -7,6 +7,7 @@ require_relative 'vicenzo/rspec/describe_inside_context'
|
|
|
7
7
|
require_relative 'vicenzo/rspec/dynamic_example_generation'
|
|
8
8
|
require_relative 'vicenzo/rspec/inconsistent_sibling_structure'
|
|
9
9
|
require_relative 'vicenzo/rspec/iteration_inside_example'
|
|
10
|
+
require_relative 'vicenzo/rspec/lambda_premise'
|
|
10
11
|
require_relative 'vicenzo/rspec/let_is_method_result'
|
|
11
12
|
require_relative 'vicenzo/rspec/mutated_premises'
|
|
12
13
|
require_relative 'vicenzo/rspec/nested_context_improper_start'
|
|
@@ -14,6 +15,8 @@ require_relative 'vicenzo/rspec/nested_let_redefinition'
|
|
|
14
15
|
require_relative 'vicenzo/rspec/nested_subject_redefinition'
|
|
15
16
|
require_relative 'vicenzo/rspec/leaky_definition'
|
|
16
17
|
require_relative 'vicenzo/rspec/let_aliasing_subject'
|
|
18
|
+
require_relative 'vicenzo/rspec/scenario_describe'
|
|
19
|
+
require_relative 'vicenzo/rspec/shared_premises'
|
|
17
20
|
require_relative 'vicenzo/rspec/subject_defined_as_let'
|
|
18
21
|
require_relative 'vicenzo/rspec/subject_is_method_result'
|
|
19
22
|
require_relative 'vicenzo/rspec/top_level_context'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-vicenzo
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.12.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bruno Vicenzo
|
|
@@ -85,6 +85,7 @@ files:
|
|
|
85
85
|
- lib/rubocop/cop/vicenzo/rspec/dynamic_example_generation.rb
|
|
86
86
|
- lib/rubocop/cop/vicenzo/rspec/inconsistent_sibling_structure.rb
|
|
87
87
|
- lib/rubocop/cop/vicenzo/rspec/iteration_inside_example.rb
|
|
88
|
+
- lib/rubocop/cop/vicenzo/rspec/lambda_premise.rb
|
|
88
89
|
- lib/rubocop/cop/vicenzo/rspec/leaky_definition.rb
|
|
89
90
|
- lib/rubocop/cop/vicenzo/rspec/let_aliasing_subject.rb
|
|
90
91
|
- lib/rubocop/cop/vicenzo/rspec/let_is_method_result.rb
|
|
@@ -98,6 +99,8 @@ files:
|
|
|
98
99
|
- lib/rubocop/cop/vicenzo/rspec/nested_let_redefinition.rb
|
|
99
100
|
- lib/rubocop/cop/vicenzo/rspec/nested_subject_redefinition.rb
|
|
100
101
|
- lib/rubocop/cop/vicenzo/rspec/returned_chain.rb
|
|
102
|
+
- lib/rubocop/cop/vicenzo/rspec/scenario_describe.rb
|
|
103
|
+
- lib/rubocop/cop/vicenzo/rspec/shared_premises.rb
|
|
101
104
|
- lib/rubocop/cop/vicenzo/rspec/subject_defined_as_let.rb
|
|
102
105
|
- lib/rubocop/cop/vicenzo/rspec/subject_is_method_result.rb
|
|
103
106
|
- lib/rubocop/cop/vicenzo/rspec/top_level_context.rb
|