Skip to content

Commit c4646ff

Browse files
committed
Make FilePath check suffix when given a non-const top-level node
This helps with the standard feature format: RSpec.feature "Some feature" do; end
1 parent 0be6148 commit c4646ff

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Master (Unreleased)
44

5+
* Update `RSpec/FilePath` to check suffix when given a non-constant top-level node (e.g. features). ([@topalovic][])
56
* Add missing documentation for `single_statement_only` style of `RSpec/ImplicitSubject` cop. ([@tejasbubane][])
67
* Fix an exception in `DescribedClass` when accessing a constant on a variable in a spec that is nested in a namespace. ([@rrosenblum][])
78
* Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][])
@@ -621,3 +622,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
621622
[@Tietew]: https://github.com/Tietew
622623
[@rrosenblum]: https://github.com/rrosenblum
623624
[@paydaylight]: https://github.com/paydaylight
625+
[@topalovic]: https://github.com/topalovic

lib/rubocop/cop/rspec/file_path.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ class FilePath < Base
6161

6262
MSG = 'Spec path should end with `%<suffix>s`.'
6363

64-
# @!method const_described(node)
65-
def_node_matcher :const_described, <<~PATTERN
64+
# @!method example_group(node)
65+
def_node_matcher :example_group, <<~PATTERN
6666
(block
67-
$(send #rspec? _example_group $(const ...) $...) ...
67+
$(send #rspec? _example_group $_ $...) ...
6868
)
6969
PATTERN
7070

@@ -74,17 +74,17 @@ class FilePath < Base
7474
def on_top_level_example_group(node)
7575
return unless top_level_groups.one?
7676

77-
const_described(node) do |send_node, described_class, arguments|
77+
example_group(node) do |send_node, example_group, arguments|
7878
next if routing_spec?(arguments)
7979

80-
ensure_correct_file_path(send_node, described_class, arguments)
80+
ensure_correct_file_path(send_node, example_group, arguments)
8181
end
8282
end
8383

8484
private
8585

86-
def ensure_correct_file_path(send_node, described_class, arguments)
87-
pattern = pattern_for(described_class, arguments.first)
86+
def ensure_correct_file_path(send_node, example_group, arguments)
87+
pattern = pattern_for(example_group, arguments.first)
8888
return if filename_ends_with?(pattern)
8989

9090
# For the suffix shown in the offense message, modify the regular
@@ -99,11 +99,13 @@ def routing_spec?(args)
9999
args.any?(&method(:routing_metadata?))
100100
end
101101

102-
def pattern_for(described_class, method_name)
103-
return pattern_for_spec_suffix_only? if spec_suffix_only?
102+
def pattern_for(example_group, method_name)
103+
if spec_suffix_only? || !example_group.const_type?
104+
return pattern_for_spec_suffix_only?
105+
end
104106

105107
[
106-
expected_path(described_class),
108+
expected_path(example_group),
107109
name_pattern(method_name),
108110
'[^/]*_spec\.rb'
109111
].join

spec/rubocop/cop/rspec/file_path_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
RUBY
6565
end
6666

67+
it 'registers an offense for a feature file missing _spec' do
68+
expect_offense(<<-RUBY, 'spec/features/my_feature.rb')
69+
feature "my feature" do; end
70+
^^^^^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
71+
RUBY
72+
end
73+
6774
it 'registers an offense for a file without the .rb extension' do
6875
expect_offense(<<-RUBY, 'spec/models/user_specxrb')
6976
describe User do; end
@@ -265,6 +272,13 @@ class Foo
265272
RUBY
266273
end
267274

275+
it 'registers an offense when a feature file is missing _spec.rb suffix' do
276+
expect_offense(<<-RUBY, 'spec/my_feature.rb')
277+
feature "my feature" do; end
278+
^^^^^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
279+
RUBY
280+
end
281+
268282
it 'registers an offense when the file extension is not .rb' do
269283
expect_offense(<<-RUBY, 'whatever_specxrb')
270284
describe MyClass do; end

0 commit comments

Comments
 (0)