Add a V2 config helper to merge errors since that seems common

This commit is contained in:
Mitchell Hashimoto 2013-01-18 13:03:07 -08:00
parent 515ed8f119
commit e651eb3aa1
3 changed files with 50 additions and 6 deletions

View File

@ -1,3 +1,5 @@
require "vagrant/config/v2/util"
module Vagrant
module Config
module V2
@ -54,14 +56,14 @@ 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
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

View File

@ -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

View File

@ -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