simplecov 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +65 -1425
  3. data/lib/simplecov/atomic_file.rb +70 -0
  4. data/lib/simplecov/cli/clean.rb +43 -3
  5. data/lib/simplecov/cli/command_helpers.rb +55 -0
  6. data/lib/simplecov/cli/coverage.rb +21 -28
  7. data/lib/simplecov/cli/coverage_file.rb +65 -0
  8. data/lib/simplecov/cli/diff.rb +43 -48
  9. data/lib/simplecov/cli/dotfile.rb +12 -6
  10. data/lib/simplecov/cli/merge.rb +12 -10
  11. data/lib/simplecov/cli/open.rb +3 -5
  12. data/lib/simplecov/cli/report.rb +31 -23
  13. data/lib/simplecov/cli/serve/report_preparer.rb +29 -0
  14. data/lib/simplecov/cli/serve/static_file_handler.rb +122 -0
  15. data/lib/simplecov/cli/serve.rb +30 -94
  16. data/lib/simplecov/cli/uncovered.rb +20 -25
  17. data/lib/simplecov/cli.rb +15 -2
  18. data/lib/simplecov/combine/branches_combiner.rb +43 -19
  19. data/lib/simplecov/combine/coverage_accumulator.rb +268 -0
  20. data/lib/simplecov/combine/identity_interner.rb +30 -0
  21. data/lib/simplecov/combine/interned_counts.rb +30 -0
  22. data/lib/simplecov/combine/lines_combiner.rb +48 -20
  23. data/lib/simplecov/combine/methods_combiner.rb +46 -21
  24. data/lib/simplecov/combine/results_combiner.rb +12 -37
  25. data/lib/simplecov/combine.rb +5 -23
  26. data/lib/simplecov/command_guesser.rb +66 -9
  27. data/lib/simplecov/configuration/coverage.rb +12 -15
  28. data/lib/simplecov/configuration/coverage_criteria.rb +34 -38
  29. data/lib/simplecov/configuration/eval_coverage.rb +41 -0
  30. data/lib/simplecov/configuration/filters.rb +11 -44
  31. data/lib/simplecov/configuration/formatting.rb +18 -9
  32. data/lib/simplecov/configuration/groups.rb +42 -0
  33. data/lib/simplecov/configuration/merging.rb +8 -7
  34. data/lib/simplecov/configuration/thresholds.rb +14 -13
  35. data/lib/simplecov/configuration.rb +13 -43
  36. data/lib/simplecov/coverage_json.rb +24 -0
  37. data/lib/simplecov/coverage_statistics.rb +1 -1
  38. data/lib/simplecov/coverage_violations.rb +15 -5
  39. data/lib/simplecov/defaults.rb +7 -3
  40. data/lib/simplecov/directive.rb +1 -1
  41. data/lib/simplecov/exit_codes/check.rb +33 -0
  42. data/lib/simplecov/exit_codes/maximum_coverage_drop_check.rb +11 -20
  43. data/lib/simplecov/exit_codes/maximum_overall_coverage_check.rb +3 -16
  44. data/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb +14 -23
  45. data/lib/simplecov/exit_codes/minimum_coverage_by_group_check.rb +14 -25
  46. data/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb +4 -17
  47. data/lib/simplecov/exit_codes.rb +1 -0
  48. data/lib/simplecov/exit_handling.rb +11 -41
  49. data/lib/simplecov/file_list.rb +5 -10
  50. data/lib/simplecov/filter.rb +43 -9
  51. data/lib/simplecov/formatter/base.rb +11 -0
  52. data/lib/simplecov/formatter/coverage_json_writer.rb +97 -0
  53. data/lib/simplecov/formatter/html_formatter/public/index.html +35 -5
  54. data/lib/simplecov/formatter/html_formatter/viewer_data_validator.rb +96 -0
  55. data/lib/simplecov/formatter/html_formatter.rb +67 -47
  56. data/lib/simplecov/formatter/json_formatter/errors_formatter.rb +54 -56
  57. data/lib/simplecov/formatter/json_formatter/result_hash_formatter.rb +78 -88
  58. data/lib/simplecov/formatter/json_formatter/source_file_formatter.rb +70 -76
  59. data/lib/simplecov/formatter/json_formatter.rb +5 -48
  60. data/lib/simplecov/formatter/multi_formatter.rb +1 -1
  61. data/lib/simplecov/formatter/simple_formatter.rb +8 -5
  62. data/lib/simplecov/formatter.rb +12 -0
  63. data/lib/simplecov/group_names.rb +32 -0
  64. data/lib/simplecov/last_run.rb +16 -9
  65. data/lib/simplecov/lines_classifier.rb +29 -8
  66. data/lib/simplecov/load_global_config.rb +5 -2
  67. data/lib/simplecov/parallel_adapters/base.rb +17 -0
  68. data/lib/simplecov/parallel_adapters/generic.rb +2 -2
  69. data/lib/simplecov/parallel_adapters/parallel_tests.rb +2 -2
  70. data/lib/simplecov/parallel_coordination.rb +6 -1
  71. data/lib/simplecov/parallel_result_merger.rb +230 -0
  72. data/lib/simplecov/report_deferral.rb +49 -0
  73. data/lib/simplecov/report_stamp.rb +28 -0
  74. data/lib/simplecov/result.rb +40 -10
  75. data/lib/simplecov/result_adapter.rb +48 -16
  76. data/lib/simplecov/result_merger/resultset_file.rb +43 -7
  77. data/lib/simplecov/result_merger/resultset_run_identity.rb +67 -0
  78. data/lib/simplecov/result_merger/resultset_store.rb +15 -12
  79. data/lib/simplecov/result_merger/unloaded_files.rb +103 -0
  80. data/lib/simplecov/result_merger.rb +69 -42
  81. data/lib/simplecov/result_processing.rb +82 -43
  82. data/lib/simplecov/run_identity.rb +77 -0
  83. data/lib/simplecov/simulate_coverage.rb +35 -11
  84. data/lib/simplecov/source_file/method.rb +7 -1
  85. data/lib/simplecov/source_file/ruby_data_parser.rb +25 -3
  86. data/lib/simplecov/source_file/skip_chunks.rb +7 -10
  87. data/lib/simplecov/source_file/source_loader.rb +23 -7
  88. data/lib/simplecov/source_file/statistics.rb +24 -16
  89. data/lib/simplecov/static_coverage_extractor/condition_folding.rb +203 -13
  90. data/lib/simplecov/static_coverage_extractor/location_conventions.rb +19 -30
  91. data/lib/simplecov/static_coverage_extractor/method_collector.rb +7 -0
  92. data/lib/simplecov/static_coverage_extractor/prism_compat.rb +55 -0
  93. data/lib/simplecov/static_coverage_extractor/value_position.rb +6 -14
  94. data/lib/simplecov/static_coverage_extractor/visitor.rb +23 -36
  95. data/lib/simplecov/unloaded_file_injector.rb +75 -0
  96. data/lib/simplecov/version.rb +1 -1
  97. data/lib/simplecov.rb +11 -5
  98. data/schemas/coverage-v1.0.schema.json +2 -2
  99. data/schemas/coverage.schema.json +2 -2
  100. data/sig/simplecov.rbs +206 -67
  101. metadata +28 -14
  102. data/doc/alternate-formatters.md +0 -66
  103. data/doc/commercial-services.md +0 -25
  104. data/doc/editor-integration.md +0 -18
  105. data/lib/simplecov/combine/files_combiner.rb +0 -70
  106. data/lib/simplecov/formatter/html_formatter/public/application.css +0 -1
  107. data/lib/simplecov/formatter/html_formatter/public/application.js +0 -18
  108. data/lib/simplecov/formatter/html_formatter/public/favicon_green.png +0 -0
  109. data/lib/simplecov/formatter/html_formatter/public/favicon_red.png +0 -0
  110. data/lib/simplecov/formatter/html_formatter/public/favicon_yellow.png +0 -0
