Refactor Vagrant::Env a bit and requiring root path.
This commit is contained in:
parent
efe98df4b0
commit
ce7f7dd168
|
@ -128,7 +128,7 @@ error
|
|||
# which action to take and calls the respective action method
|
||||
# (see {box_add} and {box_remove})
|
||||
def box(argv)
|
||||
Env.load!(:suppress_errors => true)
|
||||
Env.load!
|
||||
|
||||
sub_commands = ["list", "add", "remove"]
|
||||
|
||||
|
|
|
@ -19,8 +19,8 @@ module Vagrant
|
|||
def tmp_path; File.join(home_path, "tmp"); end
|
||||
def boxes_path; File.join(home_path, "boxes"); end
|
||||
|
||||
def load!(opts={})
|
||||
load_root_path!(Pathname.new(Dir.pwd), opts)
|
||||
def load!
|
||||
load_root_path!
|
||||
load_config!
|
||||
load_home_directory!
|
||||
load_box!
|
||||
|
@ -60,6 +60,8 @@ module Vagrant
|
|||
end
|
||||
|
||||
def load_box!
|
||||
return unless root_path
|
||||
|
||||
@@box = Box.find(Vagrant.config.vm.box) if Vagrant.config.vm.box
|
||||
|
||||
if @@box
|
||||
|
@ -84,17 +86,10 @@ module Vagrant
|
|||
end
|
||||
end
|
||||
|
||||
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
|
||||
to manage. Please create a #{ROOTFILE_NAME} and place it in your project
|
||||
root.
|
||||
msg
|
||||
return
|
||||
end
|
||||
def load_root_path!(path=nil)
|
||||
path ||= Pathname.new(Dir.pwd)
|
||||
|
||||
return false if path.to_s == '/'
|
||||
|
||||
file = "#{path}/#{ROOTFILE_NAME}"
|
||||
if File.exist?(file)
|
||||
|
@ -102,10 +97,23 @@ msg
|
|||
return true
|
||||
end
|
||||
|
||||
load_root_path!(path.parent, opts)
|
||||
load_root_path!(path.parent)
|
||||
end
|
||||
|
||||
def require_root_path
|
||||
if !root_path
|
||||
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
|
||||
to manage. Please create a #{ROOTFILE_NAME} and place it in your project
|
||||
root.
|
||||
msg
|
||||
end
|
||||
end
|
||||
|
||||
def require_box
|
||||
require_root_path
|
||||
|
||||
if !box
|
||||
if !Vagrant.config.vm.box
|
||||
error_and_exit(<<-msg)
|
||||
|
@ -125,6 +133,8 @@ msg
|
|||
end
|
||||
|
||||
def require_persisted_vm
|
||||
require_root_path
|
||||
|
||||
if !persisted_vm
|
||||
error_and_exit(<<-error)
|
||||
The task you're trying to run requires that the vagrant environment
|
||||
|
|
|
@ -7,6 +7,7 @@ class CommandsTest < Test::Unit::TestCase
|
|||
@persisted_vm = mock("persisted_vm")
|
||||
@persisted_vm.stubs(:execute!)
|
||||
Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm)
|
||||
Vagrant::Env.stubs(:require_persisted_vm)
|
||||
end
|
||||
|
||||
context "init" do
|
||||
|
@ -194,7 +195,7 @@ class CommandsTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "load the environment" do
|
||||
Vagrant::Env.expects(:load!).with(:suppress_errors => true).once
|
||||
Vagrant::Env.expects(:load!).once
|
||||
Vagrant::Commands.box(["add"])
|
||||
end
|
||||
|
||||
|
|
|
@ -15,6 +15,16 @@ class EnvTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
context "requiring a VM" do
|
||||
setup do
|
||||
Vagrant::Env.stubs(:require_root_path)
|
||||
Vagrant::Env.stubs(:error_and_exit)
|
||||
end
|
||||
|
||||
should "require root path" do
|
||||
Vagrant::Env.expects(:require_root_path).once
|
||||
Vagrant::Env.require_persisted_vm
|
||||
end
|
||||
|
||||
should "error and exit if no persisted VM was found" do
|
||||
assert_nil Vagrant::Env.persisted_vm
|
||||
Vagrant::Env.expects(:error_and_exit).once
|
||||
|
@ -121,7 +131,7 @@ class EnvTest < Test::Unit::TestCase
|
|||
test "load! should load the config and set the persisted_uid" do
|
||||
Vagrant::Env.expects(:load_config!).once
|
||||
Vagrant::Env.expects(:load_vm!).once
|
||||
Vagrant::Env.expects(:load_root_path!).with(Pathname.new(Dir.pwd), {}).once
|
||||
Vagrant::Env.expects(:load_root_path!).once
|
||||
Vagrant::Env.expects(:load_home_directory!).once
|
||||
Vagrant::Env.expects(:load_box!).once
|
||||
Vagrant::Env.load!
|
||||
|
@ -168,9 +178,21 @@ class EnvTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
context "loading the root path" do
|
||||
test "should walk the parent directories looking for rootfile" do
|
||||
Vagrant::Env.expects(:error_and_exit).once
|
||||
should "default the path to the pwd if nil" do
|
||||
@path = mock("path")
|
||||
@path.stubs(:to_s).returns("/")
|
||||
Pathname.expects(:new).with(Dir.pwd).returns(@path)
|
||||
Vagrant::Env.load_root_path!(nil)
|
||||
end
|
||||
|
||||
should "not default the path to pwd if its not nil" do
|
||||
@path = mock("path")
|
||||
@path.stubs(:to_s).returns("/")
|
||||
Pathname.expects(:new).never
|
||||
Vagrant::Env.load_root_path!(@path)
|
||||
end
|
||||
|
||||
should "should walk the parent directories looking for rootfile" do
|
||||
paths = [
|
||||
Pathname.new("/foo/bar/baz"),
|
||||
Pathname.new("/foo/bar"),
|
||||
|
@ -182,32 +204,15 @@ class EnvTest < Test::Unit::TestCase
|
|||
File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(false).in_sequence(search_seq)
|
||||
end
|
||||
|
||||
assert_nil Vagrant::Env.load_root_path!(paths.first)
|
||||
assert !Vagrant::Env.load_root_path!(paths.first)
|
||||
end
|
||||
|
||||
test "should print out an error and exit if not found" do
|
||||
should "should return false if not found" do
|
||||
path = Pathname.new("/")
|
||||
|
||||
Vagrant::Env.expects(:error_and_exit).once
|
||||
Vagrant::Env.load_root_path!(path)
|
||||
assert !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
|
||||
should "should set the path for the rootfile" do
|
||||
path = "/foo"
|
||||
File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(true)
|
||||
|
||||
|
@ -239,6 +244,13 @@ class EnvTest < Test::Unit::TestCase
|
|||
@box = mock("box")
|
||||
|
||||
Vagrant::Env.stubs(:load_config!)
|
||||
Vagrant::Env.stubs(:root_path).returns("foo")
|
||||
end
|
||||
|
||||
should "do nothing if the root path is nil" do
|
||||
Vagrant::Box.expects(:find).never
|
||||
Vagrant::Env.stubs(:root_path).returns(nil)
|
||||
Vagrant::Env.load_vm!
|
||||
end
|
||||
|
||||
should "not load the box if its not set" do
|
||||
|
@ -264,6 +276,16 @@ class EnvTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
context "requiring boxes" do
|
||||
setup do
|
||||
Vagrant::Env.stubs(:require_root_path)
|
||||
Vagrant::Env.stubs(:error_and_exit)
|
||||
end
|
||||
|
||||
should "require root path" do
|
||||
Vagrant::Env.expects(:require_root_path).once
|
||||
Vagrant::Env.require_box
|
||||
end
|
||||
|
||||
should "error and exit if no box is found" do
|
||||
mock_config do |config|
|
||||
config.vm.box = nil
|
||||
|
@ -290,4 +312,18 @@ class EnvTest < Test::Unit::TestCase
|
|||
Vagrant::Env.require_box
|
||||
end
|
||||
end
|
||||
|
||||
context "requiring root_path" do
|
||||
should "error and exit if no root_path is set" do
|
||||
Vagrant::Env.expects(:root_path).returns(nil)
|
||||
Vagrant::Env.expects(:error_and_exit).once
|
||||
Vagrant::Env.require_root_path
|
||||
end
|
||||
|
||||
should "not error and exit if root_path is set" do
|
||||
Vagrant::Env.expects(:root_path).returns("foo")
|
||||
Vagrant::Env.expects(:error_and_exit).never
|
||||
Vagrant::Env.require_root_path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue