load_root_path! can now suppress errors if option is set

This commit is contained in:
Mitchell Hashimoto 2010-02-11 00:39:11 -08:00
parent 09bd40c207
commit fef985009f
2 changed files with 22 additions and 10 deletions

View File

@ -48,8 +48,9 @@ module Vagrant
end
end
def load_root_path!(path=Pathname.new(Dir.pwd))
def load_root_path!(path=Pathname.new(Dir.pwd), opts={})
if path.to_s == '/'
return false if opts[:suppress_errors]
error_and_exit(<<-msg)
A `#{ROOTFILE_NAME}` was not found! This file is required for vagrant to run
since it describes the expected environment that vagrant is supposed
@ -62,10 +63,10 @@ msg
file = "#{path}/#{ROOTFILE_NAME}"
if File.exist?(file)
@@root_path = path.to_s
return
return true
end
load_root_path!(path.parent)
load_root_path!(path.parent, opts)
end
def require_persisted_vm

View File

@ -1,11 +1,6 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class EnvTest < Test::Unit::TestCase
def dot_file_expectation
File.expects(:exists?).at_least_once.returns(true)
File.expects(:open).with(dotfile, 'r').returns(['foo'])
end
def mock_persisted_vm(returnvalue="foovm")
filemock = mock("filemock")
filemock.expects(:read).returns("foo")
@ -15,7 +10,6 @@ class EnvTest < Test::Unit::TestCase
end
setup do
Vagrant::Env.stubs(:error_and_exit)
mock_config
end
@ -114,6 +108,8 @@ class EnvTest < Test::Unit::TestCase
context "loading the root path" do
test "should walk the parent directories looking for rootfile" do
Vagrant::Env.expects(:error_and_exit).once
paths = [
Pathname.new("/foo/bar/baz"),
Pathname.new("/foo/bar"),
@ -135,11 +131,26 @@ class EnvTest < Test::Unit::TestCase
Vagrant::Env.load_root_path!(path)
end
should "return false if suppress errors is set and no root path is found" do
path = Pathname.new("/")
Vagrant::Env.expects(:error_and_exit).never
assert !Vagrant::Env.load_root_path!(path, :suppress_errors => true)
end
should "pipe suppress errors flag through recursion" do
path = Pathname.new("/foo/bar/baz")
File.expects(:exist?).times(3).returns(false)
Vagrant::Env.expects(:error_and_exit).never
assert !Vagrant::Env.load_root_path!(path, :suppress_errors => true)
end
test "should set the path for the rootfile" do
path = "/foo"
File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(true)
Vagrant::Env.load_root_path!(Pathname.new(path))
assert Vagrant::Env.load_root_path!(Pathname.new(path))
assert_equal path, Vagrant::Env.root_path
end
end