Skip to content

Commit

Permalink
Merge pull request #12127 from ItsEcholot/bugfix/required_ruby_versio…
Browse files Browse the repository at this point in the history
…n_gem_requirement_matching

[Fix #12105] Adjust target ruby gem requirement matcher and version parsing to support multiple version constraints
  • Loading branch information
koic committed Aug 17, 2023
2 parents 2cb9dea + 252af0d commit 23dc4de
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12105](https://github.com/rubocop/rubocop/issues/12105): Fix target ruby `Gem::Requirement` matcher and version parsing to support multiple version constraints. ([@ItsEcholot][])
14 changes: 9 additions & 5 deletions lib/rubocop/target_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ class GemspecFile < Source
(send _ :required_ruby_version= $_)
PATTERN

# @!method gem_requirement?(node)
def_node_matcher :gem_requirement?, <<~PATTERN
(send (const(const _ :Gem):Requirement) :new $str)
# @!method gem_requirement_versions(node)
def_node_matcher :gem_requirement_versions, <<~PATTERN
(send (const(const _ :Gem):Requirement) :new
{$str+ | (send $str :freeze)+ | (array $str+) | (array (send $str :freeze)+)}
)
PATTERN

def name
Expand Down Expand Up @@ -194,10 +196,12 @@ def version_from_gemspec_file(file)
end

def version_from_right_hand_side(right_hand_side)
gem_requirement_versions = gem_requirement_versions(right_hand_side)

if right_hand_side.array_type?
version_from_array(right_hand_side)
elsif gem_requirement?(right_hand_side)
right_hand_side.children.last.value
elsif gem_requirement_versions
gem_requirement_versions.map(&:value)
else
right_hand_side.value
end
Expand Down
56 changes: 56 additions & 0 deletions spec/rubocop/target_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,62 @@
create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end

it 'sets first known ruby version that satisfies range requirement' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new('>= 2.3.1', '< 3.0.0')
s.licenses = ['MIT']
end
HEREDOC

create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end

it 'sets first known ruby version that satisfies range requirement in array notation' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new(['>= 2.3.1', '< 3.0.0'])
s.licenses = ['MIT']
end
HEREDOC

create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end

it 'sets first known ruby version that satisfies range requirement with frozen strings' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new('>= 2.3.1'.freeze, '< 3.0.0'.freeze)
s.licenses = ['MIT']
end
HEREDOC

create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end

it 'sets first known ruby version that satisfies range requirement in array notation with frozen strings' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new(['>= 2.3.1'.freeze, '< 3.0.0'.freeze])
s.licenses = ['MIT']
end
HEREDOC

create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end
end

context 'when file contains `required_ruby_version` as an array' do
Expand Down

0 comments on commit 23dc4de

Please sign in to comment.