From 8d24f779f05109fa60c79b6f7f0fe0a75dbafe44 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 13 Jan 2011 16:54:34 -0800 Subject: [PATCH] Provisioner config object should have access to the top config --- lib/vagrant/config/base.rb | 11 ++++++-- lib/vagrant/config/top.rb | 4 ++- lib/vagrant/config/vm.rb | 2 +- lib/vagrant/config/vm/provisioner.rb | 5 +++- test/vagrant/config/base_test.rb | 6 ---- test/vagrant/config/vm/provisioner_test.rb | 33 ++++++++++++++-------- test/vagrant/config_test.rb | 3 -- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/lib/vagrant/config/base.rb b/lib/vagrant/config/base.rb index 65a67da52..768892068 100644 --- a/lib/vagrant/config/base.rb +++ b/lib/vagrant/config/base.rb @@ -4,9 +4,6 @@ module Vagrant # basic things such as the environment instance variable which all # config classes need as well as a basic `to_json` implementation. class Base - # {Environment} that this config belongs to - attr_accessor :env - # {Top} of this configuration stack attr_accessor :top @@ -32,6 +29,14 @@ module Vagrant end end + # A helper to access the environment that this configuration is for. + # This is obtained by getting the env from the {Top}. + # + # @return [Vagrant::Envrionment] + def env + top.env + end + # Allows setting options from a hash. By default this simply calls # the `#{key}=` method on the config class with the value, which is # the expected behavior most of the time. diff --git a/lib/vagrant/config/top.rb b/lib/vagrant/config/top.rb index a9e7de8b6..2c4847f7d 100644 --- a/lib/vagrant/config/top.rb +++ b/lib/vagrant/config/top.rb @@ -9,6 +9,9 @@ module Vagrant class Top < Base @@configures = {} if !defined?(@@configures) + # The environment that this configuration is for. + attr_reader :env + class << self # The list of registered configuration classes as well as the key # they're registered under. @@ -27,7 +30,6 @@ module Vagrant def initialize(env=nil) self.class.configures_list.each do |key, klass| config = klass.new - config.env = env config.top = self instance_variable_set("@#{key}".to_sym, config) end diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index 349a25eeb..0d2036da2 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -61,7 +61,7 @@ module Vagrant end def provision(name, options=nil, &block) - @provisioners << Provisioner.new(name, options, &block) + @provisioners << Provisioner.new(top, name, options, &block) end # This shows an error message to smooth the transition for the diff --git a/lib/vagrant/config/vm/provisioner.rb b/lib/vagrant/config/vm/provisioner.rb index 8b4343e9a..89c4c8e3d 100644 --- a/lib/vagrant/config/vm/provisioner.rb +++ b/lib/vagrant/config/vm/provisioner.rb @@ -3,11 +3,13 @@ module Vagrant class VMConfig < Base # Represents a single configured provisioner for a VM. class Provisioner + attr_reader :top attr_reader :shortcut attr_reader :provisioner attr_reader :config - def initialize(shortcut, options=nil, &block) + def initialize(top, shortcut, options=nil, &block) + @top = top @shortcut = shortcut @provisioner = shortcut @provisioner = Provisioners::Base.registered[shortcut] if shortcut.is_a?(Symbol) @@ -29,6 +31,7 @@ module Vagrant # Instantiate the config class and configure it @config = @provisioner.const_get(*const_args).new + @config.top = top @config.set_options(options) if options block.call(@config) if block end diff --git a/test/vagrant/config/base_test.rb b/test/vagrant/config/base_test.rb index ee184a030..ed251c67a 100644 --- a/test/vagrant/config/base_test.rb +++ b/test/vagrant/config/base_test.rb @@ -42,12 +42,6 @@ class ConfigBaseTest < Test::Unit::TestCase assert_equal @json, @base.to_json end - should "not include env in the JSON hash" do - @base.env = "FOO" - hash = @base.instance_variables_hash - assert !hash.has_key?(:env) - end - should "not include top in the JSON hash" do @base.top = "FOO" hash = @base.instance_variables_hash diff --git a/test/vagrant/config/vm/provisioner_test.rb b/test/vagrant/config/vm/provisioner_test.rb index bf8838fcd..ba45acbfe 100644 --- a/test/vagrant/config/vm/provisioner_test.rb +++ b/test/vagrant/config/vm/provisioner_test.rb @@ -3,36 +3,47 @@ require "test_helper" class ConfigVMProvisionerTest < Test::Unit::TestCase setup do @klass = Vagrant::Config::VMConfig::Provisioner + @top = Vagrant::Config::Top.new(nil) end context "initializing" do + should "expose the top instance that the provisioner belongs to" do + instance = @klass.new(@top, :chef_solo) + assert_equal @top, instance.top + end + should "expose the shortcut used" do - instance = @klass.new(:chef_solo) + instance = @klass.new(@top, :chef_solo) assert_equal :chef_solo, instance.shortcut end should "expose the provisioner class if its a valid shortcut" do - instance = @klass.new(:chef_solo) + instance = @klass.new(@top, :chef_solo) assert_equal Vagrant::Provisioners::ChefSolo, instance.provisioner end should "expose the provisioner class if its a valid class" do - instance = @klass.new(Vagrant::Provisioners::ChefSolo) + instance = @klass.new(@top, Vagrant::Provisioners::ChefSolo) assert_equal Vagrant::Provisioners::ChefSolo, instance.provisioner end should "have a nil provisioner class if invalid" do - instance = @klass.new(:i_shall_never_exist) + instance = @klass.new(@top, :i_shall_never_exist) assert_nil instance.provisioner end should "have a nil config instance if invalid" do - instance = @klass.new(:i_shall_never_exist) + instance = @klass.new(@top, :i_shall_never_exist) assert_nil instance.config end + should "set the top of the config object to the given top" do + instance = @klass.new(@top, :chef_solo) + assert_equal @top, instance.config.top + end + should "configure the provisioner if valid" do - instance = @klass.new(:chef_solo) do |chef| + instance = @klass.new(@top, :chef_solo) do |chef| chef.cookbooks_path = "foo" end @@ -40,7 +51,7 @@ class ConfigVMProvisionerTest < Test::Unit::TestCase end should "configure the provisioner with a hash if valid" do - instance = @klass.new(:chef_solo, :cookbooks_path => "foo") + instance = @klass.new(@top, :chef_solo, :cookbooks_path => "foo") assert_equal "foo", instance.config.cookbooks_path end end @@ -51,20 +62,20 @@ class ConfigVMProvisionerTest < Test::Unit::TestCase end should "be invalid if provisioner is valid" do - instance = @klass.new(:i_shall_never_exist) + instance = @klass.new(@top, :i_shall_never_exist) instance.validate(@errors) assert !@errors.errors.empty? end should "be invalid if provisioner doesn't inherit from provisioners base" do klass = Class.new - instance = @klass.new(klass) + instance = @klass.new(@top, klass) instance.validate(@errors) assert !@errors.errors.empty? end should "be valid with a valid provisioner" do - instance = @klass.new(:chef_solo) do |chef| + instance = @klass.new(@top, :chef_solo) do |chef| chef.add_recipe "foo" end @@ -73,7 +84,7 @@ class ConfigVMProvisionerTest < Test::Unit::TestCase end should "be invalid if a provisioner's config is invalid" do - instance = @klass.new(:chef_solo) + instance = @klass.new(@top, :chef_solo) instance.validate(@errors) assert !@errors.errors.empty? end diff --git a/test/vagrant/config_test.rb b/test/vagrant/config_test.rb index f84f1d8d6..cc945a30f 100644 --- a/test/vagrant/config_test.rb +++ b/test/vagrant/config_test.rb @@ -21,7 +21,6 @@ class ConfigTest < Test::Unit::TestCase context "with an instance" do setup do - # @env = vagrant_env @instance = @klass.new end @@ -112,7 +111,6 @@ class ConfigTest < Test::Unit::TestCase key = "key#{i}" klass = mock("klass#{i}") instance = mock("instance#{i}") - instance.expects(:env=).with(env) instance.expects(:top=).with() do |top| assert top.is_a?(@klass::Top) true @@ -128,7 +126,6 @@ class ConfigTest < Test::Unit::TestCase key = "my_foo_bar_key" klass = mock("klass") instance = mock("instance") - instance.stubs(:env=) instance.stubs(:top=) klass.expects(:new).returns(instance) @klass::Top.configures(key, klass)