From 20dd6657062c650505b81343e49febd9ad9461ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 19 Mar 2019 19:06:14 +0100 Subject: [PATCH] Introduce `{original,unbundled}_{system,exec}` helpers Because the previous helpers, `clean_exec` and `clean_system`, use deprecated behavior and thus should be deprecated too. --- lib/bundler.rb | 38 ++++++++++- spec/runtime/with_unbundled_env_spec.rb | 88 ++++++++++++++++++++++--- 2 files changed, 115 insertions(+), 11 deletions(-) diff --git a/lib/bundler.rb b/lib/bundler.rb index 1c6406807da..d500a578619 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -335,12 +335,46 @@ def with_unbundled_env with_env(unbundled_env) { yield } end + # Run subcommand with the environment present before Bundler was activated + def original_system(*args) + with_original_env { Kernel.system(*args) } + end + + # @deprecated Use `unbundled_system` instead def clean_system(*args) - with_clean_env { Kernel.system(*args) } + Bundler::SharedHelpers.major_deprecation( + 2, + "`Bundler.clean_system` has been deprecated in favor of `Bundler.unbundled_system`. " \ + "If you instead want to run the command in the environment before bundler was originally loaded, use `Bundler.original_system`" + ) + + with_env(unbundled_env) { Kernel.system(*args) } + end + + # Run subcommand in an environment with all bundler related variables removed + def unbundled_system(*args) + with_unbundled_env { Kernel.system(*args) } end + # Run a `Kernel.exec` to a subcommand with the environment present before Bundler was activated + def original_exec(*args) + with_original_env { Kernel.exec(*args) } + end + + # @deprecated Use `unbundled_exec` instead def clean_exec(*args) - with_clean_env { Kernel.exec(*args) } + Bundler::SharedHelpers.major_deprecation( + 2, + "`Bundler.clean_exec` has been deprecated in favor of `Bundler.unbundled_exec`. " \ + "If you instead want to exec to a command in the environment before bundler was originally loaded, use `Bundler.original_exec`" + ) + + with_env(unbundled_env) { Kernel.exec(*args) } + end + + # Run a `Kernel.exec` to a subcommand in an environment with all bundler related variables removed + def unbundled_exec(*args) + with_env(unbundled_env) { Kernel.exec(*args) } end def local_platform diff --git a/spec/runtime/with_unbundled_env_spec.rb b/spec/runtime/with_unbundled_env_spec.rb index c47f2912c5f..1ed853f467f 100644 --- a/spec/runtime/with_unbundled_env_spec.rb +++ b/spec/runtime/with_unbundled_env_spec.rb @@ -188,20 +188,90 @@ def build_bundler_context end end - describe "Bundler.clean_system", :bundler => "< 2" do + describe "Bundler.original_system" do + it "runs system inside with_original_env" do + code = 'exit Bundler.original_system(%(test "\$BUNDLE_FOO" = "bar"))' + lib = File.expand_path("../../lib", __dir__) + system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'") + expect($?.exitstatus).to eq(0) + end + end + + describe "Bundler.clean_system" do it "runs system inside with_clean_env" do - Bundler.clean_system(%(echo 'if [ "$BUNDLE_PATH" = "" ]; then exit 42; else exit 1; fi' | /bin/sh)) - expect($?.exitstatus).to eq(42) + code = 'exit Bundler.clean_system(%(test "\$BUNDLE_FOO" = "bar"))' + lib = File.expand_path("../../lib", __dir__) + system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'") + expect($?.exitstatus).to eq(1) + end + end + + describe "Bundler.unbundled_system" do + it "runs system inside with_unbundled_env" do + code = 'exit Bundler.clean_system(%(test "\$BUNDLE_FOO" = "bar"))' + lib = File.expand_path("../../lib", __dir__) + system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'") + expect($?.exitstatus).to eq(1) end end - describe "Bundler.clean_exec", :bundler => "< 2" do + describe "Bundler.original_exec" do + let(:code) do + <<~RUBY + Process.fork do + exit Bundler.original_exec(%(test "\$BUNDLE_FOO" = "bar")) + end + + _, status = Process.wait2 + + exit(status.exitstatus) + RUBY + end + + it "runs exec inside with_original_env" do + lib = File.expand_path("../../lib", __dir__) + system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'") + expect($?.exitstatus).to eq(0) + end + end + + describe "Bundler.clean_exec" do + let(:code) do + <<~RUBY + Process.fork do + exit Bundler.clean_exec(%(test "\$BUNDLE_FOO" = "bar")) + end + + _, status = Process.wait2 + + exit(status.exitstatus) + RUBY + end + it "runs exec inside with_clean_env" do - pid = Kernel.fork do - Bundler.clean_exec(%(echo 'if [ "$BUNDLE_PATH" = "" ]; then exit 42; else exit 1; fi' | /bin/sh)) - end - Process.wait(pid) - expect($?.exitstatus).to eq(42) + lib = File.expand_path("../../lib", __dir__) + system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'") + expect($?.exitstatus).to eq(1) + end + end + + describe "Bundler.unbundled_exec" do + let(:code) do + <<~RUBY + Process.fork do + exit Bundler.unbundled_exec(%(test "\$BUNDLE_FOO" = "bar")) + end + + _, status = Process.wait2 + + exit(status.exitstatus) + RUBY + end + + it "runs exec inside with_clean_env" do + lib = File.expand_path("../../lib", __dir__) + system({ "BUNDLE_FOO" => "bar" }, "ruby -I#{lib} -rbundler -e '#{code}'") + expect($?.exitstatus).to eq(1) end end end