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
@@ -5,6 +5,7 @@ require "digest"
5
5
  require "fileutils"
6
6
  require "rbconfig"
7
7
  require "coverage"
8
+ require "prism"
8
9
  require "set"
9
10
  require_relative "minitest_integration"
10
11
  require_relative "test_runners"
@@ -21,7 +22,10 @@ module Mutineer
21
22
  # Maps `(source_file, line) -> [test_files]` so each mutant runs only against
22
23
  # the tests that actually exercise its line. Built once, then queried per
23
24
  # mutant via #tests_for. Persisted to .mutineer/coverage.json with a
24
- # content-based digest that rebuilds the map whenever any tracked file changes.
25
+ # content-based digest. A later run recaptures every test when a run input,
26
+ # a loaded file, or the shape of a source changes. A change that stays inside
27
+ # method bodies, without moving a line or editing a method that ran at load,
28
+ # recaptures only the tests in that source's map entries (#421).
25
29
  #
26
30
  # Keys are "file:line" strings (relative to project_root) everywhere, in
27
31
  # memory and on disk, so load/save needs no key transformation.
@@ -29,6 +33,11 @@ module Mutineer
29
33
  # Seconds per coverage subprocess before the parent kills it.
30
34
  DEFAULT_CAPTURE_TIMEOUT = 120
31
35
 
36
+ # The combined clean check runs the clean-check test files in one process.
37
+ # Its deadline is this factor times the summed capture seconds of those
38
+ # files, and never less than the capture timeout.
39
+ COMBINED_TIMEOUT_FACTOR = 1.5
40
+
32
41
  # File descriptor in a capture subprocess that carries the JSON result to
33
42
  # the parent. Stdout stays free for test output, which goes to File::NULL.
34
43
  RESULT_FD = 3
@@ -42,7 +51,37 @@ module Mutineer
42
51
  # and either can quote DATABASE_URL.
43
52
  REDACTION_PATH = File.expand_path("redaction.rb", __dir__)
44
53
 
45
- attr_reader :project_root, :failed_test_files, :failed_clean_tests, :phase_a_ran, :map
54
+ # Cap for a failing clean-suite report on the SmokeCheckError line (#360).
55
+ CLEAN_FAILURE_DETAIL_LIMIT = 800
56
+
57
+ # Key in coverage.json that marks a map built after #426. Standalone
58
+ # capture no longer credits every test with lines that ran only while the
59
+ # sources loaded. A standalone cache without this key was built the old
60
+ # way and is rebuilt once. Boot and daemon maps already omitted those
61
+ # lines; a hit there does not read the key.
62
+ NARROW_LOAD_LINES = "narrow_load_lines"
63
+
64
+ # Keys a cache needs before a method-body edit can recapture part of the
65
+ # map (#421). A cache saved before these keys existed is rebuilt once.
66
+ PARTIAL_CACHE_KEYS = %w[run_inputs sources skeletons tests load_method_bodies].freeze
67
+
68
+ # Key for the paired tests that kept load-only lines when the map was
69
+ # saved. {Pairing.infer_tests} reads files that are not in this run. An
70
+ # exact spec replaces split tests, and the content digest does not see a
71
+ # file the run did not select. A standalone hit rebuilds when this list
72
+ # no longer matches.
73
+ LOAD_LINE_PAIRS = "load_line_pairs"
74
+
75
+ attr_reader :project_root, :failed_test_files, :failed_clean_tests, :failed_clean_details,
76
+ :phase_a_ran, :map
77
+
78
+ # Clean checks that hit their deadline, keyed like {#failed_clean_tests},
79
+ # with the deadline in seconds. Each key is also in {#failed_clean_tests},
80
+ # so the run still stops, but {JobPlan.abort_if_unclean!} reports a
81
+ # timeout, not a red suite.
82
+ #
83
+ # @return [Hash{String => Numeric}]
84
+ attr_reader :clean_timeouts
46
85
 
47
86
  # The source lines that ran while the app booted or the sources and the
48
87
  # `--require` files loaded, before any test ran, as a Set of "file:line"
@@ -77,13 +116,17 @@ module Mutineer
77
116
  # @param failed_test_files [Array<String>] test files whose capture failed.
78
117
  # @param project_root [String] project root (for path relativization).
79
118
  # @param failed_clean_tests [Array<String>] test files whose unmutated run failed.
119
+ # @param failed_clean_details [Hash{String => String}] capped failure report per
120
+ # failed clean test (issue #360).
80
121
  # @param load_lines [Array<String>, Set<String>] "file:line" keys that ran at load ({#load_lines}).
81
122
  # @param load_methods [Array<String>, Set<String>] "file:line:column" keys of methods called at load
82
123
  # ({#load_methods}).
83
124
  # @param timings [Hash{String => Float}] capture seconds per test file ({#timings}).
125
+ # @param clean_timeouts [Hash{String => Numeric}] clean checks that timed out ({#clean_timeouts}).
84
126
  # @return [Mutineer::CoverageMap] a query-only map.
85
- def self.from_data(map:, failed_test_files:, project_root:, failed_clean_tests: [], load_lines: [],
86
- load_methods: [], timings: {})
127
+ def self.from_data(map:, failed_test_files:, project_root:, failed_clean_tests: [],
128
+ failed_clean_details: {}, load_lines: [],
129
+ load_methods: [], timings: {}, clean_timeouts: {})
87
130
  instance = allocate
88
131
  instance.instance_variable_set(:@map, map || {})
89
132
  instance.instance_variable_set(:@timings, timings || {})
@@ -91,7 +134,9 @@ module Mutineer
91
134
  instance.instance_variable_set(:@load_methods, Set.new(load_methods || []))
92
135
  instance.instance_variable_set(:@failed_test_files, failed_test_files || [])
93
136
  instance.instance_variable_set(:@failed_clean_tests, failed_clean_tests || [])
94
- instance.instance_variable_set(:@project_root, project_root)
137
+ instance.instance_variable_set(:@failed_clean_details, failed_clean_details || {})
138
+ instance.instance_variable_set(:@clean_timeouts, clean_timeouts || {})
139
+ instance.instance_variable_set(:@project_root, FsPath.read(project_root))
95
140
  instance
96
141
  end
97
142
 
@@ -99,13 +144,13 @@ module Mutineer
99
144
  load_paths: ["lib"], project_root: Dir.pwd,
100
145
  capture_timeout: DEFAULT_CAPTURE_TIMEOUT, boot_path: nil,
101
146
  framework: "minitest", verbose: false, require_paths: [], coverage_from: nil)
102
- @source_paths = Array(source_paths)
103
- @require_paths = Array(require_paths)
104
- @test_paths = Array(test_paths)
105
- @cache_dir = cache_dir
106
- @coverage_from = coverage_from
107
- @load_paths = Array(load_paths)
108
- @project_root = project_root
147
+ @source_paths = Array(source_paths).map { |path| FsPath.read(path) }
148
+ @require_paths = Array(require_paths).map { |path| FsPath.read(path) }
149
+ @test_paths = Array(test_paths).map { |path| FsPath.read(path) }
150
+ @cache_dir = FsPath.read(cache_dir)
151
+ @coverage_from = coverage_from && FsPath.read(coverage_from)
152
+ @load_paths = Array(load_paths).map { |path| FsPath.read(path) }
153
+ @project_root = FsPath.read(project_root)
109
154
  @capture_timeout = capture_timeout
110
155
  @boot_path = boot_path
111
156
  @framework = framework || "minitest"
@@ -113,6 +158,8 @@ module Mutineer
113
158
  @map = {}
114
159
  @failed_test_files = []
115
160
  @failed_clean_tests = []
161
+ @failed_clean_details = {}
162
+ @clean_timeouts = {}
116
163
  @load_lines = Set.new
117
164
  @load_methods = Set.new
118
165
  @timings = {}
@@ -271,6 +318,8 @@ module Mutineer
271
318
  # this source. An `app/` or `lib/` source only accepts a `test/` or
272
319
  # `spec/` file from its mirrored directory, so
273
320
  # `test/foo/bar_upsert_test.rb` does not taint `app/other/bar_upsert.rb`.
321
+ # A concern also accepts the same names at its namespace path, but only
322
+ # when it has no test at its `concerns/` path, as in {Pairing.infer_tests}.
274
323
  # Any other source still matches by basename. A fixture under
275
324
  # `test/fixtures/` and an explicit test both name their source that way.
276
325
  #
@@ -279,19 +328,46 @@ module Mutineer
279
328
  def exact_failed_test?(source_rel)
280
329
  name = File.basename(source_rel, ".rb")
281
330
  base, lib = Pairing.logical_path(source_rel)
331
+ ns = Pairing.namespace_base(source_rel)
332
+ ns = nil if ns && own_concern_test?(source_rel, ns)
282
333
  @failed_test_files.any? do |test_path|
283
334
  next false unless failed_test_target_name(test_path) == name
284
335
 
285
336
  rel = relativize(test_path)
286
337
  dir = File.dirname(rel)
287
338
  if app_or_lib_source?(source_rel) && conventional_test_dir?(dir)
288
- mirrored_test_dir?(rel, base, lib) || spec_mirror?(rel, base, lib)
339
+ mirrored_test_dir?(rel, base, lib) || spec_mirror?(rel, base, lib) ||
340
+ namespace_mirror?(rel, ns)
289
341
  else
290
342
  true
291
343
  end
292
344
  end
293
345
  end
294
346
 
347
+ # True when `test_rel` sits in the namespace mirror of a concern.
348
+ # The check is path only. A test at the concerns path does not block it.
349
+ #
350
+ # @param test_rel [String] project-relative test path.
351
+ # @param ns_base [String, nil] namespace base from {Pairing.namespace_base}.
352
+ # @return [Boolean]
353
+ def namespace_mirror?(test_rel, ns_base)
354
+ return false unless ns_base
355
+
356
+ dirs = [Pairing.mirror_dir("test", ns_base), Pairing.mirror_dir("spec", ns_base)]
357
+ dirs.include?(File.dirname(test_rel))
358
+ end
359
+
360
+ # True when pairing finds a test for this concern at its `concerns/`
361
+ # path, so a test at its namespace path is not paired with it.
362
+ #
363
+ # @param source_rel [String] project-relative concern path.
364
+ # @param ns_base [String] namespace base from {Pairing.namespace_base}.
365
+ # @return [Boolean]
366
+ def own_concern_test?(source_rel, ns_base)
367
+ Pairing.infer_tests(source_rel, project_root: @project_root)
368
+ .any? { |t| !namespace_mirror?(t, ns_base) }
369
+ end
370
+
295
371
  # True when pairing gives this source a mirrored test directory.
296
372
  #
297
373
  # @param source_rel [String] project-relative source path.
@@ -323,12 +399,43 @@ module Mutineer
323
399
 
324
400
  # True when a failed split test file pairs with this source. Directory and
325
401
  # the longer-source check match {Pairing}, so a test outside the mirror, or
326
- # one owned by `user_session.rb`, does not taint `user.rb`.
402
+ # one owned by `user_session.rb`, does not taint `user.rb`. A concern with
403
+ # no test at its `concerns/` path also accepts a split name at its
404
+ # namespace path (#408). A longer concern owns that name only when it
405
+ # pairs with the file. A namespace spec leaves split names unpaired, so
406
+ # a failed split does not mark the concern.
327
407
  #
328
408
  # @param source_rel [String] project-relative source path.
329
409
  # @return [Boolean]
330
410
  def split_failed_test?(source_rel)
331
411
  base, lib = Pairing.logical_path(source_rel)
412
+ return true if failed_split_in?(base, lib, concern: false)
413
+
414
+ ns = Pairing.namespace_base(source_rel)
415
+ return false unless ns
416
+ return false if own_concern_test?(source_rel, ns)
417
+ return false if namespace_spec?(source_rel)
418
+
419
+ failed_split_in?(ns, false, concern: true)
420
+ end
421
+
422
+ # True when the concern's namespace pairing is one spec file.
423
+ # Split names do not join a spec, so a failed split must not mark it.
424
+ #
425
+ # @param source_rel [String] project-relative concern path.
426
+ # @return [Boolean]
427
+ def namespace_spec?(source_rel)
428
+ Pairing.infer_tests(source_rel, project_root: @project_root).any? { |t| t.end_with?("_spec.rb") }
429
+ end
430
+
431
+ # True when one failed split file in the mirror of `base` pairs with it.
432
+ #
433
+ # @param base [String] logical source path without extension.
434
+ # @param lib [Boolean] whether the source originated from lib/.
435
+ # @param concern [Boolean] true for a concern's namespace mirror, so a
436
+ # longer concern file can own the name.
437
+ # @return [Boolean]
438
+ def failed_split_in?(base, lib, concern:)
332
439
  name = File.basename(base)
333
440
  @failed_test_files.any? do |t|