@@ -0,0 +1,268 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "branches_combiner"
4
+ require_relative "lines_combiner"
5
+ require_relative "methods_combiner"
6
+
7
+ module SimpleCov
8
+ module Combine
9
+ #
10
+ # Folds any number of resultsets' coverage into one, absorbing them one
11
+ # at a time.
12
+ #
13
+ # This replaces a pairwise `reduce` over the resultsets, which rebuilt
14
+ # the entire accumulated structure on every one of its N-1 steps: a
15
+ # fresh outer file hash, a fresh lines array for every file, and a fresh
16
+ # branch / method table for every file whose keys were re-interned from
17
+ # their tuples each time. On a 160-worker run over ~1,800 files that is
18
+ # ~290,000 whole-file rebuilds to produce ~1,800 files of output.
19
+ #
20
+ # The accumulated side is private to the fold, so it can be updated in
21
+ # place instead, and the interned tables only have to be turned back into
22
+ # tuple-keyed hashes once, at the end.
23
+ #
24
+ # Resultsets are absorbed one at a time rather than taken as a list, so
25
+ # `ResultMerger.merge_results` can keep reading and discarding them one
26
+ # file at a time — reading 100s of CI jobs' worth of coverage into memory
27
+ # at once is what that method is careful not to do.
28
+ #
29
+ class CoverageAccumulator
30
+ # A file some process actually loaded has at least one executed line;
31
+ # a simulated (never-loaded) file's lines are all `nil` or `0`. This is
32
+ # the signal the merge reconciles synthesized tuples on, and the one
33
+ # `ResultMerger` re-derives a merged result's not-loaded set from, so it
34
+ # lives here rather than being spelled out at each site.
35
+ #
36
+ # `Array()` plus the `Numeric` test rather than a bare
37
+ # `any?(&:positive?)` because this reads straight off a parsed
38
+ # resultset, which is external input: a file written by another
39
+ # SimpleCov version, or hand-edited, can carry anything under
40
+ # "lines" — a Hash coerces to pairs, a String to itself. Keeping a
41
+ # malformed entry to a wrong answer instead of a NoMethodError out
42
+ # of the middle of a merge is the point.
43
+ def self.executed?(lines)
44
+ counts = Array(lines) #: Array[untyped]
45
+ counts.any? { |count| count.is_a?(Numeric) && count.positive? }
46
+ end
47
+
48
+ #
49
+ # Folds `[command_names, coverage]` pairs into one merged coverage.
50
+ #
51
+ # `pairs` is only ever iterated, so a caller that reads resultsets off
52
+ # disk can hand in a lazy enumerable and never hold more than one in
53
+ # memory — which is what `ResultMerger.merge_results` is careful about.
54
+ #
55
+ # @return [Array] the concatenated command names and the merged coverage
56
+ #
57
+ def self.fold(pairs)
58
+ accumulator = new
59
+ command_names = [] #: Array[String]
60
+
61
+ pairs.each do |names, coverage|
62
+ command_names.concat(names)
63
+ accumulator.absorb(coverage)
64
+ end
65
+
66
+ [command_names, accumulator.result]
67
+ end
68
+
69
+ def initialize
70
+ @files = {} #: Hash[String, untyped]
71
+ @absorbed = false
72
+ # Whether a criterion is enabled can't change mid-fold, so read it once
73
+ # here rather than once per file. `branch_coverage?` reaches
74
+ # `Coverage.supported?`, and a large parallel run merges thousands of
75
+ # files.
76
+ @branch_coverage = SimpleCov.branch_coverage?
77
+ @method_coverage = SimpleCov.method_coverage?
78
+ end
79
+
80
+ #
81
+ # Folds one resultset's coverage (filename => per-file coverage) into
82
+ # the accumulator. A `nil` coverage is ignored — a resultset that
83
+ # carried nothing contributes nothing.
84
+ #
85
+ # @return [CoverageAccumulator] self, so absorbs can be chained
86
+ #
87
+ def absorb(coverage)
88
+ return self unless coverage
89
+
90
+ @absorbed = true
91
+ coverage.each do |filename, file_coverage|
92
+ @files[filename] = merge_file(@files[filename], file_coverage)
93
+ end
94
+
95
+ self
96
+ end
97
+
98
+ #
99
+ # The merged coverage, or `nil` when nothing was absorbed at all — the
100
+ # caller needs to tell "no results" apart from "results that cover
101
+ # nothing", and only the former means there is no report to build.
102
+ #
103
+ # A file only one resultset carried is returned exactly as it came in,
104
+ # untouched: with nothing to merge it into, copying it would only cost
105
+ # memory.
106
+ #
107
+ # @return [Hash, nil]
108
+ #
109
+ def result
110
+ return nil unless @absorbed
111
+
112
+ @files.transform_values do |entry|
113
+ entry.is_a?(MergedFile) ? entry.to_h : entry
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ # The first sighting of a file is kept as-is; the second promotes it to
120
+ # a `MergedFile` that owns its state, and every later one folds into
121
+ # that same object.
122
+ def merge_file(existing, file_coverage)
123
+ case existing
124
+ when nil then file_coverage
125
+ when MergedFile then existing.absorb(file_coverage)
126
+ else MergedFile.new(existing, branches: @branch_coverage, methods: @method_coverage).absorb(file_coverage)
127
+ end
128
+ end
129
+
130
+ #
131
+ # One file's accumulated coverage, against state this object owns
132
+ # outright rather than rebuilt for each merged pair.
133
+ #
134
+ class MergedFile
135
+ # A criterion is carried for this file when the data carries it, not
136
+ # when this process happens to measure it. A merge runs on behalf of
137
+ # the processes that produced the resultsets and does not necessarily
138
+ # share their configuration: `simplecov merge` never ran SimpleCov.start
139
+ # at all, and dropping a table it did not ask for would lose branch data
140
+ # the producers did measure. A nil table means nobody measured that
141
+ # criterion; an empty one means it was measured and the file has none.
142
+ def initialize(coverage, branches:, methods:)
143
+ @branch_coverage = branches
144
+ @method_coverage = methods
145
+ @lines = coverage["lines"]&.dup
146
+ # Branch coverage always reports a table, even an empty one, so
147
+ # `SourceFile` can tell "no branches in this file" from "branch
148
+ # coverage was off". Method coverage stays `nil` until some resultset
149
+ # actually carries methods.
150
+ @branches = BranchesCombiner.absorb({}, coverage["branches"]) if branches || coverage["branches"]
151
+ @methods = MethodsCombiner.absorb(nil, coverage["methods"]) if methods || coverage["methods"]
152
+ end
153
+
154
+ #
155
+ # Folds one more resultset's coverage for this file in.
156
+ #
157
+ # @return [MergedFile] self
158
+ #
159
+ def absorb(coverage)
160
+ reconcile_synthesized(coverage)
161
+ @lines = LinesCombiner.merge_into(@lines, coverage["lines"])
162
+ self
163
+ end
164
+
165
+ def to_h
166
+ merged = {"lines" => @lines} #: Hash[String, untyped]
167
+ merged["branches"] = BranchesCombiner.materialize(@branches) if @branches
168
+ merged["methods"] = MethodsCombiner.materialize(@methods) if @methods
169
+ merged
170
+ end
171
+
172
+ private
173
+
174
+ def new_table
175
+ {} #: Hash[untyped, untyped]
176
+ end
177
+
178
+ # When exactly one side of the merge was actually executed, its branch
179
+ # and method tuples are authoritative and the other side's are
180
+ # dropped. A simulated entry (SimulateCoverage backfills
181
+ # tracked-but-unloaded files) synthesizes those tuples statically, so
182
+ # a location that drifts from what Coverage emits would otherwise be
183
+ # unioned in by position and survive as a phantom, permanently-missed
184
+ # branch (see #1233). This contains any such drift to denominator
185
+ # inflation for files no process loaded, rather than a false miss on a
186
+ # covered file. Lines are never dropped: a simulated file's line shape
187
+ # is correct and carries the unloaded-file denominator (#1059).
188
+ #
189
+ # The accumulated side's executed-ness is re-read on every absorb
190
+ # because folding in an executed resultset can flip it, exactly as it
191
+ # would have flipped between two steps of the pairwise fold.
192
+ def reconcile_synthesized(coverage)
193
+ return absorb_tuples(coverage) unless judgeable?(@lines) && judgeable?(coverage["lines"])
194
+
195
+ accumulated_executed = executed?(@lines)
196
+ incoming_executed = executed?(coverage["lines"])
197
+
198
+ if accumulated_executed == incoming_executed
199
+ absorb_tuples(coverage)
200
+ elsif incoming_executed
201
+ replace_tuples(coverage)
202
+ else
203
+ drop_incoming_tuples(coverage)
204
+ end
205
+ end
206
+
207
+ # Both sides agree on whether they ran: union their tuples. A criterion
208
+ # only the incoming side carries comes into play here, because its
209
+ # tuples survive. It deliberately does not come into play on the path
210
+ # that drops them: promoting there would advertise an empty table
211
+ # sourced from data this merge just discarded, which claims the file
212
+ # has no branches when what is true is that nobody measured them.
213
+ def absorb_tuples(coverage)
214
+ @branches = BranchesCombiner.absorb(@branches || new_table, coverage["branches"]) if
215
+ @branches || coverage["branches"]
216
+ @methods = MethodsCombiner.absorb(@methods, coverage["methods"]) if @methods || coverage["methods"]
217
+ end
218
+
219
+ # Only the accumulated side ran, so the incoming tuples are synthesized
220
+ # and contribute nothing. When the discarded side carried a methods
221
+ # table, a measuring process still gets one — the empty table stands in
222
+ # for the blanked side, which is what a pair against a blank one used
223
+ # to produce for a file that has no methods yet. When no side has
224
+ # carried methods at all, the table stays nil (the rule `initialize`
225
+ # documents), the same answer the union path gives.
226
+ def drop_incoming_tuples(coverage)
227
+ @methods ||= {} if @method_coverage && coverage["methods"] #: Hash[untyped, untyped]
228
+ end
229
+
230
+ # Only the incoming side ran, so what's accumulated is synthesized:
231
+ # start its tuples over from the executed side's. The authoritative
232
+ # side decides which criteria the file carries, not just their tuples.
233
+ # Keeping a table in play that only the dropped side carried would make
234
+ # the answer depend on which side was seen first.
235
+ def replace_tuples(coverage)
236
+ @branches = authoritative_table(BranchesCombiner, coverage["branches"], @branch_coverage)
237
+ # Methods stay nil until some resultset carries them (see
238
+ # `initialize`), so a measuring process keeps an empty table only
239
+ # when one did.
240
+ @methods = authoritative_table(MethodsCombiner, coverage["methods"], @method_coverage && !@methods.nil?)
241
+ end
242
+
243
+ # The executed side's tuples when it has them. Otherwise `keep_empty`
244
+ # decides whether the criterion survives as an empty table, which is
245
+ # what keeps `SourceFile` able to tell "no branches here" from
246
+ # "branches were not measured".
247
+ def authoritative_table(combiner, table, keep_empty)
248
+ return combiner.absorb(new_table, table) if table
249
+
250
+ keep_empty ? new_table : nil
251
+ end
252
+
253
+ # Whether a side can be judged at all. Absent lines do not mean the side
254
+ # never ran: a branch-only or method-only run omits them even for the
255
+ # files it loaded, so treating that as synthesized would discard real
256
+ # tuples. Unknown means union, which is already what happens when
257
+ # neither side carries lines.
258
+ def judgeable?(lines)
259
+ !Array(lines).empty?
260
+ end
261
+
262
+ def executed?(lines)
263
+ CoverageAccumulator.executed?(lines)
264
+ end
265
+ end
266
+ end
267
+ end
268
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module Combine
5
+ #
6
+ # Builds the raw-key => interned-identity caches the tuple combiners keep.
7
+ #
8
+ # The cache maps a raw key to its interned identity on first sight and
9
+ # keeps it: the pairwise fold would otherwise re-derive the accumulator's
10
+ # identities on every one of its merges, always to the same answer.
11
+ #
12
+ # Identities exist only to be `combine`'s hash keys, and `Array#hash` is
13
+ # not memoized, so keying on the identity tuple itself would rehash its
14
+ # elements for every key of both sides of every merge. An Integer hashes
15
+ # as an immediate. Two keys share an id exactly when their identities are
16
+ # equal, so the string and array forms of one key still merge together.
17
+ #
18
+ module IdentityInterner
19
+ module_function
20
+
21
+ def build
22
+ ids = {} #: Hash[::Array[untyped], Integer]
23
+ Hash.new do |cache, key|
24
+ identity = yield(key)
25
+ cache[key] = ids[identity] ||= ids.size
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module Combine
5
+ # The inner merge loop the branches and methods combiners share:
6
+ # fold `source`'s tuple => count pairs into `target`, an interned
7
+ # table keyed by each tuple's merge identity and holding
8
+ # `[raw_key, count]` pairs. The first-seen raw key is kept for
9
+ # display; counts sum. `target` and its pairs are always built by
10
+ # this loop, so updating them in place can't reach data a caller
11
+ # still holds.
12
+ module InternedCounts
13
+ module_function
14
+
15
+ # @return [Hash] `target`
16
+ def absorb_counts(target, source, identities)
17
+ source.each do |key, count|
18
+ interned = identities[key]
19
+ entry = target[interned]
20
+ if entry
21
+ entry[1] += count
22
+ else
23
+ target[interned] = [key, count]
24
+ end
25
+ end
26
+ target
27
+ end
28
+ end
29
+ end
30
+ end
@@ -5,40 +5,68 @@ module SimpleCov
5
5
  #
