mutineer 1.8.2 → 1.9.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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +611 -0
  3. data/README.md +84 -49
  4. data/lib/mutineer/changed_lines.rb +2 -1
  5. data/lib/mutineer/child_cause.rb +149 -0
  6. data/lib/mutineer/child_stdout.rb +12 -1
  7. data/lib/mutineer/cli.rb +536 -58
  8. data/lib/mutineer/command_redact.rb +38 -0
  9. data/lib/mutineer/config.rb +112 -6
  10. data/lib/mutineer/coverage_map.rb +1345 -156
  11. data/lib/mutineer/daemon_backend.rb +40 -17
  12. data/lib/mutineer/daemon_client.rb +63 -15
  13. data/lib/mutineer/daemon_server.rb +75 -5
  14. data/lib/mutineer/database_urls.rb +189 -0
  15. data/lib/mutineer/external_backend.rb +47 -1
  16. data/lib/mutineer/fs_path.rb +125 -0
  17. data/lib/mutineer/isolation.rb +374 -20
  18. data/lib/mutineer/job_plan.rb +349 -39
  19. data/lib/mutineer/minitest_integration/file_order.rb +56 -0
  20. data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
  21. data/lib/mutineer/minitest_integration.rb +26 -58
  22. data/lib/mutineer/mutant_id.rb +4 -1
  23. data/lib/mutineer/mutation.rb +6 -1
  24. data/lib/mutineer/mutator_registry.rb +6 -3
  25. data/lib/mutineer/mutators/collection_method.rb +195 -6
  26. data/lib/mutineer/mutators/literal_mutation.rb +33 -3
  27. data/lib/mutineer/mutators/range_literal.rb +78 -29
  28. data/lib/mutineer/mutators/rescue_body.rb +126 -0
  29. data/lib/mutineer/mutators/return_nil.rb +23 -6
  30. data/lib/mutineer/output_filter.rb +451 -0
  31. data/lib/mutineer/pairing.rb +463 -31
  32. data/lib/mutineer/parser.rb +14 -0
  33. data/lib/mutineer/project_path.rb +15 -8
  34. data/lib/mutineer/rails_worker_db.rb +356 -6
  35. data/lib/mutineer/reach_probe.rb +229 -0
  36. data/lib/mutineer/redaction.rb +264 -32
  37. data/lib/mutineer/reload_guards.rb +460 -39
  38. data/lib/mutineer/reporter.rb +465 -57
  39. data/lib/mutineer/result.rb +6 -7
  40. data/lib/mutineer/run_seed.rb +16 -0
  41. data/lib/mutineer/runner.rb +175 -51
  42. data/lib/mutineer/source_bytes.rb +187 -0
  43. data/lib/mutineer/statement_lines.rb +108 -14
  44. data/lib/mutineer/test_runners/rspec.rb +118 -10
  45. data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
  46. data/lib/mutineer/test_runners.rb +3 -2
  47. data/lib/mutineer/triage.rb +55 -10
  48. data/lib/mutineer/version.rb +1 -1
  49. data/lib/mutineer.rb +1 -0
  50. data/skills/mutineer/SKILL.md +12 -4
  51. metadata +12 -1
@@ -41,13 +41,22 @@ module Mutineer
41
41
  # recorded even when a new id is also listed, since the old entry still
42
42
  # over-matches other files), `id_map` (every reported mutant's new id =>
43
43
  # its old-format id), `dropped_id_map` (the same for each dropped repeat of
44
- # an edit: its id is still current for `mutineer migrate`) and `declined`
44
+ # an edit: its id is still current for `mutineer migrate`), `copies` (each
45
+ # kept mutant's id => the ids dropped as repeats of that edit, in drop
46
+ # order, #374) and `declined`
45
47
  # (operator name => count of candidates that operator found and did not
46
48
  # mutate, {Mutators::Base#declined}; only counts above zero, keys sorted).
47
49
  # The name is the {MutatorRegistry::ALL} key, or the class's `to_s` for an
48
50
  # operator outside the registry. `--only` narrows `declined`, and `--since`
49
51
  # does not: a declined candidate has no mutation to place on a line.
50
52
  #
53
+ # The extras also hold `subjects` (how many methods were discovered, each
54
+ # counted once across path spellings) and `unmutated_subjects` (#386): one
55
+ # `{subject:, file:, line:}` hash per method the selected operators gave no
56
+ # mutation, an empty method included, sorted by file, line and name. It is
57
+ # decided before suppression and repeat removal, so a method whose mutants
58
+ # are all ignored is not listed. `--only` narrows both; `--since` does not.
59
+ #
51
60
  # Mutations are collected in {MutatorRegistry::ALL} order, whatever the
