From 70fb8041286bf91435e4eccaaf72579f1c75f250 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 23 Jun 2012 12:27:32 -0700 Subject: [PATCH] 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. --- lib/vagrant/config/loader.rb | 4 ++-- lib/vagrant/config/version_base.rb | 13 +++++++++++++ test/unit/vagrant/config/loader_test.rb | 22 +++++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/config/loader.rb b/lib/vagrant/config/loader.rb index 88dc0179fc4..8eac2e74715 100644 --- a/lib/vagrant/config/loader.rb +++ b/lib/vagrant/config/loader.rb @@ -103,8 +103,8 @@ def load end end - @logger.debug("Configuration loaded successfully") - result + @logger.debug("Configuration loaded successfully, finalizing and returning") + current_config_klass.finalize(result) end protected diff --git a/lib/vagrant/config/version_base.rb b/lib/vagrant/config/version_base.rb index 647ef8ecf70..d505a810bda 100644 --- a/lib/vagrant/config/version_base.rb +++ b/lib/vagrant/config/version_base.rb @@ -16,6 +16,19 @@ def self.init 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 diff --git a/test/unit/vagrant/config/loader_test.rb b/test/unit/vagrant/config/loader_test.rb index 862fce7f852..a6fa096b427 100644 --- a/test/unit/vagrant/config/loader_test.rb +++ b/test/unit/vagrant/config/loader_test.rb @@ -11,7 +11,7 @@ # 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 @@ def self.merge(old, new) 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|