Module: SimpleCov::CLI::Coverage

Extended by:
CommandHelpers, Coverage
Included in:
Coverage
Defined in:
lib/simplecov/cli/coverage.rb

Constant Summary

Constants included from CommandHelpers

SimpleCov::CLI::CommandHelpers::STATS_ROW_FORMAT

Instance Method Summary collapse

Methods included from CommandHelpers

build_parser, command_name, common_options, error, error_nil, on_help, one?, parse_common, quiet_option, recorded_contexts, stats_row

Instance Method Details

#emit(match, opts, stdout) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/simplecov/cli/coverage.rb', line 57

def emit(match, opts, stdout)
  filename, payload = match
  if opts.fetch(:json)
    entry = {filename => payload} #: Hash[untyped, untyped]
    stdout.puts(JSON.pretty_generate(entry))
  else
    print_human(filename, payload, stdout, CLI.color_enabled?(opts, stdout))
  end
end

#emit_criterion(stdout, payload, criterion, color) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/simplecov/cli/coverage.rb', line 72

def emit_criterion(stdout, payload, criterion, color)
  return unless payload.key?(criterion.fetch(:percent))

  pct = payload.fetch(criterion.fetch(:percent)).to_f
  stdout.puts(stats_row(criterion.fetch(:label),
    Color.colorize_percent(pct, enabled: color),
    payload[criterion.fetch(:covered)],
    payload[criterion.fetch(:total)]))
end

#locate_match(opts, stderr) ⇒ Object



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

def locate_match(opts, stderr)
  coverage = CoverageFile.load_coverage(opts.fetch(:input), command: "coverage", stderr: stderr)
  return unless coverage

  match = lookup(coverage, opts.fetch(:path))
  return report_not_found(stderr, coverage, opts) if match.nil?
  return match if match.last.instance_of?(Hash)

  CoverageFile.report_invalid(stderr, "coverage", opts.fetch(:input),
    "entry for #{opts.fetch(:path)} must be an object")
end

#lookup(coverage_hash, path) ⇒ Object



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

def lookup(coverage_hash, path)
  CoverageFile.lookup(coverage_hash, path)
end

#parse(args, stderr:) ⇒ Object



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

def parse(args, stderr:)
  opts, rest = parse_common(args)
  if rest.empty?
    stderr.puts("simplecov coverage: missing file argument")
    return nil
  end

  opts[:path] = rest.first
  opts
end


67
68
69
70
# File 'lib/simplecov/cli/coverage.rb', line 67

def print_human(filename, payload, stdout, color)
  stdout.puts(filename)
  CoverageFile::CRITERIA.each_value { |c| emit_criterion(stdout, payload, c, color) }
end

#report_not_found(stderr, coverage, opts) ⇒ Object



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

def report_not_found(stderr, coverage, opts)
  message = CoverageFile.not_found_message(coverage, opts.fetch(:path), opts.fetch(:input))
  stderr.puts("simplecov coverage: #{message}")
end

#run(args, stdout:, stderr:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/simplecov/cli/coverage.rb', line 14

def run(args, stdout:, stderr:)
  opts = parse(args, stderr: stderr)
  return 1 unless opts

  match = locate_match(opts, stderr)
  return 1 unless match

  emit(match, opts, stdout)
  0
end