Skip to content

Commit

Permalink
Merge pull request puppetlabs#548 from cprice-puppet/cleanup/master/r…
Browse files Browse the repository at this point in the history
…uby-185-execution-spec-failures

Fix ruby 1.8.5 failures for execution_spec
  • Loading branch information
pcarlisle committed Mar 1, 2012
2 parents bf8a77c + 85077ea commit 196c85f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions spec/unit/util/execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def call_exec_windows(command, arguments, stdin, stdout, stderr)
Puppet::Util::Execution.send(:execute_windows, command, arguments, stdin, stdout, stderr)
end

# utility method for cloning the ENV object. Because it is not ACTUALLY a Hash instance,
# we can't use the built-in #clone method, and because Ruby 1.8.5 doesn't support some of
# the newer constructors for the Hash object, this slightly hacky syntax was the best I
# could come up with. At least it's isolated to one line of code :)
def clone_env()
ENV.inject(Hash.new) {|result,entry| result[entry[0]] = entry[1] ; result }
end


describe "execution methods" do
let(:pid) { 5501 }
Expand Down Expand Up @@ -58,7 +66,7 @@ def stub_process_wait(exitstatus)
# there is a danger here that ENV will be modified by exec_posix. Normally it would only affect the ENV
# of a forked process, but here, we're stubbing Kernel.fork, so the method has the ability to override the
# "real" ENV. To guard against this, we'll capture a snapshot of ENV before each test.
@saved_env = Hash[ENV.map {|key,val| [key, val]}]
@saved_env = clone_env

# Now, we're going to effectively "mock" the magic ruby 'ENV' variable by creating a local definition of it
# inside of the module we're testing.
Expand All @@ -71,7 +79,7 @@ def stub_process_wait(exitstatus)
Puppet::Util::Execution.send(:remove_const, :ENV)

# capture the current environment and make sure it's the same as it was before the test
cur_env = Hash[ENV.map {|key,val| [key, val]}]
cur_env = clone_env

# we will get some fairly useless output if we just use the raw == operator on the hashes here, so we'll
# be a bit more explicit and laborious in the name of making the error more useful...
Expand Down Expand Up @@ -286,14 +294,13 @@ def stub_process_wait(exitstatus)
# of a forked process, but, in some of the previous tests in this file we're stubbing Kernel.fork., which could
# allow the method to override the "real" ENV. This shouldn't be a problem for these tests because they are
# not stubbing Kernel.fork, but, better safe than sorry... so, to guard against this, we'll capture a snapshot
# of ENV before each test. We have to use this somewhat kludgy code to clone the ENV var, because ENV doesn't
# provide a useful == method.
@saved_env = Hash[ENV.map {|key,val| [key, val]}]
# of ENV before each test.
@saved_env = clone_env
end

after :each do
# capture the current environment and make sure it's the same as it was before the test
cur_env = Hash[ENV.map {|key,val| [key, val]}]
cur_env = clone_env
# we will get some fairly useless output if we just use the raw == operator on the hashes here, so we'll
# be a bit more explicit and laborious in the name of making the error more useful...
@saved_env.each_pair { |key,val| cur_env[key].should == val }
Expand Down

0 comments on commit 196c85f

Please sign in to comment.