Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make json format approvals resilient to differences in pretty json fo… #85

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/approvals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'approvals/reporters'
require 'approvals/filter'
require 'approvals/writer'
require 'approvals/verifier'
require 'approvals/namers/default_namer'

module Approvals
Expand Down
8 changes: 8 additions & 0 deletions lib/approvals/approval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def writer
@writer ||= Writer.for(@format)
end

def verifier
@verifier ||= Verifier.for(@format)
end

def verify
unless File.exist?(namer.output_dir)
FileUtils.mkdir_p(namer.output_dir)
Expand Down Expand Up @@ -63,6 +67,10 @@ def approved?
BINARY_FORMATS = [:binary]

def received_matches?
return verifier
.new(received_path, approved_path)
.verify if verifier

if BINARY_FORMATS.include?(@format) # Read without ERB
IO.read(received_path).chomp == IO.read(approved_path).chomp
else
Expand Down
15 changes: 15 additions & 0 deletions lib/approvals/verifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'approvals/verifiers/json_verifier'

module Approvals
module Verifier
REGISTRY = {
json: Verifiers::JsonVerifier,
}

class << self
def for(format)
REGISTRY[format]
end
end
end
end
26 changes: 26 additions & 0 deletions lib/approvals/verifiers/json_verifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Approvals
module Verifiers
class JsonVerifier
def initialize(received_path, approved_path)
self.received_path = received_path
self.approved_path = approved_path
end

def verify
approved == received
end

private

attr_accessor :approved_path, :received_path

def approved
JSON.parse(File.read(approved_path))
end

def received
JSON.parse(File.read(received_path))
end
end
end
end
5 changes: 5 additions & 0 deletions spec/approvals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def filter(data)
Approvals.verify json, :format => :json, :namer => namer
end

it "verifies json where the pretty generation is different but the content is the same" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add some tests how diffs show when you changed something, and the pretty generation is different of the approved and received file.

Copy link
Contributor Author

@Goltergaul Goltergaul Dec 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as i saw the diff is generated by rspec by just diffing what is written in approved.json and received.json Not sure how i could test that, as that is simply read from those files by the ApprovalError class. Since the contents of those files still is different because of the differences in the JSON gem, this will still show whitespace differences. But it also will show those when you check a changed approval into git that was generated on a different ruby.

I guess there is no sane workaround from that but using a pure ruby json generator. But i did not find any good one.

hash = { foo: {} }

Approvals.verify hash, :format => :json, :namer => namer
end

it "verifies json and is newline agnostic" do
json = '{"pet":{"species":"turtle","color":"green","name":"Anthony"}}'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"foo": {


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "2017-01-01 00:00:00 +0100"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"foo": {



}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bar": 1
}
33 changes: 33 additions & 0 deletions spec/verifiers/json_verifier_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

describe Approvals::Verifiers::JsonVerifier do
subject(:instance) do
described_class.new(received_path, approved_path)
end

context "with same json content but different formatting" do
let(:received_path) do
"./spec/fixtures/json_approval_with_different_whitespace/received.json"
end
let(:approved_path) do
"./spec/fixtures/json_approval_with_different_whitespace/approved.json"
end

it "passes verification" do
expect(instance.verify).to be_truthy
end
end

context "with different json content" do
let(:received_path) do
"./spec/fixtures/json_approval_with_different_whitespace/received_different_content.json"
end
let(:approved_path) do
"./spec/fixtures/json_approval_with_different_whitespace/approved.json"
end

it "does not passe verification" do
expect(instance.verify).to be_falsy
end
end
end