diff --git a/lib/vagrant/config/v2/root.rb b/lib/vagrant/config/v2/root.rb index d35b5f2f4..419319c29 100644 --- a/lib/vagrant/config/v2/root.rb +++ b/lib/vagrant/config/v2/root.rb @@ -1,3 +1,5 @@ +require "vagrant/config/v2/util" + module Vagrant module Config module V2 @@ -54,16 +56,16 @@ module Vagrant # we merge them into our total errors list. result = instance.validate(machine) if result && !result.empty? - result.each do |key, value| - if !value.empty? - errors[key] ||= [] - errors[key] += value - end - end + errors = Util.merge_errors(errors, result) end end end + # Go through and delete empty keys + errors.keys.each do |key| + errors.delete(key) if errors[key].empty? + end + errors end diff --git a/lib/vagrant/config/v2/util.rb b/lib/vagrant/config/v2/util.rb new file mode 100644 index 000000000..798517017 --- /dev/null +++ b/lib/vagrant/config/v2/util.rb @@ -0,0 +1,21 @@ +module Vagrant + module Config + module V2 + class Util + # This merges two error hashes from validate methods. + # + # @param [Hash] first + # @param [Hash] second + # @return [Hash] Merged result + def self.merge_errors(first, second) + first.dup.tap do |result| + second.each do |key, value| + result[key] ||= [] + result[key] += value + end + end + end + end + end + end +end diff --git a/test/unit/vagrant/config/v2/util_test.rb b/test/unit/vagrant/config/v2/util_test.rb new file mode 100644 index 000000000..ee0d4d2f7 --- /dev/null +++ b/test/unit/vagrant/config/v2/util_test.rb @@ -0,0 +1,21 @@ +require File.expand_path("../../../../base", __FILE__) + +require "vagrant/config/v2/util" + +describe Vagrant::Config::V2::Util do + describe "merging errors" do + it "should merge matching keys and leave the rest alone" do + first = { "one" => ["foo"], "two" => ["two"] } + second = { "one" => ["bar"], "three" => ["three"] } + + expected = { + "one" => ["foo", "bar"], + "two" => ["two"], + "three" => ["three"] + } + + result = described_class.merge_errors(first, second) + result.should == expected + end + end +end