52
61
  # order of `operator_classes`. Two operators can give the same mutated
53
62
  # source (#275), and the copy of the registry-first operator is the one
@@ -62,7 +71,8 @@ module Mutineer
62
71
  # @param operator_classes [Array<Class>] resolved operators.
63
72
  # @return [Array(Array, Array<Result>, Hash<String,String>, Hash{Symbol => Hash})]
64
73
  # jobs, ignored, source_map, and extras (`:legacy_ignore_matches`,
65
- # `:id_map`, `:dropped_id_map`, `:declined`).
74
+ # `:id_map`, `:dropped_id_map`, `:copies`, `:declined`, `:subjects`,
75
+ # `:unmutated_subjects`).
66
76
  def self.collect_jobs(config, operator_classes)
67
77
  # Registry order, so a repeat across operators keeps the registry-first
68
78
  # copy (#275). A class outside ALL goes last; ties keep the input order.
@@ -84,9 +94,14 @@ module Mutineer
84
94
  legacy_ignore_matches = {}
85
95
  id_map = {}
86
96
  dropped_id_map = {}
97
+ # Kept id => dropped ids. Separate from dropped_id_map, which migrate
98
+ # reads as dropped id => old-format id (#374).
99
+ copies = {}
87
100
  declined = Hash.new(0)
101
+ # [id_path, def offset] => nil, or the method's entry when it got no mutation.
102
+ subjects = {}
88
103
  Project.discover(config.sources, only: config.only).each do |subject|
89
- source = (source_map[subject.file] ||= File.read(subject.file, encoding: "UTF-8"))
104
+ source = (source_map[subject.file] ||= Parser.read(subject.file))
90
105
  disabled = (disabled_map[subject.file] ||= suppress_map(source, subject.file))
91
106
  # A repeated operator name runs once, so its declines count once.
92
107
  mutations = operator_classes.uniq.flat_map do |klass|
@@ -97,16 +112,29 @@ module Mutineer
97
112
  end
98
113
  id_path = (id_paths[subject.file] ||= ProjectPath.relative(subject.file, config.project_root))
99
114
  decls = name_decls[[id_path, subject.qualified_name]]
100
- ordinal = (decls[subject.def_node.location.start_offset] ||= decls.size)
115
+ loc = subject.def_node.location
116
+ ordinal = (decls[loc.start_offset] ||= decls.size)
117
+ # The first path spelling of a def wins; later spellings are the same method.
118
+ key = [id_path, loc.start_offset]
119
+ unless subjects.key?(key)
120
+ # end_line is for --since narrowing (#420); the public list keeps
121
+ # only subject/file/line.
122
+ subjects[key] = {
123
+ subject: subject.qualified_name, file: subject.file,
124
+ line: loc.start_line, end_line: loc.end_line,
125
+ unmutated: mutations.empty?
126
+ }
127
+ end
101
128
  ids = MutantId.for_subject(subject, source, mutations, path: id_path, subject_ordinal: ordinal)
102
129
  legacy_ids = MutantId.legacy_for_subject(subject, source, mutations)
103
130
  lines = mutations.map { |m| source.byteslice(0, m.start_offset).count("\n") + 1 }
104
131
  keys = result_keys(mutations, source, lines)
105
132
  # A repeat of an earlier edit on the same line, from any operator, is
106
133
  # dropped (#159, #275), separately among the run and the ignored
107
- # mutants, so an ignored copy never hides a copy that should run. A
108
- # dropped copy is not reported, but its id stays current for migrate.
109
- seen = { run: Set.new, ignored: Set.new }
134
+ # mutants, so an ignored copy never hides a copy that should run. The
135
+ # dropped id stays current for migrate, and `copies` names it on the
136
+ # kept mutant so a report can show it (#374).
137
+ seen = { run: {}, ignored: {} }
110
138
  mutations.each_with_index do |mutation, i|
111
139
  id = ids[i]
112
140
  legacy = legacy_ids[i]
@@ -117,10 +145,13 @@ module Mutineer
117
145
  if ignore_set.include?(legacy)
118
146
  (legacy_ignore_matches[legacy] ||= []) << { id: id, file: id_path, subject: subject.qualified_name }
119
147
  end
120
- unless seen[ignored ? :ignored : :run].add?(keys[i])
148
+ bucket = seen[ignored ? :ignored : :run]
149
+ if (kept_id = bucket[keys[i]])
121
150
  dropped_id_map[id] = legacy
