Use a temporary directory for the VAGRANT_CWD when running tests

Tests before were picking up a Vagrantfile in the Vagrant source
directory, which can cause some funny failures. This ensures that each
test run will actually establish a new temporary CWD so that a
Vagrantfile is hopefully never found.
This commit is contained in:
Mitchell Hashimoto 2012-06-23 23:56:39 -07:00
parent 6231bef3f0
commit 37c54c7b20
3 changed files with 33 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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