Skip to content

Commit

Permalink
[Fix #11920] Skip files that don't satisfy gem version reqs
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Mar 5, 2024
1 parent d11fa84 commit bf7faee
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/new_requires_gem_api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12186](https://github.com/rubocop/rubocop/pull/12186): Add new `requires_gem` API for declaring which gems a Cop needs. ([@amomchilov][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def active_support_extensions_enabled?
end

def relevant_file?(file)
return false unless target_satisfies_all_gem_version_requirements?
return true unless @config.clusivity_config_for_badge?(self.class.badge)

file == RuboCop::AST::ProcessedSource::STRING_SOURCE_NAME ||
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def support_target_ruby_version?(cop)
end

def support_target_rails_version?(cop)
# In this case, the rails version was already checked by `#excluded_file?`
return true if defined?(RuboCop::Rails::TargetRailsVersion::USES_REQUIRES_GEM_API)

return true unless cop.class.respond_to?(:support_target_rails_version?)

cop.class.support_target_rails_version?(cop.target_rails_version)
Expand Down
14 changes: 13 additions & 1 deletion lib/rubocop/rspec/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,19 @@ def source_range(range, buffer: source_buffer)
let(:config) do
hash = { 'AllCops' => all_cops_config, cop_class.cop_name => cur_cop_config }.merge!(other_cops)

RuboCop::Config.new(hash, "#{Dir.pwd}/.rubocop.yml")
config = RuboCop::Config.new(hash, "#{Dir.pwd}/.rubocop.yml")

rails_version_in_gemfile = Gem::Version.new(
rails_version || RuboCop::Config::DEFAULT_RAILS_VERSION
)

allow(config).to receive(:gem_versions_in_target).and_return(
{
'railties' => rails_version_in_gemfile
}
)

config
end

let(:cop) { cop_class.new(config, cop_options) }
Expand Down
65 changes: 65 additions & 0 deletions spec/rubocop/cop/cop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,71 @@ def autocorrect(node); end

it { is_expected.to be(true) }
end

describe 'for a cop with gem version requirements', :restore_registry do
subject { cop.relevant_file?(file) }

let(:file) { 'foo.rb' }

let(:cop_class) do
stub_cop_class('CopSpec::CopWithGemReqs') do
requires_gem 'gem1', '>= 1.2.3'
end
end

before do
allow(config).to receive(:gem_versions_in_target).and_return(gem_versions_in_target)
end

context 'the target doesn\'t satisfy any of the gem requirements' do
let(:gem_versions_in_target) { {} }

it { is_expected.to be(false) }
end

context 'the target has a required gem, but in a version that\'s too old' do
let(:gem_versions_in_target) { { 'gem1' => Gem::Version.new('1.2.2') } }

it { is_expected.to be(false) }
end

context 'the target has a required gem, in a supported version' do
let(:gem_versions_in_target) { { 'gem1' => Gem::Version.new('1.2.3') } }

it { is_expected.to be(true) }
end

context 'for a cop with multiple gem requirements' do
let(:cop_class) do
stub_cop_class('CopSpec::CopWithGemReqs') do
requires_gem 'gem1', '>= 1.2.3'
requires_gem 'gem2', '>= 4.5.6'
end
end

context 'the target satisfies one but not all of the gem requirements' do
let(:gem_versions_in_target) do
{
'gem1' => Gem::Version.new('1.2.3'),
'gem2' => Gem::Version.new('4.5.5')
}
end

it { is_expected.to be(false) }
end

context 'the target has all the required gems with sufficient versions' do
let(:gem_versions_in_target) do
{
'gem1' => Gem::Version.new('1.2.3'),
'gem2' => Gem::Version.new('4.5.6')
}
end

it { is_expected.to be(true) }
end
end
end
end

describe '#safe_autocorrect?' do
Expand Down

0 comments on commit bf7faee

Please sign in to comment.