151
+ (copies[kept_id] ||= []) << id
122
152
  next
123
153
  end
154
+ bucket[keys[i]] = id
124
155
 
125
156
  id_map[id] = legacy
126
157
  if ignored
@@ -132,8 +163,23 @@ module Mutineer
132
163
  end
133
164
  end
134
165
  declined = declined.select { |_, count| count.positive? }.sort.to_h
166
+ infos = subjects.values
167
+ unmutated = public_unmutated(infos)
135
168
  [jobs, ignored_results, source_map, { legacy_ignore_matches: legacy_ignore_matches, id_map: id_map,
136
- dropped_id_map: dropped_id_map, declined: declined }]
169
+ dropped_id_map: dropped_id_map, copies: copies, declined: declined,
170
+ subjects: infos.size, unmutated_subjects: unmutated,
171
+ subject_ranges: infos }]
172
+ end
173
+
174
+ # The reportable `{subject, file, line}` rows for methods with no mutants.
175
+ #
176
+ # @api private
177
+ # @param infos [Array<Hash>] internal subject rows from {collect_jobs}.
178
+ # @return [Array<Hash>]
179
+ def self.public_unmutated(infos)
180
+ infos.select { |h| h[:unmutated] }
181
+ .map { |h| { subject: h[:subject], file: h[:file], line: h[:line] } }
182
+ .sort_by { |h| [h[:file], h[:line], h[:subject]] }
137
183
  end
138
184
 
139
185
  # One key per mutation; two mutations share a key exactly when they are on
@@ -168,9 +214,8 @@ module Mutineer
168
214
  #
169
215
  # @param coverage_map [Mutineer::CoverageMap] the built or loaded map.
170
216
  # @param framework [String, nil] the run's framework (nil is Minitest, as in
171
- # {TestRunners.for}). For Minitest, the message names the seed: an
172
- # order-dependent suite can pass in a random order and fail in the
173
- # pinned one (#282).
217
+ # {TestRunners.for}). The message names the seed: an order-dependent
218
+ # suite can pass in a random order and fail in the pinned one (#282).
174
219
  # @return [void]
175
220
  # @raise [Mutineer::SmokeCheckError] when any captured test failed clean,
176
221
  # or when no test recorded coverage and a capture failed.
@@ -182,11 +227,79 @@ module Mutineer
182
227
  files = coverage_map.failed_clean_tests
183
228
  return if files.empty?
184
229
 
185
- seed_hint = TestRunners.for(framework) == TestRunners::Minitest ? minitest_seed_hint : ""
230
+ # A clean check that hit its deadline is not a red suite: no test failed,
231
+ # so the seed hint does not apply.
232
+ timeouts = (coverage_map.clean_timeouts || {}).slice(*files)
233
+ files -= timeouts.keys
234
+ raise SmokeCheckError, clean_timeout_message(timeouts) if files.empty?
235
+
236
+ seed_hint = seed_hint_for(framework)
186
237
  # The hint opens with "the unmutated suite is not green", so the files
187
- # follow it rather than repeat that sentence (#360).
238
+ # follow it rather than repeat that sentence (#360). Failure names and
239
+ # the first error follow when capture stored them. A check that timed
240
+ # out next to a red file is named too, so one run reports both.
241
+ also = timeouts.empty? ? "" : " Also, #{clean_timeout_message(timeouts)}"
242
+ detail = clean_failure_detail(coverage_map, files)
188
243
  raise SmokeCheckError,
