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
@@ -7,6 +7,7 @@ require_relative "daemon_client"
7
7
  require_relative "progress"
8
8
  require_relative "job_plan"
9
9
  require_relative "orphan_guard"
10
+ require_relative "reach_probe"
10
11
 
11
12
  module Mutineer
12
13
  # Daemon execution backend. Boots the app ONCE in a persistent subprocess under
@@ -53,7 +54,8 @@ module Mutineer
53
54
  # source map, and the {JobPlan.collect_jobs} extras.
54
55
  def self.execute(config, operator_classes)
55
56
  jobs, ignored_results, source_map, extras = JobPlan.collect_jobs(config, operator_classes)
56
- jobs, ignored_results, extras[:unscoped_mutants] = JobPlan.scope_since(jobs, ignored_results, source_map, config)
57
+ jobs, ignored_results, extras[:unscoped_mutants] =
58
+ JobPlan.scope_since(jobs, ignored_results, source_map, config, extras: extras)
57
59
  abs_tests = config.tests.map { |t| File.expand_path(t, config.project_root) }
58
60
 
59
61
  # Nothing to mutate (`--since` matched no changed line, or every mutant is
@@ -251,7 +253,9 @@ module Mutineer
251
253
  map: data["map"] || {},
252
254
  failed_test_files: data["failed_test_files"] || [],
253
255
  project_root: config.project_root,
254
- failed_clean_tests: data["failed_clean_tests"]
256
+ failed_clean_tests: data["failed_clean_tests"],
257
+ failed_clean_details: data["failed_clean_details"] || {},
258
+ clean_timeouts: data["clean_timeouts"] || {}
255
259
  ), framework: config.framework)
256
260
  end
257
261
 
@@ -433,20 +437,38 @@ module Mutineer
433
437
  # A :maybe mutant that cannot be placed stays no_coverage, as in-process.
434
438
  sel && sel[0] == :maybe ? Result.no_coverage : Result.unplaceable
435
439
  else
436
- payload = { "code" => mutated, "source_file" => File.expand_path(subject.file, config.project_root) }
437
- if subject.reload_hook_call
438
- # #297: the daemon child places the hook-block def, as in-process (Runner.run).
439
- payload["code"], hook_def = JobPlan.hook_mutant(mutated, mutation, subject, source)
440
- payload["hook_def"] = hook_def.to_h { |key, value| [key.to_s, key == :name ? value.to_s : value] }
440
+ # Same probe as in-process (#345): the daemon child loads this text.
441
+ sentinel = sel && sel[0] == :maybe ? ReachProbe.sentinel : nil
442
+ reach_key = sentinel && ReachProbe.reach_key
443
+ begin
444
+ payload = { "code" => mutated, "source_file" => File.expand_path(subject.file, config.project_root) }
445
+ if subject.reload_hook_call
446
+ # #297: the daemon child places the hook-block def, as in-process (Runner.run).
447
+ payload["code"], hook_def = JobPlan.hook_mutant(mutated, mutation, subject, source,
448
+ reach_path: sentinel&.path, reach_key: reach_key)
449
+ payload["hook_def"] = hook_def.to_h { |key, value| [key.to_s, key == :name ? value.to_s : value] }
450
+ elsif sentinel
451
+ payload["code"] = ReachProbe.wrap_mutation(mutated, mutation, sentinel.path, reach_key)
452
+ end
453
+ # The daemon clears these after load and before tests (#345).
454
+ if sentinel
455
+ payload["reach_path"] = sentinel.path
456
+ payload["reach_key"] = reach_key
457
+ end
458
+ verdict = client.request(
459
+ id: req_id, worker: worker, timeout: config.daemon_timeout || DEFAULT_TIMEOUT,
460
+ payload: payload, tests: sel ? sel[1] : abs_tests
461
+ )
462
+ # A survivor whose line ran at load, as in-process (Runner.run).
463
+ details = client.last_cause if verdict == "error"
464
+ loaded = JobPlan.load_verdict(result_for(verdict, details), subject.file, mutation, subject, source,
465
+ coverage_map)
466
+ # A :maybe survivor the probe saw run counts; one it did not stays
467
+ # no_coverage, as in-process (#254, #345).
468
+ JobPlan.selection_verdict(sel && sel[0], loaded, reached: ReachProbe.reached?(sentinel))
469
+ ensure
470
+ ReachProbe.close_sentinel(sentinel)
441
471
  end
