From 811ae08ea3a60453e18a8bcb9dfaefafd848f779 Mon Sep 17 00:00:00 2001 From: Eito Katagiri Date: Thu, 6 Feb 2020 09:10:09 +0900 Subject: [PATCH] Use relative path to match with glob --- CHANGELOG.md | 3 +++ lib/rubocop/cop/rspec/file_path.rb | 4 +++- spec/rubocop/cop/rspec/file_path_spec.rb | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7897bccd..452db612e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) @@ -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 diff --git a/lib/rubocop/cop/rspec/file_path.rb b/lib/rubocop/cop/rspec/file_path.rb index b4d81df62..e368424ef 100644 --- a/lib/rubocop/cop/rspec/file_path.rb +++ b/lib/rubocop/cop/rspec/file_path.rb @@ -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) + File.fnmatch?("*#{glob}", filename) end def relevant_rubocop_rspec_file?(_file) diff --git a/spec/rubocop/cop/rspec/file_path_spec.rb b/spec/rubocop/cop/rspec/file_path_spec.rb index 389474553..1e3162474 100644 --- a/spec/rubocop/cop/rspec/file_path_spec.rb +++ b/spec/rubocop/cop/rspec/file_path_spec.rb @@ -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' } } }