189
- "#{ExternalBackend.generic_env_hint}. Failed: #{files.join(', ')}.#{seed_hint}"
244
+ "#{ExternalBackend.generic_env_hint}. Failed: #{files.join(', ')}.#{seed_hint}#{also}#{detail}"
245
+ end
246
+
247
+ # Says which clean checks hit their deadline and how to raise it. It names
248
+ # the longest deadline that passed: the combined check's deadline can be
249
+ # above --capture-timeout, so any lower value would change nothing.
250
+ #
251
+ # @api private
252
+ # @param timeouts [Hash{String => Numeric}] {CoverageMap#clean_timeouts}.
253
+ # @return [String]
254
+ def self.clean_timeout_message(timeouts)
255
+ checks = timeouts.map do |rel, seconds|
256
+ what = rel == "combined suite" ? "the combined clean check (the clean-check test files in one process)" : rel
257
+ "#{what} timed out after #{seconds}s"
258
+ end
259
+ "the unmutated suite did not finish: #{checks.join('; ')}. " \
260
+ "Mutineer stopped it, so it reports no failing test. " \
261
+ "Set --capture-timeout (capture_timeout in the config) above #{timeouts.values.max}."
262
+ end
263
+
264
+ # Joins capped failure reports for the unclean files (#360).
265
+ #
266
+ # @api private
267
+ # @param coverage_map [Mutineer::CoverageMap]
268
+ # @param files [Array<String>]
269
+ # @return [String] a leading newline plus the reports, or empty.
270
+ def self.clean_failure_detail(coverage_map, files)
271
+ details = coverage_map.failed_clean_details || {}
272
+ parts = files.filter_map do |file|
273
+ text = details[file].to_s.strip
274
+ next if text.empty?
275
+
276
+ "#{file}:\n#{text}"
277
+ end
278
+ return "" if parts.empty?
279
+
280
+ text = parts.join("\n")
281
+ limit = CoverageMap::CLEAN_FAILURE_DETAIL_LIMIT
282
+ return "\n#{text}" if text.length <= limit
283
+
284
+ "\n#{text[0, limit].rstrip}\n…"
285
+ end
286
+
287
+ # The not-green hint for this framework, or empty when it has no seed.
288
+ #
289
+ # @api private
290
+ # @param framework [String, nil]
291
+ # @return [String]
292
+ def self.seed_hint_for(framework)
293
+ # `==`, not `case`: `when` uses Module#===, and a module is not an
294
+ # instance of itself.
295
+ runner = TestRunners.for(framework)
296
+ if runner == TestRunners::Minitest
297
+ minitest_seed_hint
298
+ elsif runner == TestRunners::RSpec
299
+ rspec_seed_hint
300
+ else
301
+ ""
302
+ end
190
303
  end
191
304
 
192
305
  # Names the configured seed, and accounts for helper overrides.
@@ -202,6 +315,21 @@ module Mutineer
202
315
  "Change SEED, including helper settings, to choose another order."
203
316
  end
204
317
 
318
+ # Names the seed a random RSpec order uses. A defined order is unchanged.
319
+ #
320
+ # @api private
321
+ # @return [String]
322
+ def self.rspec_seed_hint
323
+ seed = TestRunners::RSpecSeed.advertised_seed
324
+ " Mutineer runs a random RSpec order with seed #{seed}, unless you set a seed " \
325
+ "(--seed, --order rand:N, SPEC_OPTS, .rspec, or config.seed). " \
326
+ "RSpec reads SPEC_OPTS after the command line, so a seed there replaces " \
327
+ "--seed on the rspec command. " \
328
+ "A defined order is left as it is. " \
329
+ "An order-dependent example can fail here and pass in another order. " \
330
+ "Check the listed spec files with the same seed."
331
+ end
332
+
205
333
  # The reload mutant text for a def in a concern's `included` or `prepended`
206
334
  # block (#297): the mutated file with one statement added just before the
207
335
  # hook call, `::Mutineer::ConcernBlocks.store_hook_def do <mutated def> end`.
@@ -218,11 +346,15 @@ module Mutineer
218
346
  # @param mutation [Mutineer::Mutation] the mutation.
219
347
  # @param subject [Mutineer::Subject] a subject with a {Subject#reload_hook_call}.
220
348
  # @param source [String] the original source text.
349
+ # @param reach_path [String, nil] sentinel for a `:maybe` mutant (#345).
350
+ # The placed def records a run of the mutated expression.
351
+ # @param reach_key [String, nil] fiber-local key for `reach_path`.
221
352
  # @return [Array(String, Hash{Symbol => Object})] the mutant text, and the
222
353
  # original def's `line`, `name` and `singleton`.
223
- def self.hook_mutant(mutated, mutation, subject, source)
354
+ def self.hook_mutant(mutated, mutation, subject, source, reach_path: nil, reach_key: nil)
224
355
  at = subject.reload_hook_call.location.start_offset
225
- stored = "::Mutineer::ConcernBlocks.store_hook_def do\n#{Isolation.mutated_def(mutation, subject, source)}\nend; "
356
+ stored = "::Mutineer::ConcernBlocks.store_hook_def do\n" \
357
+ "#{Isolation.mutated_def(mutation, subject, source, reach_path: reach_path, reach_key: reach_key)}\nend; "
226
358
  [mutated.byteslice(0...at) + stored + mutated.byteslice(at..),
227
359
  { line: subject.def_node.location.start_line, name: subject.name, singleton: subject.singleton }]
228
360
  end
@@ -239,8 +371,8 @@ module Mutineer
239
371
  # multi-line `a && b`, #254), and a first or last line that holds other
240
372
  # code (the opener of a one-line block, #289), but never the `def` line.
