diff --git a/lib/rubocop/config_loader.rb b/lib/rubocop/config_loader.rb index 60064c510cad..b04bd49f962b 100644 --- a/lib/rubocop/config_loader.rb +++ b/lib/rubocop/config_loader.rb @@ -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 @@ -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 diff --git a/lib/rubocop/rspec/shared_contexts.rb b/lib/rubocop/rspec/shared_contexts.rb index 9469089a85c7..b2bafec34c80 100644 --- a/lib/rubocop/rspec/shared_contexts.rb +++ b/lib/rubocop/rspec/shared_contexts.rb @@ -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 diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index a487b1541888..d93b95a5b2bd 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -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' }