Module: SimpleCov::CLI::CoverageFile

Defined in:
lib/simplecov/cli/coverage_file.rb

Overview

Shared input boundary for commands that consume JSONFormatter's coverage.json. It validates only the stable outer shape so older schema versions remain readable.

Constant Summary collapse

CRITERIA =

The coverage.json fields backing each criterion, keyed by the criterion's singular name. The one table the coverage, uncovered, and diff subcommands all read from, so a schema change lands in exactly one place.

{
  line: {label: "Line", percent: "lines_covered_percent",
         covered: "covered_lines", total: "total_lines"},
  branch: {label: "Branch", percent: "branches_covered_percent",
           covered: "covered_branches", total: "total_branches"},
  method: {label: "Method", percent: "methods_covered_percent",
           covered: "covered_methods", total: "total_methods"}
}.freeze

Class Method Summary collapse

Class Method Details

.load_coverage(path, command:, stderr:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/simplecov/cli/coverage_file.rb', line 36

def load_coverage(path, command:, stderr:)
  document = load_document(path, command: command, stderr: stderr)
  return unless document

  none = {} #: Hash[String, untyped]
  coverage = document.fetch("coverage", none)
  return coverage if coverage.is_a?(Hash)

  report_invalid(stderr, command, path, '"coverage" must be an object')
end

.load_document(path, command:, stderr:) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/simplecov/cli/coverage_file.rb', line 26

def load_document(path, command:, stderr:)
  SimpleCov::CoverageJSON.load(path)
rescue Errno::ENOENT
  report_missing(stderr, command, path)
rescue SimpleCov::CoverageJSON::Error => e
  report_invalid(stderr, command, path, e.message)
rescue SystemCallError => e
  report_unreadable(stderr, command, path, e.message)
end

.report_invalid(stderr, command, path, reason) ⇒ Object



47
48
49
50
51
# File 'lib/simplecov/cli/coverage_file.rb', line 47

def report_invalid(stderr, command, path, reason)
  detail = reason.lines.first.to_s.strip
  stderr.puts("simplecov #{command}: input file #{path.inspect} isn't valid JSON (#{detail})")
  nil
end

.report_missing(stderr, command, path) ⇒ Object



53
54
55
56
# File 'lib/simplecov/cli/coverage_file.rb', line 53

def report_missing(stderr, command, path)
  stderr.puts("simplecov #{command}: #{path} not found")
  nil
end

.report_unreadable(stderr, command, path, reason) ⇒ Object



58
59
60
61
62
# File 'lib/simplecov/cli/coverage_file.rb', line 58

def report_unreadable(stderr, command, path, reason)
  detail = reason.lines.first.to_s.strip
  stderr.puts("simplecov #{command}: cannot read #{path.inspect} (#{detail})")
  nil
end