[rubygems/rubygems] Only activate bundler when needed

Loading Bundler beforehand was actually replacing ENV with a backup of
the pre-Bundler environment through `Bundler::EnvironmentPreserver`. I
think that was making a bug in `ENV.replace` not bite our tests, because
Bundler includes proper patches to workaround that issue. So this commit
also includes these patches in RubyGems tests.

https://github.com/rubygems/rubygems/commit/8e079149b9
This commit is contained in:
David Rodríguez 2021-12-04 13:15:32 +01:00 committed by git
parent 20eff52087
commit 668e78f01b
2 changed files with 287 additions and 234 deletions

View File

@ -2,21 +2,11 @@
require "rubygems" require "rubygems"
# If bundler gemspec exists, add to stubs
bundler_gemspec = File.expand_path("../../bundler/bundler.gemspec", __dir__)
if File.exist?(bundler_gemspec)
Gem::Specification.dirs.unshift File.dirname(bundler_gemspec)
Gem::Specification.class_variable_set :@@stubs, nil
Gem::Specification.stubs
Gem::Specification.dirs.shift
end
begin begin
gem "test-unit", "~> 3.0" gem "test-unit", "~> 3.0"
rescue Gem::LoadError rescue Gem::LoadError
end end
require "bundler"
require "test/unit" require "test/unit"
ENV["JARS_SKIP"] = "true" if Gem.java_platform? # avoid unnecessary and noisy `jar-dependencies` post install hook ENV["JARS_SKIP"] = "true" if Gem.java_platform? # avoid unnecessary and noisy `jar-dependencies` post install hook
@ -404,7 +394,6 @@ class Gem::TestCase < Test::Unit::TestCase
Gem.loaded_specs.clear Gem.loaded_specs.clear
Gem.instance_variable_set(:@activated_gem_paths, 0) Gem.instance_variable_set(:@activated_gem_paths, 0)
Gem.clear_default_specs Gem.clear_default_specs
Bundler.reset!
Gem.configuration.verbose = true Gem.configuration.verbose = true
Gem.configuration.update_sources = true Gem.configuration.update_sources = true
@ -460,7 +449,7 @@ class Gem::TestCase < Test::Unit::TestCase
FileUtils.rm_rf @tempdir FileUtils.rm_rf @tempdir
ENV.replace(@orig_env) restore_env
Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE
Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE, Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE,
@ -1581,6 +1570,23 @@ Also, a list:
PUBLIC_KEY = nil PUBLIC_KEY = nil
PUBLIC_CERT = nil PUBLIC_CERT = nil
end if Gem::HAVE_OPENSSL end if Gem::HAVE_OPENSSL
private
def restore_env
unless Gem.win_platform?
ENV.replace(@orig_env)
return
end
# Fallback logic for Windows below to workaround
# https://bugs.ruby-lang.org/issues/16798. Can be dropped once all
# supported rubies include the fix for that.
ENV.clear
@orig_env.each {|k, v| ENV[k] = v }
end
end end
# https://github.com/seattlerb/minitest/blob/13c48a03d84a2a87855a4de0c959f96800100357/lib/minitest/mock.rb#L192 # https://github.com/seattlerb/minitest/blob/13c48a03d84a2a87855a4de0c959f96800100357/lib/minitest/mock.rb#L192

View File