334
441
  test_rel = relativize(t)
@@ -336,6 +443,7 @@ module Mutineer
336
443
  next false unless Pairing.split_entry?(entry, name)
337
444
  next false unless mirrored_test_dir?(test_rel, base, lib)
338
445
  next false if Pairing.claimed_by_longer_source?(@project_root, base, entry, File.dirname(test_rel))
446
+ next false if concern && Pairing.claimed_by_longer_concern?(@project_root, base, entry)
339
447
 
340
448
  true
341
449
  end
@@ -369,7 +477,8 @@ module Mutineer
369
477
 
370
478
  # Shared cache dance for both build paths: hit the digest-keyed cache, else
371
479
  # yield to populate @map and persist it. A digest match is not proof that
372
- # today's unmutated suite still passes — re-check on a cache hit.
480
+ # today's unmutated suite still passes, so a hit re-checks the tests a
481
+ # mutant can run ({#clean_check_test_paths}), alone and then together (#398).
373
482
  #
374
483
  # @param after_fork [Proc, nil] boot-mode fork hook forwarded to a clean re-check.
375
484
  # @yield when the cache is missing or stale.
@@ -379,47 +488,94 @@ module Mutineer
379
488
  return reuse_capture(after_fork) if @coverage_from
380
489
 
381
490
  cached = read_cache
382
- # A cache from before load methods (#209) or test timings (#203) were
383
- # saved rebuilds once. A cache that does not record this process's
491
+ # A cache from before load methods (#209), test timings (#203), the
492
+ # standalone load-line narrowing (#426), or partial recapture (#421)
493
+ # rebuilds once. A cache that does not record this process's
384
494
  # PARALLEL_WORKERS value rebuilds too. Rails reads that value inside
385
495
  # parallelize, so a map from another worker count selects the wrong
386
496
  # tests. The daemon writes this same file. Boot mode reads its load
387
497
  # lines and methods from the live boot instead (#build_via_fork), but
388
498
  # its map from before #209 lacks the `def` lines of the methods each
389
499
  # test called.
390
- if cached && cached["digest"] == @digest && dependencies_match?(cached) &&
391
- cached.key?("load_methods") && cached.key?("timings") &&
392
- parallel_workers_match?(cached)
393
- @map = cached["map"] || {}
394
- @timings = cached["timings"] || {}
395
- unless @boot_path
396
- @load_lines = Set.new(cached["load_lines"])
397
- @load_methods = Set.new(cached["load_methods"])
398
- end
399
- @failed_test_files = cached["failed_test_files"] || []
400
- @failed_clean_tests = []
401
- @loaded_dependencies = cached["dependencies"] || {}
402
- retry_failed_captures(after_fork)
500
+ if cache_hit?(cached)
501
+ apply_cache(cached)
502
+ recaptured = retry_failed_captures(after_fork)
403
503
  warn_incomplete unless @failed_test_files.empty?
404
- verify_cached_clean(after_fork: after_fork)
504
+ checked = clean_check_test_paths
505
+ # A retried capture already ran its test clean, so it does not run alone again.
506
+ verify_cached_clean(after_fork: after_fork,
507
+ test_paths: checked.reject { |t| recaptured.include?(relativize(t)) })
405
508
  verify_combined_clean(after_fork: after_fork)
509
+ warn_unchecked("coverage cache hit", checked.size)
406
510
  save
407
511
  return self
408
512
  end
409
513
 
514
+ plan = partial_plan(cached)
515
+ return self if plan && apply_partial(plan, cached, after_fork)
516
+
410
517
  yield
411
518
  verify_combined_clean(after_fork: after_fork)
519
+ warn_unchecked("coverage capture", clean_check_test_paths.size, miss: true)
412
520
  save
413
521
  self
414
522
  end
415
523
 
524
+ # The tests the clean check runs: the tests that cover a line of this run's
525
+ # sources (#398). A mutant runs only tests from the map
526
+ # ({JobPlan.coverage_selection}), so a test outside this set never runs
527
+ # with a mutant and cannot change a verdict. Mutants exist only in the
528
+ # sources, so only source keys count: standalone capture also maps the
529
+ # other project files a test ran, its own file too. A map with no source
530
+ # key keeps every test, because the daemon then runs every test for each
531
+ # mutant. A new way to choose a mutant's tests must add those tests here,
532
+ # or a red test could reach a mutant run.
533
+ #
534
+ # @api private
535
+ # @return [Array<String>] the `--test` paths in this set, in their order.
536
+ def clean_check_test_paths
537
+ @source_rels ||= @source_paths.to_set { |p| relativize(absolute(p)) }
538
+ covering = @map.each_with_object(Set.new) do |(key, tests), set|
539
+ set.merge(tests) if @source_rels.include?(key_source(key))
540
+ end
541
+ return @test_paths if covering.empty?
542
+
543
+ @test_paths.select { |t| covering.include?(relativize(t)) }
544
+ end
545
+
546
+ # Tells the user on stderr how many test files the clean check left out
547
+ # (#398). Prints nothing when it left out none.
548
+ #
549
+ # @api private
550
+ # @param label [String] what the run did: "coverage cache hit",
551
+ # "--coverage-from" or "coverage capture".
552
+ # @param checked [Integer] the number of test files in {#clean_check_test_paths}.
553
+ # @param miss [Boolean] true after a fresh capture, which ran every test
554
+ # alone, so only the together check leaves files out.
555
+ # @return [void]
556
+ def warn_unchecked(label, checked, miss: false)
557
+ others = @test_paths.size - checked
558
+ return unless others.positive?
559
+
560
+ one = others == 1
561
+ covering = checked == 1 ? "1 test file that covers" : "#{checked} test files that cover"
562
+ head, tail =
563
+ if miss
564
+ ["the together check covers only the #{covering}", "ran alone only"]
565
+ else
566
+ ["the clean check ran the #{covering}",
567
+ "never #{one ? 'runs' : 'run'} with a mutant and #{one ? 'was' : 'were'} not checked"]
568
+ end
569
+ warn "[mutineer] #{label}: #{head} the sources; #{others} other #{one ? 'test file' : 'test files'} #{tail}."
570
+ end
571
+
416
572
  # Takes the map from the `--coverage-from` capture instead of capturing,
417
573
  # keeping only this run's sources, then runs the tests clean. A mutant runs
418
574
  # only the tests that cover its line, often one file alone, so each test
419
575
  # that covers one of this run's sources runs alone, as on a cache hit: a
420
- # test that fails alone here would otherwise kill every mutant it runs. The
421
- # other tests never run alone in this run, so only the combined check
422
- # covers them.
576
+ # test that fails alone here would otherwise kill every mutant it runs.
577
+ # Those tests then run together. The other tests never run with a mutant
578
+ # in this run, so they are not checked (#398).
423
579
  #
424
580
  # @api private
425
581
  # @param after_fork [Proc, nil] boot-mode fork hook.
@@ -432,11 +588,13 @@ module Mutineer
432
588
  @timings = cached["timings"] || {}
433
589
  @failed_test_files = []
434
590
  @failed_clean_tests = []
591
+ @failed_clean_details = {}
592
+ @clean_timeouts = {}
435
593
  @loaded_dependencies = cached["dependencies"]
436
- covering = @map.values.flatten.to_set
437
- verify_cached_clean(after_fork: after_fork,
438
- test_paths: @test_paths.select { |t| covering.include?(relativize(t)) })
594
+ checked = clean_check_test_paths
595
+ verify_cached_clean(after_fork: after_fork, test_paths: checked)
439
596
  verify_combined_clean(after_fork: after_fork)
597
+ warn_unchecked("--coverage-from", checked.size)
440
598
  save
441
599
  self
442
600
  end
@@ -471,7 +629,7 @@ module Mutineer
471
629
  return "it has no source fingerprints; capture it again with this version and --rails/--boot"
472
630
  end
473
631
  return "its entries are not the shape a coverage cache saves; capture it again" unless well_formed_capture?(cached)
474
- return "its tests, --require files, boot file, load paths, framework or Minitest seed differ, or an older version made it; capture it again" unless cached["inputs"] == compute_digest(inputs_only: true)
632
+ return "its tests, --require files, boot file, load paths, framework or test seed differ, or an older version made it; capture it again" unless cached["inputs"] == compute_digest(inputs_only: true)
475
633
 
476
634
  fingerprints = source_fingerprints
477
635
  missing = fingerprints.keys - cached["sources"].keys
@@ -555,7 +713,10 @@ module Mutineer
555
713
  @timings = {}
556
714
  @failed_test_files = []
557
715
  @failed_clean_tests = []
716
+ @failed_clean_details = {}
717
+ @clean_timeouts = {}
558
718
  @loaded_dependencies = {}
719
+ @paired_in_run = nil
559
720
 
560
721
  @test_paths.each do |test_path|
561
722
  payload = timed(test_path) { capture(test_path) }
@@ -576,6 +737,8 @@ module Mutineer
576
737
  @timings = {}
577
738
  @failed_test_files = []
578
739
  @failed_clean_tests = []
740
+ @failed_clean_details = {}
741
+ @clean_timeouts = {}
579
742
  @loaded_dependencies = {}
580
743
  abs_sources = abs_source_paths
581
744
 
@@ -644,13 +807,17 @@ module Mutineer
644
807
  # no-op. Injected so this file needs neither Runner (Prism) nor Rails.
645
808
  after_fork&.call
646
809
  Coverage.result(clear: true, stop: false) # discard pre-test delta
647
- passed = TestRunners.for(@framework).run([abs_test]).zero?
810
+ passed, report = run_clean_with_report([abs_test])
648
811
  # {file => {lines: [...], methods: {...}}}; reduce to the counts
649
812
  # array record() expects, keeping only our source files.
813
+ wanted = abs_sources.map { |path| FsPath.read(path) }
814
+ # Coverage keys are US-ASCII with invalid bytes under LC_ALL=C (#346).
650
815
  coverage = Coverage.result(stop: false)
651
- .select { |f, _| abs_sources.include?(f) }
816
+ .to_h { |f, v| [FsPath.read(f), v] }
817
+ .select { |f, _| wanted.include?(f) }
652
818
  .transform_values { |v| v.is_a?(Hash) ? lines_with_called_defs(v) : v }
653
819
  { "passed" => passed, "coverage" => coverage,
820
+ "failure_report" => passed ? nil : report,
654
821
  "loaded_files" => capture_loaded_files }
655
822
  rescue Exception => e # rubocop:disable Lint/RescueException
656
823
  # Stringify (an arbitrary Exception may not marshal); the parent
@@ -758,12 +925,60 @@ module Mutineer
758
925
  record_load(payload["load_coverage"])
759
926
 
760
927
  unless payload["passed"]
761
- @failed_clean_tests << relativize(test_path)
928
+ record_failed_clean(relativize(test_path), payload["failure_report"])
762
929
  return
763
930
  end
764
931
 
765
932
  coverage = payload["coverage"]
766
- record(coverage, test_path) if coverage.is_a?(Hash)
933
+ return unless coverage.is_a?(Hash)
934
+
935
+ # Post-load counts only. Standalone capture clears the load snapshot
936
+ # before the test runs; a fork capture already discarded the boot delta.
937
+ record(map_coverage(coverage), test_path)
938
+ # Lines that ran only while the sources loaded. The paired test keeps
939
+ # them, so a reload mutant a test checks through a value built at load
940
+ # still runs (#348). An unrelated test does not.
941
+ record_paired_load_lines(payload["load_coverage"], coverage, test_path)
942
+ end
943
+
944
+ # Notes a red unmutated suite for {JobPlan.abort_if_unclean!} (issue #360).
945
+ #
946
+ # @api private
947
+ # @param rel [String] project-relative test path, or `"combined suite"`.
948
+ # @param detail [String, nil] failing test names / first error, capped.
949
+ # @return [void]
950
+ def record_failed_clean(rel, detail = nil)
951
+ @failed_clean_tests << rel unless @failed_clean_tests.include?(rel)
952
+ text = detail.to_s.strip
953
+ return if text.empty?
954
+
955
+ @failed_clean_details[rel] = truncate_clean_detail(text)
956
+ end
957
+
958
+ # Keeps the first failure block of a clean-failure report within
959
+ # {CLEAN_FAILURE_DETAIL_LIMIT} characters. RSpec progress dots can fill
960
+ # the head of the sink; prefer RSpec's `Failures:` / load-error header or
961
+ # Minitest's numbered `N) Failure:` / `N) Error:` line. Do not match a bare
962
+ # `Error:` — that cuts RSpec's `Failure/Error:` mid-token (#360).
963
+ #
964
+ # @api private
965
+ # @param text [String] redacted reporter output.
966
+ # @return [String]
967
+ def truncate_clean_detail(text)
968
+ starts = [text.index("Failures:")]
969
+ # RSpec load errors and after(:suite)/after(:context) errors share this
970
+ # header; do not require "while loading" or progress dots fill the cap.
971
+ if (match = text.match(/^An error occurred /m))
972
+ starts << match.begin(0)
973
+ end
974
+ if (match = text.match(/^\s*\d+\) (?:Failure|Error):/m))
975
+ starts << match.begin(0)
976
+ end
977
+ start = starts.compact.min || 0
978
+ report = start.zero? ? text : text[start..]
979
+ return report if report.length <= CLEAN_FAILURE_DETAIL_LIMIT
980
+
981
+ "#{report[0, CLEAN_FAILURE_DETAIL_LIMIT].rstrip}\n…"
767
982
  end
