Skip to content
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
22 changes: 16 additions & 6 deletions lib/capybara_screenshot_diff/reporters/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ def output_path
def passed = total - failures.size
def failed = failures.size

def success_rate
return 0 if total.zero?
(passed.to_f / total * 100).round(1)
def summary
return if total.zero?

screenshots_label = (total == 1) ? "1 screenshot" : "#{total} screenshots"

if failures.empty?
"[snap_diff] #{screenshots_label} compared, no failures."
else
failures_label = (failures.size == 1) ? "1 failure" : "#{failures.size} failures"
"[snap_diff] #{screenshots_label} compared, #{failures_label}. Report: #{output_path}"
end
end

def render
Expand Down Expand Up @@ -130,9 +138,11 @@ def write_report

at_exit do
CapybaraScreenshotDiff.reporters_mutex.synchronize { CapybaraScreenshotDiff.reporters.dup }.each do |reporter|
result = reporter.finalize
$stdout.puts "[snap_diff] HTML report: #{result}" if result.is_a?(Pathname)
reporter.finalize
if (msg = reporter.summary)
$stdout.puts msg
end
rescue => e
warn "[snap_diff] Reporter #{reporter.class} failed (#{e.class}: #{e.message})" if ENV["DEBUG"]
warn "[snap_diff] Reporter #{reporter.class} failed (#{e.class}: #{e.message})"
end
end
57 changes: 57 additions & 0 deletions test/unit/reporters/html_reporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,63 @@ def synchronize
assert_operator fake_mutex.synchronize_calls, :>=, 1
end

test "#finalize returns output_path when there are failures" do
reporter = HTML.new(output_path: @output_path)
reporter.record([build_failing_assertion("fail")])
result = reporter.finalize

assert_instance_of Pathname, result
end

test "#finalize returns nil when no failures" do
reporter = HTML.new(output_path: @output_path)
reporter.record([build_passing_assertion("pass")])
result = reporter.finalize

assert_nil result
end

test "#summary returns screenshot count and status" do
reporter = HTML.new(output_path: @output_path)
reporter.record([build_passing_assertion("ok"), build_failing_assertion("fail")])
reporter.finalize

summary = reporter.summary
assert_includes summary, "1 failure"
assert_includes summary, "2 screenshots"
assert_includes summary, @output_path.to_s
end
Comment thread
sourcery-ai[bot] marked this conversation as resolved.

test "#summary pluralizes failures label for multiple failures" do
reporter = HTML.new(output_path: @output_path)
reporter.record([
build_failing_assertion("first failure"),
build_failing_assertion("second failure")
])
reporter.finalize

summary = reporter.summary
assert_includes summary, "2 failures"
assert_includes summary, "2 screenshots"
assert_includes summary, @output_path.to_s
end

test "#summary when all pass shows no failures" do
reporter = HTML.new(output_path: @output_path)
reporter.record([build_passing_assertion("ok")])
reporter.finalize

summary = reporter.summary
assert_includes summary, "1 screenshot"
assert_includes summary, "no failures"
refute_includes summary, @output_path.to_s
end

test "#summary when no screenshots recorded" do
reporter = HTML.new(output_path: @output_path)
assert_nil reporter.summary
end

test "#finalize can retry after write_report failure" do
reporter = HTML.new(output_path: @output_path)
reporter.record([build_failing_assertion("retry")])
Expand Down
Loading