[rubygems/rubygems] Gemfile ruby file: covers more version formats

Increase test coverage and be explicit about what is and is not supported.

https://github.com/rubygems/rubygems/commit/a096397a00
This commit is contained in:
Martin Emde 2023-09-12 18:54:39 -07:00 committed by git
parent 0d33bc0cde
commit 66ffa15ce0
2 changed files with 72 additions and 40 deletions

View File

@ -10,13 +10,8 @@ module Bundler
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] &&
@ -25,5 +20,26 @@ module Bundler
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

View File

@ -21,11 +21,13 @@ RSpec.describe Bundler::RubyDsl do
: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)
@ -97,53 +99,67 @@ RSpec.describe Bundler::RubyDsl do
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") }
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"))
end
it_behaves_like "it stores the ruby version"
context "and a version" do
let(:ruby_version_arg) { "2.0.0" }
context "with a @gemset" do
let(:file_content) { "ruby-#{version}@gemset\n" }
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(Gem::Requirement::BadRequirementError, "Illformed requirement [\"#{version}@gemset\"]")
end
end
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
it_behaves_like "it stores the ruby version"
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