241
373
  # Those tests may not have run the mutant, so the answer is
242
- # `[:maybe, abs_test_paths]` and {selection_verdict} treats a survivor as
243
- # no_coverage.
374
+ # `[:maybe, abs_test_paths]`. {selection_verdict} keeps a survivor that
375
+ # the probe saw run, and leaves one it did not see as no_coverage (#345).
244
376
  #
245
377
  # An empty selection is `:ran_at_load` when the mutant's line ran while the
246
378
  # app booted or its class loaded ({ran_at_load?}): no test ran it, but the
@@ -284,23 +416,28 @@ module Mutineer
284
416
  return [:verdict, Result.no_coverage]
285
417
  end
286
418
 
287
- [kind, coverage_map.order_tests(source_file, chosen).map { |t| File.expand_path(t, coverage_map.project_root) }]
419
+ # The clean checks name each test by its real path (ProjectPath.absolute).
420
+ # A random RSpec order hashes that name, so a mutant run uses the same
421
+ # spelling, or a symlinked root (/var on macOS) reorders the examples (#282).
422
+ [kind, coverage_map.order_tests(source_file, chosen).map { |t| ProjectPath.absolute(t, coverage_map.project_root) }]
288
423
  end
289
424
 
290
425
  # The verdict of a mutant whose tests {coverage_selection} chose as `:maybe`:
291
426
  # they ran its statement but may not have run the mutated code. A kill is a
292
- # real kill. A survivor is no_coverage, because the run cannot tell "never
293
- # ran" from "ran unchecked" (#254); like any no_coverage mutant it has no
294
- # `--matrix` row. An error or a timeout stays what it is: the mutant ran, and
295
- # a failure is reported, not hidden. A kill keeps its whole `--matrix` row.
296
- # A result of any other selection comes back unchanged.
427
+ # real kill. A survivor the probe saw run is a real survivor and keeps its
428
+ # `--matrix` row (#345). A survivor the probe did not see is no_coverage,
429
+ # the same as any other unreached mutant, and has no `--matrix` row (#254).
430
+ # An error or a timeout stays what it is: the mutant ran, and a failure is
431
+ # reported, not hidden. A kill keeps its whole `--matrix` row. A result of
432
+ # any other selection comes back unchanged.
297
433
  #
298
434
  # @param kind [Symbol, nil, false] the first element of {coverage_selection}'s
299
435
  # answer, or a falsy value when no selection ran.
300
436
  # @param result [Mutineer::Result] the mutant's verdict.
437
+ # @param reached [Boolean] true when the mutated expression ran in the child.
301
438
  # @return [Mutineer::Result]
302
- def self.selection_verdict(kind, result)
303
- return result unless kind == :maybe && result.survived?
439
+ def self.selection_verdict(kind, result, reached: false)
440
+ return result unless kind == :maybe && result.survived? && !reached
304
441
 
305
442
  result.with(status: :no_coverage, kills: nil)
306
443
  end
@@ -315,9 +452,20 @@ module Mutineer
315
452
  # count at load without it. The `def` line never counts: Ruby counts it
316
453
  # when the method is defined.
317
454
  #
455
+ # A `define_method` or `define_singleton_method` call stores its block.
456
+ # Reloading the method does not run the class that already called it, such
457
+ # as `Greeter` calling `greets`, so a mutant in that call or in the stored
458
+ # block is the original method when the call's line ran at load (#435).
459
+ # The line of `each` on an empty array counts without the block, so a block
460
+ # that is not one of those calls stays scored (#286). Re-running the
461
+ # caller's class body is #389. `CI` is not read here. Rails eager load,
462
+ # which the test env turns on when `CI` is set, is what puts the line in
463
+ # the load set.
464
+ #
318
465
  # A one-line or endless def keeps its body on the `def` line, so it uses
319
466
  # the method's own call count at load instead (#209), for code that runs
320
- # each time the method does ({StatementLines.runs_with_method?}).
467
+ # each time the method does ({StatementLines.runs_with_method?}). A block
468
+ # on that `def` line is unchanged (#389).
321
469
  #
322
470
  # @param source_file [String] the mutated source file path.
323
471
  # @param mutation [Mutineer::Mutation] the mutation.
@@ -337,8 +485,12 @@ module Mutineer
337
485
  end
338
486
 
339
487
  body = (loc.start_line..loc.end_line)
