From dd42150ddb21166fd78dff5dbc5c2e58b12eea94 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Sun, 19 Mar 2017 19:47:01 -0700 Subject: [PATCH] Invoke `bundle` correctly within feature tests During the feature tests we create a separate directory with its own `Gemfile` and use `bundle` and `bundle exec` within that directory. However if `bundle exec` was used to run the feature tests, then a bundler environment is already loaded, and so the test Gemfile is never consulted. Running `bundle` doesn't have the desired effect, because the child process inherits the bundler environment variables from the parent process. Bundler's solution for this problem is the `Bundle.with_clean_env` method. This ensures that the child process doesn't inherit the parent's bundler environment. --- spec/support/test_app.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/spec/support/test_app.rb b/spec/support/test_app.rb index ac1e5071..2bdf37ba 100644 --- a/spec/support/test_app.rb +++ b/spec/support/test_app.rb @@ -44,14 +44,14 @@ module TestApp end Dir.chdir(test_app_path) do - `bundle` + run "bundle" end end def install_test_app_with(config) create_test_app Dir.chdir(test_app_path) do - `bundle exec cap install STAGES=#{stage}` + run "cap install STAGES=#{stage}" end write_local_deploy_file(config) end @@ -91,14 +91,15 @@ module TestApp end def cap(task, subdirectory=nil) - run "bundle exec cap #{stage} #{task}", subdirectory + run "cap #{stage} #{task} --trace", subdirectory end def run(command, subdirectory=nil) output = nil + command = "bundle exec #{command}" unless command =~ /^bundle\b/ dir = subdirectory ? test_app_path.join(subdirectory) : test_app_path Dir.chdir(dir) do - output = `#{command}` + output = with_clean_bundler_env { `#{command}` } end [$CHILD_STATUS.success?, output] end @@ -187,4 +188,9 @@ module TestApp def git_wrapper_path "/tmp/git-ssh-my_app_name-#{stage}-#{current_user}.sh" end + + def with_clean_bundler_env(&block) + return yield unless defined?(Bundler) + Bundler.with_clean_env(&block) + end end