Module: SimpleCov::CLI::Report
- Defined in:
- lib/simplecov/cli/report.rb
Overview
‘simplecov report [–input PATH]` — pretty-print the overall totals row plus per-group totals from a JSONFormatter coverage.json. Same numbers as the HTML report’s totals row, for contexts where opening a browser isn’t an option (CI logs, ssh sessions, terminal-only workflows).
Constant Summary collapse
- SECTIONS =
[%w[Line lines], %w[Branch branches], %w[Method methods]].freeze
Class Method Summary collapse
- .collect_section(totals) ⇒ Object
- .emit_json(stdout, data) ⇒ Object
- .emit_section(stdout, display, section, color) ⇒ Object
- .emit_text(stdout, data, color) ⇒ Object
- .emit_totals(stdout, label, totals, color) ⇒ Object
- .load_data(input, stderr) ⇒ Object
- .parse(args) ⇒ Object
- .run(args, stdout:, stderr:) ⇒ Object
Class Method Details
.collect_section(totals) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/simplecov/cli/report.rb', line 74 def collect_section(totals) SECTIONS.each_with_object({}) do |(_, key), out| section = totals[key] next unless section.is_a?(Hash) && section["total"].to_i.positive? out[key] = {"percent" => section["percent"], "covered" => section["covered"], "total" => section["total"]} end end |
.emit_json(stdout, data) ⇒ Object
68 69 70 71 72 |
# File 'lib/simplecov/cli/report.rb', line 68 def emit_json(stdout, data) payload = {"All Files" => collect_section(data.fetch("total", {}))} data.fetch("groups", {}).each { |name, group| payload[name] = collect_section(group) } stdout.puts(JSON.pretty_generate(payload)) end |
.emit_section(stdout, display, section, color) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/simplecov/cli/report.rb', line 58 def emit_section(stdout, display, section, color) return unless section.is_a?(Hash) && section["total"].to_i.positive? stdout.puts(format(" %<label>-7s %<pct>s (%<covered>d / %<total>d)", label: "#{display}:", pct: SimpleCov::Color.colorize_percent(section["percent"].to_f, enabled: color), covered: section["covered"] || 0, total: section["total"] || 0)) end |
.emit_text(stdout, data, color) ⇒ Object
47 48 49 50 |
# File 'lib/simplecov/cli/report.rb', line 47 def emit_text(stdout, data, color) emit_totals(stdout, "All Files", data.fetch("total", {}), color) data.fetch("groups", {}).each { |name, group| emit_totals(stdout, name, group, color) } end |
.emit_totals(stdout, label, totals, color) ⇒ Object
52 53 54 55 56 |
# File 'lib/simplecov/cli/report.rb', line 52 def emit_totals(stdout, label, totals, color) stdout.puts(label) SECTIONS.each { |display, key| emit_section(stdout, display, totals[key], color) } stdout.puts end |
.load_data(input, stderr) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/simplecov/cli/report.rb', line 40 def load_data(input, stderr) return JSON.parse(File.read(input)) if File.exist?(input) stderr.puts("simplecov report: #{input} not found") nil end |
.parse(args) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/simplecov/cli/report.rb', line 30 def parse(args) opts = {input: SimpleCov::CLI.default_input, json: false, no_color: false} OptionParser.new do |o| o.on("--input PATH") { |v| opts[:input] = v } o.on("--json") { opts[:json] = true } o.on("--no-color") { opts[:no_color] = true } end.parse(args) opts end |
.run(args, stdout:, stderr:) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/simplecov/cli/report.rb', line 18 def run(args, stdout:, stderr:) opts = parse(args) return 1 unless (data = load_data(opts[:input], stderr)) if opts[:json] emit_json(stdout, data) else emit_text(stdout, data, SimpleCov::CLI.color_enabled?(opts, stdout)) end 0 end |