do not reset bundler environment if unneeded

This commit is contained in:
Jon Ruskin 2021-06-15 17:47:18 -07:00
Родитель 4588fc7d9a
Коммит 224b243d76
2 изменённых файлов: 47 добавлений и 13 удалений

Просмотреть файл

@ -134,28 +134,32 @@ module Licensed
@lockfile_path ||= gemfile_path.dirname.join(GEMFILES[gemfile_path.basename.to_s])
end
private
# helper to clear all bundler environment around a yielded block
def with_local_configuration
# force bundler to use the local gem file
original_bundle_gemfile, ENV["BUNDLE_GEMFILE"] = ENV["BUNDLE_GEMFILE"], gemfile_path.to_s
# silence any bundler warnings while running licensed
bundler_ui, ::Bundler.ui = ::Bundler.ui, ::Bundler::UI::Silent.new
# reset all bundler configuration
::Bundler.reset!
# and re-configure with settings for current directory
::Bundler.configure
original_bundle_gemfile = nil
if gemfile_path.to_s != ENV["BUNDLE_GEMFILE"]
# force bundler to use the local gem file
original_bundle_gemfile, ENV["BUNDLE_GEMFILE"] = ENV["BUNDLE_GEMFILE"], gemfile_path.to_s
# reset all bundler configuration
::Bundler.reset!
# and re-configure with settings for current directory
::Bundler.configure
end
yield
ensure
ENV["BUNDLE_GEMFILE"] = original_bundle_gemfile
::Bundler.ui = bundler_ui
if original_bundle_gemfile
ENV["BUNDLE_GEMFILE"] = original_bundle_gemfile
# restore bundler configuration
::Bundler.configure
# restore bundler configuration
::Bundler.configure
end
::Bundler.ui = bundler_ui
end
# Returns whether the current licensed execution is running ruby-packer

Просмотреть файл

@ -252,5 +252,35 @@ if Licensed::Shell.tool_available?("bundle")
end
end
end
describe "#with_local_configuration" do
it "resets the Bundler environment" do
begin
original_gem_home, ENV["GEM_HOME"] = ENV["GEM_HOME"], "foo"
Dir.chdir(fixtures) do
source.with_local_configuration do
refute_equal "foo", ENV["GEM_HOME"]
end
end
ensure
ENV["GEM_HOME"] = original_gem_home
end
end
it "does not reset Bundler environment when the correct environment is already set" do
begin
original_gem_home, ENV["GEM_HOME"] = ENV["GEM_HOME"], "foo"
original_bundle_gemfile, ENV["BUNDLE_GEMFILE"] = ENV["BUNDLE_GEMFILE"], source.gemfile_path.to_s
Dir.chdir(fixtures) do
source.with_local_configuration do
assert_equal "foo", ENV["GEM_HOME"]
end
end
ensure
ENV["BUNDLE_GEMFILE"] = original_bundle_gemfile
ENV["GEM_HOME"] = original_gem_home
end
end
end
end
end