From 863ebe2d2fbad4f599e5cc4c0ea2b472843a9993 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 11 Jan 2012 22:12:41 -0800 Subject: [PATCH] Custom merging for VM config --- lib/vagrant/config/vm.rb | 10 ++-- test/unit/vagrant/config/vm_test.rb | 71 +++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 test/unit/vagrant/config/vm_test.rb diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index 6d98b7755..7c0887551 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -31,11 +31,11 @@ module Vagrant # Custom merge method since some keys here are merged differently. def merge(other) result = super - result.forwarded_ports = @forwarded_ports + other.forwarded_ports - result.shared_folders = @shared_folders.merge(other.shared_folders) - result.networks = @networks + other.networks - result.provisioners = @provisioners + other.provisioners - result.customizations = @customizations + other.customizations + result.instance_variable_set(:@forwarded_ports, @forwarded_ports + other.forwarded_ports) + result.instance_variable_set(:@shared_folders, @shared_folders.merge(other.shared_folders)) + result.instance_variable_set(:@networks, @networks + other.networks) + result.instance_variable_set(:@provisioners, @provisioners + other.provisioners) + result.instance_variable_set(:@customizations, @customizations + other.customizations) result end diff --git a/test/unit/vagrant/config/vm_test.rb b/test/unit/vagrant/config/vm_test.rb new file mode 100644 index 000000000..eec642b94 --- /dev/null +++ b/test/unit/vagrant/config/vm_test.rb @@ -0,0 +1,71 @@ +require File.expand_path("../../../base", __FILE__) + +describe Vagrant::Config::VMConfig do + include_context "unit" + + it "merges by appending forwarded ports" do + a = described_class.new + a.forward_port 80, 8080 + + b = described_class.new + b.forward_port 100, 1000 + + c = a.merge(b) + c.forwarded_ports.length.should == 2 + c.forwarded_ports[0][:guestport].should == 80 + c.forwarded_ports[0][:hostport].should == 8080 + c.forwarded_ports[1][:guestport].should == 100 + c.forwarded_ports[1][:hostport].should == 1000 + end + + it "merges by merging shared folders" do + a = described_class.new + a.share_folder "a", "/guest", "/host" + a.share_folder "b", "/guest", "/host" + + b = described_class.new + b.share_folder "c", "/guest", "/host" + + c = a.merge(b) + c.shared_folders.has_key?("a").should be + c.shared_folders.has_key?("b").should be + c.shared_folders.has_key?("c").should be + end + + it "merges by appending networks" do + a = described_class.new + a.network :hostonly, "33.33.33.10" + + b = described_class.new + b.network :hostonly, "33.33.33.11" + + c = a.merge(b) + c.networks.length.should == 2 + c.networks[0].should == [:hostonly, ["33.33.33.10"]] + c.networks[1].should == [:hostonly, ["33.33.33.11"]] + end + + it "merges by appending provisioners" do + a = described_class.new + a.provision :foo + + b = described_class.new + b.provision :bar + + c = a.merge(b) + c.provisioners.length.should == 2 + c.provisioners[0].shortcut.should == :foo + c.provisioners[1].shortcut.should == :bar + end + + it "merges by appending customizations" do + a = described_class.new + a.customize "a" + + b = described_class.new + b.customize "b" + + c = a.merge(b) + c.customizations.should == ["a", "b"] + end +end