From d487e286f423f25de62423c75c221c7611db016e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 19 Jan 2012 20:54:09 -0800 Subject: [PATCH] Don't merge config keys that start with __. This allows config classes to store internal state somehow. --- lib/vagrant/config/base.rb | 7 ++++++- test/unit/vagrant/config/base_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/config/base.rb b/lib/vagrant/config/base.rb index dda537983..f3b95f960 100644 --- a/lib/vagrant/config/base.rb +++ b/lib/vagrant/config/base.rb @@ -33,7 +33,12 @@ module Vagrant def merge(other) result = self.class.new instance_variables_hash.merge(other.instance_variables_hash).each do |key, value| - result.instance_variable_set("@#{key}".to_sym, value) + # Ignore keys that start with a double underscore. This allows + # configuration classes to still hold around internal state + # that isn't propagated. + if !key.start_with?("__") + result.instance_variable_set("@#{key}".to_sym, value) + end end result diff --git a/test/unit/vagrant/config/base_test.rb b/test/unit/vagrant/config/base_test.rb index 918c3a0cf..f8a3d0a9e 100644 --- a/test/unit/vagrant/config/base_test.rb +++ b/test/unit/vagrant/config/base_test.rb @@ -22,4 +22,26 @@ describe Vagrant::Config::Base do result.one.should == 2 result.two.should == 5 end + + it "doesn't merge values that start with a double underscore" do + bar_class = Class.new(foo_class) do + @@counter = 0 + def initialize + @__test = @@counter + @@counter += 1 + end + end + + one = bar_class.new + one.one = 2 + one.two = 1 + + two = bar_class.new + two.two = 5 + + # Verify the counters + one.instance_variable_get(:@__test).should == 0 + two.instance_variable_get(:@__test).should == 1 + one.merge(two).instance_variable_get(:@__test).should == 2 + end end