mutineer 1.8.1 → 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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +733 -0
  3. data/README.md +85 -50
  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 +540 -59
  8. data/lib/mutineer/command_redact.rb +38 -0
  9. data/lib/mutineer/concern_blocks.rb +95 -24
  10. data/lib/mutineer/config.rb +112 -6
  11. data/lib/mutineer/coverage_map.rb +1345 -156
  12. data/lib/mutineer/daemon_backend.rb +41 -18
  13. data/lib/mutineer/daemon_client.rb +104 -21
  14. data/lib/mutineer/daemon_server.rb +85 -14
  15. data/lib/mutineer/database_urls.rb +189 -0
  16. data/lib/mutineer/external_backend.rb +93 -6
  17. data/lib/mutineer/fs_path.rb +125 -0
  18. data/lib/mutineer/isolation.rb +380 -25
  19. data/lib/mutineer/job_plan.rb +352 -41
  20. data/lib/mutineer/minitest_integration/file_order.rb +56 -0
  21. data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
  22. data/lib/mutineer/minitest_integration.rb +26 -58
  23. data/lib/mutineer/mutant_id.rb +4 -1
  24. data/lib/mutineer/mutation.rb +10 -4
  25. data/lib/mutineer/mutator_registry.rb +6 -3
  26. data/lib/mutineer/mutators/collection_method.rb +195 -6
  27. data/lib/mutineer/mutators/literal_mutation.rb +33 -3
  28. data/lib/mutineer/mutators/range_literal.rb +78 -29
  29. data/lib/mutineer/mutators/rescue_body.rb +126 -0
  30. data/lib/mutineer/mutators/return_nil.rb +23 -6
  31. data/lib/mutineer/output_filter.rb +451 -0
  32. data/lib/mutineer/pairing.rb +463 -31
  33. data/lib/mutineer/parser.rb +53 -0
  34. data/lib/mutineer/project_path.rb +15 -8
  35. data/lib/mutineer/rails_worker_db.rb +366 -6
  36. data/lib/mutineer/reach_probe.rb +229 -0
  37. data/lib/mutineer/redaction.rb +301 -41
  38. data/lib/mutineer/reload_guards.rb +985 -0
  39. data/lib/mutineer/reporter.rb +467 -58
  40. data/lib/mutineer/result.rb +6 -7
  41. data/lib/mutineer/run_seed.rb +16 -0
  42. data/lib/mutineer/runner.rb +194 -55
  43. data/lib/mutineer/source_bytes.rb +187 -0
  44. data/lib/mutineer/statement_lines.rb +108 -14
  45. data/lib/mutineer/test_runners/rspec.rb +118 -10
  46. data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
  47. data/lib/mutineer/test_runners.rb +3 -2
  48. data/lib/mutineer/triage.rb +55 -10
  49. data/lib/mutineer/version.rb +1 -1
  50. data/lib/mutineer.rb +1 -0
  51. data/skills/mutineer/SKILL.md +12 -4
  52. metadata +13 -1
@@ -4,15 +4,27 @@ require_relative "project_path"
4
4
 
5
5
  module Mutineer
6
6
  # Source -> test pairing by path convention (#11). Pure stdlib path logic:
7
- # no Rails, no class loading, no process. Three jobs:
7
+ # no Rails, no class loading, no process. Six jobs:
8
8
  # * expand_sources — a directory argument becomes its sorted **/*.rb files;
9
9
  # a file inside the root becomes its root-relative path.
10
+ # * expand_test: one --test value (#380). A directory becomes the test
11
+ # files under it. A glob becomes the files it matches.
12
+ # An existing file stays as typed.
13
+ # * resolve_test_map — expand each `tests:` map entry's source and test
14
+ # patterns (#382), clean and dedupe the file lists, and
15
+ # record every pattern that matched no file.
16
+ # * mapped_tests_for — the mapped tests for one source from a resolved
17
+ # map, concatenated in entry order (#382).
10
18
  # * infer_tests — a source's test files by convention (app/ and lib/
11
19
  # sources map to test/.../_test.rb, test/.../<name>_*_test.rb,
12
20
  # test/.../test_*.rb or spec/.../_spec.rb), preserving
13
21
  # namespaced subdirectories. The first EXISTING exact
14
22
  # candidate stays first. Split Minitest files join a
15
23
  # Minitest match. A matched spec is left as that one file.
24
+ # A concern at app/<dir>/concerns/<rest>.rb with no test
25
+ # there also tries exact and split names at <dir>/<rest>.
26
+ # A longer source keeps its file. A model keeps a split
27
+ # file it already pairs with (#408).
16
28
  # * test_given_as_source? — whether a positional argument is a test file
