diff --git a/lib/vagrant/config/top.rb b/lib/vagrant/config/top.rb index 4d49c290a..8c2d0e5f4 100644 --- a/lib/vagrant/config/top.rb +++ b/lib/vagrant/config/top.rb @@ -7,8 +7,9 @@ module Vagrant # # If you're looking to create your own configuration class, see {Base}. class Top < Base - def initialize + def initialize(registry=nil) @keys = {} + @registry = registry || Vagrant.config_keys end # We use method_missing as a way to get the configuration that is used @@ -16,7 +17,7 @@ module Vagrant def method_missing(name, *args) return @keys[name] if @keys.has_key?(name) - config_klass = Vagrant.config_keys.get(name.to_sym) + config_klass = @registry.get(name.to_sym) if config_klass # Instantiate the class and return the instance @keys[name] = config_klass.new diff --git a/test/unit/vagrant/config/top_test.rb b/test/unit/vagrant/config/top_test.rb new file mode 100644 index 000000000..21daf8c8e --- /dev/null +++ b/test/unit/vagrant/config/top_test.rb @@ -0,0 +1,27 @@ +require File.expand_path("../../../base", __FILE__) + +require "vagrant/registry" + +describe Vagrant::Config::Top do + include_context "unit" + + let(:registry) { Vagrant::Registry.new } + let(:instance) { described_class.new(registry) } + + it "should load in the proper config class" do + registry.register(:foo, Object) + + instance.foo.should be_kind_of(Object) + end + + it "should load the proper config class only once" do + registry.register(:foo, Object) + + obj = instance.foo + instance.foo.should eql(obj) + end + + it "still raises a method missing error if invalid key" do + expect { instance.foo }.to raise_error(NoMethodError) + end +end