From 7e19d6849b34589575b225aed57e40adfd479327 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 23 Jun 2012 12:06:54 -0700 Subject: [PATCH] Config loader no longer assumes latest version for procs. Previously, all procs were assumed to just be the current version. This is certainly not going to be true always so now the version number of the configuration must be explicit if you're assigning a proc to the configuration loader. --- lib/vagrant/config/loader.rb | 52 +++++++++++++++---------- lib/vagrant/environment.rb | 2 +- plugins/kernel_v1/config/vm_subvm.rb | 10 +++++ test/unit/vagrant/config/loader_test.rb | 7 +++- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/lib/vagrant/config/loader.rb b/lib/vagrant/config/loader.rb index a93b2aa01..88dc0179f 100644 --- a/lib/vagrant/config/loader.rb +++ b/lib/vagrant/config/loader.rb @@ -114,30 +114,42 @@ module Vagrant # the configuration object and are expected to mutate this # configuration object. def procs_for_source(source) - # If the source is just a proc, we assume it is for the latest - # 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 [[@version_order.last, source]] if source.is_a?(Proc) + # Convert all pathnames to strings so we just have their path + source = source.to_s if source.is_a?(Pathname) - # Assume all string sources are actually pathnames - source = Pathname.new(source) if source.is_a?(String) + if source.is_a?(Array) + # An array must be formatted as [version, proc], so verify + # that and then return it + raise ArgumentError, "String source must have format [version, proc]" if source.length != 2 - if source.is_a?(Pathname) - @logger.debug("Load procs for pathname: #{source.inspect}") - - begin - return Config.capture_configures do - Kernel.load source - end - rescue SyntaxError => e - # Report syntax errors in a nice way. - raise Errors::VagrantfileSyntaxError, :file => e.message - end + # Return it as an array since we're expected to return an array + # of [version, proc] pairs, but an array source only has one. + return [source] + elsif source.is_a?(String) + # Strings are considered paths, so load them + return procs_for_path(source) + else + raise ArgumentError, "Unknown configuration source: #{source.inspect}" end - - raise Exception, "Unknown configuration source: #{source.inspect}" end + + # This returns an array of `Proc` objects for the given path source. + # + # @param [String] path Path to the file which contains the proper + # `Vagrant.configure` calls. + # @return [Array] + def procs_for_path(path) + @logger.debug("Load procs for pathname: #{path}") + + begin + return Config.capture_configures do + Kernel.load path + end + rescue SyntaxError => e + # Report syntax errors in a nice way. + raise Errors::VagrantfileSyntaxError, :file => e.message + end + end end end end diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 5d23818c4..3d4714f06 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -395,7 +395,7 @@ module Vagrant if subvm # We have subvm configuration, so set that up as well. - config_loader.set(:vm, subvm.proc_stack) + config_loader.set(:vm, subvm.config_procs) end # We activate plugins here because the files which we're loading diff --git a/plugins/kernel_v1/config/vm_subvm.rb b/plugins/kernel_v1/config/vm_subvm.rb index eca7e33fa..451da5dcb 100644 --- a/plugins/kernel_v1/config/vm_subvm.rb +++ b/plugins/kernel_v1/config/vm_subvm.rb @@ -11,6 +11,16 @@ module VagrantPlugins def initialize @options = {} end + + # This returns an array of the procs to configure this VM, with + # the proper version pre-pended for the configuration loader. + # + # @return [Array] + def config_procs + proc_stack.map do |proc| + ["1", proc] + end + end end end end diff --git a/test/unit/vagrant/config/loader_test.rb b/test/unit/vagrant/config/loader_test.rb index 8f8e5ea51..862fce7f8 100644 --- a/test/unit/vagrant/config/loader_test.rb +++ b/test/unit/vagrant/config/loader_test.rb @@ -5,6 +5,9 @@ require "vagrant/registry" describe Vagrant::Config::Loader do include_context "unit" + # This is the current version of configuration for the tests. + let(:current_version) { version_order.last } + # This is just a dummy implementation of a configuraiton loader which # simply acts on hashes. let(:test_loader) do @@ -46,7 +49,7 @@ describe Vagrant::Config::Loader do end instance.load_order = [:proc] - instance.set(:proc, proc) + instance.set(:proc, [[current_version, proc]]) config = instance.load config[:foo].should == "yep" @@ -60,7 +63,7 @@ describe Vagrant::Config::Loader do end instance.load_order = [:proc] - instance.set(:proc, proc) + instance.set(:proc, [[current_version, proc]]) 5.times do result = instance.load