diff options
author | Andre Arko <website+github.com@arko.net> | 2011-04-29 12:27:26 -0700 |
---|---|---|
committer | Andre Arko <website+github.com@arko.net> | 2011-04-29 12:27:26 -0700 |
commit | 4faa8e4a24d4665d1a4eabb4c64e00c90b2cb827 (patch) | |
tree | a87fb05827bc14d1aff5ae4280fd6e792c422391 | |
parent | ca6c21ed1fcc1d766770fd0fe6e4f5bc1815d829 (diff) | |
parent | 8a2b8b49da876f6a42ae14ecb82d73b8b3ad345c (diff) | |
download | bundler-4faa8e4a24d4665d1a4eabb4c64e00c90b2cb827.tar.gz |
Merged pull request #1057 from wuputah/clean-env.
with_clean_env => with_original_env and other goodies [closes #900]
-rw-r--r-- | lib/bundler.rb | 17 | ||||
-rw-r--r-- | spec/runtime/with_clean_env_spec.rb | 67 |
2 files changed, 76 insertions, 8 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb index d8a452e325..5d1a5a27a6 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -185,7 +185,7 @@ module Bundler @settings ||= Settings.new(app_config_path) end - def with_clean_env + def with_original_env bundled_env = ENV.to_hash ENV.replace(ORIGINAL_ENV) yield @@ -193,6 +193,21 @@ module Bundler ENV.replace(bundled_env.to_hash) end + def with_clean_env + with_original_env do + ENV.delete_if { |k,_| k[0,7] == 'BUNDLE_' } + yield + end + end + + def clean_system(*args) + with_clean_env { Kernel.system(*args) } + end + + def clean_exec(*args) + with_clean_env { Kernel.exec(*args) } + end + def default_gemfile SharedHelpers.default_gemfile end diff --git a/spec/runtime/with_clean_env_spec.rb b/spec/runtime/with_clean_env_spec.rb index 51ecc29e53..d7f5cfbb8e 100644 --- a/spec/runtime/with_clean_env_spec.rb +++ b/spec/runtime/with_clean_env_spec.rb @@ -1,15 +1,68 @@ require "spec_helper" -describe "Bundler.with_clean_env" do +describe "Bundler.with_env helpers" do - it "should reset and restore the environment" do - gem_path = ENV['GEM_PATH'] + shared_examples_for "Bundler.with_*_env" do + it "should reset and restore the environment" do + gem_path = ENV['GEM_PATH'] - Bundler.with_clean_env do - `echo $GEM_PATH`.strip.should_not == gem_path + Bundler.with_clean_env do + `echo $GEM_PATH`.strip.should_not == gem_path + end + + ENV['GEM_PATH'].should == gem_path end + end + + around do |example| + bundle_path = Bundler::ORIGINAL_ENV['BUNDLE_PATH'] + Bundler::ORIGINAL_ENV['BUNDLE_PATH'] = "./Gemfile" + example.run + Bundler::ORIGINAL_ENV['BUNDLE_PATH'] = bundle_path + end + + describe "Bundler.with_clean_env" do + + it_should_behave_like "Bundler.with_*_env" - ENV['GEM_PATH'].should == gem_path + it "should not pass any bundler environment variables" do + Bundler.with_clean_env do + `echo $BUNDLE_PATH`.strip.should_not == './Gemfile' + end + end + + it "should not change ORIGINAL_ENV" do + Bundler::ORIGINAL_ENV['BUNDLE_PATH'].should == './Gemfile' + end + + end + + describe "Bundler.with_original_env" do + + it_should_behave_like "Bundler.with_*_env" + + it "should pass bundler environment variables set before Bundler was run" do + Bundler.with_original_env do + `echo $BUNDLE_PATH`.strip.should == './Gemfile' + end + 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}) + $?.exitstatus.should == 42 + end + end + + describe "Bundler.clean_exec" do + 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) + $?.exitstatus.should == 42 + end end -end
\ No newline at end of file +end |