From 23ad402fb3bd5149adc456ca3c54204c38fc271f Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Tue, 26 May 2020 15:40:59 +0200 Subject: [PATCH] Fix `RSpec/FilePath` detection across sibling directories. --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rspec/file_path.rb | 1 + spec/rubocop/cop/rspec/file_path_spec.rb | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9983f8231..c8094c424 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Expand `Capybara/VisibilityMatcher` to support more than just `have_selector`. ([@twalpole][]) * Add new `SpecSuffixOnly` option to `RSpec/FilePath` cop. ([@zdennis][]) * Allow `RSpec/RepeatedExampleGroupBody` to differ only by described_class. ([@robotdana][]) +* Fix `RSpec/FilePath` detection across sibling directories. ([@rolfschmidt][]) ## 1.39.0 (2020-05-01) @@ -512,3 +513,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@twalpole]: https://github.com/twalpole [@zdennis]: https://github.com/zdennis [@robotdana]: https://github.com/robotdana +[@rolfschmidt]: https://github.com/rolfschmidt diff --git a/lib/rubocop/cop/rspec/file_path.rb b/lib/rubocop/cop/rspec/file_path.rb index 9268dd00d..4407c871b 100644 --- a/lib/rubocop/cop/rspec/file_path.rb +++ b/lib/rubocop/cop/rspec/file_path.rb @@ -126,6 +126,7 @@ def ignore_methods? def filename_ends_with?(glob) filename = RuboCop::PathUtil.relative_path(processed_source.buffer.name) + .gsub('../', '') File.fnmatch?("*#{glob}", filename) end diff --git a/spec/rubocop/cop/rspec/file_path_spec.rb b/spec/rubocop/cop/rspec/file_path_spec.rb index c9d96eba8..833075f52 100644 --- a/spec/rubocop/cop/rspec/file_path_spec.rb +++ b/spec/rubocop/cop/rspec/file_path_spec.rb @@ -179,6 +179,24 @@ RUBY end + it 'uses relative path for sibling directory project' do + allow(RuboCop::PathUtil) + .to receive(:relative_path) + .and_return('../ext-project/spec/models/bar_spec.rb') + expect_no_offenses(<<-RUBY, '/home/ext-project/spec/models/bar_spec.rb') + describe Bar do; end + RUBY + end + + it 'uses relative path for different path project' do + allow(RuboCop::PathUtil) + .to receive(:relative_path) + .and_return('../../opt/ext-project/spec/models/bar_spec.rb') + expect_no_offenses(<<-RUBY, '/opt/ext-project/spec/models/bar_spec.rb') + describe Bar do; end + RUBY + end + context 'when configured with CustomTransform' do let(:cop_config) { { 'CustomTransform' => { 'FooFoo' => 'foofoo' } } }