17
29
  # by these same names, given as a source by mistake
18
30
  # after `--test` (#293).
@@ -33,19 +45,311 @@ module Mutineer
33
45
  # @param project_root [String] repository root for relative expansion.
34
46
  # @return [Array<String>] flattened, deduped source file list.
35
47
  def expand_sources(args, project_root:)
36
- root = File.expand_path(project_root)
48
+ root = FsPath.read(File.expand_path(project_root))
37
49
  # The root as typed and as its real path (macOS `/var` vs `/private/var`),
38
50
  # resolved once for every file below.
39
51
  roots = [root, ProjectPath.root_real(root)].uniq
40
52
  Array(args).flat_map do |arg|
53
+ arg = FsPath.read(arg)
41
54
  abs = File.expand_path(arg, root)
42
55
  if File.directory?(abs)
43
- Dir.glob(File.join(abs, "**", "*.rb")).sort.map { |f| existing_relative(f, roots) || f }
56
+ # Escape metacharacters in the directory name so `component[1]` stays
57
+ # literal (same rule as {directory_test_files}).
58
+ listed = if (rel = root_relative(abs, roots))
59
+ fs_glob(File.join(glob_literal(rel), "**", "*.rb"), base: root)
60
+ else
61
+ fs_glob(File.join(glob_literal(abs), "**", "*.rb"))
62
+ end
63
+ listed.sort_by(&:b).map do |f|
64
+ full = File.absolute_path?(f) ? f : File.expand_path(f, root)
65
+ existing_relative(full, roots) || full
66
+ end
44
67
  else
45
68
  # `..` resolves as text, as FileSwap and the daemon already read it.
46
69
  [(existing_relative(abs, roots) if File.exist?(abs)) || arg]
47
70
  end
