Class: Browsable::Replay

Inherits:
Object
  • Object
show all
Defined in:
lib/browsable/replay.rb

Overview

Rehydrates a JSON audit dump (the kind produced by ‘Browsable::TestReport#to_json`) into a Report-shaped object that any v0.1 formatter can render. Used by the `browsable replay` CLI to translate a test-suite report into a different format in CI without re-running tests.

Defined Under Namespace

Classes: Suggestion

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Replay

Returns a new instance of Replay.



21
22
23
# File 'lib/browsable/replay.rb', line 21

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/browsable/replay.rb', line 13

def data
  @data
end

Class Method Details

.from_file(path) ⇒ Object



15
16
17
18
19
# File 'lib/browsable/replay.rb', line 15

def self.from_file(path)
  raw = File.read(path)
  data = JSON.parse(raw)
  new(data)
end

Instance Method Details

#as_jsonObject



87
# File 'lib/browsable/replay.rb', line 87

def as_json = data

#config_fileObject



71
# File 'lib/browsable/replay.rb', line 71

def config_file = nil

#empty?Boolean

Returns:

  • (Boolean)


52
# File 'lib/browsable/replay.rb', line 52

def empty?   = findings.empty?

#errorsObject



49
# File 'lib/browsable/replay.rb', line 49

def errors   = findings.select(&:error?)

#exit_code(fail_on:) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/browsable/replay.rb', line 89

def exit_code(fail_on:)
  case fail_on.to_s
  when "warning" then errors.any? || warnings.any? ? 1 : 0
  when "error"   then errors.any? ? 1 : 0
  else                0
  end
end

#findingsObject

—- Report-compatible interface —————————————– Browsable::Formatters::* call these on the report they’re handed. We implement the same surface here so the same formatters work for replay.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/browsable/replay.rb', line 33

def findings
  @findings ||= Array(data["findings"]).map do |f|
    Finding.new(
      feature_id: f["feature_id"],
      feature_name: f["feature_name"],
      file: f["file"],
      line: (f["line"] || 1).to_i,
      column: (f["column"] || 1).to_i,
      required_browser_versions: f["required_browser_versions"] || {},
      target_browser_versions: f["target_browser_versions"] || {},
      severity: (f["severity"] || "warning").to_sym,
      message: f["message"].to_s
    )
  end
end

#findings_by_fileObject



54
55
56
57
58
59
60
# File 'lib/browsable/replay.rb', line 54

def findings_by_file
  findings
    .group_by(&:file)
    .sort_by(&:first)
    .to_h
    .transform_values { |g| g.sort_by { |f| [f.line, f.column] } }
end

#infosObject



51
# File 'lib/browsable/replay.rb', line 51

def infos    = findings.select(&:info?)

#notesObject



68
# File 'lib/browsable/replay.rb', line 68

def notes      = Array(data["notes"])

#policiesObject



69
# File 'lib/browsable/replay.rb', line 69

def policies   = []

#render(format: :human, io: $stdout) ⇒ Object



25
26
27
# File 'lib/browsable/replay.rb', line 25

def render(format: :human, io: $stdout)
  io.puts(formatter_for(format).new(self).render)
end

#rootObject



70
# File 'lib/browsable/replay.rb', line 70

def root       = data.dig("target", "root") || Dir.pwd

#skipsObject



62
63
64
65
66
# File 'lib/browsable/replay.rb', line 62

def skips
  Array(data["skips"]).map do |skip|
    Report::Skip.new(kind: skip["kind"].to_sym, reason: skip["reason"])
  end
end

#suggestionObject



80
81
82
83
84
85
# File 'lib/browsable/replay.rb', line 80

def suggestion
  s = data["suggested_policy"]
  return nil unless s

  Suggestion.new(line: s["line"].to_s, bumps: symbolize_bumps(s["bumps"]))
end

#targetObject



73
74
75
76
77
78
# File 'lib/browsable/replay.rb', line 73

def target
  tdata = data["target"]
  return nil unless tdata

  Target.new(tdata["query"] || "replay", resolved: tdata["browsers"])
end

#warningsObject



50
# File 'lib/browsable/replay.rb', line 50

def warnings = findings.select(&:warning?)