Environment#load! implemented to set up an instance of environment

This commit is contained in:
Mitchell Hashimoto 2010-03-18 14:43:17 -07:00
parent 8c314e3e17
commit 047c094e41
2 changed files with 255 additions and 125 deletions

View File

@ -8,6 +8,7 @@ module Vagrant
include Util
attr_accessor :cwd
attr_reader :root_path
attr_reader :config
attr_reader :box
@ -17,6 +18,14 @@ module Vagrant
# Path Helpers
#---------------------------------------------------------------
# Specifies the "current working directory" for this environment.
# This is vital in determining the root path and therefore the
# dotfile, rootpath vagrantfile, etc. This defaults to the
# actual cwd (`Dir.pwd`).
def cwd
@cwd || Dir.pwd
end
# The path to the `dotfile`, which contains the persisted UUID of
# the VM if it exists.
def dotfile_path
@ -27,6 +36,37 @@ module Vagrant
# Load Methods
#---------------------------------------------------------------
# Loads this entire environment, setting up the instance variables
# such as `vm`, `config`, etc. on this environment. The order this
# method calls its other methods is very particular.
def load!
load_root_path!
load_config!
load_home_directory!
load_box!
load_config!
load_vm!
end
# Loads the root path of this environment, given the starting
# directory (the "cwd" of this environment for lack of better words).
# This method allows an environment in `/foo` to be detected from
# `/foo/bar` (similar to how git works in subdirectories)
def load_root_path!(path=nil)
path = Pathname.new(File.expand_path(path || cwd))
# Stop if we're at the root.
return false if path.root?
file = "#{path}/#{ROOTFILE_NAME}"
if File.exist?(file)
@root_path = path.to_s
return true
end
load_root_path!(path.parent)
end
# Loads this environment's configuration and stores it in the {config}
# variable. The configuration loaded by this method is specified to
# this environment, meaning that it will use the given root directory

View File

@ -10,6 +10,17 @@ class EnvironmentTest < Test::Unit::TestCase
@env = mock_environment
end
context "cwd" do
should "default to Dir.pwd" do
assert_equal Dir.pwd, @env.cwd
end
should "return cwd if set" do
@env.cwd = "foo"
assert_equal "foo", @env.cwd
end
end
context "dotfile path" do
setup do
@env.stubs(:root_path).returns("foo")
@ -21,6 +32,84 @@ class EnvironmentTest < Test::Unit::TestCase
end
end
context "loading" do
setup do
@env = mock_environment
end
context "overall load method" do
should "load! should load the config and set the persisted_uid" do
call_seq = sequence("call_sequence")
@env.expects(:load_root_path!).once.in_sequence(call_seq)
@env.expects(:load_config!).once.in_sequence(call_seq)
@env.expects(:load_home_directory!).once.in_sequence(call_seq)
@env.expects(:load_box!).once.in_sequence(call_seq)
@env.expects(:load_config!).once.in_sequence(call_seq)
@env.expects(:load_vm!).once.in_sequence(call_seq)
@env.load!
end
end
context "loading the root path" do
setup do
@env.cwd = "/foo"
end
should "default the path to the cwd instance var if nil" do
@path = mock("path")
@path.stubs(:root?).returns(true)
Pathname.expects(:new).with(@env.cwd).returns(@path)
@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("/")
File.expects(:expand_path).with(@path).returns("/")
Pathname.expects(:new).with("/").returns(@path)
@path.stubs(:root?).returns(true)
@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"),
Pathname.new("/foo")
]
search_seq = sequence("search_seq")
paths.each do |path|
File.expects(:exist?).with("#{path}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(false).in_sequence(search_seq)
end
assert !@env.load_root_path!(paths.first)
end
should "return false if not found" do
path = Pathname.new("/")
assert !@env.load_root_path!(path)
end
should "return false if not found on windows-style root" do
# TODO: Is there _any_ way to test this on unix machines? The
# expand path doesn't work [properly for the test] on unix machines.
if RUBY_PLATFORM.downcase.include?("mswin")
# Note the escaped back slash
path = Pathname.new("C:\\")
assert !@env.load_root_path!(path)
end
end
should "should set the path for the rootfile" do
path = "/foo"
File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(true)
assert Vagrant::Env.load_root_path!(Pathname.new(path))
assert_equal path, Vagrant::Env.root_path
end
end
context "loading config" do
setup do
@root_path = "/foo"
@ -170,3 +259,4 @@ class EnvironmentTest < Test::Unit::TestCase
end
end
end
end