summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Hull <joshbuddy@gmail.com>2010-03-26 16:32:51 -0400
committerAndre Arko <andre@arko.net>2010-03-30 10:13:57 -0700
commitea9d8e1ab720d2974e507933c8d331ac81a30652 (patch)
tree92eb83db19254e945ba6a217e3403eca62b31114
parenta97a0c335bb4ce6ea546f6c18df4120f5d383cb8 (diff)
downloadbundler-ea9d8e1ab720d2974e507933c8d331ac81a30652.tar.gz
Add Bundler.with_clean_env for blocks of ruby without a bundled env
e.g. if you are in a bundled Rakefile but need to shell out to something on your system that uses ruby like Homebrew Fixes #225
-rw-r--r--lib/bundler.rb10
-rw-r--r--spec/runtime/with_clean_env_spec.rb15
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb
index 9d6ea3b4bd..38f28b9a63 100644
--- a/lib/bundler.rb
+++ b/lib/bundler.rb
@@ -3,8 +3,10 @@ require 'pathname'
require 'yaml'
require 'bundler/rubygems_ext'
+
module Bundler
VERSION = "0.9.13"
+ ORIGINAL_ENV = ENV.to_hash
autoload :Definition, 'bundler/definition'
autoload :Dependency, 'bundler/dependency'
@@ -109,6 +111,14 @@ module Bundler
@settings ||= Settings.new(root)
end
+ def with_clean_env
+ bundled_env = ENV.to_hash
+ ENV.replace(ORIGINAL_ENV)
+ yield
+ ensure
+ ENV.replace(bundled_env.to_hash)
+ end
+
private
def default_gemfile
diff --git a/spec/runtime/with_clean_env_spec.rb b/spec/runtime/with_clean_env_spec.rb
new file mode 100644
index 0000000000..3894e414a9
--- /dev/null
+++ b/spec/runtime/with_clean_env_spec.rb
@@ -0,0 +1,15 @@
+require File.expand_path('../../spec_helper', __FILE__)
+
+describe "Bundler.with_clean_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
+ end
+
+ ENV['GEM_PATH'].should == gem_path
+ end
+
+end \ No newline at end of file