Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the config loader Bundler-aware #7983

Merged
merged 1 commit into from May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@
* [#7886](https://github.com/rubocop-hq/rubocop/issues/7886): Fix a bug in `AllowComments` logic in `Lint/SuppressedException`. ([@jonas054][])
* [#7991](https://github.com/rubocop-hq/rubocop/issues/7991): Fix an error for `Layout/EmptyLinesAroundAttributeAccessor` when attribute method is method chained. ([@koic][])
* [#7993](https://github.com/rubocop-hq/rubocop/issues/7993): Fix a false positive for `Migration/DepartmentName` when a disable comment contains an unexpected character for department name. ([@koic][])
* [#7983](https://github.com/rubocop-hq/rubocop/pull/7983): Make the config loader Bundler-aware. ([@CvX][])

### Changes

Expand Down Expand Up @@ -4538,3 +4539,4 @@
[@jeffcarbs]: https://github.com/jeffcarbs
[@laurmurclar]: https://github.com/laurmurclar
[@jethrodaniel]: https://github.com/jethrodaniel
[@CvX]: https://github.com/CvX
10 changes: 8 additions & 2 deletions lib/rubocop/config_loader_resolver.rb
Expand Up @@ -208,8 +208,14 @@ def transform(config)
end

def gem_config_path(gem_name, relative_config_path)
spec = Gem::Specification.find_by_name(gem_name)
File.join(spec.gem_dir, relative_config_path)
if defined?(Bundler)
gem = Bundler.load.specs[gem_name].first
gem_path = gem.full_gem_path if gem
end

gem_path ||= Gem::Specification.find_by_name(gem_name).gem_dir

File.join(gem_path, relative_config_path)
rescue Gem::LoadError => e
raise Gem::LoadError,
"Unable to find gem #{gem_name}; is the gem installed? #{e}"
Expand Down
78 changes: 56 additions & 22 deletions spec/rubocop/config_loader_spec.rb
Expand Up @@ -823,31 +823,65 @@ class Loop < Cop
YAML
end

it 'returns values from the gem config with local overrides' do
gem_class = Struct.new(:gem_dir)
%w[gemone gemtwo].each do |gem_name|
mock_spec = gem_class.new(File.join(gem_root, gem_name))
allow(Gem::Specification).to receive(:find_by_name)
.with(gem_name).and_return(mock_spec)
context 'and the gem is globally installed' do
before do
gem_class = Struct.new(:gem_dir)
%w[gemone gemtwo].each do |gem_name|
mock_spec = gem_class.new(File.join(gem_root, gem_name))
allow(Gem::Specification).to receive(:find_by_name)
.with(gem_name).and_return(mock_spec)
end
allow(Gem).to receive(:path).and_return([gem_root])
end
allow(Gem).to receive(:path).and_return([gem_root])

expected = { 'Enabled' => true, # overridden in .rubocop.yml
'CountComments' => true, # overridden in local.yml
'Max' => 200 } # inherited from somegem
expect do
expect(configuration_from_file['Metrics/MethodLength']
.to_set.superset?(expected.to_set)).to be(true)
end.to output('').to_stderr
it 'returns values from the gem config with local overrides' do
expected = { 'Enabled' => true, # overridden in .rubocop.yml
'CountComments' => true, # overridden in local.yml
'Max' => 200 } # inherited from somegem
expect do
expect(configuration_from_file['Metrics/MethodLength']
.to_set.superset?(expected.to_set)).to be(true)
end.to output('').to_stderr

expected = { 'Enabled' => true, # gemtwo/config/default.yml
'Max' => 72, # gemtwo/config/strict.yml
'AllowHeredoc' => false, # gemtwo/config/strict.yml
'AllowURI' => false } # overridden in .rubocop.yml
expect(
configuration_from_file['Layout/LineLength']
.to_set.superset?(expected.to_set)
).to be(true)
end
end

context 'and the gem is bundled' do
before do
specs = {
'gemone' => [OpenStruct.new(full_gem_path: File.join(gem_root, 'gemone'))],
'gemtwo' => [OpenStruct.new(full_gem_path: File.join(gem_root, 'gemtwo'))]
}

expected = { 'Enabled' => true, # gemtwo/config/default.yml
'Max' => 72, # gemtwo/config/strict.yml
'AllowHeredoc' => false, # gemtwo/config/strict.yml
'AllowURI' => false } # overridden in .rubocop.yml
expect(
configuration_from_file['Layout/LineLength']
.to_set.superset?(expected.to_set)
).to be(true)
allow(Bundler).to receive(:load).and_return(OpenStruct.new(specs: specs))
end

it 'returns values from the gem config with local overrides' do
expected = { 'Enabled' => true, # overridden in .rubocop.yml
'CountComments' => true, # overridden in local.yml
'Max' => 200 } # inherited from somegem
expect do
expect(configuration_from_file['Metrics/MethodLength']
.to_set.superset?(expected.to_set)).to be(true)
end.to output('').to_stderr

expected = { 'Enabled' => true, # gemtwo/config/default.yml
'Max' => 72, # gemtwo/config/strict.yml
'AllowHeredoc' => false, # gemtwo/config/strict.yml
'AllowURI' => false } # overridden in .rubocop.yml
expect(
configuration_from_file['Layout/LineLength']
.to_set.superset?(expected.to_set)
).to be(true)
end
end
end

Expand Down