Class: Guardrails::VisualDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/guardrails/visual_diff.rb,
lib/guardrails/visual_diff/snap_diff.rb

Overview

Consumes screenshot-diff tool output and folds findings into the unified audit report. Same playbook as ‘A11yDeep` for axe — parser only, no Capybara / Chromium / Playwright runtime deps. Users keep their existing visual-regression toolchain; Guardrails provides the merge + report + exit-code contract.

Adapter selection comes from ‘Guardrails.configuration.visual_diff.adapter` (default `:snap_diff`, the Rails-native baselines-in-git workflow). BackstopJS support tracked in issue #15.

Defined Under Namespace

Classes: Finding, SnapDiff

Instance Method Summary collapse

Constructor Details

#initialize(root:, output: $stdout, adapter: nil, threshold: nil) ⇒ VisualDiff

Returns a new instance of VisualDiff.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/guardrails/visual_diff.rb', line 38

def initialize(root:, output: $stdout,
               adapter: nil, threshold: nil)
  @root = Pathname(root)
  @output = output
  cfg = Guardrails.configuration.visual_diff
  # Coerce constructor inputs the same way Configuration setters
  # do, so `VisualDiff.new(adapter: "snap_diff", threshold: "0.1")`
  # works the same as `Guardrails.configure { |c| c.visual_diff.
  # adapter = "snap_diff"; c.visual_diff.threshold = "0.1" }`.
  @adapter_name = coerce_adapter(adapter) || cfg.adapter
  @threshold = threshold.nil? ? cfg.threshold : Float(threshold)
end

Instance Method Details

#any_failing?(findings) ⇒ Boolean

Returns true when any finding’s mismatch_ratio exceeds the threshold. Adapters that don’t emit a ratio (snap_diff: presence of a .diff.png is binary) report nil, which we treat as “unconditional fail” — those findings always fail.

Returns:

  • (Boolean)


61
62
63
# File 'lib/guardrails/visual_diff.rb', line 61

def any_failing?(findings)
  findings.any? { |f| failing?(f) }
end

#collectObject



65
66
67
# File 'lib/guardrails/visual_diff.rb', line 65

def collect
  adapter.collect
end

#runObject



51
52
53
54
55
# File 'lib/guardrails/visual_diff.rb', line 51

def run
  findings = collect
  print_report(findings)
  findings
end