6
6
  # Combine two different lines coverage results on same file
7
7
  #
8
- # Should be called through `SimpleCov.combine`.
8
+ # Should be called through `CoverageAccumulator`.
9
9
  module LinesCombiner
10
10
  module_function
11
11
 
12
- # Build a fresh array sized to the longer input. The previous
13
- # implementation mutated whichever input was longer in place,
14
- # which could surprise callers holding a reference to that array
15
- # (e.g. the parsed `coverage` key of a resultset hash being
16
- # passed into a second merge).
17
- def combine(coverage_a, coverage_b)
18
- size = [coverage_a.size, coverage_b.size].max
19
- Array.new(size) { |i| merge_line_coverage(coverage_a[i], coverage_b[i]) }
20
- end
21
-
12
+ # Folds `source` into `target` rather than building a third array.
13
+ # Only for a `target` the caller owns outright: a caller holding a
14
+ # reference to it (e.g. the parsed `coverage` key of a resultset
15
+ # hash being passed into a second merge) would see it change.
16
+ # `source` is never touched.
17
+ #
22
18
  # Two runs of the same source file should agree on which lines
23
19
  # are coverage-relevant (`nil` for comments / whitespace, `0`+
24
20
  # for executable). When they don't, treat "relevant on either
25
21
  # side" as relevant rather than masking a real `0` as `nil`,
