yamldiff 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yamldiff.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'mocha'
8
+ gem 'rspec'
9
+ gem 'fakefs', require: 'fakefs/safe'
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jonathan R. Wallace
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Yamldiff
2
+
3
+ Yamldiff prints out a list of missing keys for two yaml files.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'yamldiff'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install yamldiff
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
@@ -0,0 +1,3 @@
1
+ module Yamldiff
2
+ VERSION = "0.0.1"
3
+ end
data/lib/yamldiff.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "yamldiff/version"
2
+
3
+ class YamldiffError
4
+ attr_reader :key, :context
5
+ def initialize(key, context)
6
+ @key = key
7
+ @context = context
8
+ end
9
+ end
10
+
11
+ class YamldiffKeyError < YamldiffError; end
12
+ class YamldiffKeyValueTypeError < YamldiffError; end
13
+ class YamldiffKeyValueError < YamldiffError; end
14
+
15
+ class Yamldiff
16
+ class << self
17
+ # Compare the two yaml files
18
+ def diff_yaml(first, second, errors_for = {})
19
+ primary = YAML.load(File.open(first))
20
+ secondary = YAML.load(File.open(second))
21
+ errors_for[secondary] = compare_hashes(first, second)
22
+ end
23
+
24
+ # Iterate through all keys in the first hash, checking each key for
25
+ # 1. existence
26
+ # 2. value class equivalence
27
+ # 3. value type equivalence
28
+ # in the second hash.
29
+ #
30
+ # Adapted from http://stackoverflow.com/a/6274367/91029
31
+ def compare_hashes(first, second, context = [])
32
+ errors = []
33
+
34
+ first.each do |key, value|
35
+ unless second.key?(key)
36
+ errors << YamldiffKeyError.new(key, context) # "Missing key : #{key} in path #{context.join(".")}"
37
+ next
38
+ end
39
+
40
+ value2 = second[key]
41
+ if (value.class != value2.class)
42
+ errors << YamldiffKeyValueTypeError.new(key, context) # "Key value type mismatch : #{key} in path #{context.join(".")}"
43
+ next
44
+ end
45
+
46
+ if value.is_a?(Hash)
47
+ errors << compare_hashes(value, second[key], (context << key))
48
+ next
49
+ end
50
+
51
+ if (value != value2)
52
+ errors << YamldiffKeyValueError.new(key, context) # "Key value mismatch : #{key} in path #{context.join(".")}"
53
+ end
54
+ end
55
+
56
+ errors.flatten
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ describe Yamldiff do
4
+ describe ".compare_hashes" do
5
+ it "should be empty for equal hashes" do
6
+ subject.compare_hashes({ a: 1 }, { a: 1 }).should be_empty
7
+ end
8
+
9
+ it "should be empty for nested hashes" do
10
+ first = { a: { b: 2 } }
11
+ second = { a: { b: 2 } }
12
+ subject.compare_hashes(first, second).should be_empty
13
+ end
14
+
15
+ it "should recognize when a key is missing" do
16
+ first = { a: 1, b: 2 }
17
+ second = { a: 1 }
18
+ result = subject.compare_hashes(first, second).first
19
+
20
+ result.should be_an_instance_of(YamldiffKeyError)
21
+ result.key.should eql :b
22
+ result.context.should be_empty
23
+ end
24
+
25
+ it "should recognize when two keys' values are of a different type" do
26
+ first = { a: 1 }
27
+ second = { a: "1" }
28
+
29
+ result = subject.compare_hashes(first, second).first
30
+ result.should be_an_instance_of(YamldiffKeyValueTypeError)
31
+ result.key.should eql :a
32
+ result.context.should be_empty
33
+ end
34
+
35
+ it "should recognize when two keys' values are not equal" do
36
+ first = { a: { b: 2 } }
37
+ second = { a: { b: 1 } }
38
+
39
+ result = subject.compare_hashes(first, second).first
40
+ result.should be_an_instance_of(YamldiffKeyValueError)
41
+ result.key.should eql :b
42
+ result.context.should == [:a]
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,8 @@
1
+ require 'yamldiff'
2
+ require 'mocha'
3
+ require 'fakefs/spec_helpers'
4
+
5
+ RSpec.configure do |c|
6
+ c.mock_with :mocha
7
+ c.include FakeFS::SpecHelpers, fakefs: true
8
+ end
data/yamldiff.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/yamldiff/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jonathan R. Wallace"]
6
+ gem.email = ["jonathan.wallace@gmail.com"]
7
+ gem.description = %q{Shows the difference yaml keys}
8
+ gem.summary = %q{Yamldiff will tell you when and where you have differences between two yaml files. It prints out a list of missing keys for the second file.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "yamldiff"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Yamldiff::VERSION
17
+ gem.add_development_dependency 'fakefs'
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rspec'
20
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamldiff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jonathan R. Wallace
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fakefs
16
+ requirement: &70230341344320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70230341344320
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70230341343220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70230341343220
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70230341342000 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70230341342000
47
+ description: Shows the difference yaml keys
48
+ email:
49
+ - jonathan.wallace@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - lib/yamldiff.rb
60
+ - lib/yamldiff/version.rb
61
+ - spec/lib/yamldiff_spec.rb
62
+ - spec/spec_helper.rb
63
+ - yamldiff.gemspec
64
+ homepage: ''
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.17
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Yamldiff will tell you when and where you have differences between two yaml
88
+ files. It prints out a list of missing keys for the second file.
89
+ test_files:
90
+ - spec/lib/yamldiff_spec.rb
91
+ - spec/spec_helper.rb