Class: SnapDiff::Reporters::HTML

Inherits:
Object
  • Object
show all
Defined in:
lib/snap_diff/reporters/html.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_path: nil, embed_images: false) ⇒ HTML

Returns a new instance of HTML.



18
19
20
21
22
23
24
25
# File 'lib/snap_diff/reporters/html.rb', line 18

def initialize(output_path: nil, embed_images: false)
  @explicit_output_path = output_path
  @embed_images = embed_images
  @failures = []
  @total = 0
  @finalized = false
  @mutex = Mutex.new
end

Instance Attribute Details

#failuresObject (readonly)

Returns the value of attribute failures.



16
17
18
# File 'lib/snap_diff/reporters/html.rb', line 16

def failures
  @failures
end

#totalObject (readonly)

Returns the value of attribute total.



16
17
18
# File 'lib/snap_diff/reporters/html.rb', line 16

def total
  @total
end

Class Method Details

.default_output_pathObject



107
108
109
110
# File 'lib/snap_diff/reporters/html.rb', line 107

def self.default_output_path
  root = SnapDiff.config.root || Pathname.pwd
  root / SnapDiff.config.save_path / "snap_diff_report.html"
end

.template_pathObject



103
104
105
# File 'lib/snap_diff/reporters/html.rb', line 103

def self.template_path
  File.expand_path("templates/report.html.erb", __dir__)
end

Instance Method Details

#dump_stateObject

Fork-parallel handoff (issue #258). A forked worker never reaches Minitest.after_run, so it hands its records to the parent as plain data instead. JSON on purpose -- boring, and a half-written file is discarded rather than half-read. Image paths were resolved against output_path, which is the same in both processes.



72
73
74
# File 'lib/snap_diff/reporters/html.rb', line 72

def dump_state
  @mutex.synchronize { {"total" => @total, "failures" => @failures} }
end

#failedObject



84
# File 'lib/snap_diff/reporters/html.rb', line 84

def failed = failures.size

#finalizeObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/snap_diff/reporters/html.rb', line 52

def finalize
  @mutex.synchronize do
    return if @finalized
    return if failures.empty?

    write_report
    @finalized = true
    output_path
  end
end

#merge_state!(state) ⇒ Object



76
77
78
79
80
81
# File 'lib/snap_diff/reporters/html.rb', line 76

def merge_state!(state)
  @mutex.synchronize do
    @total += state["total"]
    @failures.concat(state["failures"].map { |entry| entry.transform_keys(&:to_sym) })
  end
end

#output_pathObject



63
64
65
# File 'lib/snap_diff/reporters/html.rb', line 63

def output_path
  @output_path ||= Pathname.new(@explicit_output_path || self.class.default_output_path)
end

#passedObject



83
# File 'lib/snap_diff/reporters/html.rb', line 83

def passed = total - failures.size

#record(assertions) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/snap_diff/reporters/html.rb', line 27

def record(assertions)
  return if @finalized

  failures = []
  total = 0

  assertions.each do |assertion|
    compare = assertion.compare
    next unless compare

    total += 1
    next unless compare.difference&.different?

    failures << failure_entry_for(assertion.name, compare)
  rescue => e
    warn "[snap_diff] Reporter skipped '#{assertion.name}': #{e.message}" if ENV["DEBUG"]
  end

  @mutex.synchronize do
    return if @finalized
    @total += total
    @failures.concat(failures)
  end
end

#renderObject



99
100
101
# File 'lib/snap_diff/reporters/html.rb', line 99

def render
  ERB.new(File.read(self.class.template_path)).result(binding)
end

#summaryString?

Both customer personas named this path as the best output in the product, so it gets its own line -- but only when a report was actually written.

The counts this used to carry moved to SnapDiff::Reporting (issue #269): they are printed for every user, and this file is not. See Reporting.counts_summary.

Returns:

  • (String, nil)

    nil when no report was written



95
96
97
# File 'lib/snap_diff/reporters/html.rb', line 95

def summary
  "[snap_diff] Report: #{output_path}" if @finalized
end