Config loading test is now powered by real Vagrantfiles, rather than mocks

This commit is contained in:
Mitchell Hashimoto 2010-09-07 11:30:48 -07:00
parent 5f6e3acf40
commit 3cca2f1bb4
4 changed files with 54 additions and 60 deletions

View File

@ -231,7 +231,7 @@ module Vagrant
loader.queue << File.expand_path("config/default.rb", Vagrant.source_root) loader.queue << File.expand_path("config/default.rb", Vagrant.source_root)
loader.queue << File.join(box.directory, ROOTFILE_NAME) if !first_run && box loader.queue << File.join(box.directory, ROOTFILE_NAME) if !first_run && box
loader.queue << File.join(home_path, ROOTFILE_NAME) if !first_run && home_path loader.queue << File.join(home_path, ROOTFILE_NAME) if !first_run && home_path
loader.queue << File.join(root_path, ROOTFILE_NAME) if root_path loader.queue << File.join(root_path, ROOTFILE_NAME)
# If this environment is representing a sub-VM, then we push that # If this environment is representing a sub-VM, then we push that
# proc on as the last configuration. # proc on as the last configuration.

View File

@ -35,5 +35,12 @@ module VagrantTestHelpers
path ||= vagrantfile path ||= vagrantfile
Vagrant::Environment.new(:cwd => path).load! Vagrant::Environment.new(:cwd => path).load!
end end
# Creates the folder to contain a vagrant box
def vagrant_box(name)
result = boxes_path.join(name)
FileUtils.mkdir_p(result)
result
end
end end
end end

View File

@ -11,7 +11,16 @@ module VagrantTestHelpers
# Path to the "home" directory for the tests # Path to the "home" directory for the tests
def home_path def home_path
tmp_path.join("home") result = tmp_path.join("home")
FileUtils.mkdir_p(result)
result
end
# Path to the boxes directory in the home directory
def boxes_path
result = home_path.join("boxes")
FileUtils.mkdir_p(result)
result
end end
# Cleans all the test temp paths # Cleans all the test temp paths

View File

@ -212,7 +212,7 @@ class EnvironmentTest < Test::Unit::TestCase
context "loading logger" do context "loading logger" do
should "lazy load the logger only once" do should "lazy load the logger only once" do
result = Vagrant::Util::ResourceLogger.new("vagrant", mock_environment) result = Vagrant::Util::ResourceLogger.new("vagrant", vagrant_env)
Vagrant::Util::ResourceLogger.expects(:new).returns(result).once Vagrant::Util::ResourceLogger.expects(:new).returns(result).once
env = vagrant_env env = vagrant_env
assert_equal result, env.logger assert_equal result, env.logger
@ -313,10 +313,6 @@ class EnvironmentTest < Test::Unit::TestCase
end end
context "loading" do context "loading" do
setup do
@env = mock_environment
end
context "overall load method" do context "overall load method" do
should "load! should call proper sequence and return itself" do should "load! should call proper sequence and return itself" do
env = @klass.new(:cwd => vagrantfile) env = @klass.new(:cwd => vagrantfile)
@ -331,73 +327,55 @@ class EnvironmentTest < Test::Unit::TestCase
context "loading config" do context "loading config" do
setup do setup do
@root_path = "/foo" clean_paths
@home_path = "/bar" @env = @klass.new(:cwd => vagrantfile)
@env.stubs(:root_path).returns(@root_path) end
@env.stubs(:home_path).returns(@home_path)
# Temporary def create_box_vagrantfile
@env.stubs(:load_home_directory!) vagrantfile(vagrant_box("box"), 'config.package.name = "box.box"')
@env.stubs(:load_box!) end
@loader = Vagrant::Config.new(@env) def create_home_vagrantfile
Vagrant::Config.stubs(:new).returns(@loader) vagrantfile(home_path, 'config.package.name = "home.box"')
@loader.stubs(:load!) end
def create_root_vagrantfile
vagrantfile(@env.root_path, 'config.package.name = "root.box"')
end end
should "load from the project root" do should "load from the project root" do
@env.load_config! assert_equal "package.box", @env.config.package.name
assert @loader.queue.include?(File.expand_path("config/default.rb", Vagrant.source_root))
end end
should "load from the root path" do should "load from box if specified" do
@env.load_config! create_box_vagrantfile
assert @loader.queue.include?(File.join(@root_path, @klass::ROOTFILE_NAME)) vagrantfile(@env.root_path, "config.vm.box = 'box'")
assert_equal "box.box", @env.config.package.name
end end
should "not load from the root path if nil" do should "load from home path if exists" do
@env.stubs(:root_path).returns(nil) create_home_vagrantfile
@env.load_config! assert_equal "home.box", @env.config.package.name
assert !@loader.queue.include?(File.join(@root_path, @klass::ROOTFILE_NAME))
end end
should "load from the home directory" do should "load from root path" do
@env.load_config! create_home_vagrantfile
assert @loader.queue.include?(File.join(@env.home_path, @klass::ROOTFILE_NAME)) create_root_vagrantfile
assert_equal "root.box", @env.config.package.name
end end
should "not load from the home directory if the config is nil" do should "load from a sub-vm configuration if environment represents a VM" do
@env.stubs(:home_path).returns(nil) create_home_vagrantfile
@env.load_config! vagrantfile(@env.root_path, <<-vf)
assert !@loader.queue.include?(File.join(@home_path, @klass::ROOTFILE_NAME)) config.package.name = "root.box"
end config.vm.define :web do |web|
web.package.name = "web.box"
end
vf
should "load from the box directory if it is not nil" do assert_equal "root.box", @env.config.package.name
dir = "foo" assert_equal "web.box", @env.vms[:web].env.config.package.name
box = mock("box")
box.stubs(:directory).returns(dir)
@env.expects(:box).twice.returns(box)
@env.load_config!
assert @loader.queue.include?(File.join(dir, @klass::ROOTFILE_NAME))
end
should "load a sub-VM configuration if specified" do
vm_name = :foobar
proc = Proc.new {}
parent_env = mock_environment
parent_env.config.vm.define(vm_name, &proc)
@env.stubs(:parent).returns(parent_env)
@env.stubs(:vm).returns(mock("vm", :name => vm_name))
@env.load_config!
assert @loader.queue.flatten.include?(proc)
end
should "execute after loading and set result to environment config" do
result = mock("result")
@loader.stubs(:load!).returns(result)
@env.load_config!
assert_equal result, @env.config
end end
should "reload the logger after executing" do should "reload the logger after executing" do