module ForkingTestRunner::CoverageCapture

Attributes

coverage[RW]

Public Class Methods

activate!() click to toggle source
# File lib/forking_test_runner/coverage_capture.rb, line 16
def activate!
  require 'coverage'
  (class << Coverage; self; end).prepend self
end
capture!() click to toggle source
# File lib/forking_test_runner/coverage_capture.rb, line 21
def capture!
  self.coverage = Coverage.peek_result.dup
end
merge_coverage(a, b) click to toggle source
# File lib/forking_test_runner/coverage_capture.rb, line 25
def merge_coverage(a, b)
  merged = a.dup
  b.each do |file, coverage|
    orig = merged[file]
    merged[file] = if orig
      if coverage.is_a?(Array)
        merge_lines_coverage(orig, coverage)
      else
        {
          lines: merge_lines_coverage(orig.fetch(:lines), coverage.fetch(:lines)),
          branches: merge_branches_coverage(orig.fetch(:branches), coverage.fetch(:branches))
        }
      end
    else
      coverage
    end
  end
  merged
end

Private Class Methods

merge_branches_coverage(a, b) click to toggle source

assuming b has same or more keys since it comes from a fork {foo: {bar: 0, baz: 1}} + {foo: {bar: 1, baz: 0}} -> {foo: {bar: 1, baz: 1}}

# File lib/forking_test_runner/coverage_capture.rb, line 58
def merge_branches_coverage(a, b)
  b.each_with_object({}) do |(branch, v), all|
    vb = v.dup
    if part = a[branch]
      part.each do |nested, a_count|
        vb[nested] = a_count + vb[nested].to_i
      end
    end
    all[branch] = vb
  end
end
merge_lines_coverage(a, b) click to toggle source

assuming b has same or more keys since it comes from a fork

nil,1,0

+ [nil,nil,2] -> [nil,1,2]

# File lib/forking_test_runner/coverage_capture.rb, line 49
def merge_lines_coverage(a, b)
  b.each_with_index.map do |b_count, i|
    a_count = a[i]
    a_count.nil? && b_count.nil? ? nil : a_count.to_i + b_count.to_i
  end
end

Public Instance Methods

capture_coverage!() click to toggle source

deprecated, single_cov checks for this, so leave it here

# File lib/forking_test_runner/coverage_capture.rb, line 11
def capture_coverage!; end
result() click to toggle source

override Coverage.result to add pre-fork captured coverage

Calls superclass method
# File lib/forking_test_runner/coverage_capture.rb, line 5
def result
  return super unless captured = CoverageCapture.coverage
  CoverageCapture.merge_coverage(super, captured)
end