/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
- module InEnvironment
- private
- # Create an environment for a test. At the completion of the yielded
- # block, the environment is restored to its original conditions.
- def in_environment(settings=nil)
- settings ||= {}
- full_settings = {"RAKEOPT" => nil}.merge(settings)
- original_settings = set_env(full_settings)
- yield
- ensure
- set_env(original_settings) rescue nil
- end
- # Set the environment according to the settings hash.
- def set_env(settings) # :nodoc:
- result = {}
- settings.each do |k, v|
- result[k] = ENV[k]
- if k == 'PWD'
- result[k] = Dir.pwd
- Dir.chdir(v)
- elsif v.nil?
- ENV.delete(k)
- else
- ENV[k] = v
- end
- end
- result
- end
- end