From cb2513b1ec0f360435f4f4ae1eb402439d3c0cfc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 18 Mar 2010 14:54:43 -0700 Subject: [PATCH] Environment#load_config! loads from the box directory and home directory as well --- lib/vagrant/environment.rb | 9 +++++-- test/vagrant/environment_test.rb | 40 +++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 36845f2ad..1cba0613d 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -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) diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 6418f831c..fe770038f 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -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