Permalink
Browse files

Merge pull request #2 from chrismcg/diff_output

Human readable diff output and command line tool
  • Loading branch information...
wallace committed Jun 12, 2012
2 parents 86afa1e + e2f2e93 commit c51f36d98ef4e8b95bf663e30cf02b4ff01468e7
View
@@ -0,0 +1,6 @@
+#/usr/bin/env ruby
+$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
+require 'yamldiff'
+
+errors = Yamldiff.diff_yaml(ARGV[0], ARGV[1])
+puts errors[ARGV[1]]
View
@@ -1,4 +1,5 @@
require 'yaml'
+require 'diffy'
require "yamldiff/version"
require "yamldiff/yamldiff_error"
require "yamldiff/yamldiff"
View
@@ -20,13 +20,13 @@ def compare_hashes(first, second, context = [])
first.each do |key, value|
unless second.key?(key)
- errors << YamldiffKeyError.new(key, context) # "Missing key : #{key} in path #{context.join(".")}"
+ errors << YamldiffKeyError.new(key, context)
next
end
value2 = second[key]
if (value.class != value2.class)
- errors << YamldiffKeyValueTypeError.new(key, context) # "Key value type mismatch : #{key} in path #{context.join(".")}"
+ errors << YamldiffKeyValueTypeError.new(key, context)
next
end
@@ -36,7 +36,7 @@ def compare_hashes(first, second, context = [])
end
if (value != value2)
- errors << YamldiffKeyValueError.new(key, context) # "Key value mismatch : #{key} in path #{context.join(".")}"
+ errors << YamldiffKeyValueError.new(key, context, Diffy::Diff.new(value.to_s + "\n", value2.to_s + "\n"))
end
end
@@ -6,6 +6,31 @@ def initialize(key, context)
end
end
-class YamldiffKeyError < YamldiffError; end
-class YamldiffKeyValueTypeError < YamldiffError; end
-class YamldiffKeyValueError < YamldiffError; end
+class YamldiffKeyError < YamldiffError
+ def to_s
+ "Missing key: #{@context.join(".")}.#{@key}"
+ end
+end
+
+class YamldiffKeyValueTypeError < YamldiffError
+ def to_s
+ "Key value type mismatch: #{@context.join(".")}.#{@key}"
+ end
+end
+
+class YamldiffKeyValueError < YamldiffError
+ def initialize(key, context, diff = nil)
+ super key, context
+ @diff = diff
+ end
+
+ def to_s
+ output = []
+ output << "Key content differs: #{@context.join(".")}.#{@key}"
+ if @diff
+ output << "Diff:"
+ output << @diff
+ end
+ output.join("\n")
+ end
+end
@@ -0,0 +1,31 @@
+require "spec_helper"
+
+describe YamldiffKeyError, "#to_s" do
+ it "outputs human readable text" do
+ YamldiffKeyError.new('key', ['root', 'namespace']).to_s.should == "Missing key: root.namespace.key"
+ end
+end
+
+describe YamldiffKeyValueTypeError, "#to_s" do
+ it "outputs human readable text" do
+ YamldiffKeyValueTypeError.new('key', ['root', 'namespace']).to_s.should == "Key value type mismatch: root.namespace.key"
+ end
+end
+
+describe YamldiffKeyValueError, "#to_s" do
+ it "outputs human readable text" do
+ YamldiffKeyValueError.new('key', ['root', 'namespace']).to_s.should == "Key content differs: root.namespace.key"
+ end
+
+ it "outputs diff if given" do
+ str1 = "foo\n"
+ str2 = "bar\n"
+ diff = Diffy::Diff.new(str1, str2)
+ YamldiffKeyValueError.new('key', ['root', 'namespace'], diff).to_s.should == <<-OUTPUT
+Key content differs: root.namespace.key
+Diff:
+-foo
++bar
+OUTPUT
+ end
+end
View
@@ -117,5 +117,21 @@
result[second].first.key.should eql("bar")
result[second].first.context.should eql(["en", "baz"])
end
+
+ it "diffs the output when the values are different and passes diff to error" do
+ first = "./en.yml"
+ second = "./es.yml"
+ File.open(first, "w") do |f|
+ f.puts("en: ")
+ f.puts(" app_name: 'Verbosefish'")
+ end
+ File.open(second, "w") do |f|
+ f.puts("en: ")
+ f.puts(" app_name: 'Verboszefish'")
+ end
+ Diffy::Diff.expects(:new).with("Verbosefish\n", "Verboszefish\n").returns("DIFF")
+ YamldiffKeyValueError.expects(:new).with('app_name', ['en'], 'DIFF')
+ result = subject.diff_yaml(first, second)
+ end
end
end
View
@@ -14,6 +14,7 @@ Gem::Specification.new do |gem|
gem.name = "yamldiff"
gem.require_paths = ["lib"]
gem.version = Yamldiff::VERSION
+ gem.add_dependency 'diffy'
gem.add_development_dependency 'fakefs'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'

0 comments on commit c51f36d

Please sign in to comment.