26
22
  # which would silently drop an uncovered line from the
27
- # denominator and inflate the percentage.
28
- #
29
- # Logic:
23
+ # denominator and inflate the percentage:
30
24
  #
31
25
  # => nil + nil = nil
32
26
  # => nil + int = int (preserves a relevant-but-uncovered 0)
33
27
  # => int + int = int (sum)
34
28
  #
35
- # @param [Integer || nil] first_val
36
- # @param [Integer || nil] second_val
37
- # @return [Integer || nil]
38
- def merge_line_coverage(first_val, second_val)
39
- return nil if first_val.nil? && second_val.nil?
29
+ # The loop is written out rather than dispatching a block per
30
+ # element because this is the innermost loop of a merge: a
31
+ # 160-worker run over ~1,800 files folds tens of millions of line
32
+ # counts through it, and a call per count is a measurable share of
33
+ # that.
34
+ #
35
+ # @return [Array] the array to keep — `target` itself once there is one
36
+ def merge_into(target, source)
37
+ return target unless source
38
+ return source.dup unless target
39
+
40
+ size = source.size
41
+ target.concat(Array.new(size - target.size)) if target.size < size
42
+ sum_into(target, source, size)
43
+ end
44
+
45
+ # Split out only to keep `merge_into` short; it is the same loop.
46
+ #
47
+ # The `Integer` tests coerce malformed counts (a "3" written by a
48
+ # hand-edited or foreign resultset, a JSON float) with `to_i`, so
49
+ # external input merges to a wrong answer instead of raising out of
50
+ # the middle of a merge. An `is_a?` per count is far cheaper than
51
+ # the block dispatch this loop exists to avoid, and the well-formed
52
+ # fast path stays branch-for-branch what it was.
53
+ def sum_into(target, source, size)
54
+ index = 0
55
+ while index < size
56
+ if (value = source[index])
57
+ value = value.to_i unless value.is_a?(Integer)
58
+ existing = target[index]
59
+ target[index] = existing.is_a?(Integer) ? existing + value : coerce_add(existing, value)
60
+ end
61
+ index += 1
62
+ end
63
+ target
64
+ end
40
65
 