442
- verdict = client.request(
443
- id: req_id, worker: worker, timeout: config.daemon_timeout || DEFAULT_TIMEOUT,
444
- payload: payload, tests: sel ? sel[1] : abs_tests
445
- )
446
- # A survivor whose line ran at load, as in-process (Runner.run).
447
- loaded = JobPlan.load_verdict(result_for(verdict), subject.file, mutation, subject, source, coverage_map)
448
- # Tests that may not have run the mutant, as in-process (#254).
449
- JobPlan.selection_verdict(sel && sel[0], loaded)
450
472
  end
451
473
  r.with(subject: subject, mutation: mutation, id: id)
452
474
  end
@@ -499,14 +521,15 @@ module Mutineer
499
521
  # resolved tool-side before a request is ever sent.
500
522
  #
501
523
  # @param verdict [String] the daemon's verdict word.
524
+ # @param details [String, nil] the cause the daemon sent with an `"error"`.
502
525
  # @api private
503
526
  # @return [Mutineer::Result] the matching result.
504
- def self.result_for(verdict)
527
+ def self.result_for(verdict, details = nil)
505
528
  case verdict
506
529
  when "survived" then Result.survived
507
530
  when "killed" then Result.killed
508
531
  when "timeout" then Result.timeout
509
- else Result.error("daemon verdict: #{verdict}")
532
+ else Result.error(details || "daemon verdict: #{verdict}")
510
533
  end
511
534
  end
512
535
 
@@ -87,6 +87,17 @@ module Mutineer
87
87
  # @return [Hash, nil]
88
88
  attr_reader :databases
89
89
 
90
+ # Why the last {#request} answered `"error"`: the redacted `Class: message`
91
+ # line from the daemon's child (#388), or `daemon crashed` / `daemon
92
+ # stopped answering` when no reply came. nil when the reply carried no
93
+ # cause, or the last request answered anything else. Read it right after
94
+ # {#request}: a client serves one worker thread at a time
95
+ # ({DaemonBackend} gives each thread its own), so no other request can
96
+ # replace it in between.
97
+ #
98
+ # @return [String, nil]
99
+ attr_reader :last_cause
100
+
90
101
  # Ask the daemon to create `slots` worker databases. Raises when the copy
91
102
  # fails or another run holds the database, so the caller stops before any
92
103
  # mutant is scored.
@@ -115,8 +126,12 @@ module Mutineer
115
126
  # @param timeout [Numeric] per-mutant wall-clock timeout (seconds).
116
127
  # @param worker [Integer] worker slot; the daemon routes the fork to
117
128
  # `<db>-<worker>` for isolation. Defaults to 0 (serial).
118
- # @return [String] one of survived/killed/error/timeout.
129
+ # @return [String] one of survived/killed/error/timeout. An `"error"`
130
+ # reply's cause is in {#last_cause}.
119
131
  def request(id:, payload:, tests:, timeout:, worker: 0)
132
+ @last_cause = nil
133
+ # A write that fails reads no reply, so a stale flag must not name it a hang.
134
+ @timed_out = false
120
135
  # close_io nils the pipes, so a client whose respawn never completed would
121
136
  # otherwise fail per-mutant forever (NoMethodError on nil) and let the backend
122
137
  # score every remaining mutant against nothing. Deadness is a property of the
@@ -134,8 +149,12 @@ module Mutineer
134
149
  rescue Errno::EPIPE, IOError
135
150
  nil
136
151
  end
137
- return reply["verdict"] if reply && reply["id"] == id
152
+ if reply && reply["id"] == id
153
+ @last_cause = reply["details"] if reply["verdict"] == "error"
154
+ return reply["verdict"]
155
+ end
138
156
 
157
+ @last_cause = @timed_out ? "daemon stopped answering" : "daemon crashed"
139
158
  restart!
140
159
  "error"
141
160
  end
@@ -353,9 +372,12 @@ module Mutineer
353
372
  "-r", DAEMON_PATH, "-e", "Mutineer::DaemonServer.run",
354
373
  unsetenv_others: true, chdir: @app_root
355
374
  )