340
- lines = StatementLines.for(subject.def_node, source, mutation.start_offset)
341
- lines.any? { |l| l != def_line && body.cover?(l) && coverage_map.ran_at_load?(source_file, l) }
488
+ ran = lambda do |lines|
489
+ lines.any? { |l| l != def_line && body.cover?(l) && coverage_map.ran_at_load?(source_file, l) }
490
+ end
491
+ return true if ran.call(StatementLines.for(subject.def_node, source, mutation.start_offset))
492
+
493
+ ran.call(StatementLines.stored_method_lines(subject.def_node, source, mutation.start_offset))
342
494
  end
343
495
 
344
496
  # A survivor whose line ran at load becomes `ran_at_load` ({ran_at_load?});
@@ -450,40 +602,198 @@ module Mutineer
450
602
  nil
451
603
  end
452
604
 
605
+ # Counts scheduled mutants in each source pairing skipped.
606
+ # One source is parsed at a time. A source that raises records nil and
607
+ # an error string. This method prints nothing. The caller prints the error.
608
+ # An empty job list counts 0. `--only` and `--since` narrow the count.
609
+ # They do not drop the source. Ignored mutants are not in the count.
610
+ # The count is not a validity check.
611
+ #
612
+ # The third element holds the id maps from those sources, taken before
613
+ # `--since` narrows the count. A real run drops unpaired sources before
614
+ # {collect_jobs}, so the unmatched-ignore check merges these maps back in
615
+ # and agrees with a dry run of the same files (#377). A source that could
616
+ # not be read contributes no ids.
617
+ #
618
+ # @param config [Mutineer::Config] run configuration. Reads `unpaired_sources`.
619
+ # @param operator_classes [Array<Class>] resolved operators.
620
+ # @return [Array(Hash{String => Integer, nil}, Array<Hash>, Hash)]
621
+ # file => count, in source order; `{file:, error:}` failures; and
622
+ # `{id_map:, dropped_id_map:, legacy_ignore_matches:}` for those sources.
623
+ def self.unpaired_counts(config, operator_classes)
624
+ counts = {}
625
+ failures = []
626
+ pending_jobs = []
627
+ pending_ignored = []
628
+ pending_map = {}
629
+ pending_files = []
630
+ id_map = {}
631
+ dropped_id_map = {}
632
+ legacy_ignore_matches = {}
633
+
634
+ config.unpaired_sources.each do |file|
635
+ copy = config.dup
636
+ copy.sources = [file]
637
+ begin
638
+ jobs, ignored, source_map, file_extras = collect_jobs(copy, operator_classes)
639
+ rescue StandardError => e
640
+ counts[file] = nil
641
+ failures << { file: file, error: "#{e.class}: #{e.message}" }
642
+ next
643
+ end
644
+ id_map.merge!(file_extras[:id_map])
645
+ dropped_id_map.merge!(file_extras[:dropped_id_map])
646
+ file_extras[:legacy_ignore_matches].each do |old, hits|
647
+ (legacy_ignore_matches[old] ||= []).concat(hits)
648
+ end
649
+
650
+ if jobs.empty? || !config.since
651
+ counts[file] = jobs.size
652
+ else
653
+ pending_jobs.concat(jobs)
654
+ pending_ignored.concat(ignored)
655
+ pending_map.merge!(source_map)
656
+ pending_files << file
657
+ counts[file] = 0
658
+ end
659
+ end
660
+
661
+ apply_unpaired_since!(counts, pending_jobs, pending_ignored, pending_map, pending_files, config) if config.since
662
+ [counts, failures, { id_map: id_map, dropped_id_map: dropped_id_map,
663
+ legacy_ignore_matches: legacy_ignore_matches }]
664
+ end
665
+
666
+ # Replaces pending unpaired counts with the jobs `--since` keeps.
667
+ # One {scope_since} call covers every file that produced a job.
668
+ #
669
+ # @api private
670
+ # @param counts [Hash{String => Integer, nil}] file => count, updated in place.
671
+ # @param jobs [Array] scheduled jobs from those files.
672
+ # @param ignored [Array<Mutineer::Result>] suppressed mutants from those files.
673
+ # @param source_map [Hash{String => String}] source text by file.
674
+ # @param files [Array<String>] unpaired files that produced jobs.
675
+ # @param config [Mutineer::Config] run configuration.
676
+ # @return [void]
677
+ def self.apply_unpaired_since!(counts, jobs, ignored, source_map, files, config)
678
+ return if files.empty?
679
+
680
+ copy = config.dup
681
+ copy.sources = files
682
+ kept, = scope_since(jobs, ignored, source_map, copy)
683
+ tally = Hash.new(0)
684
+ kept.each do |subject,|
685
+ key = unpaired_path(subject.file, files, config.project_root)
686
+ tally[key] += 1 if key
687
+ end
688
+ files.each { |file| counts[file] = tally[file] }
689
+ end
690
+
691
+ # The unpaired path spelling that names the same file as `subject_file`.
692
+ #
693
+ # @api private
694
+ # @param subject_file [String] path stored on the subject.
695
+ # @param files [Array<String>] unpaired paths in the caller's spelling.
696
+ # @param project_root [String] root for relative paths.
697
+ # @return [String, nil] the matching spelling, or nil.
698
+ def self.unpaired_path(subject_file, files, project_root)
699
+ absolute = File.expand_path(subject_file, project_root)
700
+ files.find { |file| File.expand_path(file, project_root) == absolute }
701
+ end
702
+
453
703
  # Narrows the jobs and the suppressed (ignored) results to --since when it
