Class: Reviewer::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/reviewer/report.rb,
lib/reviewer/report/formatter.rb

Overview

Collects results from multiple tool runs and provides serialization

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

SCHEMA_VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReport

Returns a new instance of Report.



41
42
43
44
# File 'lib/reviewer/report.rb', line 41

def initialize
  @results = []
  @duration = nil
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



39
40
41
# File 'lib/reviewer/report.rb', line 39

def duration
  @duration
end

#resultsObject (readonly)

Returns the value of attribute results.



39
40
41
# File 'lib/reviewer/report.rb', line 39

def results
  @results
end

Class Method Details

.empty(message: 'No tools ran') ⇒ Hash

Builds an empty report envelope with a context-specific message.

Parameters:

  • message (String) (defaults to: 'No tools ran')

    why no tool results are present

Returns:

  • (Hash)

    a schema-versioned empty envelope



28
29
30
31
32
33
34
35
36
37
# File 'lib/reviewer/report.rb', line 28

def self.empty(message: 'No tools ran')
  {
    schema_version: SCHEMA_VERSION,
    state: 'empty',
    success: true,
    message: message,
    summary: empty_summary,
    tools: []
  }
end

.empty_summaryHash

Builds the zero-valued summary shared by empty and error envelopes.

Returns:

  • (Hash)

    all result-state counts and duration set to zero



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/reviewer/report.rb', line 13

def self.empty_summary
  {
    total: 0,
    passed: 0,
    failed: 0,
    skipped: 0,
    missing: 0,
    not_run: 0,
    duration: 0
  }
end

Instance Method Details

#add(result) ⇒ Array<Runner::Result>

Adds a Runner::Result to the collection

Parameters:

Returns:



50
51
52
# File 'lib/reviewer/report.rb', line 50

def add(result)
  @results << result
end

#exit_codeInteger

The process exit code, derived from the verdict rather than from whatever a tool returned. Forwarding a tool's status meant a tool passing under its max_exit_status still exited non-zero, and a tool exiting 2 -- GNU ls does, on an unknown option -- was indistinguishable from the usage errors Session::USAGE_ERROR reports.

Returns:

  • (Integer)

    0 when the run passed, 1 when it did not



76
# File 'lib/reviewer/report.rb', line 76

def exit_code = success? ? 0 : 1

#missing?Boolean

Whether any tools were missing

Returns:

  • (Boolean)

    true if any results are missing



88
89
90
# File 'lib/reviewer/report.rb', line 88

def missing?
  missing_results.any?
end

#missing_resultsArray<Runner::Result>

Returns results for tools whose executables were not found

Returns:



81
82
83
# File 'lib/reviewer/report.rb', line 81

def missing_results
  results.select(&:missing?)
end

#missing_toolsArray<Runner::Result>

Returns data for missing tools (name and key)

Returns:



95
96
97
# File 'lib/reviewer/report.rb', line 95

def missing_tools
  missing_results
end

#record_duration(seconds) ⇒ Float

Records the total duration for all tool runs

Parameters:

  • seconds (Float)

    the total elapsed time in seconds

Returns:

  • (Float)

    the recorded duration



58
59
60
# File 'lib/reviewer/report.rb', line 58

def record_duration(seconds)
  @duration = seconds
end

#success?Boolean

Whether all executed tools in the report succeeded

Returns:

  • (Boolean)

    true if all executed results are successful



65
66
67
# File 'lib/reviewer/report.rb', line 65

def success?
  executed_results.all?(&:success?)
end

#summaryHash

Counts each result by its explicit state.

Returns:

  • (Hash)

    result counts and total duration



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/reviewer/report.rb', line 101

def summary
  {
    total: results.size,
    passed: results.count(&:passed?),
    failed: results.count(&:failed?),
    skipped: results.count(&:skipped?),
    missing: results.count(&:missing?),
    not_run: results.count(&:not_run?),
    duration: duration
  }
end

#to_h(empty_message: 'No tools ran') ⇒ Hash

Converts the report to a hash suitable for serialization

Parameters:

  • empty_message (String) (defaults to: 'No tools ran')

    message used when no results are present

Returns:

  • (Hash)

    structured hash with summary and tool results



117
118
119
120
121
122
123
124
125
126
# File 'lib/reviewer/report.rb', line 117

def to_h(empty_message: 'No tools ran')
  return self.class.empty(message: empty_message) if results.empty?

  {
    schema_version: SCHEMA_VERSION,
    success: success?,
    summary: summary,
    tools: results.map(&:to_h)
  }
end

#to_json(*_args) ⇒ String

Converts the report to formatted JSON

Returns:

  • (String)

    JSON representation of the report



131
132
133
# File 'lib/reviewer/report.rb', line 131

def to_json(*_args)
  JSON.pretty_generate(to_h)
end