Environment#load_config! loads from the box directory and home directory as well

This commit is contained in:
Mitchell Hashimoto 2010-03-18 14:54:43 -07:00
parent 047c094e41
commit cb2513b1ec
2 changed files with 46 additions and 3 deletions

View File

@ -32,6 +32,11 @@ module Vagrant
File.join(root_path, config.vagrant.dotfile_name)
end
# The path to the home directory, which is usually in `~/.vagrant/~
def home_path
config ? config.vagrant.home : nil
end
#---------------------------------------------------------------
# Load Methods
#---------------------------------------------------------------
@ -74,6 +79,8 @@ module Vagrant
def load_config!
# Prepare load paths for config files
load_paths = [File.join(PROJECT_ROOT, "config", "default.rb")]
load_paths << File.join(box.directory, ROOTFILE_NAME) if box
load_paths << File.join(home_path, ROOTFILE_NAME) if home_path
load_paths << File.join(root_path, ROOTFILE_NAME) if root_path
# Clear out the old data
@ -94,8 +101,6 @@ module Vagrant
# Loads the home directory path and creates the necessary subdirectories
# within the home directory if they're not already created.
def load_home_directory!
home_path = File.expand_path(config.vagrant.home)
# Setup the array of necessary home directories
dirs = HOME_SUBDIRS.collect { |subdir| File.join(home_path, subdir) }
dirs.unshift(home_path)

View File

@ -30,6 +30,17 @@ class EnvironmentTest < Test::Unit::TestCase
assert_equal File.join(@env.root_path, @env.config.vagrant.dotfile_name), @env.dotfile_path
end
end
context "home path" do
should "return nil if config is not yet loaded" do
@env.stubs(:config).returns(nil)
assert_nil @env.home_path
end
should "return the home path if it loaded" do
assert_equal @env.config.vagrant.home, @env.home_path
end
end
end
context "loading" do
@ -113,8 +124,9 @@ class EnvironmentTest < Test::Unit::TestCase
context "loading config" do
setup do
@root_path = "/foo"
@env = Vagrant::Environment.new
@home_path = "/bar"
@env.stubs(:root_path).returns(@root_path)
@env.stubs(:home_path).returns(@home_path)
File.stubs(:exist?).returns(false)
Vagrant::Config.stubs(:execute!)
@ -142,6 +154,32 @@ class EnvironmentTest < Test::Unit::TestCase
@env.load_config!
end
should "load from the home directory" do
File.expects(:exist?).with(File.join(@env.home_path, Vagrant::Environment::ROOTFILE_NAME)).once
@env.load_config!
end
should "not load from the home directory if the config is nil" do
@env.stubs(:home_path).returns(nil)
File.expects(:exist?).twice.returns(false)
@env.load_config!
end
should "not load from the box directory if it is nil" do
@env.expects(:box).once.returns(nil)
File.expects(:exist?).twice.returns(false)
@env.load_config!
end
should "load from the box directory if it is not nil" do
dir = "foo"
box = mock("box")
box.stubs(:directory).returns(dir)
@env.expects(:box).twice.returns(box)
File.expects(:exist?).with(File.join(dir, Vagrant::Environment::ROOTFILE_NAME)).once
@env.load_config!
end
should "load the files only if exist? returns true" do
File.expects(:exist?).once.returns(true)
@env.expects(:load).once