48
- end.uniq
71
+ end.map { |path| FsPath.read(path) }.uniq
72
+ end
73
+
74
+ # Characters that make a --test value a glob when no path has that name.
75
+ # An existing file is never read as a glob, so a name may contain these.
76
+ TEST_GLOB_MARK = /[*?\[{]/
77
+
78
+ # How one --test value is read. Blank is not a directory: File.expand_path
79
+ # of "" is the project root, and that would select every test.
80
+ #
81
+ # @param value [String] one --test value, as typed.
82
+ # @param project_root [String] repository root.
83
+ # @return [Symbol] :blank, :directory, :file, :glob, or :plain.
84
+ def classify_test_value(value, project_root:)
85
+ return :blank if value.nil? || value.strip.empty?
86
+
87
+ abs = File.expand_path(value, project_root)
88
+ return :directory if File.directory?(abs)
89
+ return :file if File.exist?(abs)
90
+ return :glob if value.match?(TEST_GLOB_MARK)
91
+
92
+ :plain
93
+ end
94
+
95
+ # One --test value, as paths. [] means a directory or glob matched nothing.
96
+ # A file stays as typed. A missing plain path stays as typed, so the CLI
97
+ # can still say "no such file". Paths from a directory or glob are sorted.
98
+ # Inside the project they are root-relative. Outside it they are absolute.
99
+ #
100
+ # @param value [String] one --test value, as typed.
101
+ # @param project_root [String] repository root.
102
+ # @return [Array<String>] matching paths, or [value] when it is not expanded.
103
+ def expand_test(value, project_root:)
104
+ root = File.expand_path(project_root)
105
+ roots = [root, ProjectPath.root_real(root)].uniq
106
+ case classify_test_value(value, project_root: project_root)
107
+ when :directory
108
+ directory_test_files(File.expand_path(value, root), root, roots)
109
+ when :glob
110
+ glob_test_files(value, root, roots)
111
+ else
112
+ [value]
113
+ end
114
+ end
115
+
116
+ # Expand every entry of a checked `test_map` into cleaned file lists.
117
+ # Source patterns use Dir.glob then {expand_sources}, so a directory
118
+ # match becomes its `.rb` files and `./lib/a.rb` / `lib//a.rb` clean to
119
+ # the same path. Test patterns go through {expand_test}; a missing plain
120
+ # path is dropped (no match). Overlapping patterns keep each file once,
121
+ # sorted. Patterns with no file are listed under `:unmatched` with a
122
+ # 1-based entry number and the key (`"sources"` or `"tests"`). Does not
123
+ # print or exit; the CLI reports unmatched patterns (#382).
124
+ #
125
+ # @param test_map [Array<Hash{Symbol => Array<String>}>] checked map entries
126
+ # with `:sources` and `:tests` pattern lists.
127
+ # @param project_root [String] repository root for relative expansion.
128
+ # @return [Hash{Symbol => Array}] `:entries` is
129
+ # `[{sources: [...], tests: [...]}, ...]`; `:unmatched` is
130
+ # `[{entry: Integer, key: String, pattern: String}, ...]`.
131
+ def resolve_test_map(test_map, project_root:)
132
+ root = File.expand_path(project_root)
133
+ roots = [root, ProjectPath.root_real(root)].uniq
134
+ unmatched = []
135
+ entries = Array(test_map).each_with_index.map do |entry, index|
136
+ number = index + 1
137
+ sources = expand_map_patterns(Array(entry[:sources]), number, "sources", unmatched) do |pattern|
138
+ expand_map_source_pattern(pattern, root, roots)
139
+ end
140
+ tests = expand_map_patterns(Array(entry[:tests]), number, "tests", unmatched) do |pattern|
141
+ expand_map_test_pattern(pattern, root, roots)
142
+ end
143
+ { sources: sources, tests: tests }
144
+ end
145
+ { entries: entries, unmatched: unmatched }
146
+ end
147
+
148
+ # Mapped test files for one source from a resolved map. Each entry whose
149
+ # expanded sources include `source` contributes its tests, in entry order.
150
+ # An unmatched source returns `[]`. Does not dedupe across entries; the
151
+ # CLI unions the run's test list (#382).
152
+ #
153
+ # @param source [String] root-relative source path, as in the resolved map.
154
+ # @param resolved [Hash{Symbol => Array}] result of {resolve_test_map}.
155
+ # @return [Array<String>] mapped test paths in entry order.
156
+ def mapped_tests_for(source, resolved)
157
+ Array(resolved[:entries]).each_with_object([]) do |entry, out|
158
+ out.concat(entry[:tests]) if entry[:sources].include?(source)
159
+ end
160
+ end
161
+
162
+ # Characters Dir.glob reads as a pattern. A real directory name may
163
+ # contain them. They are escaped so the name stays a literal prefix.
164
+ GLOB_META = /([*?\[\]{}\\])/
165
+
166
+ # A path used as a literal prefix in Dir.glob.
167
+ #
168
+ # @param path [String] directory path, relative or absolute.
169
+ # @return [String] the same path with pattern characters escaped.
170
+ def glob_literal(path)
171
+ FsPath.read(path).gsub(GLOB_META) { "\\#{Regexp.last_match(1)}" }
172
+ end
173
+
174
+ # `Dir.glob` with each path read as UTF-8, or binary when it is not (#346).
175
+ #
176
+ # @param args [Array] arguments forwarded to {Dir.glob}.
177
+ # @return [Array<String>]
178
+ def fs_glob(*args, **kwargs)
179
+ Dir.glob(*args, **kwargs).map { |path| FsPath.read(path) }
180
+ end
181
+
182
+ # `Dir.children` with each name read as UTF-8, or binary when it is not (#346).
183
+ #
184
+ # @param dir [String] directory to list.
185
+ # @return [Array<String>]
186
+ def fs_children(dir)
187
+ Dir.children(dir).map { |path| FsPath.read(path) }
188
+ end
189
+
190
+ # Test files under a directory. The name rule is {TEST_NAME}. Dot entries
191
+ # stay out, because Dir.glob skips them. The result is sorted.
192
+ # The directory name is literal, even when it contains a pattern character.
193
+ #
194
+ # @param abs [String] absolute directory path.
195
+ # @param root [String] expanded project root.
196
+ # @param roots [Array<String>] the project root, expanded and real.
197
+ # @return [Array<String>] root-relative paths, or absolute paths outside the root.
198
+ def directory_test_files(abs, root, roots)
199
+ listed = if (rel = root_relative(abs, roots))
200
+ fs_glob(File.join(glob_literal(rel), "**", "*.rb"), base: root)
201
+ else
202
+ fs_glob(File.join(glob_literal(abs), "**", "*.rb"))
203
+ end
204
+ listed.filter_map { |match| named_test_file(match, root, roots) }.sort_by(&:b)
205
+ end
206
+
207
+ # Files a glob matches from the project root. Directories are ignored.
208
+ # There is no test-name filter. An absolute pattern ignores base.
209
+ # Dir.glob keeps a leading "./"; the path is then made root-relative.
210
+ #
211
+ # @param pattern [String] glob as typed.
212
+ # @param root [String] expanded project root.
213
+ # @param roots [Array<String>] the project root, expanded and real.
214
+ # @return [Array<String>] sorted matching file paths.
215
+ def glob_test_files(pattern, root, roots)
216
+ listed = if File.absolute_path?(pattern)
217
+ fs_glob(pattern)
218
+ else
219
+ fs_glob(pattern, base: root)
220
+ end
221
+ listed.filter_map { |match| glob_file(match, root, roots) }.sort_by(&:b)
222
+ end
223
+
224
+ # A directory match whose basename is a test name, as a stored path.
225
+ #
226
+ # @param match [String] path from Dir.glob.
227
+ # @param root [String] expanded project root.
228
+ # @param roots [Array<String>] the project root, expanded and real.
229
+ # @return [String, nil] stored path, or nil when the match is not a test file.
230
+ def named_test_file(match, root, roots)
231
+ full = File.absolute_path?(match) ? match : File.expand_path(match, root)
232
+ return unless File.file?(full)
233
+ return unless File.basename(full).match?(TEST_NAME)
234
+
235
+ existing_relative(full, roots) || full
236
+ end
237
+
238
+ # A glob match that is a file, as a stored path. Directories are dropped.
239
+ #
240
+ # @param match [String] path from Dir.glob.
241
+ # @param root [String] expanded project root.
242
+ # @param roots [Array<String>] the project root, expanded and real.
243
+ # @return [String, nil] stored path, or nil when the match is not a file.
244
+ def glob_file(match, root, roots)
245
+ full = File.absolute_path?(match) ? match : File.expand_path(match, root)
246
+ return unless File.file?(full)
247
+
248
+ existing_relative(full, roots) || full
249
+ end
250
+
251
+ # Expand each pattern for one map key. An empty expansion records the
252
+ # pattern under `unmatched`. Results are deduped and sorted.
253
+ #
254
+ # @api private
255
+ # @param patterns [Array<String>] source or test patterns from one entry.
256
+ # @param number [Integer] 1-based entry index.
257
+ # @param key [String] `"sources"` or `"tests"`.
258
+ # @param unmatched [Array<Hash>] accumulator for patterns with no file.
259
+ # @yieldparam pattern [String] one pattern to expand.
260
+ # @yieldreturn [Array<String>] matching file paths.
261
+ # @return [Array<String>] cleaned, deduped, sorted paths.
262
+ def expand_map_patterns(patterns, number, key, unmatched)
263
+ patterns.flat_map do |pattern|
264
+ files = yield(pattern)
265
+ unmatched << { entry: number, key: key, pattern: pattern } if files.empty?
266
+ files
267
+ end.uniq.sort_by(&:b)
268
+ end
269
+
270
+ # One map source pattern: Dir.glob then {expand_sources}, so a matched
271
+ # directory becomes its `.rb` files and path spellings clean the same way.
272
+ # Matches outside the project root are dropped (brace patterns like
273
+ # `{..,app}/x.rb` can escape a text-only `..` check).
274
+ #
275
+ # @api private
276
+ # @param pattern [String] relative source pattern.
277
+ # @param root [String] expanded project root.
278
+ # @param roots [Array<String>] the project root, expanded and real.
279
+ # @return [Array<String>] matching source paths, sorted.
280
+ def expand_map_source_pattern(pattern, root, roots)
281
+ # An existing file or directory is literal, even when its name holds a
282
+ # glob character (same rule as {classify_test_value} for --test).
283
+ abs = File.expand_path(pattern, root)
284
+ listed = if File.exist?(abs)
285
+ [pattern]
286
+ else
287
+ fs_glob(pattern, base: root)
288
+ end
289
+ return [] if listed.empty?
290
+
291
+ expand_sources(listed, project_root: root).filter_map do |path|
292
+ path_abs = File.expand_path(path, root)
293
+ next unless File.exist?(path_abs)
294
+
295
+ existing_relative(path_abs, roots)
296
+ end.uniq.sort_by(&:b)
297
+ end
298
+
299
+ # One map test pattern. Directories (typed or matched by a glob) expand
300
+ # with the {TEST_NAME} filter, matching a typed `--test` directory. Files
301
+ # a glob matches stay unfiltered (including `test_helper.rb`), matching
302
+ # `--test` globs. A missing plain path is not a match. Matches outside the
303
+ # project root are dropped (#382 review).
304
+ #
305
+ # @api private
306
+ # @param pattern [String] relative test pattern.
307
+ # @param root [String] expanded project root.
308
+ # @param roots [Array<String>] the project root, expanded and real.
309
+ # @return [Array<String>] matching test paths, sorted.
310
+ def expand_map_test_pattern(pattern, root, roots)
311
+ listed = case classify_test_value(pattern, project_root: root)
312
+ when :directory
313
+ directory_test_files(File.expand_path(pattern, root), root, roots)
314
+ when :glob
315
+ expand_map_test_glob(pattern, root, roots)
316
+ else
317
+ # :file / :plain / :blank — same as {expand_test}, then root-filter.
318
+ expand_test(pattern, project_root: root)
319
+ end
320
+ listed.filter_map do |path|
321
+ abs = File.expand_path(path, root)
322
+ next unless File.file?(abs)
323
+
324
+ existing_relative(abs, roots)
325
+ end.uniq.sort_by(&:b)
326
+ end
327
+
328
+ # Glob matches for a map `tests:` pattern. A matched directory expands with
329
+ # {directory_test_files}; a matched file is kept as-is (no name filter).
330
+ # `--test` globs still drop directories; only the map expands them (#382).
331
+ #
332
+ # @api private
333
+ # @param pattern [String] relative or absolute glob.
334
+ # @param root [String] expanded project root.
335
+ # @param roots [Array<String>] the project root, expanded and real.
336
+ # @return [Array<String>] matching test paths (may include outside-root paths).
337
+ def expand_map_test_glob(pattern, root, roots)
338
+ matches = if File.absolute_path?(pattern)
339
+ fs_glob(pattern)
340
+ else
341
+ fs_glob(pattern, base: root)
342
+ end
343
+ matches.flat_map do |match|
344
+ full = File.absolute_path?(match) ? match : File.expand_path(match, root)
345
+ if File.directory?(full)
346
+ directory_test_files(full, root, roots)
347
+ elsif File.file?(full)
348
+ [existing_relative(full, roots) || full]
349
+ else
350
+ []
351
+ end
352
+ end
49
353
  end
50
354
 
51
355
  # An existing file's root-relative path, or nil outside the root. Tries the
@@ -70,9 +374,14 @@ module Mutineer
70
374
  # @param roots [Array<String>] the project root, expanded and real.
71
375
  # @return [String, nil] root-relative path, or nil outside the root.
72
376
  def root_relative(abs, roots)
377
+ abs = FsPath.read(abs)
73
378
  roots.each do |r|
74
- prefix = r.end_with?("/") ? r : "#{r}/"
75
- return abs.delete_prefix(prefix) if abs.start_with?(prefix)
379
+ r = FsPath.read(r)
380
+ prefix = r.b
381
+ prefix += "/".b unless prefix.end_with?("/".b)
382
+ next unless FsPath.bytes_start_with?(abs, prefix)
383
+
384
+ return FsPath.bytes_delete_prefix(abs, prefix)
76
385
  end
77
386
  nil
78
387
  end
@@ -94,7 +403,11 @@ module Mutineer
94
403
  # candidate from {#candidates} stays first. Each `<basename>_*_test.rb` in
95
404
  # the mirrored test directory is added after it (#87). A spec that the exact
96
405
  # rules already found is left alone: split Minitest names do not join it
97
- # and do not replace it.
406
+ # and do not replace it. When that list is empty, a concern tries the same
407
+ # exact and split names at its namespace path (#408). The `concerns/` path
408
+ # still wins when it has any test. A longer source, including a longer
409
+ # concern, keeps a split file. A model keeps a split file it already pairs
410
+ # with: this fallback does not take one away.
98
411
  #
99
412
  # @param source_rel [String] relative source path.
100
413
  # @param project_root [String] repository root for existence checks.
@@ -103,12 +416,62 @@ module Mutineer
103
416
  def infer_tests(source_rel, project_root:, prefer: "minitest")
104
417
  base, lib = logical_path(source_rel)
105
418
  root = File.expand_path(project_root)
419
+ found = tests_at(base, lib, root, prefer)
420
+ return found unless found.empty?
421
+
422
+ ns = namespace_base(source_rel)
423
+ return found unless ns
424
+
425
+ namespace_tests(ns, root, prefer)
426
+ end
427
+
428
+ # Exact and split tests for one logical base. A spec stays the only file.
429
+ #
430
+ # @param base [String] logical source path without extension.
431
+ # @param lib [Boolean] whether the source originated from lib/.
432
+ # @param root [String] expanded project root.
433
+ # @param prefer [String] preferred framework name.
434
+ # @return [Array<String>] existing test paths, exact candidate first.
435
+ def tests_at(base, lib, root, prefer)
106
436
  exact = candidates(base, lib, prefer).find { |rel| File.exist?(File.expand_path(rel, root)) }
107
437
  return [exact] if exact&.end_with?("_spec.rb")
108
438
 
109
439
  ([exact] + split_test_files(base, lib, root)).compact.uniq
110
440
  end
111
441
 
442
+ # Namespace exact and split tests for a concern whose `concerns/` path
443
+ # has none. A split is left out only when a longer concern has no test
444
+ # of its own and therefore pairs with that split. A longer model is
445
+ # already left out by {split_test_files}. A spec stays the only file.
446
+ #
447
+ # @param ns [String] namespace base from {namespace_base}.
448
+ # @param root [String] expanded project root.
449
+ # @param prefer [String] preferred framework name.
450
+ # @return [Array<String>] existing test paths, exact candidate first.
451
+ def namespace_tests(ns, root, prefer)
452
+ found = tests_at(ns, false, root, prefer)
453
+ return found if found.any? { |rel| rel.end_with?("_spec.rb") }
454
+
455
+ name = File.basename(ns)
456
+ found.reject do |rel|
457
+ entry = File.basename(rel)
458
+ split_entry?(entry, name) && claimed_by_longer_concern?(root, ns, entry)
459
+ end
460
+ end
461
+
462
+ # Namespace mirror for a concern, or nil.
463
+ # `app/<dir>/concerns/<rest>.rb` returns `<dir>/<rest>`.
464
+ # `concerns` must sit directly under `app/<dir>`.
465
+ #
466
+ # @param source_rel [String] relative source path.
467
+ # @return [String, nil] logical base without extension, or nil.
468
+ def namespace_base(source_rel)
469
+ match = source_rel.match(%r{\Aapp/([^/]+)/concerns/(.+)\.rb\z})
470
+ return unless match
471
+
472
+ "#{match[1]}/#{match[2]}"
473
+ end
474
+
112
475
  # Strip the source root to a logical path (no ".rb") and flag lib/
113
476
  # sources. app/foo/bar.rb and lib/foo/bar.rb both -> "foo/bar"; anything
114
477
  # else -> the path minus ".rb" (still attempted). Namespaced subdirs are
@@ -246,13 +609,15 @@ module Mutineer
246
609
  return [] unless inside_project?(dir_abs, root_abs)
247
610
  return [] unless File.directory?(dir_abs)
248
611
 
249
- Dir.children(dir_abs).filter_map do |entry|
612
+ fs_children(dir_abs).filter_map do |entry|
250
613
  next unless split_entry?(entry, name)
251
- next unless File.file?(File.join(dir_abs, entry))
614
+ next unless File.file?(dir_abs.b + "/".b + entry.b)
252
615
  next if claimed_by_longer_source?(root, base, entry, dir_rel)
253
616
 
254
- File.join(dir_rel, entry)
255
- end.sort
617
+ FsPath.read(dir_rel == "." ? entry.b : dir_rel.b + "/".b + entry.b)
618
+ # Byte order: a UTF-8 name and a binary name can both hold bytes
619
+ # above 127, and String#sort raises on that mix (#346).
620
+ end.sort_by(&:b)
256
621
  end
257
622
 
258
623
  # True when `dir_abs` is the project root or a directory inside it.
@@ -262,8 +627,12 @@ module Mutineer
262
627
  # @param root_abs [String] expanded project root.
263
628
  # @return [Boolean]
264
629
  def inside_project?(dir_abs, root_abs)
265
- prefix = root_abs == File::SEPARATOR ? File::SEPARATOR : "#{root_abs}#{File::SEPARATOR}"
266
- dir_abs == root_abs || dir_abs.start_with?(prefix)
630
+ dir_b = FsPath.read(dir_abs).b
631
+ root_b = FsPath.read(root_abs).b
632
+ return true if dir_b == root_b
633
+
634
+ prefix = root_b == "/".b ? "/".b : root_b + "/".b
635
+ FsPath.bytes_start_with?(dir_b, prefix)
267
636
  end
268
637
 
269
638
  # True when `entry` is `<name>_<piece>_test.rb` and not the exact
@@ -273,10 +642,16 @@ module Mutineer
273
642
  # @param name [String] source basename without extension.
274
643
  # @return [Boolean]
275
644
  def split_entry?(entry, name)
276
- return false unless entry.end_with?("_test.rb")
645
+ entry = FsPath.read(entry)
646
+ name = FsPath.read(name)
647
+ return false unless entry.b.end_with?("_test.rb".b)
648
+
649
+ needle = name.b + "_".b
650
+ return false unless entry.b.start_with?(needle)
277
651
 
278
- rest = entry.delete_prefix("#{name}_")
279
- rest != entry && rest.match?(/\A.+_test\.rb\z/)
652
+ rest = entry.b.byteslice(needle.bytesize..)
653
+ suffix = "_test.rb".b
654
+ !rest.nil? && rest.bytesize > suffix.bytesize && rest.end_with?(suffix)
280
655
  end
281
656
 
282
657
  # True when a longer source file on disk owns this split test. `user.rb`
@@ -293,20 +668,77 @@ module Mutineer
293
668
  # this directory. An `app/` source does not take a `test/lib/` file.
294
669
  # @return [Boolean]
295
670
  def claimed_by_longer_source?(root, base, entry, scanned_dir = nil)
296
- name = File.basename(base)
297
- stem = entry.sub(/_test\.rb\z/, "")
298
- return false unless stem.start_with?("#{name}_")
671
+ dir = File.dirname(base)
672
+ longer_owner_stems(File.basename(base), entry).any? do |longer|
673
+ longer_source_exists?(root, dir, longer, scanned_dir)
674
+ end
675
+ end
299
676
 
300
- rest = stem.delete_prefix("#{name}_")
301
- return false if rest.empty?
677
+ # True when a longer concern pairs with this split name.
678
+ # The file sits under `app/<dir>/concerns/`, a directory
679
+ # {longer_source_exists?} does not search for a namespace base.
680
+ # The longer concern claims the name only when its `concerns/` path has
681
+ # no test, because only then does it look at the namespace path.
682
+ # A model keeps a split file it already pairs with (#408).
683
+ #
684
+ # @param root [String] expanded project root.
685
+ # @param ns_base [String] namespace base, such as `models/storage/totaled`.
686
+ # @param entry [String] test file basename.
687
+ # @return [Boolean]
688
+ def claimed_by_longer_concern?(root, ns_base, entry)
689
+ dir = namespace_concerns_dir(ns_base)
690
+ return false unless dir
302
691
 
303
- dir = File.dirname(base)
304
- parts = rest.split("_", -1)
305
- (1..parts.length).any? do |i|
306
- longer = "#{name}_#{parts.first(i).join("_")}"
307
- next false unless longer_owns_entry?(entry, longer)
692
+ longer_owner_stems(File.basename(ns_base), entry).any? do |longer|
693
+ rel = File.join(dir, "#{longer}.rb")
694
+ abs = File.expand_path(rel, root)
695
+ # A source path with `..` must not make this look at a file outside the project.
696
+ next false unless inside_project?(abs, root) && File.file?(abs)
308
697
 
309
- longer_source_exists?(root, dir, longer, scanned_dir)
698
+ base, lib = logical_path(rel)
699
+ tests_at(base, lib, root, "minitest").empty?
700
+ end
701
+ end
702
+
703
+ # `app/<dir>/concerns/<rest-dir>` for a namespace base `<dir>/<rest>`.
704
+ #
705
+ # @param ns_base [String] namespace base from {namespace_base}.
706
+ # @return [String, nil] relative concerns directory, or nil when the base
707
+ # has no rest path.
708
+ def namespace_concerns_dir(ns_base)
709
+ app_dir, rest = ns_base.split("/", 2)
710
+ return unless app_dir && rest
711
+
712
+ parent = File.dirname(rest)
713
+ if parent == "."
714
+ File.join("app", app_dir, "concerns")
715
+ else
716
+ File.join("app", app_dir, "concerns", parent)
717
+ end
718
+ end
719
+
720
+ # Longer basenames that own `entry`, one per prefix of its pieces.
721
+ # `user_session__test.rb` yields nothing for `user_session`.
722
+ #
723
+ # @param name [String] shorter source basename without extension.
724
+ # @param entry [String] test file basename.
725
+ # @return [Array<String>] owner basenames without extension.
726
+ def longer_owner_stems(name, entry)
727
+ # Bytes, so a non-UTF-8 name compares without raising (#346).
728
+ name = name.b
729
+ raw = entry.b
730
+ suffix = "_test.rb".b
731
+ stem = raw.end_with?(suffix) ? raw.byteslice(0, raw.bytesize - suffix.bytesize) : raw
732
+ needle = name + "_".b
733
+ return [] unless stem.start_with?(needle)
734
+
735
+ rest = stem.byteslice(needle.bytesize..)
736
+ return [] if rest.nil? || rest.empty?
737
+
738
+ parts = rest.split("_".b, -1)
739
+ (1..parts.length).filter_map do |i|
740
+ longer = name + "_".b + parts.first(i).join("_".b)
741
+ FsPath.read(longer) if longer_owns_entry?(entry, longer)
310
742
  end
311
743
  end
312
744
 
@@ -318,7 +750,7 @@ module Mutineer
318
750
  # @param stem [String] longer source basename without extension.
319
751
  # @return [Boolean]
320
752
  def longer_owns_entry?(entry, stem)
321
- entry == "#{stem}_test.rb" || split_entry?(entry, stem)
753
+ entry.b == stem.b + "_test.rb".b || split_entry?(entry, stem)
322
754
  end
323
755
 
324
756
  # True when a longer source file exists beside this logical path.
@@ -332,12 +764,12 @@ module Mutineer
332
764
  # @param scanned_dir [String, nil] relative test directory being scanned.
333
765
  # @return [Boolean]
334
766
  def longer_source_exists?(root, dir, stem, scanned_dir = nil)
335
- file = "#{stem}.rb"
767
+ file = stem.b + ".rb".b
336
768
  folders = [dir == "." ? nil : dir]
337
769
  %w[app lib].each { |prefix| folders << (dir == "." ? prefix : File.join(prefix, dir)) }
338
770
  folders.uniq.any? do |folder|
339
- rel = folder ? File.join(folder, file) : file
340
- next false unless File.file?(File.join(root, rel))
771
+ rel = FsPath.read(folder ? folder.b + "/".b + file : file)
772
+ next false unless File.file?(root.b + "/".b + rel.b)
341
773
  next false if scanned_dir && !source_searches_dir?(rel, scanned_dir)
342
774
 
343
775
  true
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "prism"
4
+ require_relative "source_bytes"
4
5
 
5
6
  module Mutineer
6
7
  # Raised only for I/O failures while reading a source file.
@@ -25,6 +26,19 @@ module Mutineer
25
26
  raise ParseError, e.message
26
27
  end
27
28
 
29
+ # Reads a Ruby source file in the encoding its magic comment names, or
30
+ # UTF-8 when it names none (#346). The bytes are not transcoded, so
31
+ # Prism's offsets still address them and a mutant written back stays
32
+ # valid for that encoding. An unknown encoding name stays UTF-8, as
33
+ # Ruby does. I/O errors propagate (`Errno::ENOENT` and the rest).
34
+ #
35
+ # @param path [String] source file path.
36
+ # @return [String] source text, tagged with its encoding.
37
+ def self.read(path)
38
+ bytes = File.binread(path)
39
+ bytes.force_encoding(SourceBytes.encoding_for(bytes))
40
+ end
41
+
28
42
  # Parses source text with Prism.
29
43
  #
30
44
  # @param source [String] source text.
@@ -33,6 +47,45 @@ module Mutineer
33
47
  Prism.parse(source)
34
48
  end
35
49
 
50
+ # True when Ruby cannot load `source`: the loaded Prism reports a parse
51
+ # error and the running Ruby cannot compile the text either. Prism alone is
52
+ # not enough. Prism 2 reports a bare-constant operator write inside a
53
+ # method (`T += 1`) as a dynamic constant assignment, while the Prism 1
54
+ # that Ruby 3.4 and 4.0 load files with accepts it (#392). Compiling does
55
+ # not run the code.
56
+ #
57
+ # @param source [String] source text.
58
+ # @return [Boolean]
59
+ def self.invalid?(source)
60
+ return false if parse_string(source).errors.empty? # the usual case costs one parse
61
+
62
+ !compiles?(source)
63
+ end
64
+
65
+ # Serializes {.compiles?}: it swaps the process-wide `$VERBOSE`, and
66
+ # `--daemon` checks mutants on several threads.
67
+ COMPILE_LOCK = Mutex.new
68
+
69
+ # Whether the running Ruby compiles `source`, without running it. Mutineer
70
+ # forks, so it runs on CRuby, which has RubyVM. Warnings are off for the
71
+ # compile: a mutant can trigger one (`if a = 1`).
72
+ #
73
+ # @param source [String] source text.
74
+ # @return [Boolean]
75
+ def self.compiles?(source)
76
+ COMPILE_LOCK.synchronize do
77
+ verbose = $VERBOSE
78
+ $VERBOSE = nil
79
+ RubyVM::InstructionSequence.compile(source)
80
+ true
81
+ rescue SyntaxError
82
+ false
83
+ ensure
84
+ $VERBOSE = verbose
85
+ end
86
+ end
87
+ private_class_method :compiles?
88
+
36
89
  # The comments of source text, without building its syntax tree: a few
37
90
  # times cheaper than {.parse_string} for a caller that needs only these.
38
91
  #