356
- # Replies are UTF-8 JSON: with LANG unset the pipe would read as
357
- # US-ASCII (#303). The stderr drain reads bytes and marks each line
358
- # UTF-8 itself (write_redacted).
375
+ # Requests and replies are UTF-8 JSON, so both pipes are tagged
376
+ # UTF-8 and do not depend on LANG (#303, #346). `JSON.generate`
377
+ # already returns UTF-8; the stdin tag keeps a later write of a
378
+ # UTF-8 string from being converted. The stderr drain reads bytes
379
+ # and marks each line UTF-8 itself (write_redacted).
380
+ @stdin.set_encoding(Encoding::UTF_8)
359
381
  @stdout.set_encoding(Encoding::UTF_8)
360
382
  # Drain daemon stderr to the tool's stderr so child/boot errors are visible,
361
383
  # redacted line by line (#288: the app's output goes through here too).
@@ -454,29 +476,55 @@ module Mutineer
454
476
  next
455
477
  end
456
478
  pending << chunk.b
457
- while (cut = pending.index("\n"))
458
- line = pending.slice!(0..cut)
459
- writable &&= write_redacted(line)
460
- end
479
+ # Nothing new can be emitted before a newline, so a long line is not
480
+ # rescanned on every read.
481
+ next unless chunk.include?("\n")
482
+
483
+ ready, pending = Redaction.take_redactable(pending, flush: false)
484
+ writable &&= write_redacted(ready) if writable && !ready.empty?
485
+ end
486
+ if writable && !pending.empty?
487
+ ready, = Redaction.take_redactable(pending, flush: true)
488
+ write_redacted(ready) unless ready.empty?
461
489
  end
462
- write_redacted(pending) if writable && !pending.empty?
463
490
  nil
464
491
  rescue StandardError
465
492
  nil
466
493
  end
467
494
 
468
- # Write one line of daemon stderr to the tool's stderr, redacted.
495
+ # Write daemon stderr to the tool's stderr, redacted. One line at a time,
496
+ # unless a password form itself contains a newline: then the chunk is one
497
+ # redaction so the form is not split. A failed redaction still keeps each
498
+ # ordinary line's break.
469
499
  #
470
- # @param line [String] raw bytes, with or without the trailing newline.
500
+ # @param chunk [String] raw bytes, with or without a trailing newline.
471
501
  # @return [Boolean] false when the write failed.
472
- def write_redacted(line)
473
- body = line.chomp("\n").force_encoding(Encoding::UTF_8).scrub # an invalid byte must not stop the drain
474
- @errio.write(Redaction.text(body), line.end_with?("\n") ? "\n" : "")
502
+ def write_redacted(chunk)
503
+ return true if chunk.nil? || chunk.empty?
504
+
505
+ if chunk.include?("\n") && Redaction.newline_forms.any?
506
+ write_redacted_piece(chunk)
507
+ else
508
+ chunk.each_line { |line| write_redacted_piece(line) }
509
+ end
475
510
  true
476
511
  rescue StandardError
477
512
  false
478
513
  end
479
514
 
515
+ # Write one piece of daemon stderr, redacted. The trailing newline is kept
516
+ # outside the replacement.
517
+ #
518
+ # @param line [String]
519
+ # @return [void]
520
+ def write_redacted_piece(line)
521
+ newline = line.end_with?("\n")
522
+ body = (newline ? line.chomp("\n") : line).dup.force_encoding(Encoding::UTF_8)
523
+ body.scrub! unless body.valid_encoding? # an invalid byte must not stop the drain
524
+ @errio.write(Redaction.text(body))
525
+ @errio.write("\n") if newline
526
+ end
527
+
480
528
  # Close the IPC pipes, stop the stderr-drain thread, and reap the daemon so a
481
529
  # respawn or quit leaves no leaked fd, thread, or zombie.
482
530
  #
@@ -7,6 +7,10 @@ require_relative "child_coverage"
7
7
  require_relative "orphan_guard"
8
8
  require_relative "concern_blocks"
9
9
  require_relative "redaction"
10
+ require_relative "database_urls"
11
+ require_relative "output_filter"
12
+ require_relative "child_cause"
13
+ require_relative "source_bytes"
10
14
 
11
15
  module Mutineer
12
16
  # App-side daemon (persistent worker).