41
- first_val.to_i + second_val.to_i
66
+ # The rare arm: `existing` is nil (line not yet relevant in the
67
+ # target) or malformed external input.
68
+ def coerce_add(existing, value)
69
+ existing.nil? ? value : existing.to_i + value
42
70
  end
43
71
  end
44
72
  end
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../source_file/ruby_data_parser"
4
+ require_relative "identity_interner"
5
+ require_relative "interned_counts"
4
6
 
5
7
  module SimpleCov
6
8
  module Combine
7
9
  #
8
10
  # Combine different method coverage results on a single file.
9
11
  #
10
- # Should be called through `SimpleCov.combine`.
12
+ # Should be called through `CoverageAccumulator`.
11
13
  module MethodsCombiner
12
14
  module_function
13
15
 
@@ -16,33 +18,56 @@ module SimpleCov
16
18
  #
17
19
  # Method coverage maps `[class, name, start_line, start_col, end_line,
18
20
  # end_col]` keys to hit counts. Keys are matched on their SOURCE
19
- # identity — (name, location), ignoring the class element because
20
- # Ruby records one entry per receiver: the same `define_method` block
21
- # defined onto different classes in different processes arrives with
22
- # different (normalized) receivers for the same source method, and
23
- # matching on the full key would keep both, letting a never-called
24
- # receiver's 0 shadow a covered method after merge (issue #1234).
25
- # Combining sums the hit counts for matching methods and preserves
26
- # methods that only appear in one result.
21
+ # identity — the location, ignoring the class and name elements
22
+ # because Ruby records one entry per defined method: the same
23
+ # `define_method` block defined onto different classes, or under
24
+ # different names, in different processes arrives with different
25
+ # receivers or names for the same source method, and matching on the
26
+ # full key would keep both, letting a never-called copy's 0 shadow a
27
+ # covered method after merge (issue #1234). Combining sums the hit
28
+ # counts for matching methods and preserves methods that only appear
29
+ # in one result.
27
30
  #
