yamldiff 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
- module Yamldiff
2
- VERSION = "0.0.3"
1
+ class Yamldiff
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,46 @@
1
+ class Yamldiff
2
+ class << self
3
+ # Compare the two yaml files
4
+ def diff_yaml(first, second, errors_for = {})
5
+ primary = YAML.load(File.open(first))
6
+ secondary = YAML.load(File.open(second))
7
+ errors_for[second] = compare_hashes(primary, secondary)
8
+ errors_for
9
+ end
10
+
11
+ # Iterate through all keys in the first hash, checking each key for
12
+ # 1. existence
13
+ # 2. value class equivalence
14
+ # 3. value type equivalence
15
+ # in the second hash.
16
+ #
17
+ # Adapted from http://stackoverflow.com/a/6274367/91029
18
+ def compare_hashes(first, second, context = [])
19
+ errors = []
20
+
21
+ first.each do |key, value|
22
+ unless second.key?(key)
23
+ errors << YamldiffKeyError.new(key, context) # "Missing key : #{key} in path #{context.join(".")}"
24
+ next
25
+ end
26
+
27
+ value2 = second[key]
28
+ if (value.class != value2.class)
29
+ errors << YamldiffKeyValueTypeError.new(key, context) # "Key value type mismatch : #{key} in path #{context.join(".")}"
30
+ next
31
+ end
32
+
33
+ if value.is_a?(Hash)
34
+ errors << compare_hashes(value, second[key], (context << key))
35
+ next
36
+ end
37
+
38
+ if (value != value2)
39
+ errors << YamldiffKeyValueError.new(key, context) # "Key value mismatch : #{key} in path #{context.join(".")}"
40
+ end
41
+ end
42
+
43
+ errors.flatten
44
+ end
45
+ end
46
+ end
data/lib/yamldiff.rb CHANGED
@@ -1,48 +1,3 @@
1
1
  require "yamldiff/version"
2
2
  require "yamldiff/yamldiff_error"
3
-
4
- class Yamldiff
5
- class << self
6
- # Compare the two yaml files
7
- def diff_yaml(first, second, errors_for = {})
8
- primary = YAML.load(File.open(first))
9
- secondary = YAML.load(File.open(second))
10
- errors_for[secondary] = compare_hashes(first, second)
11
- end
12
-
13
- # Iterate through all keys in the first hash, checking each key for
14
- # 1. existence
15
- # 2. value class equivalence
16
- # 3. value type equivalence
17
- # in the second hash.
18
- #
19
- # Adapted from http://stackoverflow.com/a/6274367/91029
20
- def compare_hashes(first, second, context = [])
21
- errors = []
22
-
23
- first.each do |key, value|
24
- unless second.key?(key)
25
- errors << YamldiffKeyError.new(key, context) # "Missing key : #{key} in path #{context.join(".")}"
26
- next
27
- end
28
-
29
- value2 = second[key]
30
- if (value.class != value2.class)
31
- errors << YamldiffKeyValueTypeError.new(key, context) # "Key value type mismatch : #{key} in path #{context.join(".")}"
32
- next
33
- end
34
-
35
- if value.is_a?(Hash)
36
- errors << compare_hashes(value, second[key], (context << key))
37
- next
38
- end
39
-
40
- if (value != value2)
41
- errors << YamldiffKeyValueError.new(key, context) # "Key value mismatch : #{key} in path #{context.join(".")}"
42
- end
43
- end
44
-
45
- errors.flatten
46
- end
47
- end
48
- end
3
+ require "yamldiff/yamldiff"
@@ -1,45 +1,117 @@
1
1
  require "spec_helper"
2
-
2
+
3
3
  describe Yamldiff do
4
+ subject { Yamldiff }
5
+
4
6
  describe ".compare_hashes" do
5
7
  it "should be empty for equal hashes" do
6
8
  subject.compare_hashes({ a: 1 }, { a: 1 }).should be_empty
7
- end
8
-
9
+ end
10
+
9
11
  it "should be empty for nested hashes" do
10
- first = { a: { b: 2 } }
11
- second = { a: { b: 2 } }
12
+ first = { a: { b: 2 } }
13
+ second = { a: { b: 2 } }
12
14
  subject.compare_hashes(first, second).should be_empty
13
- end
14
-
15
+ end
16
+
15
17
  it "should recognize when a key is missing" do
16
- first = { a: 1, b: 2 }
17
- second = { a: 1 }
18
+ first = { a: 1, b: 2 }
19
+ second = { a: 1 }
18
20
  result = subject.compare_hashes(first, second).first
19
-
21
+
20
22
  result.should be_an_instance_of(YamldiffKeyError)
21
23
  result.key.should eql :b
