diff --git a/test/unit/base.rb b/test/unit/base.rb index a6d85d310..da7334bd0 100644 --- a/test/unit/base.rb +++ b/test/unit/base.rb @@ -9,6 +9,7 @@ require "vagrant" $:.unshift File.expand_path("../../", __FILE__) # Load in helpers +require "support/tempdir" require "unit/support/shared/base_context" # Do not buffer output @@ -19,3 +20,7 @@ $stderr.sync = true RSpec.configure do |c| c.expect_with :rspec, :stdlib end + +# Configure VAGRANT_CWD so that the tests never find an actual +# Vagrantfile anywhere, or at least this minimizes those chances. +ENV["VAGRANT_CWD"] = Tempdir.new.path diff --git a/test/unit/support/shared/base_context.rb b/test/unit/support/shared/base_context.rb index 78d9fb6df..e6452a619 100644 --- a/test/unit/support/shared/base_context.rb +++ b/test/unit/support/shared/base_context.rb @@ -27,4 +27,23 @@ shared_context "unit" do return Pathname.new(f.path) end + + # This helper provides temporary environmental variable changes. + def with_temp_env(environment) + # Build up the new environment, preserving the old values so we + # can replace them back in later. + old_env = {} + environment.each do |key, value| + old_env[key] = ENV[key] + ENV[key] = value + end + + # Call the block, returning its return value + return yield + ensure + # Reset the environment no matter what + old_env.each do |key, value| + ENV[key] = value + end + end end diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index bae13da96..f7a231e6f 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -13,7 +13,9 @@ describe Vagrant::Environment do describe "current working directory" do it "is the cwd by default" do - described_class.new.cwd.should == Pathname.new(Dir.pwd) + with_temp_env("VAGRANT_CWD" => nil) do + described_class.new.cwd.should == Pathname.new(Dir.pwd) + end end it "is set to the cwd given" do @@ -23,7 +25,12 @@ describe Vagrant::Environment do end it "is set to the environmental variable VAGRANT_CWD" do - pending "A good temporary ENV thing" + directory = File.dirname(__FILE__) + instance = with_temp_env("VAGRANT_CWD" => directory) do + described_class.new + end + + instance.cwd.should == Pathname.new(directory) end it "raises an exception if the CWD doesn't exist" do