Beginnings of Environment class. Currently loads config partially.

This commit is contained in:
Mitchell Hashimoto 2010-03-18 12:38:01 -07:00
parent e84b17e215
commit c7f32c8be8
2 changed files with 97 additions and 0 deletions

View File

@ -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

View File

@ -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