Module: ShellTest::EnvMethods

Included in:
ShellMethods
Defined in:
lib/shell_test/env_methods.rb

Class Method Summary collapse

Class Method Details

.set_env(env = {}, replace = false) ⇒ Object

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.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/shell_test/env_methods.rb', line 8

def set_env(env={}, replace=false)
  current_env = {}
  ENV.each_pair do |key, value|
    current_env[key] = value
  end

  ENV.clear if replace

  env.each_pair do |key, value|
    if value.nil?
      ENV.delete(key)
    else
      ENV[key] = value
    end
  end if env

  current_env
end

.with_env(env = {}, replace = false) ⇒ Object

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.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shell_test/env_methods.rb', line 32

def with_env(env={}, replace=false)
  current_env = nil
  begin
    current_env = set_env(env, replace)
    yield
  ensure
    if current_env
      set_env(current_env, true)
    end
  end
end