From 0563861979a9054f59fdbd7478d61429b5826833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Sun, 12 Jul 2020 15:40:58 +0200 Subject: [PATCH] Fix `traverse_files_upwards` to make searching up to the root possible Previously, the meaning of `FileFinder.root_level` would be "first non searched directory", now it is "last searched directory". This is more intuitive in my opinion (I assumed this behavior from the beginning until I found out it was not like that), but also more "correct", because it allows you to search up to the root directory if `FileFinder.root_level` is set to "/". Note that this `FileFinder.root_level?` logic is only useful for testing to provide an isolated environment not affected by the external file system where the tests are running. When rubocop runs "normally", it's ignored. However, in follow up work, I plan to start using these code paths, and this change allows me to stop artificially passing the parent of the directory where I want to stop searching. --- lib/rubocop/file_finder.rb | 4 ++-- lib/rubocop/rspec/shared_contexts.rb | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/rubocop/file_finder.rb b/lib/rubocop/file_finder.rb index 05dbb3ccd0aa..b6724f791636 100644 --- a/lib/rubocop/file_finder.rb +++ b/lib/rubocop/file_finder.rb @@ -32,10 +32,10 @@ def find_last_file_upwards(filename, start_dir) def traverse_files_upwards(filename, start_dir) Pathname.new(start_dir).expand_path.ascend do |dir| - break if FileFinder.root_level?(dir) - file = dir + filename yield(file.to_s) if file.exist? + + break if FileFinder.root_level?(dir) end end end diff --git a/lib/rubocop/rspec/shared_contexts.rb b/lib/rubocop/rspec/shared_contexts.rb index d3fb2081d60e..7bc0b59349f0 100644 --- a/lib/rubocop/rspec/shared_contexts.rb +++ b/lib/rubocop/rspec/shared_contexts.rb @@ -12,16 +12,17 @@ # get mismatched pathnames when loading config files later on. tmpdir = File.realpath(tmpdir) + virtual_home = File.expand_path(File.join(tmpdir, 'home')) + Dir.mkdir(virtual_home) + ENV['HOME'] = virtual_home + ENV.delete('XDG_CONFIG_HOME') + + working_dir = File.join(tmpdir, 'work') + # Make upwards search for .rubocop.yml files stop at this directory. - RuboCop::FileFinder.root_level = tmpdir + RuboCop::FileFinder.root_level = working_dir begin - virtual_home = File.expand_path(File.join(tmpdir, 'home')) - Dir.mkdir(virtual_home) - ENV['HOME'] = virtual_home - ENV.delete('XDG_CONFIG_HOME') - - working_dir = File.join(tmpdir, 'work') Dir.mkdir(working_dir) Dir.chdir(working_dir) do