@@ -35,9 +39,16 @@ module Mutineer
35
39
  # provision: {"cmd":"provision","slots":N} -> {"ok":true} or {"ok":false,"error":"..."}
36
40
  # run in : {"id":N,"worker":I,"payload":{"code":"<ruby>","source_file":"app/models/order.rb"},
37
41
  # "tests":["test/models/order_test.rb"],"timeout":30}
42
+ # (a :maybe mutant also sends "reach_path" and "reach_key" inside
43
+ # "payload". The child clears that file, and the fiber-local
44
+ # named by reach_key, after load and before tests, so a
45
+ # load-time call is not a test reaching the expression, #345)
38
46
  # (a mutant of a def in a concern's hook block adds to the payload
39
47
  # "hook_def":{"line":12,"name":"total","singleton":false}, #297)
40
48
  # verdict : {"id":N,"verdict":"survived"|"killed"|"error"|"timeout"}
49
+ # (an "error" adds "details" when the child sent a cause line:
50
+ # a raise → "Class: message" (#388); an in-test database error →
51
+ # "a test failed with a database error: Class: message" (#417))
41
52
  # quit in : {"cmd":"quit"}
42
53
  #
43
54
  # Worker isolation: when the app is Rails, each fork is routed to its own
@@ -85,7 +96,9 @@ module Mutineer
85
96
  @errio = errio
86
97
  @output = output
87
98
  # The client sends UTF-8 JSON; with LANG unset the pipe would read as US-ASCII (#303).
99
+ # Replies are UTF-8 too, including a non-ASCII path (#346).
88
100
  input.set_encoding(Encoding::UTF_8)
101
+ output.set_encoding(Encoding::UTF_8)
89
102
  boot_line = input.gets
90
103
  return if boot_line.nil? # client vanished before boot
91
104
 
@@ -153,6 +166,19 @@ module Mutineer
153
166
  @framework = cfg.fetch("framework", "minitest")
154
167
  @source_dirs = Array(cfg["source_dirs"]).map { |d| File.expand_path(d) }
155
168
  Dir.chdir(cfg["project_root"]) if cfg["project_root"]
169
+ # The parent's drain knows the tool's DATABASE_URL. A URL the app
170
+ # loads from a dotenv file, or assigns before it prints, is hidden
171
+ # here, where this process can see it (#341). Stderr only: stdout is
172
+ # the protocol channel reserved above. Point fd 1 at the filtered
173
+ # stderr so `puts` from the app takes the same path.
174
+ Redaction.extra_urls = DatabaseUrls.from_files(Dir.pwd)
175
+ unless OutputFilter.install(streams: [:stderr]) == :started
176
+ raise "cannot redirect stderr, so a database password would be printed"
177
+ end
178
+ if @protocol
179
+ STDOUT.reopen(STDERR)
180
+ $stdout = STDOUT
181
+ end
156
182
  ENV["RAILS_ENV"] ||= "test" if cfg["rails"]
157
183
  Array(cfg["load_paths"]).each { |d| $LOAD_PATH.unshift(File.expand_path(d)) }
158
184
  # Start Coverage BEFORE the app loads, so booted source lines are
@@ -181,6 +207,8 @@ module Mutineer
181
207
  # can surface a clean error rather than hang on the handshake.
182
208
  @output.puts(JSON.generate("ready" => false, "error" => describe_error(e)))
183
209
  @output.flush
210
+ # exit! skips at_exit, so flush the stderr pipe before it.
211
+ OutputFilter.uninstall
184
212
  exit!(1)
185
213
  end
186
214
 
@@ -192,7 +220,8 @@ module Mutineer
192
220
  # `db_role` "prepare" waits for a provision command (the first daemon).
193
221
  # "worker" only takes the shared lock (a worker daemon, including a
194
222
  # respawn). Anything else provisions now, which is what a direct client
195
- # that never sends the command needs. SQLite provisioning does nothing.
223
+ # that never sends the command needs. SQLite takes the run lock and
224
+ # copies files on first use.
196
225
  def setup_worker_db(cfg)
197
226
  require_relative "rails_worker_db"
198
227
  @worker_db = RailsWorkerDb.available? ? RailsWorkerDb : nil
@@ -289,7 +318,10 @@ module Mutineer
289
318
  # daemon does not pay for it (#228).
290
319
  Coverage.suspend if Coverage.running?
