Skip to content

Commit f3b9509

Browse files
hituzi-no-sippomatzbot
authored andcommitted
[ruby/rubygems] Fix quote handling in mise format ruby version parsing
The previous regex didn't properly match quoted strings it would capture the opening quote as part of the version if quotes were mismatched. This change properly parses double-quoted, single-quoted, and unquoted version strings separately. ruby/rubygems@81e48c8185
1 parent 3b50f4b commit f3b9509

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

lib/bundler/ruby_dsl.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,20 @@ def ruby(*ruby_version)
4343
def normalize_ruby_file(filename)
4444
file_content = Bundler.read_file(gemfile.dirname.join(filename))
4545
# match "ruby-3.2.2", ruby = "3.2.2", ruby = '3.2.2' or "ruby 3.2.2" capturing version string up to the first space or comment
46-
if /^ # Start of line
47-
ruby # Literal "ruby"
48-
[\s-]* # Optional whitespace or hyphens (for "ruby-3.2.2" format)
49-
(?:=\s*)? # Optional equals sign with whitespace (for ruby = "3.2.2" format)
50-
["']? # Optional opening quote
51-
( # Start capturing group
52-
[^\s#"']+ # One or more chars that aren't spaces, #, or quotes
53-
) # End capturing group
54-
["']? # Optional closing quote
55-
/x.match(file_content)
56-
$1
46+
version_match = /^ # Start of line
47+
ruby # Literal "ruby"
48+
[\s-]* # Optional whitespace or hyphens (for "ruby-3.2.2" format)
49+
(?:=\s*)? # Optional equals sign with whitespace (for ruby = "3.2.2" format)
50+
(?:
51+
"([^"]+)" # Double quoted version
52+
|
53+
'([^']+)' # Single quoted version
54+
|
55+
([^\s#"']+) # Unquoted version
56+
)
57+
/x.match(file_content)
58+
if version_match
59+
version_match[1] || version_match[2] || version_match[3]
5760
else
5861
file_content.strip
5962
end

spec/bundler/bundler/ruby_dsl_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ class MockDSL
193193

194194
it_behaves_like "it stores the ruby version"
195195
end
196+
197+
context "with mismatched quotes" do
198+
let(:file_content) do
199+
<<~TOML
200+
[tools]
201+
ruby = "#{version}'
202+
TOML
203+
end
204+
205+
it "raises an error" do
206+
expect { subject }.to raise_error(Bundler::InvalidArgumentError, "= is not a valid requirement on the Ruby version")
207+
end
208+
end
196209
end
197210

198211
context "with a .tool-versions file format" do

0 commit comments

Comments
 (0)