diff --git a/changelog/fix_only_parse_target_ruby_from_gemspec_if_array.md b/changelog/fix_only_parse_target_ruby_from_gemspec_if_array.md new file mode 100644 index 00000000000..73d3db97ebd --- /dev/null +++ b/changelog/fix_only_parse_target_ruby_from_gemspec_if_array.md @@ -0,0 +1 @@ +* [#12756](https://github.com/rubocop/rubocop/pull/12756): Only parse target Ruby from gemspec if array elements are strings. ([@davidrunger][]) diff --git a/lib/rubocop/target_ruby.rb b/lib/rubocop/target_ruby.rb index e28ba8da24b..daac4bc89f3 100644 --- a/lib/rubocop/target_ruby.rb +++ b/lib/rubocop/target_ruby.rb @@ -105,7 +105,7 @@ def version_from_gemspec_file(file) def version_from_right_hand_side(right_hand_side) gem_requirement_versions = gem_requirement_versions(right_hand_side) - if right_hand_side.array_type? + if right_hand_side.array_type? && right_hand_side.children.all?(&:str_type?) version_from_array(right_hand_side) elsif gem_requirement_versions gem_requirement_versions.map(&:value) diff --git a/spec/rubocop/target_ruby_spec.rb b/spec/rubocop/target_ruby_spec.rb index a947638ebfd..5fa1c7f66ec 100644 --- a/spec/rubocop/target_ruby_spec.rb +++ b/spec/rubocop/target_ruby_spec.rb @@ -188,10 +188,25 @@ context 'when file reads the required_ruby_version from another file' do it 'uses the default target ruby version' do - content = <<~HEREDOC + content = <<~'HEREDOC' + Gem::Specification.new do |s| + s.name = 'test' + s.required_ruby_version = Gem::Requirement.new(">= #{File.read('.ruby-version').rstrip}") + s.licenses = ['MIT'] + end + HEREDOC + + create_file(gemspec_file_path, content) + expect(target_ruby.version).to eq default_version + end + end + + context 'when file reads the required_ruby_version from another file in an array' do + it 'uses the default target ruby version' do + content = <<~'HEREDOC' Gem::Specification.new do |s| s.name = 'test' - s.required_ruby_version = Gem::Requirement.new(">= \#{File.read('.ruby-version').rstrip}") + s.required_ruby_version = [">= #{File.read('.ruby-version').rstrip}"] s.licenses = ['MIT'] end HEREDOC