291
320
  { "map" => cmap.map, "failed_test_files" => cmap.failed_test_files,
292
- "failed_clean_tests" => cmap.failed_clean_tests, "load_lines" => cmap.load_lines.to_a,
321
+ "failed_clean_tests" => cmap.failed_clean_tests,
322
+ "failed_clean_details" => cmap.failed_clean_details,
323
+ "clean_timeouts" => cmap.clean_timeouts,
324
+ "load_lines" => cmap.load_lines.to_a,
293
325
  "load_methods" => cmap.load_methods.to_a }
294
326
  rescue Exception => e # rubocop:disable Lint/RescueException
295
327
  error = describe_error(e)
@@ -344,7 +376,12 @@ module Mutineer
344
376
  # Seed the slot's DB until the first killed/survived fork for this worker slot.
345
377
  first_use = @worker_db && !@slot_ready[worker]
346
378
  daemon = Process.pid
379
+ cause_rd, cause_wr = IO.pipe
380
+ cause_wr.sync = true
347
381
  pid = fork do
382
+ child = Process.pid
383
+ cause_rd.close
384
+ ChildCause.arm!(cause_wr, child)
348
385
  # New process group so a per-fork timeout can SIGKILL the whole subtree,
349
386
  # and silence the child's stdout so test output stays out of the diagnostics.
350
387
  Process.setpgid(0, 0) rescue nil # rubocop:disable Style/RescueModifier
@@ -358,6 +395,11 @@ module Mutineer
358
395
  # A routing failure raises here and is scored `error`, never a false verdict.
359
396
  @worker_db&.after_fork(worker, first_use ? @schema_path : nil, seed: first_use)
360
397
  apply_payload(req["payload"])
398
+ # A reload can run the probed expression while the file loads.
399
+ # Drop that write before tests, matching the in-process child (#345).
400
+ # A request with no payload (a test stub) has nothing to clear.
401
+ loaded = req["payload"] || {}
402
+ forget_load_reach(loaded["reach_path"], loaded["reach_key"])
361
403
  # A parallel worker records each pool that runs SQL, so a test
362
404
  # that closes its shared connection is still caught below.
363
405
  if @worker_db && @shared_databases&.any?
@@ -369,10 +411,14 @@ module Mutineer
369
411
  result
370
412
  rescue Exception => e # rubocop:disable Lint/RescueException
371
413
  @errio.puts("[daemon-child] #{describe_error(e)}")
414
+ ChildCause.write(cause_wr, e, child)
372
415
  2
416
+ ensure
417
+ ChildCause.disarm!
373
418
  end
374
419
  exit!(code)
375
420
  end
421
+ cause_wr.close
376
422
  verdict = wait_verdict(pid, timeout)
377
423
  # Mark ready only when the child finished cleanly after seeding
378
424
  # (killed/survived). Timeout can interrupt mid-copy; error can be a
@@ -385,7 +431,13 @@ module Mutineer
385
431
  # A SIGKILLed timeout child skipped its Tempfile unlink. Sweep the orphan
386
432
  # so it cannot outlive the run.
387
433
  sweep_temps if verdict == "timeout"
388
- { "id" => req["id"], "verdict" => verdict }
434
+ reply = { "id" => req["id"], "verdict" => verdict }
435
+ cause = verdict == "error" && ChildCause.read(cause_rd)
436
+ reply["details"] = cause if cause
437
+ reply
438
+ ensure
439
+ cause_rd&.close
440
+ cause_wr.close if cause_wr && !cause_wr.closed?
389
441
  end
390
442
 
391
443
  # Hide kept mutant files from Zeitwerk before the app builds its loaders.
@@ -464,6 +516,21 @@ module Mutineer
464
516
  end
465
517
  end
466
518
 
519
+ # Empties the reach sentinel after the mutant file has loaded. Same rule
520
+ # as {ReachProbe.forget_load}. Written here because this file cannot
521
+ # require Prism.
522
+ #
523
+ # @param path [String, nil] sentinel path, inside the run payload.
524
+ # @param key [String, nil] fiber-local key, `mutineer_r` plus 16 hex
525
+ # digits, the same pattern as {ReachProbe::KEY_FORMAT}.
526
+ # @return [void]
527
+ def forget_load_reach(path, key)
528
+ return unless path.is_a?(String) && !path.empty?
529
+
530
+ Thread.current[key.to_sym] = nil if key.is_a?(String) && key.match?(/\Amutineer_r[0-9a-f]{16}\z/)
531
+ File.write(path, "")
532
+ end
533
+
467
534
  # Write the tool-built mutated text beside the real source and `load` it,
468
535
  # reopening the mutated class/method in THIS child only. It goes in the
469
536
  # source file's directory (like Isolation.apply_whole_file) so a
@@ -487,7 +554,9 @@ module Mutineer
487
554
  # Held until this block ends, including SIGKILL (the kernel drops it).
488
555
  # Another run's sweep sees the lock and does not delete the file.
489
556
  f.flock(File::LOCK_EX) rescue nil # rubocop:disable Style/RescueModifier
490
- f.write(payload.fetch("code"))
557
+ # JSON delivered Unicode. Write the encoding the comment names (#346).
558
+ f.binmode
559
+ f.write(SourceBytes.for_file(payload.fetch("code")))
491
560
  f.flush
492
561
  ConcernBlocks.load_mutant(payload.fetch("source_file"), f.path, hook_def: hook)
493
562
  end
@@ -497,7 +566,8 @@ module Mutineer
497
566
  # 1 = a failure/error (killed), 2 = the first failure was an in-test
498
567
  # database error (error, #296). Minitest only; rspec is not yet on this path.
499
568
  # {MinitestIntegration.run} is the method the daemon's clean check uses,
500
- # so each mutant fork gets the same seed and class list (#282). It loads
569
+ # so each mutant fork gets the same seed and, within each file, the same
570
+ # class order as the clean check (#282). It loads
501
571
  # only stdlib, so the daemon still needs no Prism. The fork requires
502
572
  # nothing else first: the test files' `require "test_helper"` loads
503
573
  # rails/test_help after the class list is reset, as in the clean check.
