Skip to content

Commit ee002a5

Browse files
hsbtmatzbot
authored andcommitted
[ruby/rubygems] Respect BUNDLE_VERSION config at Gem::BundlerVersionFinder
If we use "system" variable in BUNDLE_VERSION on Bundler configuration, we can use bundler version provided by system installation. But the current logic returns the first activated version of bundler like 2.7.2. It makes to confuse users. ruby/rubygems@4eb66d9549
1 parent 917e77b commit ee002a5

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

lib/rubygems/bundler_version_finder.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Gem::BundlerVersionFinder
44
def self.bundler_version
5+
return if bundle_config_version == "system"
6+
57
v = ENV["BUNDLER_VERSION"]
68
v = nil if v&.empty?
79

@@ -78,4 +80,33 @@ def self.lockfile_contents
7880
File.read(lockfile)
7981
end
8082
private_class_method :lockfile_contents
83+
84+
def self.bundle_config_version
85+
config_file = bundler_config_file
86+
return unless config_file && File.file?(config_file)
87+
88+
contents = File.read(config_file)
89+
contents =~ /^BUNDLE_VERSION:\s*["']?([^"'\s]+)["']?\s*$/
90+
91+
$1
92+
end
93+
private_class_method :bundle_config_version
94+
95+
def self.bundler_config_file
96+
# see Bundler::Settings#global_config_file and local_config_file
97+
# global
98+
if ENV["BUNDLE_CONFIG"] && !ENV["BUNDLE_CONFIG"].empty?
99+
ENV["BUNDLE_CONFIG"]
100+
elsif ENV["BUNDLE_USER_CONFIG"] && !ENV["BUNDLE_USER_CONFIG"].empty?
101+
ENV["BUNDLE_USER_CONFIG"]
102+
elsif ENV["BUNDLE_USER_HOME"] && !ENV["BUNDLE_USER_HOME"].empty?
103+
ENV["BUNDLE_USER_HOME"] + "config"
104+
elsif Gem.user_home && !Gem.user_home.empty?
105+
Gem.user_home + ".bundle/config"
106+
else
107+
# local
108+
"config"
109+
end
110+
end
111+
private_class_method :bundler_config_file
81112
end

test/rubygems/helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ def setup
335335
ENV["XDG_STATE_HOME"] = nil
336336
ENV["SOURCE_DATE_EPOCH"] = nil
337337
ENV["BUNDLER_VERSION"] = nil
338+
ENV["BUNDLE_CONFIG"] = nil
339+
ENV["BUNDLE_USER_CONFIG"] = nil
340+
ENV["BUNDLE_USER_HOME"] = nil
338341
ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = "true"
339342

340343
@current_dir = Dir.pwd

test/rubygems/test_gem_bundler_version_finder.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative "helper"
44
require "rubygems/bundler_version_finder"
5+
require "tempfile"
56

67
class TestGemBundlerVersionFinder < Gem::TestCase
78
def setup
@@ -56,6 +57,75 @@ def test_bundler_version_with_bundle_update_bundler
5657
assert_nil bvf.bundler_version
5758
end
5859

60+
def test_bundler_version_with_bundle_config
61+
config_content = <<~CONFIG
62+
BUNDLE_VERSION: "system"
63+
CONFIG
64+
65+
Tempfile.create("bundle_config") do |f|
66+
f.write(config_content)
67+
f.flush
68+
69+
bvf.stub(:bundler_config_file, f.path) do
70+
assert_nil bvf.bundler_version
71+
end
72+
end
73+
end
74+
75+
def test_bundler_version_with_bundle_config_single_quoted
76+
config_with_single_quoted_version = <<~CONFIG
77+
BUNDLE_VERSION: 'system'
78+
CONFIG
79+
80+
Tempfile.create("bundle_config") do |f|
81+
f.write(config_with_single_quoted_version)
82+
f.flush
83+
84+
bvf.stub(:bundler_config_file, f.path) do
85+
assert_nil bvf.bundler_version
86+
end
87+
end
88+
end
89+
90+
def test_bundler_version_with_bundle_config_version
91+
ENV["BUNDLER_VERSION"] = "1.1.1.1"
92+
93+
config_content = <<~CONFIG
94+
BUNDLE_VERSION: "1.2.3"
95+
CONFIG
96+
97+
Tempfile.create("bundle_config") do |f|
98+
f.write(config_content)
99+
f.flush
100+
101+
bvf.stub(:bundler_config_file, f.path) do
102+
assert_equal v("1.1.1.1"), bvf.bundler_version
103+
end
104+
end
105+
end
106+
107+
def test_bundler_version_with_bundle_config_non_existent_file
108+
bvf.stub(:bundler_config_file, "/non/existent/path") do
109+
assert_nil bvf.bundler_version
110+
end
111+
end
112+
113+
def test_bundler_version_with_bundle_config_without_version
114+
config_without_version = <<~CONFIG
115+
BUNDLE_JOBS: "8"
116+
BUNDLE_GEM__TEST: "minitest"
117+
CONFIG
118+
119+
Tempfile.create("bundle_config") do |f|
120+
f.write(config_without_version)
121+
f.flush
122+
123+
bvf.stub(:bundler_config_file, f.path) do
124+
assert_nil bvf.bundler_version
125+
end
126+
end
127+
end
128+
59129
def test_bundler_version_with_lockfile
60130
bvf.stub(:lockfile_contents, "") do
61131
assert_nil bvf.bundler_version

0 commit comments

Comments
 (0)