Class: SimpleCov::Combine::CoverageAccumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov/combine/coverage_accumulator.rb

Overview

Folds any number of resultsets' coverage into one, absorbing them one at a time so that a caller reading resultsets off disk never holds more than one in memory, and so the accumulated side can be updated in place rather than rebuilt once per resultset.

Defined Under Namespace

Classes: MergedFile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoverageAccumulator

Returns a new instance of CoverageAccumulator.



39
40
41
42
43
44
45
46
# File 'lib/simplecov/combine/coverage_accumulator.rb', line 39

def initialize
  # Whether a criterion is enabled can't change mid-fold, so read it once
  # here rather than once per file. `branch_coverage?` reaches
  # `Coverage.supported?`, and a large parallel run merges thousands of
  # files.
  @branch_coverage = SimpleCov.branch_coverage?
  @method_coverage = SimpleCov.method_coverage?
end

Class Method Details

.executed?(lines) ⇒ Boolean

A file some process actually loaded has at least one executed line; a simulated (never-loaded) file's lines are all nil or 0.

Array() plus the Numeric test rather than any?(&:positive?) because this reads straight off a parsed resultset, which is external input and can carry anything under "lines".

Returns:



22
23
24
25
# File 'lib/simplecov/combine/coverage_accumulator.rb', line 22

def self.executed?(lines)
  counts = Array(lines) #: Array[untyped]
  counts.any? { |count| count.is_a?(Numeric) && count.positive? }
end

.fold(pairs) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simplecov/combine/coverage_accumulator.rb', line 27

def self.fold(pairs)
  accumulator = new
  command_names = [] #: Array[String]

  pairs.each do |names, coverage|
    command_names.concat(names)
    accumulator.absorb(coverage)
  end

  [command_names, accumulator.result]
end

Instance Method Details

#absorb(coverage) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/simplecov/combine/coverage_accumulator.rb', line 48

def absorb(coverage)
  return self unless coverage

  files = (@files ||= {}) #: Hash[String, untyped]
  coverage.each do |filename, file_coverage|
    files[filename] = merge_file(files[filename], file_coverage)
  end

  self
end

#resultObject

nil until the first absorb, so callers can tell "no results" apart from "results that cover nothing": only the former means there is no report to build.



62
63
64
65
66
67
68
69
# File 'lib/simplecov/combine/coverage_accumulator.rb', line 62

def result
  @files&.transform_values do |entry|
    case entry
    when MergedFile then entry.to_h
    else entry
    end
  end
end