28
31
  # @return [Hash]
29
32
  #
30
33
  def combine(coverage_a, coverage_b)
31
- merged = {} #: Hash[untyped, [untyped, Integer]]
32
- [coverage_a, coverage_b].each_with_object(merged) do |coverage, memo|
33
- coverage.each do |key, count|
34
- method_key = source_identity(key)
35
- retained_key, existing = memo[method_key] || [key, 0]
36
- memo[method_key] = [retained_key, existing + count]
37
- end
38
- end
39
-
40
- merged.values.to_h
34
+ merged = absorb({}, coverage_a) #: Hash[untyped, [untyped, Integer]]
35
+ materialize(absorb(merged, coverage_b))
36
+ end
37
+
38
+ # Folds `coverage` into `target`, an interned table keyed by method
39
+ # source identity. See `BranchesCombiner.absorb` for why a fold keeps
40
+ # its accumulator interned.
41
+ #
42
+ # A `nil` `target` stays `nil` until some resultset actually carries
43
+ # methods, so a merge can tell "no method data anywhere" apart from
44
+ # "method data that covers nothing".
45
+ #
46
+ # @return [Hash, nil] `target`, created on the first coverage seen
47
+ def absorb(target, coverage)
48
+ return target unless coverage
49
+
50
+ target ||= {} #: Hash[untyped, [untyped, Integer]]
51
+ InternedCounts.absorb_counts(target, coverage, identities)
52
+ end
53
+
54
+ # Turns an interned table back into the tuple-keyed hash the rest of
55
+ # SimpleCov reads. Done once, at the end of a fold.
56
+ #
57
+ # @return [Hash]
58
+ def materialize(target)
59
+ target.values.to_h
60
+ end
61
+
62
+ # Bounded by the project's method count, like `RubyDataParser`'s parse
63
+ # cache.
64
+ def identities
65
+ @identities ||= IdentityInterner.build { |key| source_identity(key) }
41
66
  end
