Class: SimpleCov::Formatter::BaselineFormatter

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

Overview

Auto-ratchets the per-file coverage baseline as a formatter, for teams that want floors to tighten on every run instead of by deliberate simplecov ratchet invocations. The semantics are exactly the CLI's, and the file is rewritten only when a floor actually moved.

The exit checks still judge the run against the floors as they were when it started (the baseline is read once per path), and ratcheting never loosens a floor, so tightening before the checks cannot flip a failing file to passing.

Instance Method Summary collapse

Methods inherited from Base

#displayable_output_path, #emit_status, #entry_point_filename, #initialize, #message_prefix, #relative_or_absolute_output_path, #stats_line

Constructor Details

This class inherits a constructor from SimpleCov::Formatter::Base

Instance Method Details

#below_floors(regressed) ⇒ String

Parameters:

  • regressed (Array[String])

Returns:

  • (String)


51
52
53
54
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 51

def below_floors(regressed)
  noun = regressed.size.eql?(1) ? "1 file below its floor" : "#{regressed.size} files below their floors"
  ", #{noun}: #{regressed.join(", ")}"
end

#current_floors(result) ⇒ Baseline::current_floors

SourceFile#coverage_statistics answers for disabled criteria too, as empty statistics, so the criteria are gated on the configuration rather than on the stats' presence.

Parameters:

Returns:

  • (Baseline::current_floors)


59
60
61
62
63
64
65
66
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 59

def current_floors(result)
  criteria = measured_criteria
  floors = {} #: Baseline::current_floors
  result.files.each do |file|
    floors[file.project_filename] = criteria.to_h { |criterion| [criterion, floor_of(file, criterion)] }
  end
  floors
end

#floor_of(file, criterion) ⇒ { percent: Numeric, missed: Integer }

Parameters:

  • file (Object)
  • criterion (Symbol)

Returns:

  • ({ percent: Numeric, missed: Integer })


76
77
78
79
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 76

def floor_of(file, criterion)
  stats = file.coverage_statistics(criterion)
  {percent: SimpleCov.round_coverage(stats.percent), missed: stats.missed}
end

#format(result) ⇒ void

This method returns an undefined value.

Parameters:



18
19
20
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 18

def format(result)
  emit_status(ratchet(result))
end

#generate(current) ⇒ String

Parameters:

  • current (Baseline::current_floors)

Returns:

  • (String)


37
38
39
40
41
42
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 37

def generate(current)
  baseline = Baseline.generate(current)
  write(baseline)
  files = baseline.entries.size
  "Coverage baseline generated to #{displayable_output_path} (#{files} #{files.eql?(1) ? "file" : "files"})"
end

#measured_criteriaArray[Symbol]

Returns:

  • (Array[Symbol])


68
69
70
71
72
73
74
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 68

def measured_criteria
  criteria = [] #: Array[Symbol]
  criteria << :line if SimpleCov.line_coverage?
  criteria << :branch if SimpleCov.branch_coverage?
  criteria << :method if SimpleCov.method_coverage?
  criteria
end

#output_message(message) ⇒ String

format hands the ratchet's summary to emit_status, which passes it through to here in the result's place.

Parameters:

  • message (Object)

Returns:

  • (String)


93
94
95
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 93

def output_message(message)
  message
end

#output_pathString

The baseline lives where the exit check reads it, not under the coverage directory: it is checked-in policy, not a report artifact.

Returns:

  • (String)


87
88
89
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 87

def output_path
  File.expand_path(SimpleCov.baseline_file, SimpleCov.root)
end

#ratchet(result) ⇒ String

Parameters:

Returns:

  • (String)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 24

def ratchet(result)
  current = current_floors(result)
  existing = SimpleCov.baseline
  return generate(current) unless existing

  outcome = existing.ratchet(current)
  changed = outcome.tightened.any? || outcome.pruned.any?
  write(outcome.baseline) if changed
  note = summary(outcome, changed)
  regressed = outcome.regressed
  regressed.empty? ? note : "#{note}#{below_floors(regressed)}"
end

#summary(outcome, changed) ⇒ String

Parameters:

Returns:

  • (String)


44
45
46
47
48
49
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 44

def summary(outcome, changed)
  return "Coverage baseline unchanged at #{displayable_output_path}" unless changed

  "Coverage baseline ratcheted to #{displayable_output_path} " \
    "(#{outcome.tightened.size} tightened, #{outcome.pruned.size} pruned)"
end

#write(baseline) ⇒ void

This method returns an undefined value.

Parameters:



81
82
83
# File 'lib/simplecov/formatter/baseline_formatter.rb', line 81

def write(baseline)
  AtomicFile.write(output_path, baseline.to_yaml)
end