Move config loader order out from an ivar into a param for the #load
method
This commit is contained in:
parent
1559f7b7a7
commit
0180ed849d
|
@ -12,10 +12,6 @@ module Vagrant
|
||||||
# set later always overrides those set earlier; this is how
|
# set later always overrides those set earlier; this is how
|
||||||
# configuration "scoping" is implemented.
|
# configuration "scoping" is implemented.
|
||||||
class Loader
|
class Loader
|
||||||
# This is an array of symbols specifying the order in which
|
|
||||||
# configuration is loaded. For examples, see the class documentation.
|
|
||||||
attr_accessor :load_order
|
|
||||||
|
|
||||||
# Initializes a configuration loader.
|
# Initializes a configuration loader.
|
||||||
#
|
#
|
||||||
# @param [Registry] versions A registry of the available versions and
|
# @param [Registry] versions A registry of the available versions and
|
||||||
|
@ -69,12 +65,16 @@ module Vagrant
|
||||||
@sources[name] = procs
|
@sources[name] = procs
|
||||||
end
|
end
|
||||||
|
|
||||||
# This loads the configured sources in the configured order and returns
|
# This loads the configuration sources in the given order and returns
|
||||||
# an actual configuration object that is ready to be used.
|
# an actual configuration object that is ready to be used.
|
||||||
def load
|
#
|
||||||
@logger.debug("Loading configuration in order: #{@load_order.inspect}")
|
# @param [Array<Symbol>] order The order of configuration to load.
|
||||||
|
# @return [Object] The configuration object. This is different for
|
||||||
|
# each configuration version.
|
||||||
|
def load(order)
|
||||||
|
@logger.debug("Loading configuration in order: #{order.inspect}")
|
||||||
|
|
||||||
unknown_sources = @sources.keys - @load_order
|
unknown_sources = @sources.keys - order
|
||||||
if !unknown_sources.empty?
|
if !unknown_sources.empty?
|
||||||
# TODO: Raise exception here perhaps.
|
# TODO: Raise exception here perhaps.
|
||||||
@logger.error("Unknown config sources: #{unknown_sources.inspect}")
|
@logger.error("Unknown config sources: #{unknown_sources.inspect}")
|
||||||
|
@ -87,7 +87,7 @@ module Vagrant
|
||||||
# This will hold our result
|
# This will hold our result
|
||||||
result = current_config_klass.init
|
result = current_config_klass.init
|
||||||
|
|
||||||
@load_order.each do |key|
|
order.each do |key|
|
||||||
next if !@sources.has_key?(key)
|
next if !@sources.has_key?(key)
|
||||||
|
|
||||||
@sources[key].each do |version, proc|
|
@sources[key].each do |version, proc|
|
||||||
|
|
|
@ -40,8 +40,7 @@ describe Vagrant::Config::Loader do
|
||||||
|
|
||||||
describe "basic loading" do
|
describe "basic loading" do
|
||||||
it "should ignore non-existent load order keys" do
|
it "should ignore non-existent load order keys" do
|
||||||
instance.load_order = [:foo]
|
instance.load([:foo])
|
||||||
instance.load
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should load and return the configuration" do
|
it "should load and return the configuration" do
|
||||||
|
@ -49,9 +48,8 @@ describe Vagrant::Config::Loader do
|
||||||
config[:foo] = "yep"
|
config[:foo] = "yep"
|
||||||
end
|
end
|
||||||
|
|
||||||
instance.load_order = [:proc]
|
|
||||||
instance.set(:proc, [[current_version, proc]])
|
instance.set(:proc, [[current_version, proc]])
|
||||||
config = instance.load
|
config = instance.load([:proc])
|
||||||
|
|
||||||
config[:foo].should == "yep"
|
config[:foo].should == "yep"
|
||||||
end
|
end
|
||||||
|
@ -71,9 +69,8 @@ describe Vagrant::Config::Loader do
|
||||||
end
|
end
|
||||||
|
|
||||||
# Run the actual configuration and assert that we get the proper result
|
# Run the actual configuration and assert that we get the proper result
|
||||||
instance.load_order = [:proc]
|
|
||||||
instance.set(:proc, [[current_version, proc]])
|
instance.set(:proc, [[current_version, proc]])
|
||||||
config = instance.load
|
config = instance.load([:proc])
|
||||||
config[:foo].should == "yep"
|
config[:foo].should == "yep"
|
||||||
config[:finalized].should == true
|
config[:finalized].should == true
|
||||||
end
|
end
|
||||||
|
@ -95,9 +92,8 @@ describe Vagrant::Config::Loader do
|
||||||
|
|
||||||
# Load a version 1 proc, and verify it is upgraded to version 2
|
# Load a version 1 proc, and verify it is upgraded to version 2
|
||||||
proc = lambda { |config| config[:foo] = "yep" }
|
proc = lambda { |config| config[:foo] = "yep" }
|
||||||
instance.load_order = [:proc]
|
|
||||||
instance.set(:proc, [["1", proc]])
|
instance.set(:proc, [["1", proc]])
|
||||||
config = instance.load
|
config = instance.load([:proc])
|
||||||
config[:foo].should == "yep"
|
config[:foo].should == "yep"
|
||||||
config[:v2].should == true
|
config[:v2].should == true
|
||||||
end
|
end
|
||||||
|
@ -111,11 +107,10 @@ describe Vagrant::Config::Loader do
|
||||||
count += 1
|
count += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
instance.load_order = [:proc]
|
|
||||||
instance.set(:proc, [[current_version, proc]])
|
instance.set(:proc, [[current_version, proc]])
|
||||||
|
|
||||||
5.times do
|
5.times do
|
||||||
result = instance.load
|
result = instance.load([:proc])
|
||||||
|
|
||||||
# Verify the config result
|
# Verify the config result
|
||||||
result[:foo].should == "yep"
|
result[:foo].should == "yep"
|
||||||
|
@ -131,9 +126,8 @@ describe Vagrant::Config::Loader do
|
||||||
# We test both setting a file multiple times as well as multiple
|
# We test both setting a file multiple times as well as multiple
|
||||||
# loads, since both should not cache the data.
|
# loads, since both should not cache the data.
|
||||||
file = temporary_file("$_config_data += 1")
|
file = temporary_file("$_config_data += 1")
|
||||||
instance.load_order = [:file]
|
|
||||||
5.times { instance.set(:file, file) }
|
5.times { instance.set(:file, file) }
|
||||||
5.times { instance.load }
|
5.times { instance.load([:file]) }
|
||||||
|
|
||||||
$_config_data.should == 1
|
$_config_data.should == 1
|
||||||
end
|
end
|
||||||
|
@ -143,18 +137,16 @@ describe Vagrant::Config::Loader do
|
||||||
|
|
||||||
file = temporary_file("$_config_data += 1")
|
file = temporary_file("$_config_data += 1")
|
||||||
|
|
||||||
instance.load_order = [:proc]
|
|
||||||
instance.set(:proc, file)
|
instance.set(:proc, file)
|
||||||
5.times { instance.load }
|
5.times { instance.load([:proc]) }
|
||||||
|
|
||||||
instance.set(:proc, file)
|
instance.set(:proc, file)
|
||||||
5.times { instance.load }
|
5.times { instance.load([:proc]) }
|
||||||
|
|
||||||
$_config_data.should == 1
|
$_config_data.should == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise proper error if there is a syntax error in a Vagrantfile" do
|
it "should raise proper error if there is a syntax error in a Vagrantfile" do
|
||||||
instance.load_order = [:file]
|
|
||||||
expect { instance.set(:file, temporary_file("Vagrant:^Config")) }.
|
expect { instance.set(:file, temporary_file("Vagrant:^Config")) }.
|
||||||
to raise_exception(Vagrant::Errors::VagrantfileSyntaxError)
|
to raise_exception(Vagrant::Errors::VagrantfileSyntaxError)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue