Module: SimpleCov::CLI::DeadCode::Output

Extended by:
CommandHelpers, Output
Included in:
Output
Defined in:
lib/simplecov/cli/dead_code/output.rb

Constant Summary

Constants included from CommandHelpers

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

#count(matrix, bucket) ⇒ Object



66
67
68
# File 'lib/simplecov/cli/dead_code/output.rb', line 66

def count(matrix, bucket)
  matrix.fetch(bucket).sum { |_file, lines| lines.size }
end

#emit(stdout, opts, matrix, production) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/simplecov/cli/dead_code/output.rb', line 13

def emit(stdout, opts, matrix, production)
  return stdout.puts(JSON.generate(json_payload(matrix, production))) if opts.fetch(:json)

  stdout.puts("Production coverage: #{opts.fetch(:production)}#{window_suffix(production)}")
  stdout.puts
  last_seen = production.fetch("last_seen")
  opts.fetch(:untested) ? emit_untested(stdout, matrix, last_seen) : emit_dead(stdout, matrix, last_seen)
end

#emit_dead(stdout, matrix, last_seen) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simplecov/cli/dead_code/output.rb', line 22

def emit_dead(stdout, matrix, last_seen)
  rows = section(stdout, "Dead code (not run in production, not covered by tests):", matrix, :dead,
    last_seen)
  rows += section(stdout, "Possibly dead (not run in production, covered only by tests):", matrix,
    :possibly_dead, last_seen)
  return stdout.puts("No dead code found.") if rows.zero?

  dead = count(matrix, :dead)
  possibly = count(matrix, :possibly_dead)
  stdout.puts("#{pluralize(dead, "dead line")}, #{pluralize(possibly, "possibly dead line")}")
end

#emit_untested(stdout, matrix, last_seen) ⇒ Object



34
35
36
37
38
39
# File 'lib/simplecov/cli/dead_code/output.rb', line 34

def emit_untested(stdout, matrix, last_seen)
  rows = section(stdout, "Untested code running in production:", matrix, :untested_in_production, last_seen)
  return stdout.puts("No untested production code found.") if rows.zero?

  stdout.puts("#{pluralize(count(matrix, :untested_in_production), "untested line")} running in production")
end

#json_entry(file, lines, last_seen) ⇒ Object

JSON carries the full stamp where the text views print its date, and omits the key for a file the window never saw.



91
92
93
94
95
96
# File 'lib/simplecov/cli/dead_code/output.rb', line 91

def json_entry(file, lines, last_seen)
  entry = {file: file, lines: lines} #: Hash[Symbol, untyped]
  stamp = last_seen[file]
  entry[:last_seen] = stamp if stamp.instance_of?(String)
  entry
end

#json_payload(matrix, production) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/simplecov/cli/dead_code/output.rb', line 80

def json_payload(matrix, production)
  last_seen = production.fetch("last_seen")
  payload = {window: {started_at: production["started_at"], updated_at: production["updated_at"]}}
  %i[dead possibly_dead untested_in_production].each do |bucket|
    payload[bucket] = matrix.fetch(bucket).sort.map { |file, lines| json_entry(file, lines, last_seen) }
  end
  payload
end

#markers(matrix, file, last_seen) ⇒ Object

The row's parenthesized evidence. "entire file" says every relevant line skipped production; "last run" dates the store's last sighting of the file, which for a dead or possibly dead row means other lines of the file ran then, and its absence means the window never saw the file at all. Both can hold at once.



58
59
60
61
62
63
64
# File 'lib/simplecov/cli/dead_code/output.rb', line 58

def markers(matrix, file, last_seen)
  markers = [] #: Array[String]
  markers << "entire file" if matrix.fetch(:entire).include?(file)
  stamp = last_seen[file]
  markers << "last run #{stamp[0, 10]}" if stamp.instance_of?(String)
  " (#{markers.join(", ")})" unless markers.empty?
end

#pluralize(number, noun) ⇒ Object



70
71
72
# File 'lib/simplecov/cli/dead_code/output.rb', line 70

def pluralize(number, noun)
  "#{number} #{noun}#{"s" unless one?(number)}"
end

#section(stdout, heading, matrix, bucket, last_seen) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/simplecov/cli/dead_code/output.rb', line 41

def section(stdout, heading, matrix, bucket, last_seen)
  rows = matrix.fetch(bucket).sort
  return 0 if rows.empty?

  stdout.puts(heading)
  rows.each do |file, lines|
    stdout.puts("  #{file}:#{Patch::Output.ranges(lines, ",")}#{markers(matrix, file, last_seen)}")
  end
  stdout.puts
  rows.size
end

#window_suffix(production) ⇒ Object



74
75
76
77
78
# File 'lib/simplecov/cli/dead_code/output.rb', line 74

def window_suffix(production)
  started = production["started_at"]
  updated = production["updated_at"]
  " (window #{started} to #{updated})" if started && updated
end