768
983
 
769
984
  # Re-runs each successfully captured test on a cache hit. Digest equality
@@ -778,65 +993,130 @@ module Mutineer
778
993
  rel = relativize(test_path)
779
994
  next if @failed_test_files.include?(rel)
780
995
 
781
- ok = if @boot_path
782
- fork_clean_pass?([absolute(test_path)], after_fork)
783
- else
784
- subprocess_clean_pass?([test_path])
785
- end
786
- @failed_clean_tests << rel unless ok
996
+ ok, detail, timed_out = if @boot_path
997
+ fork_clean_pass?([absolute(test_path)], after_fork)
998
+ else
999
+ subprocess_clean_pass?([test_path])
1000
+ end
1001
+ if timed_out
1002
+ record_clean_timeout(rel, @capture_timeout)
1003
+ elsif !ok
1004
+ record_failed_clean(rel, detail)
1005
+ end
787
1006
  end
788
1007
  end
789
1008
 
790
1009
  # Re-runs tests whose previous capture crashed. A fixed helper is invisible
791
1010
  # to the source/test digest when that capture never recorded `loaded_files`.
1011
+ # A capture runs its test, so a red test lands in {#failed_clean_tests} here.
792
1012
  #
793
1013
  # @api private
794
1014
  # @param after_fork [Proc, nil] boot-mode fork hook.
795
- # @return [void]
1015
+ # @return [Array<String>] the project-relative tests it captured again.
796
1016
  def retry_failed_captures(after_fork)
797
1017
  pending = @failed_test_files.dup
798
- return if pending.empty?
1018
+ return pending if pending.empty?
799
1019
 
800
1020
  @failed_test_files = []
801
- abs_sources = abs_source_paths
802
1021
  pending.each do |rel|
803
1022
  test_path = @test_paths.find { |t| relativize(t) == rel } || rel
804
- if @boot_path
805
- accept_fork_payload(test_path, timed(test_path) { fork_capture(absolute(test_path), abs_sources, after_fork) })
806
- else
807
- payload = timed(test_path) { capture(test_path) }
808
- accept_capture_payload(test_path, payload) if payload
809
- end
1023
+ capture_one(test_path, after_fork)
810
1024
  end
1025
+ pending
811
1026
  end
812
1027
 
813
- # Runs every successfully captured test together. Per-file capture can miss a
814
- # failure that only appears when covering files share one process.
1028
+ # Captures one test file, standalone or by fork, and records the result.
1029
+ #
1030
+ # @api private
1031
+ # @param test_path [String] test file path.
1032
+ # @param after_fork [Proc, nil] boot-mode fork hook.
1033
+ # @return [void]
1034
+ def capture_one(test_path, after_fork)
1035
+ if @boot_path
1036
+ accept_fork_payload(test_path, timed(test_path) do
1037
+ fork_capture(absolute(test_path), abs_source_paths, after_fork)
1038
+ end)
1039
+ else
1040
+ payload = timed(test_path) { capture(test_path) }
1041
+ accept_capture_payload(test_path, payload) if payload
1042
+ end
1043
+ end
1044
+
1045
+ # Runs every successfully captured test of {#clean_check_test_paths}
1046
+ # together. Per-file capture can miss a failure that only appears when
1047
+ # covering files share one process.
815
1048
  #
816
1049
  # @api private
817
1050
  # @param after_fork [Proc, nil] boot-mode fork hook.
818
1051
  # @return [void]
819
1052
  def verify_combined_clean(after_fork: nil)
820
- runnable = @test_paths.reject { |t| @failed_test_files.include?(relativize(t)) }
1053
+ runnable = clean_check_test_paths.reject { |t| @failed_test_files.include?(relativize(t)) }
821
1054
  return if runnable.size < 2
822
1055
  return unless @failed_clean_tests.empty?
823
1056
 
824
- ok = if @boot_path
825
- fork_clean_pass?(runnable.map { |t| absolute(t) }, after_fork)
826
- else
827
- subprocess_clean_pass?(runnable)
828
- end
829
- @failed_clean_tests << "combined suite" unless ok
1057
+ timeout = combined_clean_timeout(runnable)
1058
+ ok, detail, timed_out = if @boot_path
1059
+ fork_clean_pass?(runnable.map { |t| absolute(t) }, after_fork, timeout: timeout)
1060
+ else
1061
+ subprocess_clean_pass?(runnable, timeout: timeout)
1062
+ end
1063
+ if timed_out
1064
+ record_clean_timeout("combined suite", timeout)
1065
+ elsif !ok
1066
+ record_failed_clean("combined suite", detail)
1067
+ end
1068
+ end
1069
+
1070
+ # The combined clean check's deadline: {COMBINED_TIMEOUT_FACTOR} times the
1071
+ # summed capture seconds of `test_paths`, and at least `@capture_timeout`.
1072
+ # One process runs them all, so the per-file capture timeout alone stops a
1073
+ # green suite that is just slow.
1074
+ #
1075
+ # @api private
1076
+ # @param test_paths [Array<String>] the tests the combined check runs.
1077
+ # @return [Numeric] seconds.
1078
+ def combined_clean_timeout(test_paths)
1079
+ measured = test_paths.sum { |t| @timings[relativize(t)].to_f }
1080
+ [@capture_timeout, (measured * COMBINED_TIMEOUT_FACTOR).ceil].max
1081
+ end
1082
+
1083
+ # Notes a clean check that hit its deadline. The run still stops, as for a
1084
+ # red suite, but the message says it timed out.
1085
+ #
1086
+ # @api private
1087
+ # @param rel [String] project-relative test path, or `"combined suite"`.
1088
+ # @param seconds [Numeric] the deadline that passed.
1089
+ # @return [void]
1090
+ def record_clean_timeout(rel, seconds)
1091
+ record_failed_clean(rel)
1092
+ @clean_timeouts[rel] = seconds
830
1093
  end
831
1094
 
832
1095
  # Runs test files in a fresh interpreter and returns whether they passed.
1096
+ # The child writes a one-line JSON result with an optional failure report
1097
+ # (#360), so a leftover process cannot hold a stderr pipe open.
833
1098
  #
834
1099
  # @api private
835
1100
  # @param test_paths [Array<String>] test file paths.
836
- # @return [Boolean]
837
- def subprocess_clean_pass?(test_paths)
838
- status, = spawn_script(clean_check_script(test_paths), result: false)
839
- status&.success? || false
1101
+ # @param timeout [Numeric] seconds before the child is killed.
1102
+ # @return [Array(Boolean, String, Boolean)] pass flag, capped failure
1103
+ # report, and whether the deadline passed.
1104
+ def subprocess_clean_pass?(test_paths, timeout: @capture_timeout)
1105
+ status, line = spawn_script(clean_check_script(test_paths), result: true, timeout: timeout)
1106
+ return [false, "", true] if status.nil?
1107
+
1108
+ data = begin
1109
+ JSON.parse(line.to_s)
1110
+ rescue JSON::ParserError
1111
+ nil
1112
+ end
1113
+ # Fail closed: a child that exits 0 without a valid result line must not
1114
+ # clear the clean check (mutation honesty).
1115
+ return [false, "", false] unless data.is_a?(Hash)
1116
+
1117
+ # Require the process exit too: an `at_exit` hook can fail after the
1118
+ # child already wrote `passed: true` (#360 warm-cache clean check).
1119
+ [status.success? && data["passed"] == true, data["failure_report"].to_s, false]
840
1120
  end
841
1121
 
842
1122
  # Runs `script` in a fresh `ruby -` that reads the script from stdin. The
@@ -844,7 +1124,7 @@ module Mutineer
844
1124
  # or the result. With `result: true`, the child writes its result as one
845
1125
  # line to fd {RESULT_FD}, a pipe that only the script uses. The child's
846
1126
  # stderr is the parent's stderr, so warnings from the script reach the
847
- # user. A wall clock of `@capture_timeout` bounds the whole call (see
1127
+ # user. A wall clock of `timeout` bounds the whole call (see
848
1128
  # {#await_child}), so a hung test cannot wedge the run.
849
1129
  #
850
1130
  # The parent reads one line, not until EOF: a process that a test leaves
@@ -855,11 +1135,12 @@ module Mutineer
855
1135
  # @api private
856
1136
  # @param script [String] Ruby script text.
857
1137
  # @param result [Boolean] whether to open the result pipe on fd {RESULT_FD}.
1138
+ # @param timeout [Numeric] seconds before the child is killed.
858
1139
  # @return [Array(Process::Status, String)] the exit status and the line the
859
1140
  # child wrote to fd {RESULT_FD} (`""` without one); `[nil, ""]` after a
860
1141
  # timeout.
861
- def spawn_script(script, result: true)
862
- deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @capture_timeout
1142
+ def spawn_script(script, result: true, timeout: @capture_timeout)
1143
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
863
1144
  script_rd, script_wr = IO.pipe
864
1145
  result_rd, result_wr = IO.pipe if result
865
1146
  options = { in: script_rd, out: File::NULL, pgroup: true }
@@ -880,12 +1161,19 @@ module Mutineer
880
1161
  end
881
1162
 
882
1163
  # Reads the one result line a {#spawn_script} child writes to fd
883
- # {RESULT_FD}.
1164
+ # {RESULT_FD}. The line is JSON and may name a non-ASCII path. The pipe
1165
+ # follows the locale, so under `LC_ALL=C` those bytes arrive tagged
1166
+ # US-ASCII and `JSON.parse` raises. The bytes are UTF-8 (#346).
884
1167
  #
885
1168
  # @api private
886
1169
  # @param io [IO] the parent end of the result pipe.
887
1170
  # @return [String] the line, or `""` at end of file.
888
- def read_result_line(io) = io.gets.to_s
1171
+ def read_result_line(io)
1172
+ line = io.gets
1173
+ return "" unless line
1174
+
1175
+ line.dup.force_encoding(Encoding::UTF_8)
1176
+ end
889
1177
 
890
1178
  # Reads the one Marshal object a forked capture child writes, without
891
1179
  # waiting for end of file.
@@ -940,23 +1228,27 @@ module Mutineer
940
1228
  # Ruby source that opens the result channel in a {#spawn_script} child. The
941
1229
  # script runs it first, so no file that a test opens can take fd
942
1230
  # {RESULT_FD}. Close-on-exec keeps the fd out of the test's own
943
- # subprocesses.
1231
+ # subprocesses. Binary mode writes the JSON bytes as they are, so a
1232
+ # non-ASCII path is not rejected by an ASCII locale (#346).
944
1233
  #
945
1234
  # @api private
946
1235
  # @return [String] Ruby script text.
947
1236
  def result_channel_expression
948
- "_result = IO.new(#{RESULT_FD}, \"w\"); _result.close_on_exec = true"
1237
+ "_result = IO.new(#{RESULT_FD}, \"w\"); _result.binmode; _result.close_on_exec = true"
949
1238
  end
950
1239
 
951
- # Runs test files in a fork of the booted parent and returns whether they passed.
952
- # Bounded by `@capture_timeout` so a hung child cannot block the CLI.
1240
+ # Runs test files in a fork of the booted parent and returns whether they
1241
+ # passed, with a capped failure report for a red suite (#360).
1242
+ # Bounded by `timeout` so a hung child cannot block the CLI.
953
1243
  #
954
1244
  # @api private
955
1245
  # @param abs_tests [Array<String>] absolute test file paths.
956
1246
  # @param after_fork [Proc, nil] boot-mode fork hook.
957
- # @return [Boolean]
958
- def fork_clean_pass?(abs_tests, after_fork)
959
- deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @capture_timeout
1247
+ # @param timeout [Numeric] seconds before the child is killed.
1248
+ # @return [Array(Boolean, String, Boolean)] pass flag, failure report, and
1249
+ # whether the deadline passed.
1250
+ def fork_clean_pass?(abs_tests, after_fork, timeout: @capture_timeout)
1251
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
960
1252
  rd, wr = IO.pipe
961
1253
  rd.binmode
962
1254
  wr.binmode
@@ -969,23 +1261,54 @@ module Mutineer
969
1261
  ChildStdout.silence
970
1262
  after_fork&.call
