Class: RailVerdict::Analyzers::SimpleCov

Inherits:
Object
  • Object
show all
Defined in:
lib/rail_verdict/analyzers/simplecov.rb

Defined Under Namespace

Classes: MalformedOutput, Probe

Constant Summary collapse

ANALYZER_ID =
"simplecov"
DEFAULT_COVERAGE_PATH =
"coverage/coverage.json"
DEFAULT_FRESHNESS_WINDOW_SECONDS =
86_400
SUPPORTED_VERSION_RANGE =
Gem::Requirement.new(">= 1", "< 2")

Instance Method Summary collapse

Instance Method Details

#probe(repository_root, runner: nil, timeout_seconds: 5.0) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rail_verdict/analyzers/simplecov.rb', line 20

def probe(repository_root, runner: nil, timeout_seconds: 5.0)
  config = load_config(repository_root)
  raw_path = config.fetch("coverage_path") { DEFAULT_COVERAGE_PATH }
  full_path = resolve_contained_coverage_path(repository_root, raw_path)
  return Probe.new(status: "unavailable", message: "coverage file is absent: #{raw_path}") unless File.file?(full_path)

  text = File.binread(full_path).force_encoding(Encoding::UTF_8)
  return Probe.new(status: "parse_failed", message: "coverage file is not valid UTF-8") unless text.valid_encoding?

  document = JSON.parse(text)
  detected = detect_format(document)
  case detected
  when :coverage_v1
    version = document["version"].to_s
    v = Gem::Version.new(version) rescue nil
    if v.nil? || !SUPPORTED_VERSION_RANGE.satisfied_by?(v)
      return Probe.new(status: "unsupported", version: version, message: "unsupported SimpleCov version #{version}")
    end
    Probe.new(status: "succeeded", version: version)
  when :native_simplecov
    version = extract_native_version(document)
    return Probe.new(status: "unsupported", message: "native SimpleCov version could not be determined") unless version
    v = Gem::Version.new(version) rescue nil
    if v.nil? || !SUPPORTED_VERSION_RANGE.satisfied_by?(v)
      return Probe.new(status: "unsupported", version: version, message: "unsupported SimpleCov version #{version}")
    end
    Probe.new(status: "succeeded", version: version)
  when :unsupported
    Probe.new(status: "unsupported", message: "unsupported coverage document shape")
  else
    Probe.new(status: "malformed", message: "coverage document is malformed")
  end
rescue JSON::ParserError => error
  Probe.new(status: "parse_failed", message: Shared.bounded_message(error.message))
rescue MalformedOutput => error
  Probe.new(status: "malformed", message: Shared.bounded_message(error.message))
rescue SystemCallError, ArgumentError => error
  Probe.new(status: "unavailable", message: Shared.bounded_message(error.message))
end

#run(repository_root, runner: nil, timeout_seconds: 10.0, probe_result: nil, configuration: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rail_verdict/analyzers/simplecov.rb', line 60

def run(repository_root, runner: nil, timeout_seconds: 10.0, probe_result: nil, configuration: nil)
  config = load_config(repository_root, configuration: configuration)
  raw_path = config.fetch("coverage_path") { DEFAULT_COVERAGE_PATH }
  freshness_window = config.fetch("freshness_window_seconds") { DEFAULT_FRESHNESS_WINDOW_SECONDS }
  invocation = { "executable" => "simplecov", "argv" => ["read", raw_path] }

  begin
    full_path = resolve_contained_coverage_path(repository_root, raw_path)
  rescue MalformedOutput => error
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "malformed", message: Shared.bounded_message(error.message)), []]
  end

  unless File.file?(full_path)
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "unavailable", message: "coverage file is absent: #{raw_path}"), []]
  end

  begin
    bytes = File.binread(full_path)
  rescue SystemCallError => error
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "unavailable", message: Shared.bounded_message(error.message)), []]
  end

  if bytes.bytesize > 8 * 1024 * 1024
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "truncated", message: "coverage file exceeds 8 MiB"), []]
  end

  text = bytes.dup.force_encoding(Encoding::UTF_8)
  unless text.valid_encoding?
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "parse_failed", message: "coverage file is not valid UTF-8"), []]
  end

  begin
    document = JSON.parse(text)
  rescue JSON::ParserError => error
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "parse_failed", message: Shared.bounded_message(error.message)), []]
  end

  detected = detect_format(document)
  if detected == :unsupported
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "unsupported", message: "unsupported coverage document shape", tool_version: probe_result&.version), []]
  elsif detected == :malformed
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "malformed", message: "coverage document is malformed", tool_version: probe_result&.version), []]
  end

  normalized = nil
  version = nil
  if detected == :coverage_v1
    version = document["version"].to_s
    v = Gem::Version.new(version) rescue nil
    if v.nil? || !SUPPORTED_VERSION_RANGE.satisfied_by?(v)
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "unsupported", message: "unsupported SimpleCov version #{version}", tool_version: version), []]
    end
    errors = validate_coverage_schema(document)
    unless errors.empty?
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "malformed", message: errors.first, tool_version: version), []]
    end
    normalized = normalize_coverage_v1(document)
  elsif detected == :native_simplecov
    version = extract_native_version(document)
    unless version
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "unsupported", message: "native SimpleCov version could not be determined", tool_version: nil), []]
    end
    v = Gem::Version.new(version) rescue nil
    if v.nil? || !SUPPORTED_VERSION_RANGE.satisfied_by?(v)
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "unsupported", message: "unsupported SimpleCov version #{version}", tool_version: version), []]
    end
    begin
      normalized = normalize_native_document(document, repository_root)
    rescue MalformedOutput => error
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "malformed", message: Shared.bounded_message(error.message), tool_version: version), []]
    end
    errors = validate_coverage_schema(normalized)
    unless errors.empty?
      return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "malformed", message: errors.first, tool_version: version), []]
    end
  else
    return [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: invocation, status: "malformed", message: "coverage document shape is not supported", tool_version: probe_result&.version), []]
  end

  mtime = File.mtime(full_path)
  stale = (Time.now - mtime) > freshness_window

  covered, executable = coverage_totals(normalized)

  summary = {
    "covered_lines" => covered,
    "executable_lines" => executable,
    "percent" => executable == 0 ? 100.0 : ((covered.to_f / executable) * 100).round(2),
    "stale" => stale,
    "freshness_window_seconds" => Integer(freshness_window),
    "coverage_path" => raw_path.to_s[0, 512],
    "files" => normalized["files"],
    "_coverage_document" => normalized
  }

  result = AnalyzerResult.new(
    analyzer: ANALYZER_ID,
    tool_version: version,
    invocation: invocation,
    execution_status: "succeeded",
    finding_ids: [],
    evidence_summary: summary
  )

  [result, []]
rescue StandardError => error
  [Shared.failure_result(analyzer_id: ANALYZER_ID, invocation: { "executable" => "simplecov", "argv" => ["read", DEFAULT_COVERAGE_PATH] }, status: "malformed", message: Shared.bounded_message(error.message)), []]
end