Skip to content

Commit

Permalink
Fix traverse_files_upwards to make searching up to the root possible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
deivid-rodriguez authored and bbatsov committed Jul 13, 2020
1 parent 18e318f commit 0563861
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/file_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions lib/rubocop/rspec/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0563861

Please sign in to comment.