Skip to content

Commit 6823a93

Browse files
martinemdedeivid-rodriguez
authored andcommitted
Merge pull request #6954 from rubygems/martinemde/improve-ruby-file-instruction-coverage
(cherry picked from commit 71ef923)
1 parent 315c061 commit 6823a93

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

bundler/lib/bundler/ruby_dsl.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ def ruby(*ruby_version)
1010
raise GemfileError, "Please define :engine" if options[:engine_version] && options[:engine].nil?
1111

1212
if options[:file]
13-
raise GemfileError, "Cannot specify version when using the file option" if ruby_version.any?
14-
file_content = Bundler.read_file(Bundler.root.join(options[:file]))
15-
if /^ruby\s+(.*)$/.match(file_content) # match .tool-versions files
16-
ruby_version << $1.split("#", 2).first.strip # remove trailing comment
17-
else
18-
ruby_version << file_content.strip
19-
end
13+
raise GemfileError, "Do not pass version argument when using :file option" unless ruby_version.empty?
14+
ruby_version << normalize_ruby_file(options[:file])
2015
end
2116

2217
if options[:engine] == "ruby" && options[:engine_version] &&
@@ -25,5 +20,26 @@ def ruby(*ruby_version)
2520
end
2621
@ruby_version = RubyVersion.new(ruby_version, options[:patchlevel], options[:engine], options[:engine_version])
2722
end
23+
24+
# Support the various file formats found in .ruby-version files.
25+
#
26+
# 3.2.2
27+
# ruby-3.2.2
28+
#
29+
# Also supports .tool-versions files for asdf. Lines not starting with "ruby" are ignored.
30+
#
31+
# ruby 2.5.1 # comment is ignored
32+
# ruby 2.5.1# close comment and extra spaces doesn't confuse
33+
#
34+
# Intentionally does not support `3.2.1@gemset` since rvm recommends using .ruby-gemset instead
35+
def normalize_ruby_file(filename)
36+
file_content = Bundler.read_file(Bundler.root.join(filename))
37+
# match "ruby-3.2.2" or "ruby 3.2.2" capturing version string up to the first space or comment
38+
if /^ruby(-|\s+)([^\s#]+)/.match(file_content)
39+
$2
40+
else
41+
file_content.strip
42+
end
43+
end
2844
end
2945
end

bundler/spec/bundler/ruby_dsl_spec.rb

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ class MockDSL
2121
:engine => engine,
2222
:engine_version => engine_version }
2323
end
24+
let(:project_root) { Pathname.new("/path/to/project") }
25+
before { allow(Bundler).to receive(:root).and_return(project_root) }
2426

2527
let(:invoke) do
2628
proc do
2729
args = []
28-
args << Array(ruby_version_arg) if ruby_version_arg
30+
args << ruby_version_arg if ruby_version_arg
2931
args << options
3032

3133
dsl.ruby(*args)
@@ -97,53 +99,67 @@ class MockDSL
9799
end
98100

99101
context "with a file option" do
100-
let(:options) { { :file => "foo" } }
101-
let(:version) { "3.2.2" }
102-
let(:ruby_version) { "3.2.2" }
102+
let(:file) { ".ruby-version" }
103+
let(:options) do
104+
{ :file => file,
105+
:patchlevel => patchlevel,
106+
:engine => engine,
107+
:engine_version => engine_version }
108+
end
103109
let(:ruby_version_arg) { nil }
104-
let(:engine_version) { version }
105-
let(:patchlevel) { nil }
106-
let(:engine) { "ruby" }
107-
let(:project_root) { Pathname.new("/path/to/project") }
110+
let(:file_content) { "#{version}\n" }
108111

109112
before do
110-
allow(Bundler).to receive(:read_file).with(project_root.join("foo")).and_return("#{version}\n")
111-
allow(Bundler).to receive(:root).and_return(Pathname.new("/path/to/project"))
113+
allow(Bundler).to receive(:read_file).with(project_root.join(file)).and_return(file_content)
112114
end
113115

114116
it_behaves_like "it stores the ruby version"
115117

118+
context "with the ruby- prefix in the file" do
119+
let(:file_content) { "ruby-#{version}\n" }
120+
121+
it_behaves_like "it stores the ruby version"
122+
end
123+
116124
context "and a version" do
117-
let(:ruby_version_arg) { "2.0.0" }
125+
let(:ruby_version_arg) { version }
118126

119127
it "raises an error" do
120-
expect { subject }.to raise_error(Bundler::GemfileError, "Cannot specify version when using the file option")
128+
expect { subject }.to raise_error(Bundler::GemfileError, "Do not pass version argument when using :file option")
121129
end
122130
end
123-
end
124131

125-
context "with a (.tool-versions) file option" do
126-
let(:options) { { :file => "foo" } }
127-
let(:version) { "3.2.2" }
128-
let(:ruby_version) { "3.2.2" }
129-
let(:ruby_version_arg) { nil }
130-
let(:engine_version) { version }
131-
let(:patchlevel) { nil }
132-
let(:engine) { "ruby" }
133-
let(:project_root) { Pathname.new("/path/to/project") }
132+
context "with a @gemset" do
133+
let(:file_content) { "ruby-#{version}@gemset\n" }
134134

135-
before do
136-
allow(Bundler).to receive(:read_file).with(project_root.join("foo")).and_return("nodejs 18.16.0\nruby #{version} # This is a comment\npnpm 8.6.12\n")
137-
allow(Bundler).to receive(:root).and_return(Pathname.new("/path/to/project"))
135+
it "raises an error" do
136+
expect { subject }.to raise_error(Gem::Requirement::BadRequirementError, "Illformed requirement [\"#{version}@gemset\"]")
137+
end
138138
end
139139

140-
it_behaves_like "it stores the ruby version"
140+
context "with a .tool-versions file format" do
141+
let(:file) { ".tool-versions" }
142+
let(:ruby_version_arg) { nil }
143+
let(:file_content) do
144+
<<~TOOLS
145+
nodejs 18.16.0
146+
ruby #{version} # This is a comment
147+
pnpm 8.6.12
148+
TOOLS
149+
end
141150

142-
context "and a version" do
143-
let(:ruby_version_arg) { "2.0.0" }
151+
it_behaves_like "it stores the ruby version"
144152

145-
it "raises an error" do
146-
expect { subject }.to raise_error(Bundler::GemfileError, "Cannot specify version when using the file option")
153+
context "with extra spaces and a very cozy comment" do
154+
let(:file_content) do
155+
<<~TOOLS
156+
nodejs 18.16.0
157+
ruby #{version}# This is a cozy comment
158+
pnpm 8.6.12
159+
TOOLS
160+
end
161+
162+
it_behaves_like "it stores the ruby version"
147163
end
148164
end
149165
end

0 commit comments

Comments
 (0)