summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorMax Martin <max@puppetlabs.com>2011-03-15 15:52:37 -0700
committerMax Martin <max@puppetlabs.com>2011-03-15 15:58:32 -0700
commit25926d1922a9b75bc87ed7feed30693a69cdea9a (patch)
tree2fa66f45306be3dfa1f37564a686e0f49d23b7be /spec
parent9016662cc108dbcced5ad9c9a33f4ecd61cac178 (diff)
downloadpuppet-25926d1922a9b75bc87ed7feed30693a69cdea9a.tar.gz
puppet-25926d1922a9b75bc87ed7feed30693a69cdea9a.tar.xz
puppet-25926d1922a9b75bc87ed7feed30693a69cdea9a.zip
(#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
Diffstat (limited to 'spec')
-rw-r--r--spec/unit/util/execution_spec.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/spec/unit/util/execution_spec.rb b/spec/unit/util/execution_spec.rb
new file mode 100644
index 000000000..312dd3b8e
--- /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