@ -615,6 +615,7 @@ class TestGem < Gem::TestCase
end end
def test_self_use_gemdeps def test_self_use_gemdeps
with_local_bundler do
with_rubygems_gemdeps("-") do with_rubygems_gemdeps("-") do
FileUtils.mkdir_p "detect/a/b" FileUtils.mkdir_p "detect/a/b"
FileUtils.mkdir_p "detect/a/Isolate" FileUtils.mkdir_p "detect/a/Isolate"
@ -632,6 +633,7 @@ class TestGem < Gem::TestCase
end end
end end
end end
end
def test_self_dir def test_self_dir
assert_equal @gemhome, Gem.dir assert_equal @gemhome, Gem.dir
@ -773,6 +775,7 @@ class TestGem < Gem::TestCase
end end
def test_self_find_files_with_gemfile def test_self_find_files_with_gemfile
with_local_bundler do
cwd = File.expand_path("test/rubygems", PROJECT_DIR) cwd = File.expand_path("test/rubygems", PROJECT_DIR)
actual_load_path = $LOAD_PATH.unshift(cwd).dup actual_load_path = $LOAD_PATH.unshift(cwd).dup
@ -806,6 +809,7 @@ class TestGem < Gem::TestCase
assert_equal expected, Gem.find_files("sff/**.rb").sort, "[ruby-core:31730]" assert_equal expected, Gem.find_files("sff/**.rb").sort, "[ruby-core:31730]"
assert_equal cwd, actual_load_path.shift assert_equal cwd, actual_load_path.shift
end end
end
def test_self_find_latest_files def test_self_find_latest_files
cwd = File.expand_path("test/rubygems", PROJECT_DIR) cwd = File.expand_path("test/rubygems", PROJECT_DIR)
@ -1631,6 +1635,7 @@ class TestGem < Gem::TestCase
end end
def test_auto_activation_of_specific_gemdeps_file def test_auto_activation_of_specific_gemdeps_file
with_local_bundler do
a = util_spec "a", "1", nil, "lib/a.rb" a = util_spec "a", "1", nil, "lib/a.rb"
b = util_spec "b", "1", nil, "lib/b.rb" b = util_spec "b", "1", nil, "lib/b.rb"
c = util_spec "c", "1", nil, "lib/c.rb" c = util_spec "c", "1", nil, "lib/c.rb"
@ -1651,8 +1656,10 @@ class TestGem < Gem::TestCase
assert_equal add_bundler_full_name(%W[a-1 b-1 c-1]), loaded_spec_names assert_equal add_bundler_full_name(%W[a-1 b-1 c-1]), loaded_spec_names
end end
end end
end
def test_auto_activation_of_used_gemdeps_file def test_auto_activation_of_used_gemdeps_file
with_local_bundler do
a = util_spec "a", "1", nil, "lib/a.rb" a = util_spec "a", "1", nil, "lib/a.rb"
b = util_spec "b", "1", nil, "lib/b.rb" b = util_spec "b", "1", nil, "lib/b.rb"
c = util_spec "c", "1", nil, "lib/c.rb" c = util_spec "c", "1", nil, "lib/c.rb"
@ -1675,6 +1682,7 @@ class TestGem < Gem::TestCase
assert_equal expected_specs, loaded_spec_names assert_equal expected_specs, loaded_spec_names
end end
end end
end
def add_bundler_full_name(names) def add_bundler_full_name(names)
names << "bundler-#{Bundler::VERSION}".freeze names << "bundler-#{Bundler::VERSION}".freeze
@ -1683,6 +1691,7 @@ class TestGem < Gem::TestCase
end end
def test_looks_for_gemdeps_files_automatically_from_binstubs def test_looks_for_gemdeps_files_automatically_from_binstubs
with_local_bundler do
a = util_spec "a", "1" do |s| a = util_spec "a", "1" do |s|
s.executables = %w[foo] s.executables = %w[foo]
s.bindir = "exe" s.bindir = "exe"
@ -1728,8 +1737,10 @@ class TestGem < Gem::TestCase
assert_equal ["b-1", "c-1"], out - out0 assert_equal ["b-1", "c-1"], out - out0
end end
end end
end
def test_looks_for_gemdeps_files_automatically_from_binstubs_in_parent_dir def test_looks_for_gemdeps_files_automatically_from_binstubs_in_parent_dir
with_local_bundler do
pend "IO.popen has issues on JRuby when passed :chdir" if Gem.java_platform? pend "IO.popen has issues on JRuby when passed :chdir" if Gem.java_platform?
a = util_spec "a", "1" do |s| a = util_spec "a", "1" do |s|
@ -1781,6 +1792,7 @@ class TestGem < Gem::TestCase
assert_equal ["b-1", "c-1"], out - out0 assert_equal ["b-1", "c-1"], out - out0
end end
end end
end
def test_register_default_spec def test_register_default_spec
Gem.clear_default_specs Gem.clear_default_specs
@ -1823,6 +1835,7 @@ class TestGem < Gem::TestCase
end end
def test_use_gemdeps def test_use_gemdeps
with_local_bundler do
gem_deps_file = "gem.deps.rb".tap(&Gem::UNTAINT) gem_deps_file = "gem.deps.rb".tap(&Gem::UNTAINT)
spec = util_spec "a", 1 spec = util_spec "a", 1
install_specs spec install_specs spec
@ -1841,8 +1854,10 @@ class TestGem < Gem::TestCase
assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names
refute_nil Gem.gemdeps refute_nil Gem.gemdeps
end end
end
def test_use_gemdeps_ENV def test_use_gemdeps_ENV
with_local_bundler do
with_rubygems_gemdeps(nil) do with_rubygems_gemdeps(nil) do
spec = util_spec "a", 1 spec = util_spec "a", 1
@ -1857,8 +1872,10 @@ class TestGem < Gem::TestCase
refute spec.activated? refute spec.activated?
end end
end end
end
def test_use_gemdeps_argument_missing def test_use_gemdeps_argument_missing
with_local_bundler do
e = assert_raise ArgumentError do e = assert_raise ArgumentError do
Gem.use_gemdeps "gem.deps.rb" Gem.use_gemdeps "gem.deps.rb"
end end
@ -1866,8 +1883,10 @@ class TestGem < Gem::TestCase
assert_equal "Unable to find gem dependencies file at gem.deps.rb", assert_equal "Unable to find gem dependencies file at gem.deps.rb",
e.message e.message
end end
end
def test_use_gemdeps_argument_missing_match_ENV def test_use_gemdeps_argument_missing_match_ENV
with_local_bundler do
with_rubygems_gemdeps("gem.deps.rb") do with_rubygems_gemdeps("gem.deps.rb") do
e = assert_raise ArgumentError do e = assert_raise ArgumentError do
Gem.use_gemdeps "gem.deps.rb" Gem.use_gemdeps "gem.deps.rb"
@ -1877,8 +1896,10 @@ class TestGem < Gem::TestCase
e.message e.message
end end
end end
end
def test_use_gemdeps_automatic def test_use_gemdeps_automatic
with_local_bundler do
with_rubygems_gemdeps("-") do with_rubygems_gemdeps("-") do
spec = util_spec "a", 1 spec = util_spec "a", 1
install_specs spec install_specs spec
@ -1895,16 +1916,20 @@ class TestGem < Gem::TestCase
assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names
end end
end end
end
def test_use_gemdeps_automatic_missing def test_use_gemdeps_automatic_missing
with_local_bundler do
with_rubygems_gemdeps("-") do with_rubygems_gemdeps("-") do
Gem.use_gemdeps Gem.use_gemdeps
assert true # count assert true # count
end end
end end
end
def test_use_gemdeps_disabled def test_use_gemdeps_disabled
with_local_bundler do
with_rubygems_gemdeps("") do with_rubygems_gemdeps("") do
spec = util_spec "a", 1 spec = util_spec "a", 1
@ -1919,8 +1944,10 @@ class TestGem < Gem::TestCase
refute spec.activated? refute spec.activated?
end end
end end
end
def test_use_gemdeps_missing_gem def test_use_gemdeps_missing_gem
with_local_bundler do
with_rubygems_gemdeps("x") do with_rubygems_gemdeps("x") do
File.open "x", "w" do |io| File.open "x", "w" do |io|
io.write 'gem "a"' io.write 'gem "a"'
@ -1941,8 +1968,10 @@ You may need to `bundle install` to install missing gems
end end
end end
end end
end
def test_use_gemdeps_specific def test_use_gemdeps_specific
with_local_bundler do
with_rubygems_gemdeps("x") do with_rubygems_gemdeps("x") do
spec = util_spec "a", 1 spec = util_spec "a", 1
install_specs spec install_specs spec
@ -1959,6 +1988,7 @@ You may need to `bundle install` to install missing gems
assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names
end end
end end
end
def test_operating_system_defaults def test_operating_system_defaults
operating_system_defaults = Gem.operating_system_defaults operating_system_defaults = Gem.operating_system_defaults
@ -2090,4 +2120,21 @@ You may need to `bundle install` to install missing gems
ensure ensure
ENV["RUBYGEMS_GEMDEPS"] = rubygems_gemdeps ENV["RUBYGEMS_GEMDEPS"] = rubygems_gemdeps
end end
def with_local_bundler
# If bundler gemspec exists, add to stubs
bundler_gemspec = File.expand_path("../../bundler/bundler.gemspec", __dir__)
if File.exist?(bundler_gemspec)
Gem::Specification.dirs.unshift File.dirname(bundler_gemspec)
Gem::Specification.class_variable_set :@@stubs, nil
Gem::Specification.stubs
Gem::Specification.dirs.shift
end
require "bundler"
yield
ensure
Bundler.reset!
end
end end