diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb new file mode 100644 index 000000000..684f4d2c1 --- /dev/null +++ b/lib/vagrant/environment.rb @@ -0,0 +1,37 @@ +module Vagrant + # Represents a single Vagrant environment. This class is responsible + # for loading all of the Vagrantfile's for the given environment and + # storing references to the various instances. + class Environment + ROOTFILE_NAME = "Vagrantfile" + + include Util + + attr_reader :root_path + attr_reader :config + + # 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 + # to load the Vagrantfile into that context. + def load_config! + # Prepare load paths for config files + load_paths = [File.join(PROJECT_ROOT, "config", "default.rb")] + load_paths << File.join(root_path, ROOTFILE_NAME) if root_path + + # Clear out the old data + Config.reset! + + # Load each of the config files in order + load_paths.each do |path| + if File.exist?(path) + logger.info "Loading config from #{path}..." + load path + end + end + + # Execute the configuration stack and store the result + @config = Config.execute! + end + end +end \ No newline at end of file diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb new file mode 100644 index 000000000..5b8b94fc4 --- /dev/null +++ b/test/vagrant/environment_test.rb @@ -0,0 +1,60 @@ +require File.join(File.dirname(__FILE__), '..', 'test_helper') + +class EnvTest < Test::Unit::TestCase + setup do + mock_config + end + + context "loading config" do + setup do + @root_path = "/foo" + @env = Vagrant::Environment.new + @env.stubs(:root_path).returns(@root_path) + + File.stubs(:exist?).returns(false) + Vagrant::Config.stubs(:execute!) + Vagrant::Config.stubs(:reset!) + end + + should "reset the configuration object" do + Vagrant::Config.expects(:reset!).once + @env.load_config! + end + + should "load from the project root" do + File.expects(:exist?).with(File.join(PROJECT_ROOT, "config", "default.rb")).once + @env.load_config! + end + + should "load from the root path" do + File.expects(:exist?).with(File.join(@root_path, Vagrant::Environment::ROOTFILE_NAME)).once + @env.load_config! + end + + should "not load from the root path if nil" do + @env.stubs(:root_path).returns(nil) + File.expects(:exist?).with(File.join(@root_path, Vagrant::Environment::ROOTFILE_NAME)).never + @env.load_config! + end + + should "load the files only if exist? returns true" do + File.expects(:exist?).once.returns(true) + @env.expects(:load).once + @env.load_config! + end + + should "not load the files if exist? returns false" do + @env.expects(:load).never + @env.load_config! + end + + should "execute after loading and set result to environment config" do + result = mock("result") + File.expects(:exist?).once.returns(true) + @env.expects(:load).once + Vagrant::Config.expects(:execute!).once.returns(result) + @env.load_config! + assert_equal result, @env.config + end + end +end