971
1263
  Coverage.result(clear: true, stop: false) if Coverage.running?
972
- wr.write(Marshal.dump(TestRunners.for(@framework).run(abs_tests).zero?))
973
- rescue Exception # rubocop:disable Lint/RescueException
974
- wr.write(Marshal.dump(false))
1264
+ passed, report = run_clean_with_report(abs_tests)
1265
+ wr.write(Marshal.dump([passed, report]))
1266
+ rescue Exception => e # rubocop:disable Lint/RescueException
1267
+ wr.write(Marshal.dump([false, Redaction.describe(e)]))
975
1268
  ensure
976
1269
  wr.close
977
1270
  exit!(0)
978
1271
  end
979
1272
  end
980
1273
  wr.close
981
- _, passed = await_child(pid, deadline) { read_marshal(rd) }
982
- passed == true
1274
+ status, payload = await_child(pid, deadline) { read_marshal(rd) }
1275
+ return [false, "", true] if status.nil?
1276
+ return [false, "", false] unless payload.is_a?(Array)
1277
+
1278
+ passed, report = payload
1279
+ [passed == true, report.to_s, false]
983
1280
  rescue StandardError
984
- false
1281
+ [false, "", false]
985
1282
  ensure
986
1283
  [rd, wr].compact.each { |io| io.close unless io.closed? }
987
1284
  end
988
1285
 
1286
+ # Runs tests in the current (forked) process and returns pass plus the
1287
+ # redacted reporter failure text when red (#360).
1288
+ #
1289
+ # @api private
1290
+ # @param abs_tests [Array<String>] absolute test file paths.
1291
+ # @return [Array(Boolean, String)]
1292
+ def run_clean_with_report(abs_tests)
1293
+ if @framework == "rspec"
1294
+ code = TestRunners.for(@framework).run(abs_tests)
1295
+ raw = TestRunners::RSpec.last_output.to_s
1296
+ else
1297
+ require "minitest"
1298
+ require "stringio"
1299
+ report = StringIO.new
1300
+ Minitest.define_singleton_method(:plugin_mutineer_clean_report_init) do |options|
1301
+ reporter << Minitest::SummaryReporter.new(report, options)
1302
+ end
1303
+ Minitest.extensions << "mutineer_clean_report" unless Minitest.extensions.include?("mutineer_clean_report")
1304
+ code = TestRunners.for(@framework).run(abs_tests)
1305
+ raw = report.string
1306
+ end
1307
+ # Forked children print this into SmokeCheckError; redact like the
1308
+ # subprocess JSON path so DATABASE_URL passwords never reach stderr.
1309
+ [code.zero?, code.zero? ? "" : Redaction.text(raw)]
1310
+ end
1311
+
989
1312
  # SIGKILLs capture child `pid` and its process group, then waits for
990
1313
  # `waiter` to reap the child. Falls back to the pid alone when the group
991
1314
  # does not exist yet.
@@ -1023,23 +1346,28 @@ module Mutineer
1023
1346
  # @param test_paths [Array<String>] test file paths.
1024
1347
  # @return [String] Ruby script text.
1025
1348
  def minitest_clean_check_script(test_paths)
1026
- loads = Array(test_paths).map { |t| "load #{absolute(t).inspect}" }.join("\n")
1027
1349
  <<~RUBY
1350
+ #{result_channel_expression}
1351
+ require "json"
1352
+ require #{FsPath::PATH.inspect}
1028
1353
  require "minitest"
1029
1354
  require "stringio"
1355
+ Mutineer::FsPath.install_minitest_path_filter!
1030
1356
  def Minitest.autorun; end
1031
1357
  _report = StringIO.new
1032
1358
  Minitest.define_singleton_method(:plugin_mutineer_report_init) { |options| reporter << Minitest::SummaryReporter.new(_report, options) }
1033
1359
  Minitest.extensions << "mutineer_report"