454
704
  # is set, as the dry run does, so a scoped report lists only mutants on
455
705
  # changed lines. Also returns how many mutants there were before narrowing
456
706
  # (nil without --since), so the CLI can tell "the changes held nothing to
457
- # test" from "nothing was mutable at all".
707
+ # test" from "nothing was mutable at all". When `extras` is given, also
708
+ # narrows `subjects` / `unmutated_subjects` to methods whose def range
709
+ # intersects the diff (#420), and drops `copies` whose kept mutant is out
710
+ # of scope (#374).
458
711
  #
459
712
  # @param jobs [Array] (subject, mutation, id) entries to run.
460
713
  # @param ignored_results [Array<Mutineer::Result>] suppressed mutants.
461
714
  # @param source_map [Hash{String => String}] source text by file.
462
715
  # @param config [Mutineer::Config] run configuration.
716
+ # @param extras [Hash, nil] {collect_jobs} extras to narrow in place.
463
717
  # @return [Array(Array, Array<Mutineer::Result>, Integer), Array(Array, Array<Mutineer::Result>, nil)]
464
718
  # the jobs to run, the ignored results to report, and the count before --since.
465
- def self.scope_since(jobs, ignored_results, source_map, config)
466
- return [jobs, ignored_results, nil] unless config.since
719
+ def self.scope_since(jobs, ignored_results, source_map, config, extras: nil)
720
+ unless config.since
721
+ extras&.delete(:subject_ranges)
722
+ return [jobs, ignored_results, nil]
723
+ end
467
724
 
468
- # One pass, so git computes the changed lines once.
725
+ # One pass, so git computes the changed lines once for jobs and subjects.
469
726
  ignored_entries = ignored_results.map { |r| [r.subject, r.mutation, r] }
470
- kept = filter_since(jobs + ignored_entries, source_map, config)
727
+ range_files = extras&.fetch(:subject_ranges, [])&.map { |h| h[:file] } || []
728
+ files = (config.sources + range_files).uniq
729
+ changed = ChangedLines.for(ref: config.since, files: files, project_root: config.project_root)
730
+ kept = filter_since(jobs + ignored_entries, source_map, config, changed: changed)
471
731
  kept_ignored, kept_jobs = kept.partition { |entry| entry.last.is_a?(Result) }
732
+ if extras
733
+ narrow_subjects_since!(extras, config, changed: changed)
734
+ narrow_copies!(extras, kept_jobs, kept_ignored.map(&:last))
735
+ end
472
736
  [kept_jobs, kept_ignored.map(&:last), jobs.size + ignored_results.size]
473
737
  end
474
738
 
739
+ # Drops copy lists whose kept mutant `--since` removed (#374). A dropped
740
+ # copy shares that mutant's line, so it leaves with the mutant.
741
+ #
742
+ # @api private
743
+ # @param extras [Hash] {collect_jobs} extras.
744
+ # @param kept_jobs [Array] jobs still in scope; each id is the last element.
745
+ # @param kept_ignored [Array<Mutineer::Result>] ignored mutants still in scope.
746
+ # @return [void]
747
+ def self.narrow_copies!(extras, kept_jobs, kept_ignored)
748
+ copies = extras[:copies]
749
+ return unless copies
750
+
751
+ keep = Set.new(kept_jobs.map(&:last))
752
+ kept_ignored.each { |result| keep.add(result.id) }
753
+ extras[:copies] = copies.select { |id, _list| keep.include?(id) }
754
+ end
755
+
756
+ # Keeps `subjects` / `unmutated_subjects` for methods whose def range
757
+ # intersects `--since` changed lines (#420). Drops the internal
758
+ # `subject_ranges` key afterward.
759
+ #
760
+ # @api private
761
+ # @param extras [Hash] {collect_jobs} extras.
762
+ # @param config [Mutineer::Config]
763
+ # @param changed [Hash{String => Set<Integer>}, nil] precomputed map from
764
+ # {ChangedLines.for}; when nil, this method loads it for the range files.
765
+ # @return [void]
766
+ def self.narrow_subjects_since!(extras, config, changed: nil)
767
+ ranges = extras.delete(:subject_ranges) || []
768
+ return if ranges.empty?
769
+
770
+ files = ranges.map { |h| h[:file] }.uniq
771
+ changed ||= ChangedLines.for(ref: config.since, files: files, project_root: config.project_root)
772
+ in_scope = ranges.select do |h|
773
+ lines = changed.fetch(FsPath.expand(h[:file], config.project_root), Set.new)
774
+ next false if lines.empty?
775
+
776
+ (h[:line]..h[:end_line]).any? { |ln| lines.include?(ln) }
777
+ end
778
+ extras[:subjects] = in_scope.size
779
+ extras[:unmutated_subjects] = public_unmutated(in_scope)
780
+ end
781
+
475
782
  # --since: keep only jobs whose mutation lands on a line changed since the git
