Module: SimpleCov::LastRun

Defined in:
lib/simplecov/last_run.rb,
sig/simplecov.rbs

Overview

Reads and writes coverage/.last_run.json — the previous run's coverage percentages used by MaximumCoverageDropCheck.

Class Method Summary collapse

Class Method Details

.last_run_pathString

Returns:

  • (String)


11
12
13
# File 'lib/simplecov/last_run.rb', line 11

def last_run_path
  File.join(SimpleCov.coverage_path, ".last_run.json")
end

.readHash[Symbol, untyped]?

nil when the file does not exist, is blank, or does not hold a JSON object. Keys are symbolized.

Returns:

  • (Hash[Symbol, untyped], nil)


1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'sig/simplecov.rbs', line 1205

def read
  return nil unless File.exist?(last_run_path)

  json = File.read(last_run_path)
  return nil if json.strip.empty?

  parsed = JSON.parse(json, symbolize_names: true)
  # The maximum_coverage_drop check digs into a Hash, so anything
  # else in a corrupt or hand-edited file counts as "no previous
  # run" rather than crashing the at_exit hook.
  parsed.is_a?(Hash) ? parsed : invalid_last_run
rescue JSON::ParserError
  invalid_last_run
end

.write(json) ⇒ void

This method returns an undefined value.

Parameters:

  • json (Hash[Symbol, untyped])


30
31
32
# File 'lib/simplecov/last_run.rb', line 30

def write(json)
  AtomicFile.write(last_run_path, "#{JSON.pretty_generate(json)}\n")
end