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)


62
63
64
65
66
# File 'lib/simplecov/formatter/base.rb', line 62

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

#emit_status(result) ⇒ void

This method returns an undefined value.

Prints output_message to stderr unless silent; the one home of the "status lines go to stderr, not through warn" decision (#1225).

Parameters:



33
34
35
36
37
38
39
40
# File 'lib/simplecov/formatter/base.rb', line 33

def emit_status(result)
  $stderr.puts output_message(result) unless @silent # rubocop:disable Style/StderrPuts
rescue IOError
  # A parallel runner can close a worker's stderr before its at_exit
  # hooks run (rspec-conductor does). Losing the status line must not
  # abort the exit tasks that follow report generation, i.e. the
  # threshold checks and the .last_run.json write.
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)


84
85
86
# File 'lib/simplecov/formatter/base.rb', line 84

def entry_point_filename
  nil
end

#message_prefixString

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

Returns:

  • (String)


45
46
47
# File 'lib/simplecov/formatter/base.rb', line 45

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)


94
95
96
97
98
# File 'lib/simplecov/formatter/base.rb', line 94

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)


49
50
51
# File 'lib/simplecov/formatter/base.rb', line 49

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)


68
69
70
71
72
73
74
75
76
77
# File 'lib/simplecov/formatter/base.rb', line 68

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)


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/simplecov/formatter/base.rb', line 104

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