476
783
  # ref. Composes with coverage selection (it only narrows the job list; each
477
784
  # surviving mutant still goes through Runner.run's coverage check). A file with
478
785
  # no changed lines (absent from the diff) contributes no jobs. Line is computed
479
786
  # exactly as Runner.run does, from the already-read source in source_map.
480
- def self.filter_since(jobs, source_map, config)
481
- changed = ChangedLines.for(ref: config.since, files: config.sources,
482
- project_root: config.project_root)
787
+ #
788
+ # @param changed [Hash{String => Set<Integer>}, nil] precomputed map from
789
+ # {ChangedLines.for}; when nil, this method loads it for `config.sources`.
790
+ def self.filter_since(jobs, source_map, config, changed: nil)
791
+ changed ||= ChangedLines.for(ref: config.since, files: config.sources,
792
+ project_root: config.project_root)
483
793
  jobs.select do |subject, mutation|
484
794
  source = source_map[subject.file]
485
795
  line = source.byteslice(0, mutation.start_offset).count("\n") + 1
486
- abs = File.expand_path(subject.file, config.project_root)
796
+ abs = FsPath.expand(subject.file, config.project_root)
487
797
  changed.fetch(abs, []).include?(line)
488
798
  end
489
799
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mutineer
4
+ class MinitestIntegration
5
+ # Runs each file's test classes in the order of `rank`, and shuffles the
6
+ # classes inside one file with Minitest.seed alone (#203, #282). A
7
+ # shuffle of the whole class list would put the same file's classes in a
8
+ # different order when another file is added, so a clean check of one
9
+ # file and a mutant run of several files would not share an order.
10
+ # Minitest still runs serial classes before `parallelize_me!` classes.
11
+ # Only the child's own class list gets {FileOrder}.
12
+ #
13
+ # @api private
14
+ # @param rank [Hash{Class => Integer}] each class's file position.
15
+ # @return [void]
16
+ def self.keep_file_order(rank)
17
+ FileOrder.rank = rank
18
+ ::Minitest::Runnable.runnables.extend(FileOrder)
19
+ end
20
+
21
+ # Extends the list of test classes. `shuffle` orders files by {rank} and
22
+ # shuffles each file on its own.
23
+ module FileOrder
24
+ class << self
25
+ # Each test class's file position.
26
+ #
27
+ # @return [Hash{Class => Integer}, nil]
28
+ attr_accessor :rank
29
+ end
30
+
31
+ # Files in rank order. Classes that share a file are shuffled with
32
+ # `Random.new(Minitest.seed)`, so their order does not depend on the
33
+ # other files in the run. A nil seed keeps load order.
34
+ #
35
+ # @return [Array<Class>]
36
+ def shuffle(*)
37
+ rank = FileOrder.rank || {}
38
+ seed = ::Minitest.seed
39
+ grouped = group_by { |klass| rank.fetch(klass, rank.size) }
40
+ grouped.sort.flat_map do |_index, classes|
41
+ next classes if classes.size < 2 || seed.nil?
42
+
43
+ classes.shuffle(random: Random.new(seed.to_i))
44
+ end
45
+ end
46
+
47
+ # Minitest 5.15 and older drop empty classes before the shuffle, so the
48
+ # filtered list keeps the file order too.
49
+ #
50
+ # @return [Array<Class>]
51
+ def reject(...)
52
+ super.extend(FileOrder)
53
+ end
54
+ end
55
+ end
56
+ end