[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 2.6.5-303-gfcfa26a

Max Martin max at puppetlabs.com
Thu Mar 17 10:49:37 UTC 2011


The following commit has been merged in the upstream branch:
commit 25926d1922a9b75bc87ed7feed30693a69cdea9a
Author: Max Martin <max at puppetlabs.com>
Date:   Tue Mar 15 15:52:37 2011 -0700

    (#6723) Fix withenv environment restoration bug
    
    Ensured that withenv properly restores the environment after it runs a block and added testing for the method.
    
    Reviewed-by: Matt Robinson and Daniel Pittman

diff --git a/lib/puppet/util/execution.rb b/lib/puppet/util/execution.rb
index dd820f8..69f4f2c 100644
--- a/lib/puppet/util/execution.rb
+++ b/lib/puppet/util/execution.rb
@@ -4,16 +4,15 @@ module Puppet::Util::Execution
   # Run some code with a specific environment.  Resets the environment back to
   # what it was at the end of the code.
   def withenv(hash)
-    oldvals = {}
+    saved = ENV.to_hash
     hash.each do |name, val|
-      name = name.to_s
-      oldvals[name] = ENV[name]
-      ENV[name] = val
+      ENV[name.to_s] = val
     end
 
     yield
   ensure
-    oldvals.each do |name, val|
+    ENV.clear
+    saved.each do |name, val|
       ENV[name] = val
     end
   end
diff --git a/spec/unit/util/execution_spec.rb b/spec/unit/util/execution_spec.rb
new file mode 100644
index 0000000..312dd3b
--- /dev/null
+++ b/spec/unit/util/execution_spec.rb
@@ -0,0 +1,49 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe Puppet::Util::Execution do
+  include Puppet::Util::Execution
+  describe "#withenv" do
+    before :each do
+      @original_path = ENV["PATH"]
+      @new_env = {:PATH => "/some/bogus/path"}
+    end
+
+    it "should change environment variables within the block then reset environment variables to their original values" do
+      withenv @new_env do
+        ENV["PATH"].should == "/some/bogus/path"
+      end
+      ENV["PATH"].should == @original_path
+    end
+
+    it "should reset environment variables to their original values even if the block fails" do
+      begin
+        withenv @new_env do
+          ENV["PATH"].should == "/some/bogus/path"
+          raise "This is a failure"
+        end
+      rescue
+      end
+      ENV["PATH"].should == @original_path
+    end
+
+    it "should reset environment variables even when they are set twice" do
+      # Setting Path & Environment parameters in Exec type can cause weirdness
+      @new_env["PATH"] = "/someother/bogus/path"
+      withenv @new_env do
+        # When assigning duplicate keys, can't guarantee order of evaluation
+        ENV["PATH"].should =~ /\/some.*\/bogus\/path/
+      end
+      ENV["PATH"].should == @original_path
+    end
+
+    it "should remove any new environment variables after the block ends" do
+      @new_env[:FOO] = "bar"
+      withenv @new_env do
+        ENV["FOO"].should == "bar"
+      end
+      ENV["FOO"].should == nil
+    end
+  end
+end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list