diff options
-rw-r--r-- | lib/bundler/ruby_dsl.rb | 30 | ||||
-rw-r--r-- | spec/bundler/bundler/ruby_dsl_spec.rb | 76 |
2 files changed, 69 insertions, 37 deletions
diff --git a/lib/bundler/ruby_dsl.rb b/lib/bundler/ruby_dsl.rb index 542dbac1e4..95151898ff 100644 --- a/lib/bundler/ruby_dsl.rb +++ b/lib/bundler/ruby_dsl.rb @@ -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 diff --git a/spec/bundler/bundler/ruby_dsl_spec.rb b/spec/bundler/bundler/ruby_dsl_spec.rb index 4af271ad59..6b00161571 100644 --- a/spec/bundler/bundler/ruby_dsl_spec.rb +++ b/spec/bundler/bundler/ruby_dsl_spec.rb @@ -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") } + 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 |