Skip to content

Commit 382e8ac

Browse files
authored
Merge pull request #912 from zdennis/features/file-path-cop-spec-suffix-only
Add SpecSuffixOnly option to RSpec/FilePath cop
2 parents d87cbcc + bffe20f commit 382e8ac

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Add new `RSpec/VariableName` cop. ([@tejasbubane][])
66
* Add new `RSpec/VariableDefinition` cop. ([@tejasbubane][])
77
* Expand `Capybara/VisibilityMatcher` to support more than just `have_selector`. ([@twalpole][])
8+
* Add new `SpecSuffixOnly` option to `RSpec/FilePath` cop. ([@zdennis][])
89

910
## 1.39.0 (2020-05-01)
1011

@@ -508,3 +509,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
508509
[@AlexWayfer]: https://github.com/AlexWayfer
509510
[@tejasbubane]: https://github.com/tejasbubane
510511
[@twalpole]: https://github.com/twalpole
512+
[@zdennis]: https://github.com/zdennis

config/default.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,13 @@ RSpec/ExpectOutput:
191191
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExpectOutput
192192

193193
RSpec/FilePath:
194-
Description: Checks that spec file paths are consistent with the test subject.
194+
Description: Checks that spec file paths are consistent and well-formed.
195195
Enabled: true
196196
CustomTransform:
197197
RuboCop: rubocop
198198
RSpec: rspec
199199
IgnoreMethods: false
200+
SpecSuffixOnly: false
200201
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FilePath
201202

202203
RSpec/Focus:

lib/rubocop/cop/rspec/file_path.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
module RuboCop
44
module Cop
55
module RSpec
6-
# Checks that spec file paths are consistent with the test subject.
6+
# Checks that spec file paths are consistent and well-formed.
77
#
8-
# Checks the path of the spec file and enforces that it reflects the
9-
# described class/module and its optionally called out method.
8+
# By default, this checks that spec file paths are consistent with the
9+
# test subject and and enforces that it reflects the described
10+
# class/module and its optionally called out method.
1011
#
1112
# With the configuration option `IgnoreMethods` the called out method will
1213
# be ignored when determining the enforced path.
@@ -15,6 +16,10 @@ module RSpec
1516
# be specified that should not as usual be transformed from CamelCase to
1617
# snake_case (e.g. 'RuboCop' => 'rubocop' ).
1718
#
19+
# With the configuration option `SpecSuffixOnly` test files will only
20+
# be checked to ensure they end in '_spec.rb'. This option disables
21+
# checking for consistency in the test subject or test methods.
22+
#
1823
# @example
1924
# # bad
2025
# whatever_spec.rb # describe MyClass
@@ -41,6 +46,16 @@ module RSpec
4146
# # good
4247
# my_class_spec.rb # describe MyClass, '#method'
4348
#
49+
# @example when configuration is `SpecSuffixOnly: true`
50+
# # good
51+
# whatever_spec.rb # describe MyClass
52+
#
53+
# # good
54+
# my_class_spec.rb # describe MyClass
55+
#
56+
# # good
57+
# my_class_spec.rb # describe MyClass, '#method'
58+
#
4459
class FilePath < Cop
4560
include RuboCop::RSpec::TopLevelDescribe
4661

@@ -70,9 +85,15 @@ def routing_spec?(args)
7085
end
7186

7287
def glob_for((described_class, method_name))
88+
return glob_for_spec_suffix_only? if spec_suffix_only?
89+
7390
"#{expected_path(described_class)}#{name_glob(method_name)}*_spec.rb"
7491
end
7592

93+
def glob_for_spec_suffix_only?
94+
'*_spec.rb'
95+
end
96+
7697
def name_glob(name)
7798
return unless name&.str_type?
7899

@@ -111,6 +132,10 @@ def filename_ends_with?(glob)
111132
def relevant_rubocop_rspec_file?(_file)
112133
true
113134
end
135+
136+
def spec_suffix_only?
137+
cop_config['SpecSuffixOnly']
138+
end
114139
end
115140
end
116141
end

manual/cops_rspec.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,10 +1164,11 @@ Enabled by default | Supports autocorrection
11641164
--- | ---
11651165
Enabled | No
11661166

1167-
Checks that spec file paths are consistent with the test subject.
1167+
Checks that spec file paths are consistent and well-formed.
11681168

1169-
Checks the path of the spec file and enforces that it reflects the
1170-
described class/module and its optionally called out method.
1169+
By default, this checks that spec file paths are consistent with the
1170+
test subject and and enforces that it reflects the described
1171+
class/module and its optionally called out method.
11711172

11721173
With the configuration option `IgnoreMethods` the called out method will
11731174
be ignored when determining the enforced path.
@@ -1176,6 +1177,10 @@ With the configuration option `CustomTransform` modules or classes can
11761177
be specified that should not as usual be transformed from CamelCase to
11771178
snake_case (e.g. 'RuboCop' => 'rubocop' ).
11781179

1180+
With the configuration option `SpecSuffixOnly` test files will only
1181+
be checked to ensure they end in '_spec.rb'. This option disables
1182+
checking for consistency in the test subject or test methods.
1183+
11791184
### Examples
11801185

11811186
```ruby
@@ -1203,6 +1208,18 @@ whatever_spec.rb # describe MyClass
12031208
# good
12041209
my_class_spec.rb # describe MyClass
12051210

1211+
# good
1212+
my_class_spec.rb # describe MyClass, '#method'
1213+
```
1214+
#### when configuration is `SpecSuffixOnly: true`
1215+
1216+
```ruby
1217+
# good
1218+
whatever_spec.rb # describe MyClass
1219+
1220+
# good
1221+
my_class_spec.rb # describe MyClass
1222+
12061223
# good
12071224
my_class_spec.rb # describe MyClass, '#method'
12081225
```
@@ -1213,6 +1230,7 @@ Name | Default value | Configurable values
12131230
--- | --- | ---
12141231
CustomTransform | `{"RuboCop"=>"rubocop", "RSpec"=>"rspec"}` |
12151232
IgnoreMethods | `false` | Boolean
1233+
SpecSuffixOnly | `false` | Boolean
12161234

12171235
### References
12181236

spec/rubocop/cop/rspec/file_path_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,21 @@
204204
RUBY
205205
end
206206
end
207+
208+
context 'when configured with SpecSuffixOnly' do
209+
let(:cop_config) { { 'SpecSuffixOnly' => true } }
210+
211+
it 'does not care about the described class' do
212+
expect_no_offenses(<<-RUBY, 'whatever_spec.rb')
213+
describe MyClass do; end
214+
RUBY
215+
end
216+
217+
it 'registers an offense when _spec.rb suffix is missing' do
218+
expect_offense(<<-RUBY, 'whatever.rb')
219+
describe MyClass do; end
220+
^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
221+
RUBY
222+
end
223+
end
207224
end

0 commit comments

Comments
 (0)