@@ -0,0 +1,189 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mutineer
4
+ # DATABASE_URL values stored in a project's dotenv files. The tool process
5
+ # does not see a URL the app loads itself during boot. Reading the files
6
+ # lets redaction hide that password (#341). Stdlib only, so the app-side
7
+ # daemon can load this file.
8
+ module DatabaseUrls
9
+ # Files dotenv-rails reads. Later files override at runtime. Every value
10
+ # is kept: hiding a password the running app did not use is safe.
11
+ FILE_NAMES = %w[
12
+ .env
13
+ .env.local
14
+ .env.development
15
+ .env.development.local
16
+ .env.test
17
+ .env.test.local
18
+ ].freeze
19
+ module_function
20
+
21
+ # DATABASE_URL values from the dotenv files under `root`. Missing files
22
+ # add nothing. An unreadable file adds nothing: a permission error must
23
+ # not stop a run. Estimate: a project dotenv file is readable by the user
24
+ # who starts the run.
25
+ #
26
+ # @param root [String, nil] project directory.
27
+ # @return [Array<String>]
28
+ def from_files(root)
29
+ return [] if root.nil? || root.empty?
30
+
31
+ FILE_NAMES.flat_map { |name| from_file(File.join(root, name)) }.uniq
32
+ end
33
+
34
+ # DATABASE_URL values in one file. Every line is read. A double-quoted
35
+ # value may continue on later lines until its closing quote. A quote that
36
+ # never closes is a plain value on its own line, as in dotenv. `${NAME}` and
37
+ # `$NAME` expand from an earlier assignment in the file, or from the
38
+ # environment when that variable is already set. A backslash before `$`
39
+ # is a literal dollar. A single-quoted value is not expanded. An invalid
40
+ # byte is replaced and does not stop the read. A directory, a missing
41
+ # path, and an unreadable file add nothing. An unknown `$NAME` is left as
42
+ # written, so that reference does not become a password.
43
+ #
44
+ # @param path [String]
45
+ # @return [Array<String>]
46
+ def from_file(path)
47
+ return [] unless File.file?(path)
48
+
49
+ vars = {}
50
+ urls = []
51
+ lines = File.open(path, "rb") { |io| io.each_line.map { |line| utf8_line(line) } }
52
+ index = 0
53
+ while index < lines.size
54
+ stop = index
55
+ chunk = lines[index]
56
+ parsed = parse_assignment(chunk)
57
+ while parsed == :open && stop + 1 < lines.size
58
+ stop += 1
59
+ chunk += lines[stop]
60
+ parsed = parse_assignment(chunk)
61
+ end
62
+ if parsed == :open
63
+ # A quote that never closes: dotenv reads that line as a plain
64
+ # value, and the lines after it as their own assignments.
65
+ stop = index
66
+ parsed = parse_assignment(lines[index], plain: true)
67
+ end
68
+ index = stop + 1
69
+ next unless parsed.is_a?(Array)
70
+
71
+ key, raw, expand_value = parsed
72
+ value = expand_value ? expand(raw, vars) : raw
73
+ vars[key] = value
74
+ urls << value if key == "DATABASE_URL" && !value.empty?
75
+ end
76
+ urls
77
+ rescue SystemCallError
78
+ []
79
+ end
80
+
81
+ # The DATABASE_URL value on one dotenv line, or nil when the line is a
82
+ # comment, another key, or an empty value.
83
+ #
84
+ # @param line [String]
85
+ # @return [String, nil]
86
+ def line_url(line)
87
+ parsed = parse_assignment(line)
88
+ return nil unless parsed.is_a?(Array) && parsed[0] == "DATABASE_URL"
89
+
90
+ parsed[1].empty? ? nil : parsed[1]
91
+ end
92
+
93
+ # One `KEY=value` assignment. The third element is false for a
94
+ # single-quoted value, which dotenv does not expand. `:open` means a
95
+ # quoted value has not reached its closing quote yet. `plain` reads a
96
+ # leading quote as part of an unquoted value.
97
+ #
98
+ # @param line [String]
99
+ # @param plain [Boolean]
100
+ # @return [Array(String, String, Boolean), Symbol, nil]
101
+ def parse_assignment(line, plain: false)
102
+ text = utf8_line(line).sub(/\A\uFEFF/, "").strip
103
+ return nil if text.empty? || text.start_with?("#")
104
+
105
+ text = text.sub(/\Aexport\s+/, "")
106
+ match = text.match(/\A([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\z/m)
107
+ return nil unless match
108
+
109
+ key = match[1]
110
+ raw = match[2]
111
+ if !plain && raw.start_with?("'")
112
+ stop = closing_quote(raw, "'")
113
+ return :open unless stop
114
+
115
+ [key, raw[1...stop], false]
116
+ elsif !plain && raw.start_with?('"')
117
+ stop = closing_quote(raw, '"')
118
+ return :open unless stop
119
+
120
+ [key, decode_double(raw[1...stop]), true]
121
+ else
122
+ [key, raw.sub(/\s+#.*\z/, "").strip, true]
123
+ end
124
+ end
125
+
126
+ # Replace `${NAME}` and `$NAME`. A backslash before `$` stays a dollar and
127
+ # is not a reference. The environment wins over an earlier assignment in
128
+ # the file. Anything else is left as written.
129
+ #
130
+ # @param value [String]
131
+ # @param vars [Hash{String => String}]
132
+ # @return [String]
133
+ def expand(value, vars)
134
+ value.gsub(/\\\$|\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)/) do
135
+ match = Regexp.last_match
136
+ next "$" if match[0] == "\\$"
137
+
138
+ name = match[1] || match[2]
139
+ if ENV.key?(name)
140
+ ENV[name].to_s
141
+ elsif vars.key?(name)
142
+ vars[name]
143
+ else
144
+ match[0]
145
+ end
146
+ end
147
+ end
148
+
149
+ # One line as UTF-8. An invalid byte becomes U+FFFD so a binary dotenv
150
+ # line cannot raise before redaction starts.
151
+ #
152
+ # @param line [String]
153
+ # @return [String]
154
+ def utf8_line(line)
155
+ text = line.to_s.dup
156
+ text.force_encoding(Encoding::UTF_8)
157
+ text.scrub
158
+ end
159
+
160
+ # Index of the closing quote. A backslash escapes the next character
161
+ # inside double quotes, so `\"` does not end the value.
162
+ #
163
+ # @param raw [String]
164
+ # @param quote [String]
165
+ # @return [Integer, nil]
166
+ def closing_quote(raw, quote)
167
+ index = 1
168
+ while index < raw.length
169
+ if quote == '"' && raw[index] == "\\"
170
+ index += 2
171
+ elsif raw[index] == quote
172
+ return index
173
+ else
174
+ index += 1
175
+ end
176
+ end
177
+ nil
178
+ end
179
+
180
+ # Double-quoted dotenv escapes this reader understands. Other backslashes
181
+ # stay as written.
182
+ #
183
+ # @param text [String]
184
+ # @return [String]
185
+ def decode_double(text)
186
+ text.gsub(/\\[nrt"\\]/, "\\n" => "\n", "\\r" => "\r", "\\t" => "\t", "\\\"" => '"', "\\\\" => "\\")
187
+ end
188
+ end
189
+ end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "fileutils"
3
4
  require "shellwords"
4
5
  require "tempfile"
6
+ require "tmpdir"
5
7
  require_relative "result"
6
8
  require_relative "rails_run_env"
7
9
  require_relative "redaction"
@@ -386,6 +388,47 @@ module Mutineer
386
388
  parts
387
389
  end
388
390
 
391
+ # RUBYOPT for a `--test-command` child: the copier file alone. The tool's
392
+ # own RUBYOPT (`-rbundler/setup` under `bundle exec`) stays out, as
393
+ # {child_env} clears every `RUBY*` key so the suite picks its own Ruby and
394
+ # gems. Empty when the copier file cannot be named without whitespace.
395
+ #
396
+ # @api private
397
+ # @param dir [String] the directory that holds command_redact.rb.
398
+ # @return [String]
399
+ def self.command_rubyopt(dir = __dir__)
400
+ path = command_redact_file(dir)
401
+ path ? "-r#{path}" : ""
402
+ end
403
+
404
+ # Path of command_redact.rb that RUBYOPT can carry. Ruby splits RUBYOPT at
405
+ # whitespace and has no quoting or escape, so a path with a space (a home
406
+ # directory named "Jane Doe") makes every Ruby child stop with "invalid
407
+ # switch in RUBYOPT". Such a path is reached through a symlink in a
408
+ # temporary directory, removed when this process exits. `require_relative`
409
+ # in the file resolves the real path, so the rest of the copier loads.
410
+ #
411
+ # @api private
412
+ # @param dir [String] the directory that holds command_redact.rb.
413
+ # @return [String, nil] nil when the temporary directory name has whitespace too.
414
+ def self.command_redact_file(dir)
415
+ path = File.expand_path("command_redact.rb", dir)
416
+ return path unless path.match?(/\s/)
417
+
418
+ COMMAND_REDACT_LINKS[dir] ||= begin
419
+ tmp = Dir.mktmpdir("mutineer-redact")
420
+ at_exit { FileUtils.remove_entry(tmp, true) }
421
+ link = File.join(tmp, "command_redact.rb")
422
+ File.symlink(path, link)
423
+ link
424
+ end
425
+ link = COMMAND_REDACT_LINKS[dir]
426
+ link.match?(/\s/) ? nil : link
427
+ end
428
+
429
+ # Symlinks {command_redact_file} made, by source directory.
430
+ COMMAND_REDACT_LINKS = {} # rubocop:disable Style/MutableConstant
431
+
389
432
  # Spawns the command to a captured combined-output tempfile, enforces a
390
433
  # wall-clock timeout (SIGKILL past the deadline), and returns
391
434
  # [kind, exit_code, output, elapsed]. Mirrors Isolation's single-waiter loop:
@@ -409,7 +452,10 @@ module Mutineer
409
452
  # explicit [program, argv0] form guarantees the no-shell exec path even for a
410
453
  # degenerate single-element argv (Process.spawn(*argv) would route a lone
411
454
  # metachar-bearing string through /bin/sh, breaking the argv-only invariant).
412
- pid = Process.spawn(child_env, [argv.first, argv.first], *argv[1..],
455
+ # RUBYOPT loads the copier in each Ruby process this command starts, so a
456
+ # password that process assigns is hidden before the bytes are captured
457
+ # (#457). A program that is not Ruby ignores RUBYOPT.
458
+ pid = Process.spawn(child_env.merge("RUBYOPT" => command_rubyopt), [argv.first, argv.first], *argv[1..],
413
459
  out: out, err: %i[child out], pgroup: true)
414
460
  kind, code = wait_with_timeout(pid, timeout)
415
461
  elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start