Skip to content

Commit

Permalink
[Fix #12714] Fix an error for Gemspec/RequiredRubyVersion
Browse files Browse the repository at this point in the history
Fixes #12714.

This PR fixes an error for `Gemspec/RequiredRubyVersion`
when `required_ruby_version` is specified with `Gem::Requirement.new`
and is higher than `TargetRubyVersion`.

When specifying only an upper version limit, an offense is registered
because lower Ruby version that matches `TargetRubyVersion` is unknown.
  • Loading branch information
koic authored and bbatsov committed Feb 27, 2024
1 parent 029984a commit 60c1f8a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12714](https://github.com/rubocop/rubocop/issues/12714): Fix an error for `Gemspec/RequiredRubyVersion` when `required_ruby_version` is specified with `Gem::Requirement.new` and is higher than `TargetRubyVersion`. ([@koic][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/gemspec/required_ruby_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def extract_ruby_version(required_ruby_version)
end
end

return unless required_ruby_version

required_ruby_version.str_content.scan(/\d/).first(2).join('.')
end

Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/gemspec/required_ruby_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
RUBY
end

it 'registers an offense when `required_ruby_version` is specified with `Gem::Requirement.new` and is higher than `TargetRubyVersion`' do
expect_offense(<<~RUBY)
Gem::Specification.new do |spec|
spec.required_ruby_version = Gem::Requirement.new('< 3.4')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `required_ruby_version` and `TargetRubyVersion` (2.7, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end

it 'recognizes a Gem::Requirement with multiple requirements and does not register an offense' do
expect_no_offenses(<<~RUBY)
Gem::Specification.new do |spec|
Expand Down

0 comments on commit 60c1f8a

Please sign in to comment.