Repository: puma/puma
File: test/test_preserve_bundler_env.rb (Ruby)

Code before:
  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

  def test_worker_forking_preserves_bundler_config_path
    skip_unless_signal_exist? :TERM

    @tcp_port = UniquePort.call
    env = {
      # Disable the .bundle/config file in the bundle_app_config_test directory
      "BUNDLE_APP_CONFIG" => "/dev/null",
      "BUNDLER_ORIG_BUNDLE_GEMFILE" => nil
    }
    cmd = "bundle exec puma -q -w 1 --prune-bundler -b tcp://#{HOST}:#{@tcp_port}"
    Dir.chdir File.expand_path("bundle_app_config_test", __dir__) do
      @server = IO.popen(env, cmd.split, "r")
    end
    wait_for_server_to_boot
    @pid = @server.pid
    reply = read_body(connect)
    assert_equal("Hello World", reply)
  end

  def test_phased_restart_preserves_unspecified_bundle_gemfile
    skip_unless_signal_exist? :USR1

    @tcp_port = UniquePort.call
    env = {
      "BUNDLE_GEMFILE" => nil,
      "BUNDLER_ORIG_BUNDLE_GEMFILE" => nil

Review comment (bug):
Locally, if I undo the change introduced to `prune_bundler`,

```sh
git restore --source HEAD^ -- lib
```

This test is still green. In other words, the test does not appear to guard against a regression for the new functionality.

Do you think it is possible to write this test in a way that it fails on `master` in the way you observed?

Code after:
  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

  def test_worker_forking_preserves_bundler_config_path
    skip_unless_signal_exist? :TERM

    @tcp_port = UniquePort.call
    env = {
      # Disable the .bundle/config file in the bundle_app_config_test directory
      "BUNDLE_APP_CONFIG" => "/dev/null",
      # Don't allow our (rake test's) original env to interfere with the child process
      "BUNDLE_GEMFILE" => nil,
      "BUNDLER_ORIG_BUNDLE_GEMFILE" => nil
    }
    cmd = "bundle exec puma -q -w 1 --prune-bundler -b tcp://#{HOST}:#{@tcp_port}"
    Dir.chdir File.expand_path("bundle_app_config_test", __dir__) do
      @server = IO.popen(env, cmd.split, "r")
    end
    wait_for_server_to_boot
    @pid = @server.pid
    reply = read_body(connect)
    assert_equal("Hello World", reply)
  end

  def test_phased_restart_preserves_unspecified_bundle_gemfile
    skip_unless_signal_exist? :USR1

    @tcp_port = UniquePort.call
    env = {