Skip to content

Commit

Permalink
Merge pull request #1441 from r7kamura/feature/routing-spec
Browse files Browse the repository at this point in the history
Change `RSpec/FilePath` so that it only checks suffix when path is under spec/routing or type is defined as routing
  • Loading branch information
pirj authored and bquorning committed Oct 25, 2022
1 parent b0a638a commit 3c78b92
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fix autocorrection loop in `RSpec/ExampleWording` for insufficient example wording. ([@pirj])
- Fix `RSpec/SortMetadata` not to reorder arguments of `include_`/`it_behaves_like`. ([@pirj])
- Fix a false positive for `RSpec/NoExpectationExample` when allowed pattern methods with arguments. ([@ydah])
- Change `RSpec/FilePath` so that it only checks suffix when path is under spec/routing or type is defined as routing. ([@r7kamura])

## 2.14.1 (2022-10-24)

Expand Down
23 changes: 15 additions & 8 deletions lib/rubocop/cop/rspec/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ def on_top_level_example_group(node)
return unless top_level_groups.one?

example_group(node) do |send_node, example_group, arguments|
next if routing_spec?(arguments)

ensure_correct_file_path(send_node, example_group, arguments)
end
end

private

def ensure_correct_file_path(send_node, example_group, arguments)
pattern = pattern_for(example_group, arguments.first)
pattern = pattern_for(example_group, arguments)
return if filename_ends_with?(pattern)

# For the suffix shown in the offense message, modify the regular
Expand All @@ -97,11 +95,13 @@ def ensure_correct_file_path(send_node, example_group, arguments)
end

def routing_spec?(args)
args.any?(&method(:routing_metadata?))
args.any?(&method(:routing_metadata?)) || routing_spec_path?
end

def pattern_for(example_group, method_name)
if spec_suffix_only? || !example_group.const_type?
def pattern_for(example_group, arguments)
method_name = arguments.first
if spec_suffix_only? || !example_group.const_type? ||
routing_spec?(arguments)
return pattern_for_spec_suffix_only
end

Expand Down Expand Up @@ -149,8 +149,7 @@ def ignore_methods?
end

def filename_ends_with?(pattern)
filename = File.expand_path(processed_source.buffer.name)
filename.match?("#{pattern}$")
expanded_file_path.match?("#{pattern}$")
end

def relevant_rubocop_rspec_file?(_file)
Expand All @@ -160,6 +159,14 @@ def relevant_rubocop_rspec_file?(_file)
def spec_suffix_only?
cop_config['SpecSuffixOnly']
end

def routing_spec_path?
expanded_file_path.include?('spec/routing/')
end

def expanded_file_path
File.expand_path(processed_source.buffer.name)
end
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/cop/rspec/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,40 @@ class Foo
RUBY
end

context 'when path is under spec/routing and it ends with _spec.rb' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, 'spec/routing/foo_spec.rb')
describe 'routes to the foo controller' do; end
RUBY
end
end

context 'when path is under spec/routing and it does not end with _spec.rb' do
it 'registers an offense' do
expect_offense(<<~RUBY, 'spec/routing/foo.rb')
describe 'routes to the foo controller' do; end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
RUBY
end
end

context 'when `type: :routing` is used and it ends with _spec.rb' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, 'spec/foo_spec.rb')
describe 'routes to the foo controller', type: :routing do; end
RUBY
end
end

context 'when `type: :routing` is used and it does not end with _spec.rb' do
it 'registers an offense' do
expect_offense(<<~RUBY, 'spec/foo.rb')
describe 'routes to the foo controller', type: :routing do; end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`.
RUBY
end
end

context 'when configured with CustomTransform' do
let(:cop_config) { { 'CustomTransform' => { 'FooFoo' => 'foofoo' } } }

Expand Down

0 comments on commit 3c78b92

Please sign in to comment.