Skip to content

Commit

Permalink
Merge pull request #1893 from seven1m/master
Browse files Browse the repository at this point in the history
Preserve BUNDLE_GEMFILE and add a test for it
  • Loading branch information
nateberkopec committed Feb 18, 2020
2 parents 71bcd84 + 1846ef3 commit 8c9b3eb
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -10,6 +10,7 @@
* Bugfixes
* Your bugfix goes here (#Github Number)
* Windows update extconf.rb for use with ssp and varied Ruby/MSYS2 combinations (#2069)
* Preserve `BUNDLE_GEMFILE` env var when using `prune_bundler` (#1893)

* Refactor
* Remove unused loader argument from Plugin initializer (#2095)
Expand Down
2 changes: 2 additions & 0 deletions lib/puma/launcher.rb
Expand Up @@ -297,8 +297,10 @@ def prune_bundler

log '* Pruning Bundler environment'
home = ENV['GEM_HOME']
bundle_gemfile = ENV['BUNDLE_GEMFILE']
Bundler.with_clean_env do
ENV['GEM_HOME'] = home
ENV['BUNDLE_GEMFILE'] = bundle_gemfile
ENV['PUMA_BUNDLER_PRUNED'] = '1'
args = [Gem.ruby, puma_wild_location, '-I', dirs.join(':'), deps.join(',')] + @original_argv
# Ruby 2.0+ defaults to true which breaks socket activation
Expand Down
1 change: 1 addition & 0 deletions test/bundle_preservation_test/.gitignore
@@ -0,0 +1 @@
Gemfile.bundle_env_preservation_test.lock
@@ -0,0 +1 @@
gem 'puma', path: '../..'
1 change: 1 addition & 0 deletions test/bundle_preservation_test/config.ru
@@ -0,0 +1 @@
run lambda { |env| [200, {'Content-Type'=>'text/plain'}, [ENV['BUNDLE_GEMFILE'].inspect]] }
18 changes: 14 additions & 4 deletions test/helpers/integration.rb
Expand Up @@ -75,14 +75,24 @@ def restart_server_and_listen(argv)
end

# reuses an existing connection to make sure that works
def restart_server(connection)
def restart_server(connection, log: false)
Process.kill :USR2, @pid
connection.write "GET / HTTP/1.1\r\n\r\n" # trigger it to start by sending a new request
wait_for_server_to_boot
wait_for_server_to_boot(log: log)
end

def wait_for_server_to_boot
true while @server.gets !~ /Ctrl-C/ # wait for server to say it booted
# wait for server to say it booted
def wait_for_server_to_boot(log: false)
if log
puts "Waiting for server to boot..."
begin
line = @server.gets
puts line if line && line.strip != ''
end while line !~ /Ctrl-C/
puts "Server booted!"
else
true while @server.gets !~ /Ctrl-C/
end
end

def connect(path = nil, unix: false)
Expand Down
35 changes: 35 additions & 0 deletions test/test_preserve_bundler_env.rb
@@ -0,0 +1,35 @@
require_relative "helper"
require_relative "helpers/integration"

class TestPreserveBundlerEnv < TestIntegration
def setup
skip NO_FORK_MSG unless HAS_FORK
super
end

# It does not wipe out BUNDLE_GEMFILE et al
def test_usr2_restart_preserves_bundler_environment
skip_unless_signal_exist? :USR2

@tcp_port = UniquePort.call
env = {
# Intentionally set this to something we wish to keep intact on restarts
"BUNDLE_GEMFILE" => "Gemfile.bundle_env_preservation_test",
# Don't allow our (rake test's) original env to interfere with the child process
"BUNDLER_ORIG_BUNDLE_GEMFILE" => nil
}
# Must use `bundle exec puma` here, because otherwise Bundler may not be defined, which is required to trigger the bug
cmd = "bundle exec puma -q -w 1 --prune-bundler -b tcp://#{HOST}:#{@tcp_port}"
Dir.chdir(File.expand_path("bundle_preservation_test", __dir__)) do
@server = IO.popen(env, cmd.split, "r")
end
wait_for_server_to_boot
@pid = @server.pid
connection = connect
initial_reply = read_body(connection)
assert_match("Gemfile.bundle_env_preservation_test", initial_reply)
restart_server connection
new_reply = read_body(connection)
assert_match("Gemfile.bundle_env_preservation_test", new_reply)
end
end

0 comments on commit 8c9b3eb

Please sign in to comment.