Refactor Vagrant::Env a bit and requiring root path.

This commit is contained in:
Mitchell Hashimoto 2010-03-08 00:34:15 -08:00
parent efe98df4b0
commit ce7f7dd168
4 changed files with 87 additions and 40 deletions

View File

@ -128,7 +128,7 @@ error
# which action to take and calls the respective action method # which action to take and calls the respective action method
# (see {box_add} and {box_remove}) # (see {box_add} and {box_remove})
def box(argv) def box(argv)
Env.load!(:suppress_errors => true) Env.load!
sub_commands = ["list", "add", "remove"] sub_commands = ["list", "add", "remove"]

View File

@ -19,8 +19,8 @@ module Vagrant
def tmp_path; File.join(home_path, "tmp"); end def tmp_path; File.join(home_path, "tmp"); end
def boxes_path; File.join(home_path, "boxes"); end def boxes_path; File.join(home_path, "boxes"); end
def load!(opts={}) def load!
load_root_path!(Pathname.new(Dir.pwd), opts) load_root_path!
load_config! load_config!
load_home_directory! load_home_directory!
load_box! load_box!
@ -60,6 +60,8 @@ module Vagrant
end end
def load_box! def load_box!
return unless root_path
@@box = Box.find(Vagrant.config.vm.box) if Vagrant.config.vm.box @@box = Box.find(Vagrant.config.vm.box) if Vagrant.config.vm.box
if @@box if @@box
@ -84,17 +86,10 @@ module Vagrant
end end
end end
def load_root_path!(path=Pathname.new(Dir.pwd), opts={}) def load_root_path!(path=nil)
if path.to_s == '/' path ||= Pathname.new(Dir.pwd)
return false if opts[:suppress_errors]
error_and_exit(<<-msg) return false if path.to_s == '/'
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
file = "#{path}/#{ROOTFILE_NAME}" file = "#{path}/#{ROOTFILE_NAME}"
if File.exist?(file) if File.exist?(file)
@ -102,10 +97,23 @@ msg
return true return true
end 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 end
def require_box def require_box
require_root_path
if !box if !box
if !Vagrant.config.vm.box if !Vagrant.config.vm.box
error_and_exit(<<-msg) error_and_exit(<<-msg)
@ -125,6 +133,8 @@ msg
end end
def require_persisted_vm def require_persisted_vm
require_root_path
if !persisted_vm if !persisted_vm
error_and_exit(<<-error) error_and_exit(<<-error)
The task you're trying to run requires that the vagrant environment The task you're trying to run requires that the vagrant environment

View File

@ -7,6 +7,7 @@ class CommandsTest < Test::Unit::TestCase
@persisted_vm = mock("persisted_vm") @persisted_vm = mock("persisted_vm")
@persisted_vm.stubs(:execute!) @persisted_vm.stubs(:execute!)
Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm) Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm)
Vagrant::Env.stubs(:require_persisted_vm)
end end
context "init" do context "init" do
@ -194,7 +195,7 @@ class CommandsTest < Test::Unit::TestCase
end end
should "load the environment" do should "load the environment" do
Vagrant::Env.expects(:load!).with(:suppress_errors => true).once Vagrant::Env.expects(:load!).once
Vagrant::Commands.box(["add"]) Vagrant::Commands.box(["add"])
end end

View File

@ -15,6 +15,16 @@ class EnvTest < Test::Unit::TestCase
end end
context "requiring a VM" do 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 should "error and exit if no persisted VM was found" do
assert_nil Vagrant::Env.persisted_vm assert_nil Vagrant::Env.persisted_vm
Vagrant::Env.expects(:error_and_exit).once 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 test "load! should load the config and set the persisted_uid" do
Vagrant::Env.expects(:load_config!).once Vagrant::Env.expects(:load_config!).once
Vagrant::Env.expects(:load_vm!).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_home_directory!).once
Vagrant::Env.expects(:load_box!).once Vagrant::Env.expects(:load_box!).once
Vagrant::Env.load! Vagrant::Env.load!
@ -168,9 +178,21 @@ class EnvTest < Test::Unit::TestCase
end end
context "loading the root path" do context "loading the root path" do
test "should walk the parent directories looking for rootfile" do should "default the path to the pwd if nil" do
Vagrant::Env.expects(:error_and_exit).once @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 = [ paths = [
Pathname.new("/foo/bar/baz"), Pathname.new("/foo/bar/baz"),
Pathname.new("/foo/bar"), 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) File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(false).in_sequence(search_seq)
end end
assert_nil Vagrant::Env.load_root_path!(paths.first) assert !Vagrant::Env.load_root_path!(paths.first)
end end
test "should print out an error and exit if not found" do should "should return false if not found" do
path = Pathname.new("/") path = Pathname.new("/")
assert !Vagrant::Env.load_root_path!(path)
Vagrant::Env.expects(:error_and_exit).once
Vagrant::Env.load_root_path!(path)
end end
should "return false if suppress errors is set and no root path is found" do should "should set the path for the rootfile" 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" path = "/foo"
File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(true) File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(true)
@ -239,6 +244,13 @@ class EnvTest < Test::Unit::TestCase
@box = mock("box") @box = mock("box")
Vagrant::Env.stubs(:load_config!) 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 end
should "not load the box if its not set" do should "not load the box if its not set" do
@ -264,6 +276,16 @@ class EnvTest < Test::Unit::TestCase
end end
context "requiring boxes" do 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 should "error and exit if no box is found" do
mock_config do |config| mock_config do |config|
config.vm.box = nil config.vm.box = nil
@ -290,4 +312,18 @@ class EnvTest < Test::Unit::TestCase
Vagrant::Env.require_box Vagrant::Env.require_box
end end
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 end