Skip to content

Commit

Permalink
Ignore configuration files outside of the project
Browse files Browse the repository at this point in the history
If by any chance, one happens to leave a `.rubocop.yml` file outside of
my project, ignore it.
  • Loading branch information
deivid-rodriguez authored and bbatsov committed Jul 13, 2020
1 parent e1a2da0 commit d1822bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class << self

attr_accessor :debug, :ignore_parent_exclusion,
:disable_pending_cops, :enable_pending_cops
attr_writer :default_configuration
attr_writer :default_configuration, :project_root

alias debug? debug
alias ignore_parent_exclusion? ignore_parent_exclusion
Expand Down Expand Up @@ -164,7 +164,7 @@ def merge_with_default(config, config_file, unset_nil: true)
private

def find_project_dotfile(target_dir)
find_file_upwards(DOTFILE, target_dir)
find_file_upwards(DOTFILE, target_dir, project_root)
end

def find_project_root
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/rspec/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
ENV.delete('XDG_CONFIG_HOME')

base_dir = example.metadata[:project_inside_home] ? virtual_home : tmpdir
working_dir = File.join(base_dir, 'work')
root = example.metadata[:root]
working_dir = root ? File.join(base_dir, 'work', root) : File.join(base_dir, 'work')

# Make upwards search for .rubocop.yml files stop at this directory.
RuboCop::FileFinder.root_level = working_dir

begin
Dir.mkdir(working_dir)
FileUtils.mkdir_p(working_dir)

Dir.chdir(working_dir) do
example.run
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/config_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@
end
end

context 'when there is a spurious rubocop config outside of the project', root: 'dir' do
let(:dir_path) { 'dir' }

before do
# Force reload of project root
described_class.project_root = nil
create_empty_file('Gemfile')
create_empty_file('../.rubocop.yml')
end

after do
# Don't leak project root change
described_class.project_root = nil
end

it 'ignores the spurious config and falls back to the provided default file if run from the project' do
expect(configuration_file_for).to end_with('config/default.yml')
end
end

context 'when a config file exists in the parent directory' do
let(:dir_path) { 'dir' }

Expand Down

0 comments on commit d1822bc

Please sign in to comment.