module ShellTest::EnvMethods

Public Instance Methods

set_env(env={}, replace=false) click to toggle source

Sets the specified ENV variables and returns the current env. If replace is true, current ENV variables are replaced; otherwise the new env variables are simply added to the existing set.

   # File lib/shell_test/env_methods.rb
 8 def set_env(env={}, replace=false)
 9   current_env = {}
10   ENV.each_pair do |key, value|
11     current_env[key] = value
12   end
13 
14   ENV.clear if replace
15 
16   env.each_pair do |key, value|
17     if value.nil?
18       ENV.delete(key)
19     else
20       ENV[key] = value
21     end
22   end if env
23 
24   current_env
25 end
with_env(env={}, replace=false) { || ... } click to toggle source

Sets the specified ENV variables for the duration of the block. If replace is true, current ENV variables are replaced; otherwise the new env variables are simply added to the existing set.

Returns the block return.

   # File lib/shell_test/env_methods.rb
32 def with_env(env={}, replace=false)
33   current_env = nil
34   begin
35     current_env = set_env(env, replace)
36     yield
37   ensure
38     if current_env
39       set_env(current_env, true)
40     end
41   end
42 end