Separate out the versions the config loader knows as init params.

This means that the Config::Loader now only knows how to load
configuration for versions used to initialize the class. This lets
things like the tests be completely isolated from what the actual
configuration is for Vagrant. This will be immensely useful to verify
that the loader functionality works for non-trivial bits (like
upgrading) without depending on Vagrant's upgrading functionality.
This commit is contained in:
Mitchell Hashimoto 2012-06-23 11:33:53 -07:00
parent a677c15e86
commit b3db82e516
3 changed files with 54 additions and 14 deletions

View File

@ -16,11 +16,21 @@ module Vagrant
# configuration is loaded. For examples, see the class documentation.
attr_accessor :load_order
def initialize
@logger = Log4r::Logger.new("vagrant::config::loader")
@sources = {}
@proc_cache = {}
@config_cache = {}
# Initializes a configuration loader.
#
# @param [Registry] versions A registry of the available versions and
# their associated loaders.
# @param [Array] version_order An array of the order of the versions
# in the registry. This is used to determine if upgrades are
# necessary. Additionally, the last version in this order is always
# considered the "current" version.
def initialize(versions, version_order)
@logger = Log4r::Logger.new("vagrant::config::loader")
@config_cache = {}
@proc_cache = {}
@sources = {}
@versions = versions
@version_order = version_order
end
# Set the configuration data for the given name.
@ -71,8 +81,8 @@ module Vagrant
end
# Get the current version config class to use
current_version = Config::VERSIONS_ORDER.last
current_config_klass = Config::VERSIONS.get(current_version)
current_version = @version_order.last
current_config_klass = @versions.get(current_version)
# This will hold our result
result = current_config_klass.init
@ -108,7 +118,7 @@ module Vagrant
# version of the configuration. This may be an ill assumption,
# but it made building the initial version of multi-versioned
# configuration easy to support the old sub-VM stuff.
return [[Config::VERSIONS_ORDER.last, source]] if source.is_a?(Proc)
return [[@version_order.last, source]] if source.is_a?(Proc)
# Assume all string sources are actually pathnames
source = Pathname.new(source) if source.is_a?(String)

View File

@ -361,7 +361,7 @@ module Vagrant
# to load the Vagrantfile into that context.
def load_config!
# Initialize the config loader
config_loader = Config::Loader.new
config_loader = Config::Loader.new(Config::VERSIONS, Config::VERSIONS_ORDER)
config_loader.load_order = [:default, :box, :home, :root, :vm]
inner_load = lambda do |*args|

View File

@ -1,9 +1,39 @@
require File.expand_path("../../../base", __FILE__)
require "vagrant/registry"
describe Vagrant::Config::Loader do
include_context "unit"
let(:instance) { described_class.new }
# This is just a dummy implementation of a configuraiton loader which
# simply acts on hashes.
let(:test_loader) do
Class.new do
def self.init
{}
end
def self.load(proc)
init.tap do |obj|
proc.call(obj)
end
end
def self.merge(old, new)
old.merge(new)
end
end
end
let(:versions) do
Vagrant::Registry.new.tap do |r|
r.register("1") { test_loader }
end
end
let(:version_order) { ["1"] }
let(:instance) { described_class.new(versions, version_order) }
it "should ignore non-existent load order keys" do
instance.load_order = [:foo]
@ -12,20 +42,20 @@ describe Vagrant::Config::Loader do
it "should load and return the configuration" do
proc = Proc.new do |config|
config.vagrant.dotfile_name = "foo"
config[:foo] = "yep"
end
instance.load_order = [:proc]
instance.set(:proc, proc)
config = instance.load
config.vagrant.dotfile_name.should == "foo"
config[:foo].should == "yep"
end
it "should only run the same proc once" do
count = 0
proc = Proc.new do |config|
config.vagrant.dotfile_name = "foo"
config[:foo] = "yep"
count += 1
end
@ -36,7 +66,7 @@ describe Vagrant::Config::Loader do
result = instance.load
# Verify the config result
result.vagrant.dotfile_name.should == "foo"
result[:foo].should == "yep"
# Verify the count is only one
count.should == 1