Configuration versions can finalize config after loading

This is useful so that it can take a look at the final loaded
configuration object and possibly make some tweaks to the configuration
object. The use case this was built for was so that config V1 can verify
that there is always at least a single VM defined as a sub-VM, the
"default" VM.
This commit is contained in:
Mitchell Hashimoto 2012-06-23 12:27:32 -07:00
parent 7e19d6849b
commit 70fb804128
3 changed files with 36 additions and 3 deletions

View File

@ -103,8 +103,8 @@ module Vagrant
end
end
@logger.debug("Configuration loaded successfully")
result
@logger.debug("Configuration loaded successfully, finalizing and returning")
current_config_klass.finalize(result)
end
protected

View File

@ -16,6 +16,19 @@ module Vagrant
raise NotImplementedError
end
# This is called just before configuration loading is complete of
# a potentially completely-merged value to perform final touch-ups
# to the configuration, if required.
#
# This is an optional method to implement. The default implementation
# will simply return the same object.
#
# @param [Object] obj Final configuration object.
# @param [Object] Finalized configuration object.
def self.finalize(obj)
obj
end
# Loads the configuration for the given proc and returns a configuration
# object. The return value is treated as an opaque object, so it can be
# anything you'd like. The return value is the object that is passed

View File

@ -11,7 +11,7 @@ describe Vagrant::Config::Loader do
# This is just a dummy implementation of a configuraiton loader which
# simply acts on hashes.
let(:test_loader) do
Class.new do
Class.new(Vagrant::Config::VersionBase) do
def self.init
{}
end
@ -55,6 +55,26 @@ describe Vagrant::Config::Loader do
config[:foo].should == "yep"
end
it "should finalize the configuration" do
# Create the finalize method on our loader
def test_loader.finalize(obj)
obj[:finalized] = true
obj
end
# Basic configuration proc
proc = lambda do |config|
config[:foo] = "yep"
end
# Run the actual configuration and assert that we get the proper result
instance.load_order = [:proc]
instance.set(:proc, [[current_version, proc]])
config = instance.load
config[:foo].should == "yep"
config[:finalized].should == true
end
it "should only run the same proc once" do
count = 0
proc = Proc.new do |config|