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

Fix required_ruby_version issue when using Gem::Requirement #9037

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#9037](https://github.com/rubocop-hq/rubocop/pull/9037): Fix `required_ruby_version` issue when using `Gem::Requirement`. ([@cetinajero][])

## 1.3.0 (2020-11-12)

### New features
Expand Down
12 changes: 12 additions & 0 deletions lib/rubocop/target_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class GemspecFile < Source
(send _ :required_ruby_version= $_)
PATTERN

def_node_matcher :gem_requirement?, <<~PATTERN
(send (const(const _ :Gem):Requirement) :new $str)
PATTERN

def name
"`required_ruby_version` parameter (in #{gemspec_filename})"
end
Expand All @@ -141,9 +145,17 @@ def find_version
return versions.compact.min
end

return gem_requirement_version(version) if gem_requirement? version

version_from_str(version.str_content)
end

def gem_requirement_version(version)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is still extracted from the gemspec it should probably be part of that existing method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New arrangement based on #9037 (comment), let me know your thoughts @bbatsov .

gem_requirement = version.children.last
versions = gem_requirement.children.map { |v| version_from_str(v) }
versions.compact.min
end

def gemspec_filename
@gemspec_filename ||= begin
basename = Pathname.new(@config.base_dir_for_path_parameters).basename.to_s
Expand Down
47 changes: 47 additions & 0 deletions spec/rubocop/target_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,53 @@
end
end

context 'when file contains `required_ruby_version` as a requirement' do
let(:base_path) { configuration.base_dir_for_path_parameters }
let(:gemspec_file_path) { File.join(base_path, 'example.gemspec') }

it 'sets target_ruby from required_ruby_version from exact requirement version' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new('2.7.4')
s.licenses = ['MIT']
end
HEREDOC

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

it 'sets target_ruby from required_ruby_version from inclusive requirement range' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new('>= 3.0.0')
s.licenses = ['MIT']
end
HEREDOC

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

it 'sets default target_ruby from exclusive requirement range' do
content =
<<-HEREDOC
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new('< 3.0.0')
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
let(:base_path) { configuration.base_dir_for_path_parameters }
let(:gemspec_file_path) { File.join(base_path, 'example.gemspec') }
Expand Down