Skip to content

Commit 84cca39

Browse files
authored
Merge pull request #869 from eitoball/use-relative-path-for-file_path-cop
Fix `FilePath` detection when absolute path includes test subject
2 parents 7970b19 + 811ae08 commit 84cca39

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

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

33
## Master (Unreleased)
44

5+
* Fix `RSpec/FilePath` detection when absolute path includes test subject. ([@eitoball][])
6+
57
## 1.38.1 (2020-02-15)
68

79
* Fix `RSpec/RepeatedDescription` to detect descriptions with interpolation and methods. ([@lazycoder9][])
@@ -489,3 +491,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
489491
[@dduugg]: https://github.com/dduugg
490492
[@lazycoder9]: https://github.com/lazycoder9
491493
[@elebow]: https://github.com/elebow
494+
[@eitoball]: https://github.com/eitoball

lib/rubocop/cop/rspec/file_path.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ def ignore_methods?
103103
end
104104

105105
def filename_ends_with?(glob)
106-
File.fnmatch?("*#{glob}", processed_source.buffer.name)
106+
filename =
107+
RuboCop::PathUtil.relative_path(processed_source.buffer.name)
108+
File.fnmatch?("*#{glob}", filename)
107109
end
108110

109111
def relevant_rubocop_rspec_file?(_file)

spec/rubocop/cop/rspec/file_path_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@
172172
RUBY
173173
end
174174

175+
it 'uses relative path' do
176+
allow(RuboCop::PathUtil)
177+
.to receive(:relative_path).and_return('spec/models/bar_spec.rb')
178+
expect_offense(<<-RUBY, '/home/foo/spec/models/bar_spec.rb')
179+
describe Foo do; end
180+
^^^^^^^^^^^^ Spec path should end with `foo*_spec.rb`.
181+
RUBY
182+
end
183+
175184
context 'when configured with CustomTransform' do
176185
let(:cop_config) { { 'CustomTransform' => { 'FooFoo' => 'foofoo' } } }
177186

0 commit comments

Comments
 (0)