diff options
author | Bundlerbot <bot@bundler.io> | 2019-03-04 14:51:41 +0000 |
---|---|---|
committer | Bundlerbot <bot@bundler.io> | 2019-03-04 14:51:41 +0000 |
commit | 900f5f1e6285de591e89664cffe5818e0b5d88d0 (patch) | |
tree | fae7b6954716d361e117bbd5895570d5c3ee32a3 | |
parent | 06bb460ad824925996c1ea449a74cca8cbf52eda (diff) | |
parent | a15bdb4afe8815d3b5770a0adb3ec853863923c2 (diff) | |
download | bundler-900f5f1e6285de591e89664cffe5818e0b5d88d0.tar.gz |
Merge #7011
7011: Do not prepend ENV vars for sys_exec calls, but use env param of Open3.popen3 r=deivid-rodriguez a=janpio
### What was the end-user problem that led to this PR?
Several tests were failing on Windows because of the same problem:
### What was your diagnosis of the problem?
On macOS and Linux you can prepend `ENV_VAR=value` in front of a command to set an environment variable for just this command execution.
On Windows this construct doesn't exit and such a command fails.
### What is your fix for the problem, implemented in this PR?
`Open3.popen3`, the method used to executed the command in `sys_exec`, also has an optional `env` parameter which you can use to supply a hash of environment variables. Instead of prepending on the command itself, the code now uses that parameter for the env variables. (Suggested by @deivid-rodriguez in https://github.com/bundler/bundler/issues/6886#issuecomment-452010623).
### Why did you choose this fix out of the possible options?
The parameter of `Open3.popen3` perfectly matches our use case.
---
Fixes 35 failing tests on Windows.
closes #6886
Co-authored-by: Jan Piotrowski <piotrowski+git@gmail.com>
-rw-r--r-- | spec/commands/binstubs_spec.rb | 2 | ||||
-rw-r--r-- | spec/commands/newgem_spec.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers.rb | 12 |
3 files changed, 8 insertions, 8 deletions
diff --git a/spec/commands/binstubs_spec.rb b/spec/commands/binstubs_spec.rb index b958677876..9126b29592 100644 --- a/spec/commands/binstubs_spec.rb +++ b/spec/commands/binstubs_spec.rb @@ -133,7 +133,7 @@ RSpec.describe "bundle binstubs <gem>" do context "when BUNDLER_VERSION is set" do it "runs the correct version of bundler" do - sys_exec "BUNDLER_VERSION='999.999.999' #{bundled_app("bin/bundle")} install" + sys_exec "#{bundled_app("bin/bundle")} install", "BUNDLER_VERSION" => "999.999.999" expect(exitstatus).to eq(42) if exitstatus expect(last_command.stderr).to include("Activating bundler (999.999.999) failed:"). and include("To install the version of bundler this project requires, run `gem install bundler -v '999.999.999'`") diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb index c7cc5e594a..0d1058ad11 100644 --- a/spec/commands/newgem_spec.rb +++ b/spec/commands/newgem_spec.rb @@ -162,7 +162,7 @@ RSpec.describe "bundle gem" do load_paths = [lib, spec] load_path_str = "-I#{load_paths.join(File::PATH_SEPARATOR)}" - sys_exec "PATH=\"\" #{Gem.ruby} #{load_path_str} #{bindir.join("bundle")} gem #{gem_name}" + sys_exec "#{Gem.ruby} #{load_path_str} #{bindir.join("bundle")} gem #{gem_name}", "PATH" => "" end it "creates the gem without the need for git" do diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index 9be5955ac3..4eb98645a5 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -131,8 +131,6 @@ module Spec load_path << spec load_path_str = "-I#{load_path.join(File::PATH_SEPARATOR)}" - env = env.map {|k, v| "#{k}='#{v}'" }.join(" ") - args = options.map do |k, v| case v when nil @@ -146,8 +144,8 @@ module Spec end end.join - cmd = "#{env} #{sudo} #{Gem.ruby} #{load_path_str} #{requires_str} #{bundle_bin} #{cmd}#{args}" - sys_exec(cmd) {|i, o, thr| yield i, o, thr if block_given? } + cmd = "#{sudo} #{Gem.ruby} #{load_path_str} #{requires_str} #{bundle_bin} #{cmd}#{args}" + sys_exec(cmd, env) {|i, o, thr| yield i, o, thr if block_given? } end bang :bundle @@ -216,10 +214,12 @@ module Spec "#{Gem.ruby} -S #{ENV["GEM_PATH"]}/bin/rake" end - def sys_exec(cmd) + def sys_exec(cmd, env = {}) command_execution = CommandExecution.new(cmd.to_s, Dir.pwd) - Open3.popen3(cmd.to_s) do |stdin, stdout, stderr, wait_thr| + env = env.map {|k, v| [k.to_s, v.to_s] }.to_h # convert env keys and values to string + + Open3.popen3(env, cmd.to_s) do |stdin, stdout, stderr, wait_thr| yield stdin, stdout, wait_thr if block_given? stdin.close |