Skip to content
Open
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
36 changes: 32 additions & 4 deletions lib/approvals/approval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,35 @@ def verify
fail_with "Approval file \"#{approved_path}\" not found."
end

@approved_content, @received_content = read_content

unless received_matches?
fail_with "Received file \"#{received_path}\" does not match approved \"#{approved_path}\"."
fail_with "Received file does not match approved:\n"+
"#{received_path}\n#{approved_path}\n#{diff_preview}"
end

success!
end

def diff_preview
approved, received = diff_lines
return unless approved and received
diff_index =
approved.each_char.with_index.find_index do |approved_char, i|
approved_char != received[i]
end || approved.length
"approved fragment: #{approved[diff_index - 10 .. diff_index + 30]}\n"+
"received fragment: #{received[diff_index - 10 .. diff_index + 30]}"
end

def diff_lines
approved = @approved_content.split("\n")
received = @received_content.split("\n")
approved.each_with_index do |line, i|
return line, received[i] unless line == received[i]
end
end

def success!
File.delete received_path
end
Expand All @@ -62,14 +84,20 @@ def approved?

BINARY_FORMATS = [:binary]

def received_matches?
def read_content
if BINARY_FORMATS.include?(@format) # Read without ERB
IO.read(received_path).chomp == IO.read(approved_path).chomp
[IO.read(approved_path).chomp,
IO.read(received_path).chomp]
else
ERB.new(IO.read(received_path).chomp).result == ERB.new(IO.read(approved_path).chomp).result
[ERB.new(IO.read(approved_path).chomp).result,
ERB.new(IO.read(received_path).chomp).result]
end
end

def received_matches?
@approved_content == @received_content
end

def fail_with(message)
Dotfile.append(diff_path)

Expand Down