Module: Polyrun::Coverage::ExampleDiff

Defined in:
lib/polyrun/coverage/example_diff.rb

Overview

Per-example line hit deltas from stdlib Coverage.peek_result snapshots.

Class Method Summary collapse

Class Method Details

.apply_track_under(delta, root:, track_under:, ignore_paths: []) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/polyrun/coverage/example_diff.rb', line 86

def apply_track_under(delta, root:, track_under:, ignore_paths: [])
  filtered = filter_lines(delta[:lines] || [], root: root, track_under: track_under, ignore_paths: ignore_paths)
  unique = filtered.size
  churn = filtered.sum { |(_p, _l, d)| d }
  max_churn = filtered.map { |(_p, _l, d)| d }.max || 0
  {
    unique_lines: unique,
    line_churn: churn,
    max_line_churn: max_churn,
    lines: filtered
  }
end

.coverage_active?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/polyrun/coverage/example_diff.rb', line 14

def coverage_active?
  defined?(::Coverage) && ::Coverage.respond_to?(:running?) && ::Coverage.running?
end

.diff(before_blob, after_blob) ⇒ Hash

rubocop:disable Metrics/AbcSize -- per-file coverage delta walk

Returns:

  • (Hash)

    :unique_lines, :line_churn, :max_line_churn, :lines (compact triples)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/polyrun/coverage/example_diff.rb', line 33

def diff(before_blob, after_blob)
  before_blob ||= {}
  after_blob ||= {}
  files = before_blob.keys | after_blob.keys

  line_entries = []
  unique = 0
  churn = 0
  max_churn = 0

  files.each do |path|
    b_lines = line_array(before_blob[path])
    a_lines = line_array(after_blob[path])
    max_len = [b_lines.size, a_lines.size].max

    (0...max_len).each do |i|
      b = b_lines[i]
      a = a_lines[i]
      next if a.nil? && b.nil?

      delta = (integer_hit(a) - integer_hit(b))
      next unless delta.positive?

      line_no = i + 1
      line_entries << [path, line_no, delta]
      unique += 1
      churn += delta
      max_churn = delta if delta > max_churn
    end
  end

  {
    unique_lines: unique,
    line_churn: churn,
    max_line_churn: max_churn,
    lines: line_entries
  }
end

.filter_lines(lines, root:, track_under:, ignore_paths: []) ⇒ Object

rubocop:enable Metrics/AbcSize



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/polyrun/coverage/example_diff.rb', line 73

def filter_lines(lines, root:, track_under:, ignore_paths: [])
  root = File.expand_path(root)
  prefixes = Array(track_under).map { |d| File.join(root, d.to_s) }
  ignore = Array(ignore_paths).map(&:to_s).reject(&:empty?)

  lines.select do |path, _line, _delta|
    p = File.expand_path(path.to_s, root)
    next false if ignore.any? { |pat| path_matches_ignore?(p, pat) }

    prefixes.any? { |pre| p == pre || p.start_with?(pre + "/") }
  end
end

.normalize_peek(raw) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/polyrun/coverage/example_diff.rb', line 18

def normalize_peek(raw)
  out = {}
  raw.each do |path, cov|
    next unless cov.is_a?(Hash)

    lines = cov[:lines] || cov["lines"]
    next unless lines.is_a?(Array)

    out[path.to_s] = {"lines" => lines.map { |x| x }}
  end
  out
end

.path_matches_ignore?(path, pattern) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
# File 'lib/polyrun/coverage/example_diff.rb', line 99

def path_matches_ignore?(path, pattern)
  return path.include?(pattern) if pattern.is_a?(String) && !pattern.start_with?("/")

  path.match?(Regexp.new(pattern))
rescue RegexpError
  path.include?(pattern.to_s)
end

.peek_blobHash{String=>Hash}

Returns path => {"lines"=>[...]}.

Returns:

  • (Hash{String=>Hash})

    path => {"lines"=>[...]}



8
9
10
11
12
# File 'lib/polyrun/coverage/example_diff.rb', line 8

def peek_blob
  return {} unless coverage_active?

  normalize_peek(::Coverage.peek_result)
end