Class: Henitai::PerTestCoverageCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/henitai/per_test_coverage_collector.rb

Overview

Accumulates per-test line coverage deltas across test examples.

Framework-agnostic core used by both the RSpec and Minitest adapters. Callers invoke record_test after each test completes and write_report once the full suite has finished.

Constant Summary collapse

REPORT_DIR_ENV =
"HENITAI_REPORTS_DIR"
REPORT_FILE_NAME =
"henitai_per_test.json"

Instance Method Summary collapse

Constructor Details

#initializePerTestCoverageCollector

Returns a new instance of PerTestCoverageCollector.



17
18
19
20
21
22
23
# File 'lib/henitai/per_test_coverage_collector.rb', line 17

def initialize
  @coverage_by_test = Hash.new do |hash, test_file|
    hash[test_file] = Hash.new { |nested, source_file| nested[source_file] = [] }
  end
  @previous_snapshot = {}
  @warned_missing_coverage = false
end

Instance Method Details

#record_test(test_file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/henitai/per_test_coverage_collector.rb', line 25

def record_test(test_file)
  snapshot = current_snapshot
  return warn_missing_coverage unless snapshot

  new_lines(snapshot).each do |source_file, lines|
    @coverage_by_test[test_file][source_file].concat(lines)
    @coverage_by_test[test_file][source_file].uniq!
    @coverage_by_test[test_file][source_file].sort!
  end
  @previous_snapshot = snapshot
end

#write_reportObject



37
38
39
40
41
42
# File 'lib/henitai/per_test_coverage_collector.rb', line 37

def write_report
  return if @coverage_by_test.empty?

  FileUtils.mkdir_p(File.dirname(report_path))
  File.write(report_path, JSON.pretty_generate(serializable_report))
end