PageRenderTime 212ms CodeModel.GetById 191ms app.highlight 6ms RepoModel.GetById 14ms app.codeStats 0ms

/tools/Ruby/lib/ruby/gems/1.8/gems/rake-0.9.2/test/in_environment.rb

http://github.com/agross/netopenspace
Ruby | 35 lines | 26 code | 6 blank | 3 comment | 2 complexity | da18ebd84a48c1aabf03864b7708d35e MD5 | raw file
 1module InEnvironment
 2  private
 3
 4  # Create an environment for a test. At the completion of the yielded
 5  # block, the environment is restored to its original conditions.
 6  def in_environment(settings=nil)
 7    settings ||= {}
 8    full_settings = {"RAKEOPT" => nil}.merge(settings)
 9    original_settings = set_env(full_settings)
10    yield
11  ensure
12    set_env(original_settings) rescue nil
13  end
14
15  # Set the environment according to the settings hash.
16  def set_env(settings) # :nodoc:
17    result = {}
18
19    settings.each do |k, v|
20      result[k] = ENV[k]
21
22      if k == 'PWD'
23        result[k] = Dir.pwd
24        Dir.chdir(v)
25      elsif v.nil?
26        ENV.delete(k)
27      else
28        ENV[k] = v
29      end
30    end
31
32    result
33  end
34
35end