Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bundler/spec/runtime/self_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@
expect(out).to eq(previous_minor)
end

it "requires the right bundler version from the config and run bundle CLI without re-exec" do
unless Bundler.rubygems.provides?(">= 4.1.0.dev")
skip "This spec can only run when Gem::BundlerVersionFinder.bundler_versions reads bundler configs"
end

lockfile_bundled_with(current_version)

bundle "config set --local version #{previous_minor}"
bundle "config set --local path.system true"
bundle "install"

script = bundled_app("script.rb")
create_file(script, "p 'executed once'")

bundle "-v", env: { "RUBYOPT" => "-r#{script}" }
expect(out).to eq(%("executed once"\n9.3.0))
end

it "does not try to install when using bundle config version global" do
lockfile_bundled_with(previous_minor)

Expand Down
73 changes: 46 additions & 27 deletions lib/rubygems/bundler_version_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def self.bundler_version
v ||= bundle_update_bundler_version
return if v == true

v ||= bundle_config_version

v ||= lockfile_version
return unless v

Expand Down Expand Up @@ -49,21 +51,7 @@ def self.lockfile_version
private_class_method :lockfile_version

def self.lockfile_contents
gemfile = ENV["BUNDLE_GEMFILE"]
gemfile = nil if gemfile&.empty?

unless gemfile
begin
Gem::Util.traverse_parents(Dir.pwd) do |directory|
next unless gemfile = Gem::GEM_DEP_FILES.find {|f| File.file?(f) }

gemfile = File.join directory, gemfile
break
end
rescue Errno::ENOENT
return
end
end
gemfile = gemfile_path

return unless gemfile

Expand All @@ -82,19 +70,24 @@ def self.lockfile_contents
private_class_method :lockfile_contents

def self.bundle_config_version
config_file = bundler_config_file
return unless config_file && File.file?(config_file)
version = nil

contents = File.read(config_file)
contents =~ /^BUNDLE_VERSION:\s*["']?([^"'\s]+)["']?\s*$/
[bundler_local_config_file, bundler_global_config_file].each do |config_file|
next unless config_file && File.file?(config_file)

$1
contents = File.read(config_file)
contents =~ /^BUNDLE_VERSION:\s*["']?([^"'\s]+)["']?\s*$/

version = $1
break if version
end

version
end
private_class_method :bundle_config_version

def self.bundler_config_file
# see Bundler::Settings#global_config_file and local_config_file
# global
def self.bundler_global_config_file
# see Bundler::Settings#global_config_file
if ENV["BUNDLE_CONFIG"] && !ENV["BUNDLE_CONFIG"].empty?
ENV["BUNDLE_CONFIG"]
elsif ENV["BUNDLE_USER_CONFIG"] && !ENV["BUNDLE_USER_CONFIG"].empty?
Expand All @@ -103,10 +96,36 @@ def self.bundler_config_file
ENV["BUNDLE_USER_HOME"] + "config"
elsif Gem.user_home && !Gem.user_home.empty?
Gem.user_home + ".bundle/config"
else
# local
"config"
end
end
private_class_method :bundler_config_file
private_class_method :bundler_global_config_file

def self.bundler_local_config_file
gemfile = gemfile_path
return unless gemfile

File.join(File.dirname(gemfile), ".bundle", "config")
end
private_class_method :bundler_local_config_file

def self.gemfile_path
gemfile = ENV["BUNDLE_GEMFILE"]
gemfile = nil if gemfile&.empty?

unless gemfile
begin
Gem::Util.traverse_parents(Dir.pwd) do |directory|
next unless gemfile = Gem::GEM_DEP_FILES.find {|f| File.file?(f) }

gemfile = File.join directory, gemfile
break
end
rescue Errno::ENOENT
return
end
end

gemfile
end
private_class_method :gemfile_path
end
25 changes: 20 additions & 5 deletions test/rubygems/test_gem_bundler_version_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_bundler_version_with_bundle_config
f.write(config_content)
f.flush

bvf.stub(:bundler_config_file, f.path) do
bvf.stub(:bundler_global_config_file, f.path) do
assert_nil bvf.bundler_version
end
end
Expand All @@ -81,7 +81,7 @@ def test_bundler_version_with_bundle_config_single_quoted
f.write(config_with_single_quoted_version)
f.flush

bvf.stub(:bundler_config_file, f.path) do
bvf.stub(:bundler_global_config_file, f.path) do
assert_nil bvf.bundler_version
end
end
Expand All @@ -98,18 +98,33 @@ def test_bundler_version_with_bundle_config_version
f.write(config_content)
f.flush

bvf.stub(:bundler_config_file, f.path) do
bvf.stub(:bundler_global_config_file, f.path) do
assert_equal v("1.1.1.1"), bvf.bundler_version
end
end
end

def test_bundler_version_with_bundle_config_non_existent_file
bvf.stub(:bundler_config_file, "/non/existent/path") do
bvf.stub(:bundler_global_config_file, "/non/existent/path") do
assert_nil bvf.bundler_version
end
end

def test_bundler_version_set_on_local_config
config_content = <<~CONFIG
BUNDLE_VERSION: "1.2.3"
CONFIG

Tempfile.create("bundle_config") do |f|
f.write(config_content)
f.flush

bvf.stub(:bundler_local_config_file, f.path) do
assert_equal v("1.2.3"), bvf.bundler_version
end
end
end

def test_bundler_version_with_bundle_config_without_version
config_without_version = <<~CONFIG
BUNDLE_JOBS: "8"
Expand All @@ -120,7 +135,7 @@ def test_bundler_version_with_bundle_config_without_version
f.write(config_without_version)
f.flush

bvf.stub(:bundler_config_file, f.path) do
bvf.stub(:bundler_global_config_file, f.path) do
assert_nil bvf.bundler_version
end
end
Expand Down