22
24
  result.context.should be_empty
23
- end
24
-
25
+ end
26
+
25
27
  it "should recognize when two keys' values are of a different type" do
26
28
  first = { a: 1 }
27
29
  second = { a: "1" }
28
-
30
+
29
31
  result = subject.compare_hashes(first, second).first
30
32
  result.should be_an_instance_of(YamldiffKeyValueTypeError)
31
33
  result.key.should eql :a
32
34
  result.context.should be_empty
33
- end
34
-
35
+ end
36
+
35
37
  it "should recognize when two keys' values are not equal" do
36
- first = { a: { b: 2 } }
37
- second = { a: { b: 1 } }
38
-
38
+ first = { a: { b: 2 } }
39
+ second = { a: { b: 1 } }
40
+
39
41
  result = subject.compare_hashes(first, second).first
40
42
  result.should be_an_instance_of(YamldiffKeyValueError)
41
43
  result.key.should eql :b
42
44
  result.context.should == [:a]
43
- end
44
- end
45
+ end
46
+ end
47
+
48
+ describe ".diff_yaml" do
49
+ it "returns no errors when the files have the same YAML structure", fakefs: true do
50
+ File.open("./en.yml", "w") do |f|
51
+ f.puts("en: ")
52
+ f.puts(" app_name: 'Verbosefish'")
53
+ f.puts("blah: 'Hello'")
54
+ end
55
+ File.open("./es.yml", "w") do |f|
56
+ f.puts("blah: 'Hello'")
57
+ f.puts("en: ")
58
+ f.puts(" app_name: 'Verbosefish'")
59
+ end
60
+
61
+ subject.diff_yaml("./en.yml", "./es.yml").values.all? {|e| e.should eql([])}
62
+ end
63
+
64
+ it "returns one top level error", fakefs: true do
65
+ first = "./en.yml"
66
+ second = "./es.yml"
67
+ File.open(first, "w") do |f|
68
+ f.puts("en: ")
69
+ f.puts(" app_name: 'Verbosefish'")
70
+ end
71
+ File.open(second, "w") do |f|
72
+ f.puts("es: ")
73
+ f.puts(" app_name: 'Verboszefish'")
74
+ end
75
+
76
+ result = subject.diff_yaml(first, second)
77
+ result[second].first.key.should eql("en")
78
+ end
79
+
80
+ it "returns second level error", fakefs: true do
81
+ first = "./en.yml"
82
+ second = "./es.yml"
83
+ File.open(first, "w") do |f|
84
+ f.puts("en: ")
85
+ f.puts(" app_name: 'Verbosefish'")
86
+ end
87
+ File.open(second, "w") do |f|
88
+ f.puts("en: ")
89
+ f.puts(" app_name: 'Verboszefish'")
90
+ end
91
+
92
+ result = subject.diff_yaml(first, second)
93
+ result[second].first.key.should eql("app_name")
94
+ result[second].first.context.should eql(["en"])
95
+ end
96
+
97
+ it "returns third level error", fakefs: true do
98
+ first = "./en.yml"
99
+ second = "./es.yml"
100
+
101
+ File.open(first, "w") do |f|
102
+ f.puts("en:")
103
+ f.puts(" app_name:")
104
+ f.puts(" foo: 'Verbosefish'")
105
+ end
106
+ File.open(second, "w") do |f|
107
+ f.puts("en:")
108
+ f.puts(" app_name:")
109
+ f.puts(" foo: 'Something completely different'")
110
+ end
111
+
112
+ result = subject.diff_yaml(first, second)
113
+ result[second].first.key.should eql("foo")
114
+ result[second].first.context.should eql(["en", "app_name"])
115
+ end
116
+ end
45
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yamldiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fakefs
16
- requirement: &70135348136260 !ruby/object:Gem::Requirement
16
+ requirement: &70330118996640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70135348136260
24
+ version_requirements: *70330118996640
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70135348135260 !ruby/object:Gem::Requirement
27
+ requirement: &70330118996040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70135348135260
35
+ version_requirements: *70330118996040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70135352244060 !ruby/object:Gem::Requirement
38
+ requirement: &70330118995380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70135352244060
46
+ version_requirements: *70330118995380
47
47
  description: Shows the difference yaml keys
48
48
  email:
49
49
  - jonathan.wallace@gmail.com
@@ -58,6 +58,7 @@ files:
58
58
  - Rakefile
59
59
  - lib/yamldiff.rb
60
60
  - lib/yamldiff/version.rb
61
+ - lib/yamldiff/yamldiff.rb
61
62
  - lib/yamldiff/yamldiff_error.rb
62
63
  - spec/lib/yamldiff_spec.rb
63
64
  - spec/spec_helper.rb