Skip to content

Commit

Permalink
Merge pull request #6954 from rubygems/martinemde/improve-ruby-file-i…
Browse files Browse the repository at this point in the history
…nstruction-coverage

(cherry picked from commit 71ef923)
  • Loading branch information
martinemde authored and deivid-rodriguez committed Sep 21, 2023
1 parent c1e2d76 commit 45ec201
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
30 changes: 23 additions & 7 deletions bundler/lib/bundler/ruby_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ def ruby(*ruby_version)
raise GemfileError, "Please define :engine" if options[:engine_version] && options[:engine].nil?

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

if options[:engine] == "ruby" && options[:engine_version] &&
Expand All @@ -25,5 +20,26 @@ def ruby(*ruby_version)
end
@ruby_version = RubyVersion.new(ruby_version, options[:patchlevel], options[:engine], options[:engine_version])
end

# Support the various file formats found in .ruby-version files.
#
# 3.2.2
# ruby-3.2.2
#
# Also supports .tool-versions files for asdf. Lines not starting with "ruby" are ignored.
#
# ruby 2.5.1 # comment is ignored
# ruby 2.5.1# close comment and extra spaces doesn't confuse
#
# Intentionally does not support `3.2.1@gemset` since rvm recommends using .ruby-gemset instead
def normalize_ruby_file(filename)
file_content = Bundler.read_file(Bundler.root.join(filename))
# match "ruby-3.2.2" or "ruby 3.2.2" capturing version string up to the first space or comment
if /^ruby(-|\s+)([^\s#]+)/.match(file_content)
$2
else
file_content.strip
end
end
end
end
76 changes: 46 additions & 30 deletions bundler/spec/bundler/ruby_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class MockDSL
:engine => engine,
:engine_version => engine_version }
end
let(:project_root) { Pathname.new("/path/to/project") }
before { allow(Bundler).to receive(:root).and_return(project_root) }

let(:invoke) do
proc do
args = []
args << Array(ruby_version_arg) if ruby_version_arg
args << ruby_version_arg if ruby_version_arg
args << options

dsl.ruby(*args)
Expand Down Expand Up @@ -97,53 +99,67 @@ class MockDSL
end

context "with a file option" do
let(:options) { { :file => "foo" } }
let(:version) { "3.2.2" }
let(:ruby_version) { "3.2.2" }
let(:file) { ".ruby-version" }
let(:options) do
{ :file => file,
:patchlevel => patchlevel,
:engine => engine,
:engine_version => engine_version }
end
let(:ruby_version_arg) { nil }
let(:engine_version) { version }
let(:patchlevel) { nil }
let(:engine) { "ruby" }
let(:project_root) { Pathname.new("/path/to/project") }
let(:file_content) { "#{version}\n" }

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

it_behaves_like "it stores the ruby version"

context "with the ruby- prefix in the file" do
let(:file_content) { "ruby-#{version}\n" }

it_behaves_like "it stores the ruby version"
end

context "and a version" do
let(:ruby_version_arg) { "2.0.0" }
let(:ruby_version_arg) { version }

it "raises an error" do
expect { subject }.to raise_error(Bundler::GemfileError, "Cannot specify version when using the file option")
expect { subject }.to raise_error(Bundler::GemfileError, "Do not pass version argument when using :file option")
end
end
end

context "with a (.tool-versions) file option" do
let(:options) { { :file => "foo" } }
let(:version) { "3.2.2" }
let(:ruby_version) { "3.2.2" }
let(:ruby_version_arg) { nil }
let(:engine_version) { version }
let(:patchlevel) { nil }
let(:engine) { "ruby" }
let(:project_root) { Pathname.new("/path/to/project") }
context "with a @gemset" do
let(:file_content) { "ruby-#{version}@gemset\n" }

before do
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")
allow(Bundler).to receive(:root).and_return(Pathname.new("/path/to/project"))
it "raises an error" do
expect { subject }.to raise_error(Gem::Requirement::BadRequirementError, "Illformed requirement [\"#{version}@gemset\"]")
end
end

it_behaves_like "it stores the ruby version"
context "with a .tool-versions file format" do
let(:file) { ".tool-versions" }
let(:ruby_version_arg) { nil }
let(:file_content) do
<<~TOOLS
nodejs 18.16.0
ruby #{version} # This is a comment
pnpm 8.6.12
TOOLS
end

context "and a version" do
let(:ruby_version_arg) { "2.0.0" }
it_behaves_like "it stores the ruby version"

it "raises an error" do
expect { subject }.to raise_error(Bundler::GemfileError, "Cannot specify version when using the file option")
context "with extra spaces and a very cozy comment" do
let(:file_content) do
<<~TOOLS
nodejs 18.16.0
ruby #{version}# This is a cozy comment
pnpm 8.6.12
TOOLS
end

it_behaves_like "it stores the ruby version"
end
end
end
Expand Down

0 comments on commit 45ec201

Please sign in to comment.