From abf3e42bdef44c9fba421d2bb08c40f0f8c49ab5 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Thu, 20 Feb 2020 15:22:30 +0300 Subject: [PATCH] Replace deprecated `Bundler.with_clean_env` with `with_unbundled_env` Also add tests for `GEM_HOME` environment variable preserving. --- History.md | 1 + lib/puma/launcher.rb | 8 +++++--- test/bundle_preservation_test/config.ru | 12 +++++++++++- test/test_preserve_bundler_env.rb | 11 ++++++++--- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/History.md b/History.md index 9a38780c9a..973b0d4f9a 100644 --- a/History.md +++ b/History.md @@ -14,6 +14,7 @@ * Preserve `BUNDLE_GEMFILE` env var when using `prune_bundler` (#1893) * Send 408 request timeout even when queue requests is disabled (#2119) * Rescue IO::WaitReadable instead of EAGAIN for blocking read (#2121) + * Replace deprecated `Bundler.with_clean_env` with `with_original_env` (#2120) * Refactor * Remove unused loader argument from Plugin initializer (#2095) diff --git a/lib/puma/launcher.rb b/lib/puma/launcher.rb index f45e035edc..077e8e8eea 100644 --- a/lib/puma/launcher.rb +++ b/lib/puma/launcher.rb @@ -296,10 +296,12 @@ def prune_bundler deps, dirs = dependencies_and_files_to_require_after_prune log '* Pruning Bundler environment' - home = ENV['GEM_HOME'] + gem_home = ENV['GEM_HOME'] bundle_gemfile = ENV['BUNDLE_GEMFILE'] - Bundler.with_clean_env do - ENV['GEM_HOME'] = home + # Here should be something like `with_previous_env`, + # because `with_original_env` saves the env from `bundle exec rake` call. + Bundler.with_unbundled_env do + ENV['GEM_HOME'] = gem_home ENV['BUNDLE_GEMFILE'] = bundle_gemfile ENV['PUMA_BUNDLER_PRUNED'] = '1' args = [Gem.ruby, puma_wild_location, '-I', dirs.join(':'), deps.join(',')] + @original_argv diff --git a/test/bundle_preservation_test/config.ru b/test/bundle_preservation_test/config.ru index 1f0f2cc619..35bff76d83 100644 --- a/test/bundle_preservation_test/config.ru +++ b/test/bundle_preservation_test/config.ru @@ -1 +1,11 @@ -run lambda { |env| [200, {'Content-Type'=>'text/plain'}, [ENV['BUNDLE_GEMFILE'].inspect]] } +run( + lambda do |env| + [ + 200, + {'Content-Type'=>'text/plain'}, + [ + [ENV['BUNDLE_GEMFILE'], ENV['GEM_HOME']].inspect + ] + ] + end +) diff --git a/test/test_preserve_bundler_env.rb b/test/test_preserve_bundler_env.rb index 1dd1b43447..9d901b9539 100644 --- a/test/test_preserve_bundler_env.rb +++ b/test/test_preserve_bundler_env.rb @@ -12,9 +12,12 @@ def test_usr2_restart_preserves_bundler_environment skip_unless_signal_exist? :USR2 @tcp_port = UniquePort.call + gem_home = "/home/bundle_env_preservation_test" + bundle_gemfile = "Gemfile.bundle_env_preservation_test" env = { + "GEM_HOME" => gem_home, # Intentionally set this to something we wish to keep intact on restarts - "BUNDLE_GEMFILE" => "Gemfile.bundle_env_preservation_test", + "BUNDLE_GEMFILE" => bundle_gemfile, # Don't allow our (rake test's) original env to interfere with the child process "BUNDLER_ORIG_BUNDLE_GEMFILE" => nil } @@ -27,9 +30,11 @@ def test_usr2_restart_preserves_bundler_environment @pid = @server.pid connection = connect initial_reply = read_body(connection) - assert_match("Gemfile.bundle_env_preservation_test", initial_reply) + assert_match(bundle_gemfile, initial_reply) + assert_match(gem_home, initial_reply) restart_server connection new_reply = read_body(connection) - assert_match("Gemfile.bundle_env_preservation_test", new_reply) + assert_match(bundle_gemfile, new_reply) + assert_match(gem_home, initial_reply) end end