42
67
 
43
68
  def source_identity(key)
44
- _class_name, *identity = SourceFile::RubyDataParser.call(key)
45
- identity
69
+ _class_name, _method_name, *location = SourceFile::RubyDataParser.call(key)
70
+ location.freeze
46
71
  end
47
72
  end
48
73
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "coverage_accumulator"
4
+
3
5
  module SimpleCov
4
6
  module Combine
5
7
  # There might be reports from different kinds of tests,
@@ -12,50 +14,23 @@ module SimpleCov
12
14
 
13
15
  #
14
16
  # Combine process explanation
15
- # => ResultCombiner: define all present files between results and start combine on file level.
16
- # ==> FileCombiner: collect result of next combine levels lines, branches, and methods.
17
+ # => ResultsCombiner: hand every result to one accumulator.
18
+ # ==> CoverageAccumulator::MergedFile: hold one file's merged coverage.
17
19
  # ===> LinesCombiner: combine lines results.
18
20
  # ===> BranchesCombiner: combine branches results.
19
21
  # ===> MethodsCombiner: combine methods results.
20
22
  #
21
- # @return [Hash]
22
- #
23
- def combine(*results)
24
- initial = {} #: Hash[untyped, untyped]
25
- results.reduce(initial) do |combined_results, next_result|
26
- combine_result_sets(combined_results, next_result)
27
- end
28
- end
29
-
30
- #
31
- # Manage combining results on files level
32
- #
33
- # @param [Hash] combined_results
34
- # @param [Hash] result
23
+ # Callers that read their results one at a time (`ResultMerger`, to
24
+ # keep a big CI run's resultsets out of memory all at once) should
25
+ # drive a `CoverageAccumulator` directly instead of collecting the
26
+ # results to pass here.
35
27
  #
