Skip to content

Commit

Permalink
Config loader no longer assumes latest version for procs.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mitchellh committed Jun 23, 2012
1 parent b3db82e commit 7e19d68
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
54 changes: 33 additions & 21 deletions lib/vagrant/config/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,42 @@ def load
# 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)

# Assume all string sources are actually pathnames
source = Pathname.new(source) if source.is_a?(String)

if source.is_a?(Pathname)
@logger.debug("Load procs for pathname: #{source.inspect}")
# Convert all pathnames to strings so we just have their path
source = source.to_s if source.is_a?(Pathname)

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

# 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
end

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
# 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<Proc>]
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

raise Exception, "Unknown configuration source: #{source.inspect}"
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/vagrant/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def load_config!

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
Expand Down
10 changes: 10 additions & 0 deletions plugins/kernel_v1/config/vm_subvm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class VagrantConfigSubVM
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
7 changes: 5 additions & 2 deletions test/unit/vagrant/config/loader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
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
Expand Down Expand Up @@ -46,7 +49,7 @@ def self.merge(old, new)
end

instance.load_order = [:proc]
instance.set(:proc, proc)
instance.set(:proc, [[current_version, proc]])
config = instance.load

config[:foo].should == "yep"
Expand All @@ -60,7 +63,7 @@ def self.merge(old, new)
end

instance.load_order = [:proc]
instance.set(:proc, proc)
instance.set(:proc, [[current_version, proc]])

5.times do
result = instance.load
Expand Down

0 comments on commit 7e19d68

Please sign in to comment.