1034
1360
  $LOAD_PATH.unshift(*#{abs_load_paths.inspect})
1035
1361
  begin
1036
- #{abs_preload_paths.inspect}.each { |f| require f }
1362
+ #{abs_preload_paths.map { |path| FsPath.filesystem(path) }.inspect}.each { |f| require f }
1037
1363
  Minitest::Runnable.reset
1038
- #{loads}
1364
+ #{minitest_file_order_setup(Array(test_paths).map { |t| FsPath.filesystem(absolute(t)).inspect }.join(", "))}
1039
1365
  #{load_failure_rescue}
1040
1366
  end
1041
1367
  _passed = Minitest.run(#{minitest_seed_args_expression})
1042
- $stderr.write(#{redacted_expression("_report.string")}) unless _passed
1368
+ _failure = _passed ? nil : #{redacted_expression("_report.string")}
1369
+ _result.puts JSON.generate("passed" => _passed == true, "failure_report" => _failure)
1370
+ _result.close
1043
1371
  exit(_passed ? 0 : 1)
1044
1372
  RUBY
1045
1373
  end
@@ -1053,6 +1381,8 @@ module Mutineer
1053
1381
  def rspec_clean_check_script(test_paths)
1054
1382
  specs = Array(test_paths).map { |t| absolute(t).inspect }.join(", ")
1055
1383
  <<~RUBY
1384
+ #{result_channel_expression}
1385
+ require "json"
1056
1386
  require "stringio"
1057
1387
  begin
1058
1388
  require "rspec/core"
@@ -1060,14 +1390,18 @@ module Mutineer
1060
1390
  exit 3
1061
1391
  end
1062
1392
  RSpec::Core::Runner.disable_autorun!
1393
+ #{rspec_seed_install}
1063
1394
  $LOAD_PATH.unshift(*#{abs_load_paths.inspect})
1064
1395
  begin
1065
1396
  #{abs_preload_paths.inspect}.each { |f| require f }
1066
1397
  #{load_failure_rescue}
1067
1398
  end
1399
+ #{rspec_order_preamble(specs)}
1068
1400
  _sink = StringIO.new
1069
1401
  status = RSpec::Core::Runner.run(["--no-color", #{specs}], _sink, _sink)
1070
- $stderr.write(#{redacted_expression("_sink.string")}) unless status.zero?
1402
+ _failure = status.zero? ? nil : #{redacted_expression("_sink.string")}
1403
+ _result.puts JSON.generate("passed" => status.zero?, "failure_report" => _failure)
1404
+ _result.close
1071
1405
  exit(status.zero? ? 0 : 1)
1072
1406
  RUBY
1073
1407
  end
@@ -1132,8 +1466,10 @@ module Mutineer
1132
1466
  #{result_channel_expression}
1133
1467
  require "coverage"
1134
1468
  require "json"
1469
+ require #{FsPath::PATH.inspect}
1135
1470
  require "minitest"
1136
1471
  require "stringio"
1472
+ Mutineer::FsPath.install_minitest_path_filter!
1137
1473
  def Minitest.autorun; end
1138
1474
  _report = StringIO.new
1139
1475
  Minitest.define_singleton_method(:plugin_mutineer_report_init) { |options| reporter << Minitest::SummaryReporter.new(_report, options) }
@@ -1141,16 +1477,18 @@ module Mutineer
1141
1477
  Coverage.start(lines: true, methods: true)
1142
1478
  $LOAD_PATH.unshift(*#{abs_load_paths.inspect})
1143
1479
  begin
1144
- #{abs_preload_paths.inspect}.each { |f| require f }
1480
+ #{abs_preload_paths.map { |path| FsPath.filesystem(path) }.inspect}.each { |f| require f }
1145
1481
  _load = #{load_coverage_expression}
1482
+ #{discard_load_counts_expression}
1146
1483
  Minitest::Runnable.reset
1147
- load #{absolute(test_path).inspect}
1484
+ #{minitest_file_order_setup(FsPath.filesystem(absolute(test_path)).inspect)}
1148
1485
  #{load_failure_rescue}
1149
1486
  end
1150
1487
  _passed = Minitest.run(#{minitest_seed_args_expression})
1151
- $stderr.write(#{redacted_expression("_report.string")}) unless _passed
1152
- _coverage = Coverage.result.transform_values { |v| v.is_a?(Hash) ? v[:lines] : v }
1488
+ _failure = _passed ? nil : #{redacted_expression("_report.string")}
1489
+ #{test_coverage_assignment}
1153
1490
  _result.puts JSON.generate("passed" => _passed == true, "coverage" => _coverage,
1491
+ "failure_report" => _failure,
1154
1492
  "load_coverage" => _load,
1155
1493
  "loaded_files" => #{loaded_files_expression})
1156
1494
  _result.close
@@ -1169,6 +1507,56 @@ module Mutineer
1169
1507
  "ENV[\"SEED\"] ? [] : [\"--seed\", #{MinitestIntegration::PINNED_SEED.to_s.inspect}]"
1170
1508
  end
1171
1509
 
1510
+ # Loads the test files and installs {MinitestIntegration::FileOrder} so the
1511
+ # subprocess clean check and coverage capture shuffle each file the same
1512
+ # way as {MinitestIntegration.run} (#282). The child requires only the
1513
+ # file-order file, not the rest of the runner.
1514
+ #
1515
+ # @api private
1516
+ # @param files_list_source [String] Ruby source for the path arguments,
1517
+ # one inspected string or several joined by commas.
1518
+ # @return [String] Ruby script text.
1519
+ def minitest_file_order_setup(files_list_source)
1520
+ path = File.expand_path("minitest_integration/file_order.rb", __dir__)
1521
+ <<~RUBY.chomp
1522
+ _rank = {}
1523
+ [#{files_list_source}].each_with_index do |_f, _i|
1524
+ load _f
1525
+ Minitest::Runnable.runnables.each { |_klass| _rank[_klass] ||= _i }
1526
+ end
1527
+ require #{path.inspect}
1528
+ Mutineer::MinitestIntegration.keep_file_order(_rank)
1529
+ RUBY
1530
+ end
1531
+
1532
+ # Installs the RSpec seed watcher. The subprocess must do this before it
1533
+ # loads `--require` files, so a `config.seed=` there is recorded (#282).
1534
+ #
1535
+ # @api private
1536
+ # @return [String] Ruby script text.
1537
+ def rspec_seed_install
1538
+ seed = File.expand_path("test_runners/rspec_seed.rb", __dir__)
1539
+ <<~RUBY.chomp
1540
+ require #{seed.inspect}
1541
+ Mutineer::TestRunners::RSpecSeed.install!
1542
+ RUBY
1543
+ end
1544
+
1545
+ # Ruby source that keeps spec files in the order given, the same hook as
1546
+ # {TestRunners::RSpec.run}. The seed watcher is installed earlier, before
1547
+ # the preloads ({#rspec_seed_install}).
1548
+ #
1549
+ # @api private
1550
+ # @param spec_list_source [String] Ruby source for the spec path arguments.
1551
+ # @return [String] Ruby script text.
1552
+ def rspec_order_preamble(spec_list_source)
1553
+ runner = File.expand_path("test_runners/rspec.rb", __dir__)
1554
+ <<~RUBY.chomp
1555
+ require #{runner.inspect}
1556
+ Mutineer::TestRunners::RSpec.keep_file_order([#{spec_list_source}])
1557
+ RUBY
1558
+ end
1559
+
1172
1560
  # Same coverage-JSON contract as the minitest path, but driven by RSpec:
1173
1561
  # require rspec/core lazily, require the sources under Coverage, then run the
1174
1562
  # one spec via RSpec::Core::Runner. The JSON goes to the result channel (see
@@ -1180,6 +1568,7 @@ module Mutineer
1180
1568
  #{result_channel_expression}
1181
1569
  require "coverage"
1182
1570
  require "json"
1571
+ require #{FsPath::PATH.inspect}
1183
1572
  require "stringio"
1184
1573
  begin
1185
1574
  require "rspec/core"
@@ -1188,18 +1577,22 @@ module Mutineer
1188
1577
  exit 3
1189
1578
  end
1190
1579
  RSpec::Core::Runner.disable_autorun!
1580
+ #{rspec_seed_install}
1191
1581
  Coverage.start(lines: true, methods: true)
1192
1582
  $LOAD_PATH.unshift(*#{abs_load_paths.inspect})
1193
1583
  begin
1194
1584
  #{abs_preload_paths.inspect}.each { |f| require f }
1195
1585
  _load = #{load_coverage_expression}
1586
+ #{discard_load_counts_expression}
1196
1587
  #{load_failure_rescue}
1197
1588
  end
1589
+ #{rspec_order_preamble(absolute(test_path).inspect)}
1198
1590
  _sink = StringIO.new
1199
1591
  _status = RSpec::Core::Runner.run(["--no-color", #{absolute(test_path).inspect}], _sink, _sink)
1200
- $stderr.write(#{redacted_expression("_sink.string")}) unless _status.zero?
1201
- _coverage = Coverage.result.transform_values { |v| v.is_a?(Hash) ? v[:lines] : v }
1592
+ _failure = _status.zero? ? nil : #{redacted_expression("_sink.string")}
1593
+ #{test_coverage_assignment}
1202
1594
  _result.puts JSON.generate("passed" => _status.zero?, "coverage" => _coverage,
1595
+ "failure_report" => _failure,
1203
1596
  "load_coverage" => _load,
1204
1597
  "loaded_files" => #{loaded_files_expression})
1205
1598
  _result.close
@@ -1207,11 +1600,185 @@ module Mutineer
1207
1600
  end
1208
1601
 
1209
1602
  # Records every source line with a non-zero execution count as covered by
1210
- # this test file. Coverage.result keys are absolute; relativize and drop any
1211
- # path outside the project (stdlib/gem files).
1603
+ # this test file. The counts are lines that ran after the sources loaded.
1604
+ # Coverage.result keys are absolute; relativize and drop any path outside
1605
+ # the project (stdlib/gem files).
1212
1606
  def record(coverage, test_path)
1213
1607
  rel_test = relativize(test_path)
1214
- each_covered_key(coverage) { |key| (@map[key] ||= []) << rel_test }
1608
+ each_covered_key(coverage) { |key| add_covering_test(key, rel_test) }
1609
+ end
1610
+
1611
+ # Adds `rel_test` to the map entry for `key`, in `@test_paths` order.
1612
+ # A partial recapture appends a test that a full capture would have
1613
+ # recorded earlier. Sorting by the run's test order keeps the two maps
1614
+ # equal (#421). A full capture appends in order, so it takes the fast
1615
+ # path: sorting on every append made capture quadratic in the test count.
1616
+ #
1617
+ # @api private
1618
+ # @param key [String] a "file:line" map key.
1619
+ # @param rel_test [String] project-relative test path.
1620
+ # @return [void]
1621
+ def add_covering_test(key, rel_test)
1622
+ list = (@map[key] ||= [])
1623
+ last = list.last
1624
+ if last.nil? || test_index(last) < test_index(rel_test)
1625
+ list << rel_test
1626
+ else
1627
+ list << rel_test unless list.include?(rel_test)
1628
+ list.sort_by! { |test| test_index(test) }
1629
+ end
1630
+ end
1631
+
1632
+ # Position of `rel` in `@test_paths`. A path outside the run sorts last.
1633
+ #
1634
+ # @api private
1635
+ # @param rel [String] project-relative test path.
1636
+ # @return [Integer]
1637
+ def test_index(rel)
1638
+ @test_index ||= @test_paths.each_with_index.to_h { |path, index| [relativize(path), index] }
1639
+ @test_index.fetch(rel, @test_index.length)
1640
+ end
1641
+
1642
+ # Line counts for the map, with `def` lines of methods the test called
1643
+ # filled in. A fork capture already did that and sends plain arrays.
1644
+ #
1645
+ # @api private
1646
+ # @param coverage [Hash] counts per absolute file.
1647
+ # @return [Hash] the same files, values reduced to line-count arrays.
1648
+ def map_coverage(coverage)
1649
+ coverage.to_h do |file, data|
1650
+ lines = if data.is_a?(Hash) && (data.key?(:methods) || data.key?("methods"))
1651
+ lines_with_called_defs(data)
1652
+ else
1653
+ data
1654
+ end
1655
+ [file, lines]
1656
+ end
1657
+ end
1658
+
1659
+ # Credits `test_path` with source lines that ran only while the sources
1660
+ # loaded, when this test is one that should keep them (#426).
1661
+ #
1662
+ # Standalone capture preloads every source in every test's process, so
1663
+ # those lines would otherwise put every `--test` file on the map and a
1664
+ # cache hit would check them all. The lines stay in {#load_lines} either
1665
+ # way. They stay on the map for a paired test in this run: under
1666
+ # `--strategy reload` that test can kill a mutant on the line by checking
1667
+ # a value the load built (#348, `Catalog.price`). When the run has no
1668
+ # paired test, every test keeps the lines, so that kill is not dropped.
1669
+ # A line the test also ran after load is already on the map.
1670
+ #
1671
+ # @api private
1672
+ # @param load_coverage [Hash, nil] the load snapshot from the child.
1673
+ # @param test_coverage [Hash] the post-load coverage from the child.
1674
+ # @param test_path [String] test file path.
1675
+ # @return [void]
1676
+ def record_paired_load_lines(load_coverage, test_coverage, test_path)
1677
+ return unless load_coverage.is_a?(Hash)
1678
+
1679
+ rel_test = relativize(test_path)
1680
+ post = line_counts_by_rel(map_coverage(test_coverage))
1681
+ @source_rels ||= @source_paths.to_set { |path| relativize(absolute(path)) }
1682
+ load_coverage.each do |abs_file, data|
1683
+ rel = relativize(abs_file)
1684
+ next unless @source_rels.include?(rel)
1685
+ next unless credit_load_only_line?(rel, rel_test)
1686
+
1687
+ counts = data.is_a?(Hash) ? data["lines"] : data
1688
+ next unless counts.is_a?(Array)
1689
+
1690
+ post_counts = post[rel]
1691
+ counts.each_with_index do |count, idx|
1692
+ next unless count&.positive?
1693
+ next if post_counts&.[](idx)&.positive?
1694
+
1695
+ add_covering_test("#{rel}:#{idx + 1}", rel_test)
1696
+ end
1697
+ end
1698
+ end
1699
+
1700
+ # Line-count arrays from a coverage hash, keyed by project-relative path.
1701
+ #
1702
+ # @api private
1703
+ # @param coverage [Hash] counts per absolute file.
1704
+ # @return [Hash{String => Array}]
1705
+ def line_counts_by_rel(coverage)
1706
+ coverage.each_with_object({}) do |(abs_file, data), out|
1707
+ counts = data.is_a?(Hash) ? (data["lines"] || data[:lines]) : data
1708
+ next unless counts.is_a?(Array)
1709
+
1710
+ out[relativize(abs_file)] = counts
1711
+ end
1712
+ end
1713
+
1714
+ # Whether `rel_test` keeps lines of `rel_source` that ran only at load.
1715
+ # A paired test in the run does. When none of the run's tests pair with
1716
+ # the source, every test does.
1717
+ #
1718
+ # @api private
1719
+ # @param rel_source [String] project-relative source path.
1720
+ # @param rel_test [String] project-relative test path.
1721
+ # @return [Boolean]
1722
+ def credit_load_only_line?(rel_source, rel_test)
1723
+ paired = paired_tests_in_run(rel_source)
1724
+ paired.empty? || paired.include?(rel_test)
1725
+ end
1726
+
1727
+ # Conventional tests for `rel_source` that this run actually captures.
1728
+ #
1729
+ # @api private
1730
+ # @param rel_source [String] project-relative source path.
1731
+ # @return [Array<String>] project-relative test paths.
1732
+ def paired_tests_in_run(rel_source)
1733
+ @paired_in_run ||= {}
1734
+ @paired_in_run[rel_source] ||= begin
1735
+ run = @test_paths.to_set { |path| relativize(path) }
1736
+ Pairing.infer_tests(rel_source, project_root: @project_root, prefer: @framework || "minitest")
1737
+ .select { |path| run.include?(path) }
1738
+ end
1739
+ end
1740
+
1741
+ # True when a standalone cache was built with the #426 load-line rule
1742
+ # and the same paired tests would keep load-only lines now. Boot and
1743
+ # daemon maps never credited those lines to tests.
1744
+ #
1745
+ # @api private
1746
+ # @param cached [Hash] parsed coverage.json.
1747
+ # @return [Boolean]
1748
+ def standalone_load_lines_current?(cached)
1749
+ return true if @boot_path
1750
+
1751
+ cached[NARROW_LOAD_LINES] == true && load_line_pairs_match?(cached)
1752
+ end
1753
+
1754
+ # The tests in this run that {#credit_load_only_line?} treats as paired,
1755
+ # per source. An empty list means every test keeps the load-only lines.
1756
+ #
1757
+ # @api private
1758
+ # @return [Hash{String => Array<String>}] project-relative source to sorted test paths.
1759
+ def load_line_pairs
1760
+ @source_paths.filter_map do |source|
1761
+ rel = relativize(absolute(source))
1762
+ next if rel.start_with?("/")
1763
+
1764
+ [rel, paired_tests_in_run(rel).sort]
1765
+ end.to_h
1766
+ end
1767
+
1768
+ # True when `cached` recorded the same {#load_line_pairs} this run has now.
1769
+ # A missing key is a map saved before the pairing decision was stored.
1770
+ #
1771
+ # @api private
1772
+ # @param cached [Hash] parsed coverage.json.
1773
+ # @return [Boolean]
1774
+ def load_line_pairs_match?(cached)
1775
+ saved = cached[LOAD_LINE_PAIRS]
1776
+ return false unless saved.is_a?(Hash)
1777
+
1778
+ current = load_line_pairs
1779
+ return false unless saved.keys.sort == current.keys.sort
1780
+
1781
+ current.all? { |rel, tests| Array(saved[rel]).sort == tests }
1215
1782
  end
1216
1783
 
1217
1784
  # Adds the configured source lines that ran at load to {#load_lines}, and
@@ -1223,42 +1790,85 @@ module Mutineer
1223
1790
  # them; a Hash entry can also hold "methods", as {#load_entry} builds it.
1224
1791
  # @return [void]
1225
1792
  def record_load(coverage)
1226
- return unless coverage.is_a?(Hash)
1793
+ lines, methods = load_sets_from(coverage)
1794
+ return if lines.nil?
1227
1795
 
1228
- @source_rels ||= @source_paths.to_set { |p| relativize(absolute(p)) }
1229
- ours = coverage.select { |abs_file, _| @source_rels.include?(relativize(abs_file)) }
1230
- each_covered_key(ours) { |key| @load_lines << key }
1796
+ @load_lines.merge(lines)
1797
+ @load_methods.merge(methods)
1798
+ return unless @collect_load
1799
+
1800
+ @seen_load_lines = lines
1801
+ @seen_load_methods = methods
1802
+ end
1803
+
1804
+ # Line and method keys that ran at load in `coverage`, limited to this
1805
+ # run's sources. `[nil, nil]` when `coverage` is not a snapshot.
1806
+ #
1807
+ # @api private
1808
+ # @param coverage [Hash, nil] counts per absolute file, as {#record_load} reads them.
1809
+ # @return [Array(Set, Set), Array(nil, nil)]
1810
+ def load_sets_from(coverage)
1811
+ return [nil, nil] unless coverage.is_a?(Hash)
1812
+
1813
+ lines = Set.new
1814
+ methods = Set.new
1815
+ ours = coverage.select { |abs_file, _| source_rels.include?(relativize(abs_file)) }
1816
+ each_covered_key(ours) { |key| lines << key }
1231
1817
  ours.each do |abs_file, data|
1232
1818
  next unless data.is_a?(Hash) && data["methods"].is_a?(Array)
1233
1819
 
1234
- data["methods"].each { |line, column| @load_methods << "#{relativize(abs_file)}:#{line}:#{column}" }
1820
+ data["methods"].each { |line, column| methods << "#{relativize(abs_file)}:#{line}:#{column}" }
1235
1821
  end
1822
+ [lines, methods]
1236
1823
  end
1237
1824
 
1238
1825
  # One file's line counts, with the `def` line of each method the test
1239
1826
  # called counted too (#209). A forked capture does not see the `def` lines
1240
- # run, since the boot defined the methods before the test. Ruby counts no
1241
- # line when an endless method runs, so without this its mutants would be
1242
- # `no_coverage` even when a test calls it. A line where more than one
1243
- # method starts is not counted: the map keys tests by line, so the test
1244
- # would also be credited with the methods it did not call.
1827
+ # run, since the boot defined the methods before the test. Standalone
1828
+ # capture clears the load snapshot before the test, so it does not either.
1829
+ # Ruby counts no line when an endless method runs, so without this its
1830
+ # mutants would be `no_coverage` even when a test calls it. A line where
1831
+ # more than one method starts is not counted: the map keys tests by line,
1832
+ # so the test would also be credited with the methods it did not call.
1245
1833
  #
1246
1834
  # @api private
1247
- # @param data [Hash] `Coverage` result for one file, with `:lines` and `:methods`.
1835
+ # @param data [Hash] `Coverage` result for one file, with `:lines` and
1836
+ # `:methods`, or the JSON shape from {#test_coverage_assignment}
1837
+ # (`"lines"` and `"methods"` as `[line, column, count]` rows).
1248
1838
  # @return [Array, nil] line counts.
1249
1839
  def lines_with_called_defs(data)
1250
- lines = data[:lines]
1251
- return lines unless lines && data[:methods]
1840
+ lines = data[:lines] || data["lines"]
1841
+ methods = data[:methods] || data["methods"]
1842
+ return lines unless lines && methods
1252
1843
 
1253
1844
  lines = lines.dup
1254
- starts = data[:methods].keys.map { |key| key[2] }.tally
1255
- data[:methods].each do |key, count|
1256
- line = key[2]
1845
+ entries = method_call_entries(methods)
1846
+ starts = entries.map(&:first).tally
1847
+ entries.each do |line, count|
1257
1848
  lines[line - 1] = [lines[line - 1].to_i, count].max if count.positive? && starts[line] == 1
1258
1849
  end
1259
1850
  lines
1260
1851
  end
1261
1852
 
1853
+ # Start line and call count for each method in a Coverage method table.
1854
+ # A Hash is the live Coverage result. An Array is the JSON shape
1855
+ # `[line, column, count]` from {#test_coverage_assignment}.
1856
+ #
1857
+ # @api private
1858
+ # @param methods [Hash, Array] method counts.
1859
+ # @return [Array<Array(Integer, Integer)>] `[start_line, count]` pairs.
1860
+ def method_call_entries(methods)
1861
+ if methods.is_a?(Hash)
1862
+ methods.map { |key, count| [key[2], count.to_i] }
1863
+ else
1864
+ Array(methods).filter_map do |row|
1865
+ next unless row.is_a?(Array) && row.size >= 3 && row[0].is_a?(Integer) && row[2].is_a?(Numeric)
1866
+
1867
+ [row[0], row[2].to_i]
1868
+ end
1869
+ end
1870
+ end
1871
+
1262
1872
  # One file's load-time coverage in the shape {#record_load} reads: the line
1263
1873
  # counts, and the `[line, column]` of each method called at least once.
1264
1874
  # {#load_coverage_expression} builds the same shape in a capture child.
@@ -1280,9 +1890,42 @@ module Mutineer
1280
1890
  # @api private
1281
1891
  # @return [String] expression to embed in a capture subprocess script.
1282
1892
  def load_coverage_expression
1283
- "Coverage.peek_result.select { |f, _| #{abs_source_paths.inspect}.include?((File.realpath(f) rescue f)) }" \
1284
- ".transform_values { |v| { \"lines\" => v[:lines], " \
1285
- "\"methods\" => v[:methods].filter_map { |k, n| k[2, 2] if n.positive? } } }"
1893
+ "Coverage.peek_result.filter_map { |f, v| key = Mutineer::FsPath.read((File.realpath(f) rescue f)); " \
1894
+ "[key, { \"lines\" => v[:lines], \"methods\" => v[:methods].filter_map { |k, n| k[2, 2] if n.positive? } }] " \
1895
+ "if #{abs_source_paths.map { |path| FsPath.read(path) }.inspect}.include?(key) }.to_h"
1896
+ end
1897
+
1898
+ # Ruby that drops the line and method counts recorded while the sources
1899
+ # loaded, and leaves Coverage running for the test (#426). The same call
1900
+ # the fork capture uses before a test. The load snapshot in `_load` is
1901
+ # already a copy, so this does not erase {#load_lines}.
1902
+ #
1903
+ # @api private
1904
+ # @return [String] one statement for a capture subprocess script.
1905
+ def discard_load_counts_expression
1906
+ "Coverage.result(clear: true, stop: false)"
1907
+ end
1908
+
1909
+ # Ruby that stores the test's post-load coverage in `_coverage`.
1910
+ # Each file keeps its line counts and every method as
1911
+ # `[start_line, start_column, count]`, so {#lines_with_called_defs} can
1912
+ # credit a `def` line the test called. A line that ran only while the
1913
+ # sources loaded is absent, because {#discard_load_counts_expression} ran
1914
+ # first. Coverage keys are US-ASCII with invalid bytes under LC_ALL=C,
1915
+ # so each key is read back with {FsPath.read} (#346).
1916
+ #
1917
+ # @api private
1918
+ # @return [String] statements for a capture subprocess script.
1919
+ def test_coverage_assignment
1920
+ <<~RUBY.chomp
1921
+ _raw = Coverage.result
1922
+ _coverage = _raw.to_h do |_f, _v|
1923
+ next [Mutineer::FsPath.read(_f), _v] unless _v.is_a?(Hash)
1924
+
1925
+ [Mutineer::FsPath.read(_f),
1926
+ { "lines" => _v[:lines], "methods" => (_v[:methods] || {}).map { |_k, _n| [_k[2], _k[3], _n] } }]
1927
+ end
1928
+ RUBY
1286
1929
  end
1287
1930
 
1288
1931
  # Yields a "file:line" key for every project line with a non-zero count.
@@ -1308,9 +1951,13 @@ module Mutineer
1308
1951
  # @api private
1309
1952
  # @return [String] expression to embed in a capture subprocess script.
1310
1953
  def loaded_files_expression
1311
- root = project_root_real
1312
- prefix = root.end_with?("/") ? root : "#{root}/"
1313
- "begin; _root = #{prefix.inspect}; $LOADED_FEATURES.filter_map { |f| next unless f.end_with?(\".rb\"); abs = (File.realpath(f) rescue next); abs if abs.start_with?(_root) }; rescue StandardError; []; end"
1954
+ root = FsPath.read(project_root_real)
1955
+ prefix = root.b
1956
+ prefix += "/".b unless prefix.end_with?("/".b)
1957
+ # Byte prefix: a UTF-8 root and a binary loaded path must not raise.
1958
+ # A raise here used to be rescued into an empty list, and an empty
1959
+ # dependency hash then matched a stale cache (#346).
1960
+ "begin; _root = #{prefix.inspect}; $LOADED_FEATURES.filter_map { |f| next unless f.b.end_with?(\".rb\".b); abs = (File.realpath(f) rescue next); Mutineer::FsPath.read(abs) if Mutineer::FsPath.bytes_start_with?(abs, _root) }; rescue StandardError; []; end"
1314
1961
  end
1315
1962
 
1316
1963
  # Canonical project root for loaded-feature matching (`/var` vs `/private/var`).
@@ -1324,13 +1971,14 @@ module Mutineer
1324
1971
  # @api private
1325
1972
  # @return [Array<String>] absolute realpaths.
1326
1973
  def capture_loaded_files
1327
- prefix = project_root_real
1328
- prefix = "#{prefix}/" unless prefix.end_with?("/")
1974
+ root = FsPath.read(project_root_real)
1975
+ prefix = root.b
1976
+ prefix += "/".b unless prefix.end_with?("/".b)
1329
1977
  $LOADED_FEATURES.filter_map do |f|
1330
- next unless f.end_with?(".rb")
1978
+ next unless f.b.end_with?(".rb".b)
1331
1979
 
1332
1980
  abs = File.realpath(f)
1333
- abs if abs.start_with?(prefix)
1981
+ FsPath.read(abs) if FsPath.bytes_start_with?(abs, prefix)
1334
1982
  rescue Errno::ENOENT
1335
1983
  nil
1336
1984
  end
@@ -1361,11 +2009,12 @@ module Mutineer
1361
2009
  # @param abs [String] absolute realpath.
1362
2010
  # @return [String, nil]
1363
2011
  def loaded_relative(abs)
1364
- root = project_root_real
1365
- prefix = root.end_with?("/") ? root : "#{root}/"
1366
- return unless abs.start_with?(prefix)
2012
+ root = FsPath.read(project_root_real)
2013
+ prefix = root.b
2014
+ prefix += "/".b unless prefix.end_with?("/".b)
2015
+ return unless FsPath.bytes_start_with?(abs, prefix)
1367
2016
 
1368
- abs.delete_prefix(prefix)
2017
+ FsPath.bytes_delete_prefix(abs, prefix)
1369
2018
  end
1370
2019
 
1371
2020
  # Byte fingerprint of a file for cache dependency checks.
@@ -1383,17 +2032,512 @@ module Mutineer
1383
2032
  #
1384
2033
  # @api private
1385
2034
  # @param cached [Hash] parsed coverage.json.
2035
+ # @param except [Array<String>] project-relative paths whose fingerprint
2036
+ # may differ. A method-body edit of a run source is checked by the
2037
+ # skeleton instead (#421). Any other loaded file still has to match.
1386
2038
  # @return [Boolean]
1387
- def dependencies_match?(cached)
2039
+ def dependencies_match?(cached, except: [])
1388
2040
  deps = cached["dependencies"]
1389
2041
  return false unless deps.is_a?(Hash)
1390
2042
 
2043
+ skip = except.to_set
1391
2044
  deps.all? do |rel, fingerprint|
2045
+ next true if skip.include?(rel)
2046
+
1392
2047
  abs = absolute(rel)
1393
2048
  File.file?(abs) && file_fingerprint(abs) == fingerprint
1394
2049
  end
1395
2050
  end
1396
2051
 
2052
+ # True when `cached` is a complete map for this exact run: same digest,
2053
+ # same loaded files, and the keys a later partial recapture needs.
2054
+ #
2055
+ # @api private
2056
+ # @param cached [Hash, nil] parsed coverage.json.
2057
+ # @return [Boolean]
2058
+ def cache_hit?(cached)
2059
+ cached && cached["digest"] == @digest && dependencies_match?(cached) &&
2060
+ cached.key?("load_methods") && cached.key?("timings") &&
2061
+ parallel_workers_match?(cached) && standalone_load_lines_current?(cached) &&
2062
+ partial_cache?(cached)
2063
+ end
2064
+
2065
+ # True when `cached` has the keys {PARTIAL_CACHE_KEYS} names.
2066
+ #
2067
+ # @api private
2068
+ # @param cached [Hash] parsed coverage.json.
2069
+ # @return [Boolean]
2070
+ def partial_cache?(cached)
2071
+ PARTIAL_CACHE_KEYS.all? { |key| cached.key?(key) }
2072
+ end
2073
+
2074
+ # Copies a saved map into this object. Boot mode keeps the load lines it
2075
+ # just read from the live boot.
2076
+ #
2077
+ # @api private
2078
+ # @param cached [Hash] parsed coverage.json.
2079
+ # @return [void]
2080
+ def apply_cache(cached)
2081
+ @map = cached["map"] || {}
2082
+ @timings = cached["timings"] || {}
2083
+ unless @boot_path
2084
+ @load_lines = Set.new(cached["load_lines"])
2085
+ @load_methods = Set.new(cached["load_methods"])
2086
+ end
2087
+ @failed_test_files = Array(cached["failed_test_files"])
2088
+ @failed_clean_tests = []
2089
+ @failed_clean_details = {}
2090
+ @clean_timeouts = {}
2091
+ @loaded_dependencies = cached["dependencies"] || {}
2092
+ end
2093
+
2094
+ # How to recapture part of `cached`, or nil when the run must capture
2095
+ # every test. Nil covers a class-body edit, a moved line, a load-time
2096
+ # method whose body changed, a loaded file outside the run's sources, a
2097
+ # run input, and a change to which tests pair with a source (#421).
2098
+ #
2099
+ # @api private
2100
+ # @param cached [Hash, nil] parsed coverage.json.
2101
+ # @return [Hash, nil] `"removed"`, `"recapture"`, and `"changed_sources"`.
2102
+ def partial_plan(cached)
2103
+ return nil unless cached.is_a?(Hash) && partial_cache?(cached)
2104
+ return nil unless cached.key?("load_methods") && cached.key?("timings")
2105
+ return nil unless parallel_workers_match?(cached) && standalone_load_lines_current?(cached)
2106
+ return nil unless cached["run_inputs"] == run_inputs_digest
2107
+ return nil unless dependencies_match?(cached, except: source_rels.to_a)
2108
+
2109
+ tests = cached["tests"]
2110
+ sources = cached["sources"]
2111
+ skeletons = cached["skeletons"]
2112
+ return nil unless tests.is_a?(Hash) && sources.is_a?(Hash) && skeletons.is_a?(Hash)
2113
+
2114
+ current_tests = test_fingerprints
2115
+ current_sources = source_fingerprints
2116
+ current_skeletons = source_skeletons
2117
+ return nil unless current_sources.keys.sort == sources.keys.sort
2118
+
2119
+ changed_sources = []
2120
+ current_sources.each do |rel, fingerprint|
2121
+ next if sources[rel] == fingerprint
2122
+
2123
+ skeleton = current_skeletons[rel]
2124
+ return nil if skeleton.nil? || skeletons[rel] != skeleton
2125
+
2126
+ changed_sources << rel
2127
+ end
2128
+ return nil unless load_method_bodies_unchanged?(cached, changed_sources)
2129
+ return nil if @boot_path && load_record_changed?(cached, changed_sources)
2130
+
2131
+ old_rels = tests.keys
2132
+ new_rels = current_tests.keys
2133
+ return nil if pairing_sets_changed?(old_rels, new_rels)
2134
+
2135
+ removed = old_rels - new_rels
2136
+ added = new_rels - old_rels
2137
+ edited = (new_rels & old_rels).select { |rel| tests[rel] != current_tests[rel] }
2138
+ failed = Array(cached["failed_test_files"]) & new_rels
2139
+ touching = tests_in_source_entries(cached["map"], changed_sources) & new_rels
2140
+ recapture = (added + edited + failed + touching).uniq
2141
+ # Keeping nothing is a full capture. It rebuilds load lines in one pass.
2142
+ return nil if removed.empty? && recapture.size == new_rels.size
2143
+
2144
+ { "removed" => removed, "recapture" => recapture, "changed_sources" => changed_sources }
2145
+ end
2146
+
2147
+ # Rebuilds the tests in `plan` and keeps the rest. Returns false when the
2148
+ # recaptured load snapshot disagrees with the cache: the caller then
2149
+ # captures every test. A kept test is not credited with another source's
2150
+ # load-only lines, because those entries are left as the full capture
2151
+ # stored them and {#record_paired_load_lines} does not add them back.
2152
+ #
2153
+ # @api private
2154
+ # @param plan [Hash] the plan from {#partial_plan}.
2155
+ # @param cached [Hash] parsed coverage.json.
2156
+ # @param after_fork [Proc, nil] boot-mode fork hook.
2157
+ # @return [Boolean] true when the partial map was saved.
2158
+ def apply_partial(plan, cached, after_fork)
2159
+ apply_cache(cached)
2160
+ removed = plan["removed"]
2161
+ recapture = plan["recapture"]
2162
+ (removed + recapture).each { |rel| drop_test(rel) }
2163
+ @failed_test_files -= (removed + recapture)
2164
+ @collect_load = true
2165
+ @seen_load_lines = nil
2166
+ @seen_load_methods = nil
2167
+ @test_paths.each do |path|
2168
+ capture_one(path, after_fork) if recapture.include?(relativize(path))
2169
+ end
2170
+ @collect_load = false
2171
+ return false if !@boot_path && recapture.any? && partial_load_diverged?(cached)
2172
+
2173
+ refresh_dependency_fingerprints(plan["changed_sources"] + recapture)
2174
+ # A removed test keeps its fingerprint: a kept test may still load
2175
+ # that file, and the map does not record which test loaded what.
2176
+ sort_map_entries
2177
+ @phase_a_ran = true if recapture.any?
2178
+ warn_recaptured(recapture.size, @test_paths.size)
2179
+ checked = clean_check_test_paths
2180
+ verify_cached_clean(after_fork: after_fork,
2181
+ test_paths: checked.reject { |test| recapture.include?(relativize(test)) })
2182
+ verify_combined_clean(after_fork: after_fork)
2183
+ warn_unchecked("coverage cache", checked.size)
2184
+ save
2185
+ true
2186
+ end
2187
+
2188
+ # Removes `rel` from every map entry and forgets its timing.
2189
+ #
2190
+ # @api private
2191
+ # @param rel [String] project-relative test path.
2192
+ # @return [void]
2193
+ def drop_test(rel)
2194
+ @map.each_value { |tests| tests.delete(rel) if tests.is_a?(Array) }
2195
+ @map.delete_if { |_key, tests| !tests.is_a?(Array) || tests.empty? }
2196
+ @timings.delete(rel)
2197
+ end
2198
+
2199
+ # Puts every map entry in `@test_paths` order, which is the order a full
2200
+ # capture records tests.
2201
+ #
2202
+ # @api private
2203
+ # @return [void]
2204
+ def sort_map_entries
2205
+ @map.each_value { |tests| tests.sort_by! { |test| test_index(test) } if tests.is_a?(Array) }
2206
+ end
2207
+
2208
+ # True when this boot's load lines or load methods for `changed_sources`
2209
+ # differ from the cache. The method body then changed what ran at boot,
2210
+ # so a kept test's coverage is not trusted.
2211
+ #
2212
+ # @api private
2213
+ # @param cached [Hash] parsed coverage.json.
2214
+ # @param changed_sources [Array<String>] project-relative source paths.
2215
+ # @return [Boolean]
2216
+ def load_record_changed?(cached, changed_sources)
2217
+ wanted = changed_sources.to_set
2218
+ pick = lambda do |keys|
2219
+ Array(keys).select { |key| wanted.include?(key_source(key)) }.to_set
2220
+ end
2221
+ pick.call(@load_lines) != pick.call(cached["load_lines"]) ||
2222
+ pick.call(@load_methods) != pick.call(cached["load_methods"])
2223
+ end
2224
+
2225
+ # True when a recaptured standalone test saw different load lines than
2226
+ # the cache recorded. The body change reached load time.
2227
+ #
2228
+ # @api private
2229
+ # @param cached [Hash] parsed coverage.json.
2230
+ # @return [Boolean]
2231
+ def partial_load_diverged?(cached)
2232
+ return false unless @seen_load_lines
2233
+
2234
+ @seen_load_lines != Set.new(Array(cached["load_lines"])) ||
2235
+ @seen_load_methods != Set.new(Array(cached["load_methods"]))
2236
+ end
2237
+
2238
+ # Writes the current fingerprint of each `rels` entry over the cached one.
2239
+ # Only a path the cache already lists is refreshed: a test file that no
2240
+ # test loads is tracked by the "tests" fingerprints, as on a full capture.
2241
+ #
2242
+ # @api private
2243
+ # @param rels [Array<String>] project-relative paths.
2244
+ # @return [void]
2245
+ def refresh_dependency_fingerprints(rels)
2246
+ rels.each do |rel|
2247
+ next unless @loaded_dependencies.key?(rel)
2248
+
2249
+ abs = absolute(rel)
2250
+ next unless File.file?(abs)
2251
+
2252
+ @loaded_dependencies[rel] = file_fingerprint(abs)
2253
+ end
2254
+ end
2255
+
2256
+ # Tests in `map` entries whose source is one of `source_rels`.
2257
+ #
2258
+ # @api private
2259
+ # @param map [Hash, nil] a "file:line" => [test files] map.
2260
+ # @param source_rels [Array<String>] project-relative source paths.
2261
+ # @return [Array<String>] project-relative test paths.
2262
+ def tests_in_source_entries(map, source_rels)
2263
+ return [] unless map.is_a?(Hash)
2264
+
2265
+ wanted = source_rels.to_set
2266
+ map.each_with_object([]) do |(key, tests), list|
2267
+ next unless wanted.include?(key_source(key))
2268
+
2269
+ Array(tests).each { |test| list << test if test.is_a?(String) && !list.include?(test) }
2270
+ end
2271
+ end
2272
+
2273
+ # True when the conventional pair for any run source is a different set
2274
+ # of tests than the cache was built with. Load-only lines would move.
2275
+ #
2276
+ # @api private
2277
+ # @param old_rels [Array<String>] project-relative tests in the cache.
2278
+ # @param new_rels [Array<String>] project-relative tests in this run.
2279
+ # @return [Boolean]
2280
+ def pairing_sets_changed?(old_rels, new_rels)
2281
+ old_set = old_rels.to_set
2282
+ new_set = new_rels.to_set
2283
+ source_rels.any? { |rel| paired_among(rel, old_set) != paired_among(rel, new_set) }
2284
+ end
2285
+
2286
+ # Conventional tests for `source_rel` that are in `test_rels`.
2287
+ #
2288
+ # @api private
2289
+ # @param source_rel [String] project-relative source path.
2290
+ # @param test_rels [Set<String>] project-relative test paths.
2291
+ # @return [Array<String>]
2292
+ def paired_among(source_rel, test_rels)
2293
+ Pairing.infer_tests(source_rel, project_root: @project_root, prefer: @framework || "minitest")
2294
+ .select { |path| test_rels.include?(path) }
2295
+ .sort
2296
+ end
2297
+
2298
+ # Project-relative paths of this run's sources.
2299
+ #
2300
+ # @api private
2301
+ # @return [Set<String>]
2302
+ def source_rels
2303
+ @source_rels ||= @source_paths.to_set { |path| relativize(absolute(path)) }
2304
+ end
2305
+
2306
+ # Fingerprint of each test file, by project-relative path.
2307
+ #
2308
+ # @api private
2309
+ # @return [Hash{String => String}]
2310
+ def test_fingerprints
2311
+ @test_paths.to_h { |path| [relativize(absolute(path)), file_fingerprint(absolute(path))] }
2312
+ end
2313
+
2314
+ # Digest of everything that forces a full capture except source and test
2315
+ # contents (#421). The source list is included, so adding a source still
2316
+ # captures every test: standalone capture loads every source.
2317
+ #
2318
+ # @api private
2319
+ # @return [String] hex digest.
2320
+ def run_inputs_digest
2321
+ digest = Digest::SHA256.new
2322
+ source_rels.to_a.sort.each do |rel|
2323
+ digest.update("source\0")
2324
+ digest.update(rel)
2325
+ digest.update("\0")
2326
+ end
2327
+ @require_paths.each { |path| digest_group(digest, "require", [digest_path(path)]) }
2328
+ digest_group(digest, "boot", [digest_path(@boot_path)]) if @boot_path
2329
+ ownership_paths.each do |rel|
2330
+ digest.update("owner\0")
2331
+ digest.update(rel)
2332
+ digest.update("\0")
2333
+ end
2334
+ @load_paths.sort.each { |load_path| digest.update("loadpath\0#{load_path}\0") }
2335
+ digest.update("framework\0#{@framework}\0")
2336
+ digest_test_seed(digest)
2337
+ digest.hexdigest
2338
+ end
2339
+
2340
+ # Skeleton digest of each source, by project-relative path. Nil when the
2341
+ # file does not parse: a partial recapture then does not run.
2342
+ #
2343
+ # @api private
2344
+ # @return [Hash{String => String, nil}]
2345
+ def source_skeletons
2346
+ @source_paths.to_h { |path| [relativize(absolute(path)), source_skeleton_digest(absolute(path))] }
2347
+ end
2348
+
2349
+ # Digest of `abs` with each method body blanked, plus the line of every
2350
+ # `def`. Two files match when the edit is inside method bodies and no
2351
+ # line moved. A comment outside a body, a class-body statement, and an
2352
+ # added or removed line do not match.
2353
+ #
2354
+ # @api private
2355
+ # @param abs [String] absolute source path.
2356
+ # @return [String, nil] hex digest, or nil when the file cannot be parsed.
2357
+ def source_skeleton_digest(abs)
2358
+ source = File.binread(abs)
2359
+ result = Prism.parse(source)
2360
+ return nil unless result.errors.empty?
2361
+
2362
+ ranges = []
2363
+ tuples = []
2364
+ each_def(result.value) do |node|
2365
+ loc = node.body&.location
2366
+ range = body_byte_range(node)
2367
+ ranges << range if range
2368
+ tuples << [node.location.start_line, node.location.start_column, loc&.start_line, loc&.end_line]
2369
+ end
2370
+ digest = Digest::SHA256.new
2371
+ digest.update(blank_method_bodies(source, ranges))
2372
+ tuples.sort.each do |tuple|
2373
+ digest.update("\0")
2374
+ digest.update(tuple.map { |part| part.nil? ? "-" : part.to_s }.join(":"))
2375
+ end
2376
+ digest.hexdigest
2377
+ rescue SystemCallError
2378
+ nil
2379
+ end
2380
+
2381
+ # Byte range of the body of `node`, after its name and parameters. Nil
2382
+ # when the body is empty. A `def` with `rescue` or `ensure` has a
2383
+ # BeginNode body that starts at the `def` keyword, so the range starts
2384
+ # where the signature ends: a same-line name or parameter edit is not a
2385
+ # body edit.
2386
+ #
2387
+ # @api private
2388
+ # @param node [Prism::DefNode] a method definition.
2389
+ # @return [Array(Integer, Integer), nil] half-open byte range.
2390
+ def body_byte_range(node)
2391
+ loc = node.body&.location
2392
+ return nil unless loc
2393
+
2394
+ signature_end = [node.name_loc, node.parameters&.location, node.rparen_loc].compact.map(&:end_offset).max
2395
+ start = [loc.start_offset, signature_end].max
2396
+ start < loc.end_offset ? [start, loc.end_offset] : nil
2397
+ end
2398
+
2399
+ # `source` with every method-body byte range replaced by one marker.
2400
+ # Nested bodies are one marker: the outer range already covers them.
2401
+ #
2402
+ # @api private
2403
+ # @param source [String] source bytes.
2404
+ # @param ranges [Array<Array(Integer, Integer)>] half-open byte ranges.
2405
+ # @return [String] binary skeleton text.
2406
+ def blank_method_bodies(source, ranges)
2407
+ return source if ranges.empty?
2408
+
2409
+ skip = Array.new(source.bytesize, false)
2410
+ ranges.each do |start, finish|
2411
+ next if start.negative? || finish > source.bytesize
2412
+
2413
+ (start...finish).each { |index| skip[index] = true }
2414
+ end
2415
+ out = +"".b
2416
+ source.each_byte.with_index do |byte, index|
2417
+ if skip[index]
2418
+ out << 0 unless index.positive? && skip[index - 1]
2419
+ else
2420
+ out << byte
2421
+ end
2422
+ end
2423
+ out
2424
+ end
2425
+
2426
+ # Yields each `def` in `node`, including defs nested in another body.
2427
+ #
2428
+ # @api private
2429
+ # @param node [Prism::Node, nil] syntax node.
2430
+ # @yieldparam node [Prism::DefNode]
2431
+ # @return [void]
2432
+ def each_def(node, &block)
2433
+ return unless node.is_a?(Prism::Node)
2434
+
2435
+ block.call(node) if node.is_a?(Prism::DefNode)
2436
+ node.child_nodes.each { |child| each_def(child, &block) }
2437
+ end
2438
+
2439
+ # True when every load-time method of `changed_sources` still has the
2440
+ # body the cache recorded. A different body can change a value other
2441
+ # tests read without those tests appearing in this source's map.
2442
+ #
2443
+ # @api private
2444
+ # @param cached [Hash] parsed coverage.json.
2445
+ # @param changed_sources [Array<String>] project-relative source paths.
2446
+ # @return [Boolean]
2447
+ def load_method_bodies_unchanged?(cached, changed_sources)
2448
+ stored = cached["load_method_bodies"]
2449
+ return false unless stored.is_a?(Hash)
2450
+
2451
+ wanted = changed_sources.to_set
2452
+ stored.all? do |key, digest|
2453
+ next true unless wanted.include?(key_source(key))
2454
+ next false unless digest.is_a?(String)
2455
+
2456
+ body_digest_at(key) == digest
2457
+ end
2458
+ end
2459
+
2460
+ # Body digest of each method in {#load_methods}, keyed the same way.
2461
+ # `"missing"` marks a `def` the file no longer has at that line.
2462
+ #
2463
+ # @api private
2464
+ # @return [Hash{String => String}]
2465
+ def current_load_method_bodies
2466
+ @load_methods.to_h { |key| [key, body_digest_at(key) || "missing"] }
2467
+ end
2468
+
2469
+ # Digest of the method body at the `def` named by `key`, in the file as
2470
+ # it is now. Nil when that `def` is gone or the file does not parse.
2471
+ #
2472
+ # @api private
2473
+ # @param key [String] a "file:line:column" load-method key.
2474
+ # @return [String, nil] hex digest.
2475
+ def body_digest_at(key)
2476
+ rel, line, column = split_method_key(key)
2477
+ text = method_bodies(rel)&.[]([line, column])
2478
+ return nil if text.nil?
2479
+
2480
+ Digest::SHA256.hexdigest(text)
2481
+ end
2482
+
2483
+ # Method bodies in `rel`, keyed by the `def` line and column Coverage uses.
2484
+ # Nil when the file does not parse.
2485
+ #
2486
+ # @api private
2487
+ # @param rel [String] project-relative source path.
2488
+ # @return [Hash{Array(Integer, Integer) => String}, nil]
2489
+ def method_bodies(rel)
2490
+ @method_bodies ||= {}
2491
+ return @method_bodies[rel] if @method_bodies.key?(rel)
2492
+
2493
+ abs = absolute(rel)
2494
+ return @method_bodies[rel] = nil unless File.file?(abs)
2495
+
2496
+ source = File.binread(abs)
2497
+ result = Prism.parse(source)
2498
+ return @method_bodies[rel] = nil unless result.errors.empty?
2499
+
2500
+ found = {}
2501
+ each_def(result.value) do |node|
2502
+ loc = node.body&.location
2503
+ text = if loc && loc.end_offset > loc.start_offset
2504
+ source.byteslice(loc.start_offset, loc.end_offset - loc.start_offset)
2505
+ else
2506
+ +""
2507
+ end
2508
+ found[[node.location.start_line, node.location.start_column]] = text
2509
+ end
2510
+ @method_bodies[rel] = found
2511
+ rescue SystemCallError
2512
+ @method_bodies[rel] = nil
2513
+ end
2514
+
2515
+ # Source path, `def` line, and `def` column of a load-method key.
2516
+ #
2517
+ # @api private
2518
+ # @param key [String] a "file:line:column" key.
2519
+ # @return [Array(String, Integer, Integer)]
2520
+ def split_method_key(key)
2521
+ rel = key_source(key)
2522
+ line, column = key.delete_prefix("#{rel}:").split(":").map(&:to_i)
2523
+ [rel, line, column]
2524
+ end
2525
+
2526
+ # Tells the user how many test files a partial recapture ran again.
2527
+ # Prints nothing when every file was recaptured or none was: the first
2528
+ # is a full capture, and the second kept the whole map.
2529
+ #
2530
+ # @api private
2531
+ # @param recaptured [Integer] test files captured again.
2532
+ # @param total [Integer] test files in the run.
2533
+ # @return [void]
2534
+ def warn_recaptured(recaptured, total)
2535
+ kept = total - recaptured
2536
+ return unless recaptured.positive? && kept.positive?
2537
+
2538
+ warn "[mutineer] coverage cache: recaptured #{recaptured} of #{total} test files; kept #{kept}."
2539
+ end
2540
+
1397
2541
  # True when `cached` records the same +PARALLEL_WORKERS+ value this process
1398
2542
  # has now. Rails reads that variable inside +parallelize+ and can run one
1399
2543
  # test file in several workers. A missing key is a map saved before this
@@ -1426,17 +2570,43 @@ module Mutineer
1426
2570
  end
1427
2571
  @load_paths.sort.each { |lp| d.update("loadpath\0#{lp}\0") }
1428
2572
  d.update("framework\0#{@framework}\0")
1429
- # The seed sets the test order, and the order can change which lines run (#282).
1430
- # This is the configured seed. A test helper that changes SEED is not
1431
- # digested, like any other helper edit.
1432
- d.update("seed\0#{MinitestIntegration.seed}\0") unless @framework == "rspec"
2573
+ digest_test_seed(d)
1433
2574
  d.hexdigest
1434
2575
  end
1435
2576
 
2577
+ # Adds the test seed inputs to `digest`. The seed sets the test order, and
2578
+ # the order can change which lines run (#282). Minitest: the configured
2579
+ # seed. A test helper that changes SEED is not digested, like any other
2580
+ # helper edit. RSpec: the pinned seed, SPEC_OPTS, and the project `.rspec`
2581
+ # files. `~/.rspec` and a `config.seed` in a spec helper are not digested;
2582
+ # a helper edit is seen when that file is part of the capture.
2583
+ # {#compute_digest} and {#run_inputs_digest} share this, so a seed change
2584
+ # never reaches a partial recapture (#421).
2585
+ #
2586
+ # @api private
2587
+ # @param digest [Digest::SHA256] the digest to update.
2588
+ # @return [void]
2589
+ def digest_test_seed(digest)
2590
+ if @framework == "rspec"
2591
+ digest.update("seed\0#{MinitestIntegration::PINNED_SEED}\0#{ENV['SPEC_OPTS']}\0")
2592
+ %w[.rspec .rspec-local].each do |name|
2593
+ path = File.join(@project_root, name)
2594
+ digest.update("rspec-opts\0#{name}\0")
2595
+ digest.update(File.file?(path) ? File.binread(path) : "")
2596
+ digest.update("\0")
2597
+ end
2598
+ else
2599
+ digest.update("seed\0#{MinitestIntegration.seed}\0")
2600
+ end
2601
+ end
2602
+
1436
2603
  # Longer source files that can take a split test from a configured source.
1437
2604
  # Only their presence is digested. A content edit of a sibling does not
1438
2605
  # change pairing, and it does not rebuild coverage. Each directory is
1439
- # listed once, even when many sources share it.
2606
+ # listed once, even when many sources share it. A concern also digests
2607
+ # longer files in its namespace directory, because those files can own a
2608
+ # namespace split name (#408). A model does not digest a concern file:
2609
+ # the model keeps a split file it already pairs with.
1440
2610
  #
1441
2611
  # @return [Array<String>] project-relative paths, sorted.
1442
2612
  def ownership_paths
@@ -1447,26 +2617,39 @@ module Mutineer
1447
2617
  next [] if rel.start_with?("/")
1448
2618
 
1449
2619
  base, = Pairing.logical_path(rel)
1450
- name = File.basename(base)
1451
- dir = File.dirname(base)
1452
- folders = [dir == "." ? nil : dir]
1453
- %w[app lib].each { |prefix| folders << (dir == "." ? prefix : File.join(prefix, dir)) }
1454
- folders.uniq.flat_map do |folder|
1455
- abs = folder ? File.join(root, folder) : root
1456
- entries = listings.fetch(abs) do
1457
- listings[abs] = File.directory?(abs) ? Dir.children(abs) : []
1458
- end
1459
- entries.filter_map do |entry|
1460
- next unless entry.end_with?(".rb")
2620
+ bases = [base]
2621
+ ns = Pairing.namespace_base(rel)
2622
+ bases << ns if ns
2623
+ bases.uniq.flat_map { |logical| owner_files(root, logical, listings) }
2624
+ end
2625
+ paths.uniq.sort
2626
+ end
1461
2627
 
1462
- stem = entry.delete_suffix(".rb")
1463
- next unless stem.start_with?("#{name}_") && stem.length > name.length
2628
+ # Longer `.rb` files that can own a split name of `base`.
2629
+ #
2630
+ # @param root [String] expanded project root.
2631
+ # @param base [String] logical source path without extension.
2632
+ # @param listings [Hash{String => Array<String>}] directory children, filled once.
2633
+ # @return [Array<String>] project-relative paths.
2634
+ def owner_files(root, base, listings)
2635
+ name = File.basename(base)
2636
+ dir = File.dirname(base)
2637
+ folders = [dir == "." ? nil : dir]
2638
+ %w[app lib].each { |prefix| folders << (dir == "." ? prefix : File.join(prefix, dir)) }
2639
+ folders.uniq.flat_map do |folder|
2640
+ abs = folder ? File.join(root, folder) : root
2641
+ entries = listings.fetch(abs) do
2642
+ listings[abs] = File.directory?(abs) ? Dir.children(abs).map { |entry| FsPath.read(entry) } : []
2643
+ end
2644
+ entries.filter_map do |entry|
2645
+ next unless entry.end_with?(".rb")
1464
2646
 
1465
- folder ? File.join(folder, entry) : entry
1466
- end
2647
+ stem = entry.delete_suffix(".rb")
2648
+ next unless stem.start_with?("#{name}_") && stem.length > name.length
2649
+
2650
+ folder ? File.join(folder, entry) : entry
1467
2651
  end
1468
2652
  end
1469
- paths.uniq.sort
1470
2653
  end
1471
2654
 
1472
2655
  # boot_path and require_paths are require-style paths (e.g. "config/environment",
@@ -1487,7 +2670,9 @@ module Mutineer
1487
2670
  # @return [Array(String, String, Array<String>)] grouped digest data.
1488
2671
  def digest_group(digest, role, paths)
1489
2672
  paths.sort.each do |p|
1490
- content = File.read(absolute(p))
2673
+ # Bytes, not a locale read: a magic-comment encoding such as EUC-JP
2674
+ # is not valid UTF-8, and the digest is of the file's bytes (#346).
2675
+ content = File.binread(absolute(p))
1491
2676
  digest.update(role)
1492
2677
  digest.update("\0")
1493
2678
  digest.update(relativize(absolute(p)))
@@ -1539,7 +2724,11 @@ module Mutineer
1539
2724
  data = { "digest" => @digest, "failed_test_files" => @failed_test_files,
1540
2725
  "dependencies" => @loaded_dependencies, "map" => @map,
1541
2726
  "load_lines" => @load_lines.to_a.sort, "load_methods" => @load_methods.to_a.sort,
1542
- "timings" => @timings, "parallel_workers" => ENV.fetch("PARALLEL_WORKERS", nil) }
2727
+ "timings" => @timings, "parallel_workers" => ENV.fetch("PARALLEL_WORKERS", nil),
2728
+ NARROW_LOAD_LINES => true, "run_inputs" => run_inputs_digest,
2729
+ "sources" => source_fingerprints, "skeletons" => source_skeletons,
2730
+ "tests" => test_fingerprints, "load_method_bodies" => current_load_method_bodies }
2731
+ data[LOAD_LINE_PAIRS] = load_line_pairs unless @boot_path
1543
2732
  if @boot_path
1544
2733
  data.merge!("inputs" => compute_digest(inputs_only: true), "sources" => source_fingerprints,
1545
2734
  "owners" => ownership_paths)