36
28
  # @return [Hash]
37
29
  #
38
- def combine_result_sets(combined_results, result)
39
- results_files = combined_results.keys | result.keys
40
-
41
- results_files.to_h do |file_name|
42
- [file_name, combine_file_coverage(
43
- combined_results[file_name],
44
- result[file_name]
45
- )]
46
- end
47
- end
48
-
49
- #
50
- # Combine two files coverage results
51
- #
52
- # @param [Hash] coverage_a
53
- # @param [Hash] coverage_b
54
- #
55
- # @return [Hash]
56
- #
57
- def combine_file_coverage(coverage_a, coverage_b)
58
- Combine.combine(Combine::FilesCombiner, coverage_a, coverage_b)
30
+ def combine(*results)
31
+ accumulator = CoverageAccumulator.new
32
+ results.each { |result| accumulator.absorb(result) }
33
+ accumulator.result || {}
59
34
  end
60
35
  end
61
36
  end
@@ -1,30 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleCov
4
- # Functionally for combining coverage results
4
+ # Namespace for the pieces that combine coverage results.
5
+ #
6
+ # `CoverageAccumulator` is the entry point: it folds any number of
7
+ # resultsets together, delegating one file's lines, branches and methods to
8
+ # the combiner modules alongside it.
5
9
  #
6
10
  module Combine
7
- module_function
8
-
9
- #
10
- # Combine two coverage based on the given combiner_module.
11
- #
12
- # Combiners should always be called through this interface,
13
- # as it takes care of short-circuiting of one of the coverages is nil.
14
- #
15
- # @return [Hash]
16
- def combine(combiner_module, coverage_a, coverage_b)
17
- return existing_coverage(coverage_a, coverage_b) if empty_coverage?(coverage_a, coverage_b)
18
-
19
- combiner_module.combine(coverage_a, coverage_b)
20
- end
21
-
22
- def empty_coverage?(coverage_a, coverage_b)
23
- !(coverage_a && coverage_b)
24
- end
25
-
26
- def existing_coverage(coverage_a, coverage_b)
27
- coverage_a || coverage_b
28
- end
29
11
  end
30
12
  end