/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

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