Created
May 5, 2017 21:44
-
-
Save fj/d036cdb127bfb0fb1a24745db1bbacdf to your computer and use it in GitHub Desktop.
Compares two YAML files and shows which keys are "missing" and which keys are "extras"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
class Hash | |
def fully_qualified_keys | |
map { |k,v| | |
v.fully_qualified_keys.map{ |a| "#{k}.#{a}" } rescue k.to_s | |
}.flatten | |
end | |
end | |
def yaml_hash_for(file) | |
YAML.load(File.open(File.expand_path file)) | |
end | |
h1 = yaml_hash_for ARGV[0] | |
h2 = yaml_hash_for ARGV[1] | |
def compare(expected, actual) | |
expected_keys = expected.fully_qualified_keys | |
actual_keys = actual.fully_qualified_keys | |
missing = expected_keys - actual_keys | |
extraneous = actual_keys - expected_keys | |
if missing.any? | |
puts "Expected these keys, but didn't find them:" | |
missing.each { |key| puts " - #{key}" } | |
end | |
if extraneous.any? | |
puts "Found these keys, but didn't expect them:" | |
extraneous.each { |key| puts " + #{key}" } | |
end | |
end | |
compare(h1, h2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment