Class: SimpleCov::Formatter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov/formatter/base.rb,
sig/simplecov.rbs

Overview

report to an output directory and emit the "Coverage report generated for X to Y" status line on stderr. Subclasses implement format.

Direct Known Subclasses

HTMLFormatter, JSONFormatter

Instance Method Summary collapse

Constructor Details

#initialize(silent: false, output_dir: nil) ⇒ Base

output_dir defaults to SimpleCov.coverage_path when nil.

Parameters:

  • silent: (Boolean) (defaults to: false)
  • output_dir: (String, nil) (defaults to: nil)


19
20
21
22
# File 'lib/simplecov/formatter/base.rb', line 19

def initialize(silent: false, output_dir: nil)
  @silent = silent
  @output_dir = output_dir
end

Instance Method Details

#displayable_output_pathString

Directory (relative to cwd when inside it) plus the subclass's entry_point_filename, for the status line. See issue #197.

Returns:

  • (String)


46
47
48
49
50
# File 'lib/simplecov/formatter/base.rb', line 46

def displayable_output_path
  directory = relative_or_absolute_output_path
  entry_point = entry_point_filename
  entry_point ? File.join(directory, entry_point) : directory
end

#entry_point_filenameString?

Subclasses name the report's entry-point file (index.html, coverage.json); nil leaves the bare directory in the status line.

Returns:

  • (String, nil)


68
69
70
# File 'lib/simplecov/formatter/base.rb', line 68

def entry_point_filename
  nil
end

#message_prefixString

Subclasses prepend a marker (e.g. "JSON ") to the summary line.

Returns:

  • (String)


29
30
31
# File 'lib/simplecov/formatter/base.rb', line 29

def message_prefix
  ""
end

#output_message(result) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Emit one summary line per criterion that the run actually measured. The header line ("Coverage report generated for X to Y") is always first; per-criterion lines follow in the order of result.coverage_statistics (which is the same insertion order as SourceFile#coverage_statistics, which in turn reflects what the user enabled).

Parameters:

Returns:

  • (String)


78
79
80
81
82
# File 'lib/simplecov/formatter/base.rb', line 78

def output_message(result)
  header = "#{message_prefix}Coverage report generated for #{result.command_name} to #{displayable_output_path}"
  body   = result.coverage_statistics.filter_map { |criterion, stat| stats_line(criterion, stat) }
  [header, *body].join("\n")
end

#output_pathString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


33
34
35
# File 'lib/simplecov/formatter/base.rb', line 33

def output_path
  @output_dir || SimpleCov.coverage_path
end

#relative_or_absolute_output_pathString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


52
53
54
55
56
57
58
59
60
61
# File 'lib/simplecov/formatter/base.rb', line 52

def relative_or_absolute_output_path
  absolute = output_path
  relative = Pathname.new(absolute).relative_path_from(Pathname.pwd).to_s
  relative.start_with?("..") ? absolute : relative
rescue ArgumentError
  # Pathname#relative_path_from raises across mixed absolute/
  # relative inputs (and across Windows drives) — keep the
  # absolute form on any unresolvable case.
  output_path
end

#stats_line(criterion, stat) ⇒ String?

nil for branch/method criteria with nothing to measure, so the summary doesn't print "Branch coverage: 0 / 0 (100.00%)" noise.

Parameters:

Returns:

  • (String, nil)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/simplecov/formatter/base.rb', line 88

def stats_line(criterion, stat)
  return if criterion != :line && !stat.total.positive?

  percent = SimpleCov.round_coverage(stat.percent)
  Kernel.format(
    "%<label>s coverage: %<covered>d / %<total>d (%<percent>s)",
    label: criterion.to_s.capitalize,
    covered: stat.covered,
    total: stat.total,
    percent: SimpleCov::Color.colorize_percent(percent)
  )
end