Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FilePath detection when absolute path includes test subject #869

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

* Fix `RSpec/FilePath` detection when absolute path includes test subject. ([@eitoball][])

## 1.38.1 (2020-02-15)

* Fix `RSpec/RepeatedDescription` to detect descriptions with interpolation and methods. ([@lazycoder9][])
Expand Down Expand Up @@ -489,3 +491,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@dduugg]: https://github.com/dduugg
[@lazycoder9]: https://github.com/lazycoder9
[@elebow]: https://github.com/elebow
[@eitoball]: https://github.com/eitoball
4 changes: 3 additions & 1 deletion lib/rubocop/cop/rspec/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def ignore_methods?
end

def filename_ends_with?(glob)
File.fnmatch?("*#{glob}", processed_source.buffer.name)
filename =
RuboCop::PathUtil.relative_path(processed_source.buffer.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to call rubocop from a subdirectory, e.g. /home/foo/spec/models/, in this case relative path will be ./bar_spec.rb, this shouldn't cause any thouble, should it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it depends. If subject is like 'Foo::Bar', it wouldn't detect. I think that most people run from project root

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this way it doesn't work if I run rubocop and provide a path to check, e.g.
rubocop --require rubocop-rspec ~/my_project/specs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eitoball Can you please check this edge case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't notice. Will do over this weekend. Thank you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A friendly reminder. This is ready to be merged apart from this edge case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi guys,
i think i had this edge case. I tried to create a pull request for it:

#917

File.fnmatch?("*#{glob}", filename)
end

def relevant_rubocop_rspec_file?(_file)
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/rspec/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@
RUBY
end

it 'uses relative path' do
allow(RuboCop::PathUtil)
.to receive(:relative_path).and_return('spec/models/bar_spec.rb')
expect_offense(<<-RUBY, '/home/foo/spec/models/bar_spec.rb')
describe Foo do; end
^^^^^^^^^^^^ Spec path should end with `foo*_spec.rb`.
RUBY
end

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

Expand Down