From 15c56a1f4c611ae0a433d5ac4c1f18059e6fc34a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 3 Dec 2011 17:29:28 -0800 Subject: [PATCH] Configuration loads. Lots of refactor to do still. --- lib/vagrant/config.rb | 14 ++++++++------ lib/vagrant/config/container.rb | 13 +++++++++++++ lib/vagrant/config/error_recorder.rb | 2 +- lib/vagrant/config/loader.rb | 13 +++++++++++-- lib/vagrant/config/nfs.rb | 2 +- lib/vagrant/config/package.rb | 2 +- lib/vagrant/config/ssh.rb | 2 +- lib/vagrant/config/vagrant.rb | 2 +- lib/vagrant/config/vm.rb | 2 +- lib/vagrant/config/vm/provisioner.rb | 2 +- lib/vagrant/config/vm/sub_vm.rb | 2 +- test/unit/vagrant/config/loader_test.rb | 12 ++++++++++++ test/unit_legacy/vagrant/config_test.rb | 14 -------------- 13 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 lib/vagrant/config/container.rb diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 1260a7f51..6ff90bfb5 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -1,17 +1,19 @@ require 'vagrant/config/base' require 'vagrant/config/loader' -# require 'vagrant/config/error_recorder' +require 'vagrant/config/error_recorder' require 'vagrant/config/top' # # The built-in configuration classes -# require 'vagrant/config/vagrant' -# require 'vagrant/config/ssh' -# require 'vagrant/config/nfs' -# require 'vagrant/config/vm' -# require 'vagrant/config/package' +require 'vagrant/config/vagrant' +require 'vagrant/config/ssh' +require 'vagrant/config/nfs' +require 'vagrant/config/vm' +require 'vagrant/config/package' module Vagrant module Config + autoload :Container, 'vagrant/config/container' + CONFIGURE_MUTEX = Mutex.new # This is the method which is called by all Vagrantfiles to configure Vagrant. diff --git a/lib/vagrant/config/container.rb b/lib/vagrant/config/container.rb new file mode 100644 index 000000000..19f9b055e --- /dev/null +++ b/lib/vagrant/config/container.rb @@ -0,0 +1,13 @@ +module Vagrant + module Config + # Contains loaded configuration values and provides access to those + # values. + # + # This is the class returned when loading configuration and stores + # the completely loaded configuration values. This class is meant to + # be immutable. + class Container + + end + end +end diff --git a/lib/vagrant/config/error_recorder.rb b/lib/vagrant/config/error_recorder.rb index eda3ee62c..5a5b8e427 100644 --- a/lib/vagrant/config/error_recorder.rb +++ b/lib/vagrant/config/error_recorder.rb @@ -1,5 +1,5 @@ module Vagrant - class Config + module Config # A class which is passed into the various {Base#validate} methods and # can be used as a helper to add error messages about a single config # class. diff --git a/lib/vagrant/config/loader.rb b/lib/vagrant/config/loader.rb index c97fca74a..0e3c37112 100644 --- a/lib/vagrant/config/loader.rb +++ b/lib/vagrant/config/loader.rb @@ -43,19 +43,28 @@ module Vagrant # This loads the configured sources in the configured order and returns # an actual configuration object that is ready to be used. def load + @logger.debug("Loading configuration in order: #{@load_order.inspect}") + unknown_sources = @sources.keys - @load_order if !unknown_sources.empty? # TODO: Raise exception here perhaps. @logger.error("Unknown config sources: #{unknown_sources.inspect}") end + top = Top.new + @load_order.each do |key| @sources[key].each do |source| + @logger.debug("Loading from: #{key}") procs_for_source(source).each do |proc| - # TODO: Call the proc with a configuration object. + proc.call(top) end end end + + @logger.debug("Configuration loaded successfully") + + top end protected @@ -65,7 +74,7 @@ module Vagrant # the configuration object and are expected to mutate this # configuration object. def procs_for_source(source) - return source if source.is_a?(Proc) + return [source] if source.is_a?(Proc) # Assume all string sources are actually pathnames source = Pathname.new(source) if source.is_a?(String) diff --git a/lib/vagrant/config/nfs.rb b/lib/vagrant/config/nfs.rb index e3a37c236..beaf63e4f 100644 --- a/lib/vagrant/config/nfs.rb +++ b/lib/vagrant/config/nfs.rb @@ -1,5 +1,5 @@ module Vagrant - class Config + module Config class NFSConfig < Base configures :nfs diff --git a/lib/vagrant/config/package.rb b/lib/vagrant/config/package.rb index 90355a080..85871c434 100644 --- a/lib/vagrant/config/package.rb +++ b/lib/vagrant/config/package.rb @@ -1,5 +1,5 @@ module Vagrant - class Config + module Config class PackageConfig < Base configures :package diff --git a/lib/vagrant/config/ssh.rb b/lib/vagrant/config/ssh.rb index 0a9cf4350..5af7caebd 100644 --- a/lib/vagrant/config/ssh.rb +++ b/lib/vagrant/config/ssh.rb @@ -1,5 +1,5 @@ module Vagrant - class Config + module Config class SSHConfig < Base configures :ssh diff --git a/lib/vagrant/config/vagrant.rb b/lib/vagrant/config/vagrant.rb index ca9315b47..3b991bd98 100644 --- a/lib/vagrant/config/vagrant.rb +++ b/lib/vagrant/config/vagrant.rb @@ -1,5 +1,5 @@ module Vagrant - class Config + module Config class VagrantConfig < Base configures :vagrant diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index aed5294a1..64db899d3 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -2,7 +2,7 @@ require 'vagrant/config/vm/sub_vm' require 'vagrant/config/vm/provisioner' module Vagrant - class Config + module Config class VMConfig < Base configures :vm diff --git a/lib/vagrant/config/vm/provisioner.rb b/lib/vagrant/config/vm/provisioner.rb index 89c4c8e3d..933701787 100644 --- a/lib/vagrant/config/vm/provisioner.rb +++ b/lib/vagrant/config/vm/provisioner.rb @@ -1,5 +1,5 @@ module Vagrant - class Config + module Config class VMConfig < Base # Represents a single configured provisioner for a VM. class Provisioner diff --git a/lib/vagrant/config/vm/sub_vm.rb b/lib/vagrant/config/vm/sub_vm.rb index 413a6986e..62b521730 100644 --- a/lib/vagrant/config/vm/sub_vm.rb +++ b/lib/vagrant/config/vm/sub_vm.rb @@ -1,5 +1,5 @@ module Vagrant - class Config + module Config class VMConfig < Base # Represents a single sub-VM in a multi-VM environment. class SubVM diff --git a/test/unit/vagrant/config/loader_test.rb b/test/unit/vagrant/config/loader_test.rb index de56d392b..3ef9c7b80 100644 --- a/test/unit/vagrant/config/loader_test.rb +++ b/test/unit/vagrant/config/loader_test.rb @@ -5,6 +5,18 @@ describe Vagrant::Config::Loader do let(:instance) { described_class.new } + it "should load and return the configuration" do + proc = Proc.new do |config| + config.vagrant.dotfile_name = "foo" + end + + instance.load_order = [:proc] + instance.set(:proc, proc) + config = instance.load + + config.vagrant.dotfile_name.should == "foo" + end + it "should raise proper error if there is a syntax error in a Vagrantfile" do instance.load_order = [:file] instance.set(:file, temporary_file("Vagrant:^Config")) diff --git a/test/unit_legacy/vagrant/config_test.rb b/test/unit_legacy/vagrant/config_test.rb index cc945a30f..38fe7edd7 100644 --- a/test/unit_legacy/vagrant/config_test.rb +++ b/test/unit_legacy/vagrant/config_test.rb @@ -5,20 +5,6 @@ class ConfigTest < Test::Unit::TestCase @klass = Vagrant::Config end - context "with the class" do - should "allow access to the last proc" do - foo = mock("object") - foo.expects(:call).once - - @klass.run { |config| foo.call } - value = @klass.last_proc.first - assert value.is_a?(Proc) - value.call(nil) - - assert @klass.last_proc.nil? - end - end - context "with an instance" do setup do @instance = @klass.new