mutineer 1.6.0 → 1.8.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 +680 -0
  3. data/README.md +210 -499
  4. data/lib/mutineer/baseline.rb +1 -1
  5. data/lib/mutineer/changed_lines.rb +2 -1
  6. data/lib/mutineer/child_coverage.rb +53 -0
  7. data/lib/mutineer/cli.rb +293 -42
  8. data/lib/mutineer/concern_blocks.rb +243 -0
  9. data/lib/mutineer/config.rb +154 -8
  10. data/lib/mutineer/coverage_map.rb +265 -27
  11. data/lib/mutineer/daemon_backend.rb +244 -75
  12. data/lib/mutineer/daemon_client.rb +73 -10
  13. data/lib/mutineer/daemon_server.rb +226 -42
  14. data/lib/mutineer/external_backend.rb +159 -15
  15. data/lib/mutineer/init.rb +124 -0
  16. data/lib/mutineer/isolation.rb +78 -24
  17. data/lib/mutineer/job_plan.rb +234 -44
  18. data/lib/mutineer/kill_channel.rb +24 -14
  19. data/lib/mutineer/migrate.rb +707 -0
  20. data/lib/mutineer/minitest_integration/in_test_database_error.rb +184 -0
  21. data/lib/mutineer/minitest_integration/kill_recorder.rb +25 -17
  22. data/lib/mutineer/minitest_integration.rb +44 -14
  23. data/lib/mutineer/mutator_registry.rb +6 -2
  24. data/lib/mutineer/mutators/array_literal.rb +4 -2
  25. data/lib/mutineer/mutators/base.rb +36 -7
  26. data/lib/mutineer/mutators/call_to_receiver.rb +148 -0
  27. data/lib/mutineer/mutators/chain_link.rb +6 -6
  28. data/lib/mutineer/mutators/condition_forcing.rb +6 -3
  29. data/lib/mutineer/mutators/operand_removal.rb +4 -2
  30. data/lib/mutineer/mutators/range_literal.rb +105 -4
  31. data/lib/mutineer/mutators/return_nil.rb +1 -0
  32. data/lib/mutineer/orphan_guard.rb +84 -0
  33. data/lib/mutineer/pairing.rb +62 -1
  34. data/lib/mutineer/project.rb +302 -14
  35. data/lib/mutineer/rails_run_env.rb +73 -0
  36. data/lib/mutineer/rails_worker_db.rb +1596 -57
  37. data/lib/mutineer/redaction.rb +199 -0
  38. data/lib/mutineer/reporter.rb +173 -31
  39. data/lib/mutineer/result.rb +52 -15
  40. data/lib/mutineer/runner.rb +148 -28
  41. data/lib/mutineer/statement_lines.rb +24 -11
  42. data/lib/mutineer/subject.rb +30 -4
  43. data/lib/mutineer/test_runners/minitest.rb +2 -1
  44. data/lib/mutineer/test_runners/rspec.rb +15 -3
  45. data/lib/mutineer/test_runners.rb +5 -2
  46. data/lib/mutineer/triage.rb +111 -0
  47. data/lib/mutineer/version.rb +1 -1
  48. data/lib/mutineer/worker_pool.rb +2 -1
  49. data/lib/mutineer.rb +4 -0
  50. data/skills/mutineer/SKILL.md +84 -0
  51. metadata +13 -1
@@ -11,6 +11,9 @@ require_relative "mutator_registry"
11
11
  require_relative "mutant_id"
12
12
  require_relative "project_path"
13
13
  require_relative "external_backend"
14
+ require_relative "isolation"
15
+ require_relative "test_runners"
16
+ require_relative "orphan_guard"
14
17
 
15
18
  module Mutineer
16
19
  # The job vocabulary every backend shares: which mutants run, which are
@@ -36,7 +39,20 @@ module Mutineer
36
39
  # `{id:, file:, subject:}` hash per matched mutant, in collection order: its
37
40
  # new id, its project-relative file and its subject's qualified name;
38
41
  # recorded even when a new id is also listed, since the old entry still
39
- # over-matches other files) and `id_map` (every new id => its old-format id).
42
+ # over-matches other files), `id_map` (every reported mutant's new id =>
43
+ # its old-format id), `dropped_id_map` (the same for each dropped repeat of
44
+ # an edit: its id is still current for `mutineer migrate`) and `declined`
45
+ # (operator name => count of candidates that operator found and did not
46
+ # mutate, {Mutators::Base#declined}; only counts above zero, keys sorted).
47
+ # The name is the {MutatorRegistry::ALL} key, or the class's `to_s` for an
48
+ # operator outside the registry. `--only` narrows `declined`, and `--since`
49
+ # does not: a declined candidate has no mutation to place on a line.
50
+ #
51
+ # Mutations are collected in {MutatorRegistry::ALL} order, whatever the
52
+ # order of `operator_classes`. Two operators can give the same mutated
53
+ # source (#275), and the copy of the registry-first operator is the one
54
+ # that runs, so the kept copy and the run order do not depend on the
55
+ # `--operators` order.
40
56
  #
41
57
  # Subjects sharing a qualified name in one file (two owner-less `def index`
42
58
  # in two DSL blocks) get a per-file ordinal in discovery order, so their ids
@@ -45,8 +61,15 @@ module Mutineer
45
61
  # @param config [Mutineer::Config] run configuration.
46
62
  # @param operator_classes [Array<Class>] resolved operators.
47
63
  # @return [Array(Array, Array<Result>, Hash<String,String>, Hash{Symbol => Hash})]
48
- # jobs, ignored, source_map, and extras (`:legacy_ignore_matches`, `:id_map`).
64
+ # jobs, ignored, source_map, and extras (`:legacy_ignore_matches`,
65
+ # `:id_map`, `:dropped_id_map`, `:declined`).
49
66
  def self.collect_jobs(config, operator_classes)
67
+ # Registry order, so a repeat across operators keeps the registry-first
68
+ # copy (#275). A class outside ALL goes last; ties keep the input order.
69
+ rank = MutatorRegistry::ALL.values
70
+ operator_classes = operator_classes.each_with_index
71
+ .sort_by { |klass, i| [rank.index(klass) || rank.size, i] }
72
+ .map(&:first)
50
73
  source_map = {}
51
74
  disabled_map = {}
52
75
  id_paths = {}
@@ -55,14 +78,23 @@ module Mutineer
55
78
  # its ordinal instead of minting a second id for the same mutant.
56
79
  name_decls = Hash.new { |h, k| h[k] = {} }
57
80
  ignore_set = config.ignore.to_set
81
+ ignore_reasons = config.ignore_reasons || {}
58
82
  jobs = []
59
83
  ignored_results = []
60
84
  legacy_ignore_matches = {}
61
85
  id_map = {}
86
+ dropped_id_map = {}
87
+ declined = Hash.new(0)
62
88
  Project.discover(config.sources, only: config.only).each do |subject|
63
- source = (source_map[subject.file] ||= File.read(subject.file))
89
+ source = (source_map[subject.file] ||= File.read(subject.file, encoding: "UTF-8"))
64
90
  disabled = (disabled_map[subject.file] ||= suppress_map(source, subject.file))
65
- mutations = operator_classes.flat_map { |klass| klass.new.mutations_for(subject, source) }
91
+ # A repeated operator name runs once, so its declines count once.
92
+ mutations = operator_classes.uniq.flat_map do |klass|
93
+ operator = klass.new
94
+ found = operator.mutations_for(subject, source)
95
+ declined[MutatorRegistry::ALL.key(klass) || klass.to_s] += operator.declined
96
+ found
97
+ end
66
98
  id_path = (id_paths[subject.file] ||= ProjectPath.relative(subject.file, config.project_root))
67
99
  decls = name_decls[[id_path, subject.qualified_name]]
68
100
  ordinal = (decls[subject.def_node.location.start_offset] ||= decls.size)
@@ -70,36 +102,44 @@ module Mutineer
70
102
  legacy_ids = MutantId.legacy_for_subject(subject, source, mutations)
71
103
  lines = mutations.map { |m| source.byteslice(0, m.start_offset).count("\n") + 1 }
72
104
  keys = result_keys(mutations, source, lines)
73
- # A repeat of an earlier edit on the same line is dropped (#159),
74
- # separately among the run and the ignored mutants, so an ignored copy
75
- # never hides a copy that should run. A dropped copy records nothing.
105
+ # A repeat of an earlier edit on the same line, from any operator, is
106
+ # dropped (#159, #275), separately among the run and the ignored
107
+ # mutants, so an ignored copy never hides a copy that should run. A
108
+ # dropped copy is not reported, but its id stays current for migrate.
76
109
  seen = { run: Set.new, ignored: Set.new }
77
110
  mutations.each_with_index do |mutation, i|
78
111
  id = ids[i]
79
112
  legacy = legacy_ids[i]
80
113
  ignored = suppressed?(mutation.operator, lines[i], [id, legacy], disabled, ignore_set)
81
- next unless seen[ignored ? :ignored : :run].add?(keys[i])
82
-
83
- id_map[id] = legacy
84
114
  # An old entry still over-matches other files even when the new id is
85
- # listed too, so every old-entry match is reported for migration.
115
+ # listed too, so every old-entry match is reported for migration,
116
+ # including a dropped copy's.
86
117
  if ignore_set.include?(legacy)
87
118
  (legacy_ignore_matches[legacy] ||= []) << { id: id, file: id_path, subject: subject.qualified_name }
88
119
  end
120
+ unless seen[ignored ? :ignored : :run].add?(keys[i])
121
+ dropped_id_map[id] = legacy
122
+ next
123
+ end
124
+
125
+ id_map[id] = legacy
89
126
  if ignored
90
- ignored_results << Result.ignored.with(subject: subject, mutation: mutation, id: id)
127
+ reason = suppression_reason(mutation.operator, lines[i], [id, legacy], disabled, ignore_reasons)
128
+ ignored_results << Result.ignored.with(subject: subject, mutation: mutation, id: id, reason: reason)
91
129
  else
92
130
  jobs << [subject, mutation, id]
93
131
  end
94
132
  end
95
133
  end
96
- [jobs, ignored_results, source_map, { legacy_ignore_matches: legacy_ignore_matches, id_map: id_map }]
134
+ declined = declined.select { |_, count| count.positive? }.sort.to_h
135
+ [jobs, ignored_results, source_map, { legacy_ignore_matches: legacy_ignore_matches, id_map: id_map,
136
+ dropped_id_map: dropped_id_map, declined: declined }]
97
137
  end
98
138
 
99
- # One key per mutation; two mutations share a key exactly when they are
100
- # the same operator on the same line and give the same mutated source (one
101
- # edit, #159). The caller drops a repeat after ids are assigned. Only a
102
- # group of same-operator, same-line mutations can repeat, so only those
139
+ # One key per mutation; two mutations share a key exactly when they are on
140
+ # the same line and give the same mutated source (one edit), from one
141
+ # operator (#159) or two (#275). The caller drops a repeat after ids are
142
+ # assigned. Only a group of same-line mutations can repeat, so only those
103
143
  # build a key from their text: the span from the group's earliest start to
104
144
  # its latest end, digested. Every other mutation gets a key of its own.
105
145
  #
@@ -109,7 +149,7 @@ module Mutineer
109
149
  # @return [Array<Object>] one key per mutation, in order.
110
150
  def self.result_keys(mutations, source, lines)
111
151
  keys = Array.new(mutations.size) { |i| i }
112
- mutations.each_index.group_by { |i| [mutations[i].operator, lines[i]] }.each_value do |group|
152
+ mutations.each_index.group_by { |i| lines[i] }.each_value do |group|
113
153
  next if group.size < 2
114
154
 
115
155
  from = group.map { |i| mutations[i].start_offset }.min
@@ -117,7 +157,7 @@ module Mutineer
117
157
  group.each do |i|
118
158
  m = mutations[i]
119
159
  span = "#{source.byteslice(from...m.start_offset)}#{m.replacement}#{source.byteslice(m.end_offset...to)}"
120
- keys[i] = [m.operator, lines[i], Digest::SHA256.digest(span)]
160
+ keys[i] = [lines[i], Digest::SHA256.digest(span)]
121
161
  end
122
162
  end
123
163
  keys
@@ -127,10 +167,14 @@ module Mutineer
127
167
  # those results would treat existing assertion failures as killed mutants.
128
168
  #
129
169
  # @param coverage_map [Mutineer::CoverageMap] the built or loaded map.
170
+ # @param framework [String, nil] the run's framework (nil is Minitest, as in
171
+ # {TestRunners.for}). For Minitest, the message names the seed: an
172
+ # order-dependent suite can pass in a random order and fail in the
173
+ # pinned one (#282).
130
174
  # @return [void]
131
175
  # @raise [Mutineer::SmokeCheckError] when any captured test failed clean,
132
176
  # or when no test recorded coverage and a capture failed.
133
- def self.abort_if_unclean!(coverage_map)
177
+ def self.abort_if_unclean!(coverage_map, framework: nil)
134
178
  if coverage_map.map.empty? && coverage_map.failed_test_files.any?
135
179
  raise SmokeCheckError, "no test recorded coverage, and capture failed for #{coverage_map.failed_test_files.join(', ')}"
136
180
  end
@@ -138,9 +182,47 @@ module Mutineer
138
182
  files = coverage_map.failed_clean_tests
139
183
  return if files.empty?
140
184
 
185
+ seed_hint = TestRunners.for(framework) == TestRunners::Minitest ? minitest_seed_hint : ""
141
186
  raise SmokeCheckError,
142
187
  "the unmutated suite is not green (#{files.join(', ')}) — " \
143
- "#{ExternalBackend.generic_env_hint}."
188
+ "#{ExternalBackend.generic_env_hint}.#{seed_hint}"
189
+ end
190
+
191
+ # Names the configured seed, and accounts for helper overrides.
192
+ #
193
+ # @api private
194
+ # @return [String]
195
+ def self.minitest_seed_hint
196
+ seed = MinitestIntegration.seed
197
+ " Mutineer runs Minitest with SEED=#{seed}, unless a test helper changes SEED. " \
198
+ "An order-dependent test can fail here and pass in another order. " \
199
+ "Check the listed test files with the same seed and helpers " \
200
+ "(for \"combined suite\", all of them together). " \
201
+ "Change SEED, including helper settings, to choose another order."
202
+ end
203
+
204
+ # The reload mutant text for a def in a concern's `included` or `prepended`
205
+ # block (#297): the mutated file with one statement added just before the
206
+ # hook call, `::Mutineer::ConcernBlocks.store_hook_def do <mutated def> end`.
207
+ # The statement sits in the same body as the hook call, so the stored def has
208
+ # the block's constant scope. After the load, the child passes the hook-def
209
+ # values to {ConcernBlocks.place_hook_def}. Shared by {Runner.run} and
210
+ # {DaemonBackend.job_result}, so both build the same text. The hook call starts
211
+ # before the def, so the mutation does not move its offset. A bad insert is
212
+ # not checked here: the child's `load` raises `SyntaxError` before any test
213
+ # runs, so the mutant scores `error`.
214
+ #
215
+ # @param mutated [String] the mutated source text.
216
+ # @param mutation [Mutineer::Mutation] the mutation.
217
+ # @param subject [Mutineer::Subject] a subject with a {Subject#reload_hook_call}.
218
+ # @param source [String] the original source text.
219
+ # @return [Array(String, Hash{Symbol => Object})] the mutant text, and the
220
+ # original def's `line`, `name` and `singleton`.
221
+ def self.hook_mutant(mutated, mutation, subject, source)
222
+ at = subject.reload_hook_call.location.start_offset
223
+ stored = "::Mutineer::ConcernBlocks.store_hook_def do\n#{Isolation.mutated_def(mutation, subject, source)}\nend; "
224
+ [mutated.byteslice(0...at) + stored + mutated.byteslice(at..),
225
+ { line: subject.def_node.location.start_line, name: subject.name, singleton: subject.singleton }]
144
226
  end
145
227
 
146
228
  # Coverage-based test selection, shared by the in-process ({Runner.run}) and daemon
@@ -149,8 +231,14 @@ module Mutineer
149
231
  # order of {CoverageMap#order_tests} (paired files, then cheapest), or
150
232
  # `[:verdict, Result]` (no_coverage / uncapturable) when none do. A line
151
233
  # Ruby does not count (a later line of a multi-line statement) uses the
152
- # tests that ran the statement that holds it ({StatementLines}), unless the
153
- # mutant sits in code of that statement that runs only sometimes.
234
+ # tests that ran the statement that holds it ({StatementLines}). When those
235
+ # lines give no tests, it retries with the loose lines of the statement:
236
+ # code of the statement that runs only sometimes (the right side of a
237
+ # multi-line `a && b`, #254), and a first or last line that holds other
238
+ # code (the opener of a one-line block, #289), but never the `def` line.
239
+ # Those tests may not have run the mutant, so the answer is
240
+ # `[:maybe, abs_test_paths]` and {selection_verdict} treats a survivor as
241
+ # no_coverage.
154
242
  #
155
243
  # An empty selection is `:ran_at_load` when the mutant's line ran while the
156
244
  # app booted or its class loaded ({ran_at_load?}): no test ran it, but the
@@ -167,14 +255,21 @@ module Mutineer
167
255
  # @param subject [Mutineer::Subject, nil] the subject (for its method body range).
168
256
  # @param source [String] the original source text.
169
257
  # @param coverage_map [Mutineer::CoverageMap] the built/loaded coverage map.
170
- # @return [Array(Symbol, Object)] `[:run, Array<String>]` or `[:verdict, Result]`.
258
+ # @return [Array(Symbol, Object)] `[:run, Array<String>]`, `[:maybe, Array<String>]`
259
+ # or `[:verdict, Result]`.
171
260
  def self.coverage_selection(source_file, mutation, subject, source, coverage_map)
172
261
  line = source.byteslice(0, mutation.start_offset).count("\n") + 1
173
262
  chosen = coverage_map.tests_for(source_file, line)
263
+ kind = :run
174
264
  if chosen.empty? && subject
175
265
  # A multi-line statement has a count on one of its lines only.
176
266
  lines = StatementLines.for(subject.def_node, source, mutation.start_offset)
177
267
  chosen = lines.flat_map { |l| coverage_map.tests_for(source_file, l) }.uniq
268
+ if chosen.empty?
269
+ lines = StatementLines.for(subject.def_node, source, mutation.start_offset, loose: true)
270
+ chosen = lines.flat_map { |l| coverage_map.tests_for(source_file, l) }.uniq
271
+ kind = :maybe
272
+ end
178
273
  end
179
274
  if chosen.empty?
180
275
  # Method BODY range, not the whole def: the def/end lines are "covered" at
@@ -187,7 +282,25 @@ module Mutineer
187
282
  return [:verdict, Result.no_coverage]
188
283
  end
189
284
 
190
- [:run, coverage_map.order_tests(source_file, chosen).map { |t| File.expand_path(t, coverage_map.project_root) }]
285
+ [kind, coverage_map.order_tests(source_file, chosen).map { |t| File.expand_path(t, coverage_map.project_root) }]
286
+ end
287
+
288
+ # The verdict of a mutant whose tests {coverage_selection} chose as `:maybe`:
289
+ # they ran its statement but may not have run the mutated code. A kill is a
290
+ # real kill. A survivor is no_coverage, because the run cannot tell "never
291
+ # ran" from "ran unchecked" (#254); like any no_coverage mutant it has no
292
+ # `--matrix` row. An error or a timeout stays what it is: the mutant ran, and
293
+ # a failure is reported, not hidden. A kill keeps its whole `--matrix` row.
294
+ # A result of any other selection comes back unchanged.
295
+ #
296
+ # @param kind [Symbol, nil, false] the first element of {coverage_selection}'s
297
+ # answer, or a falsy value when no selection ran.
298
+ # @param result [Mutineer::Result] the mutant's verdict.
299
+ # @return [Mutineer::Result]
300
+ def self.selection_verdict(kind, result)
301
+ return result unless kind == :maybe && result.survived?
302
+
303
+ result.with(status: :no_coverage, kills: nil)
191
304
  end
192
305
 
193
306
  # True when the mutant's line ran while the app booted or its class loaded,
@@ -242,13 +355,19 @@ module Mutineer
242
355
  result.with(status: :ran_at_load)
243
356
  end
244
357
 
245
- # Map each line number to :all or a set of operator symbols, using
246
- # inline `# mutineer:disable-line [ops]` markers (RuboCop semantics: the marker
247
- # sits on the same physical line as the code it silences). A bare marker
248
- # disables every operator on that line; `disable-line a, b` only the listed
249
- # operators. Block-form disable/enable ranges are intentionally not supported.
250
- # Only a real `#` comment counts: Prism lists the comments, so the marker
251
- # text inside a string, heredoc or regex silences nothing (#158).
358
+ # Map each line number to `{ ops:, reason: }`, using inline
359
+ # `# mutineer:disable-line [ops]` markers (RuboCop semantics: the marker
360
+ # sits on the same physical line as the code it silences). `ops` is `:all`
361
+ # or a set of operator symbols. `reason` is the text after `--`, or nil.
362
+ # A bare marker disables every operator on that line; `disable-line a, b`
363
+ # only the listed operators. Block-form disable/enable ranges are
364
+ # intentionally not supported. Only a real `#` comment counts: Prism lists
365
+ # the comments, so the marker text inside a string, heredoc or regex
366
+ # silences nothing (#158).
367
+ #
368
+ # @param source [String] the source text.
369
+ # @param file [String] the file name, for warnings.
370
+ # @return [Hash{Integer => Hash}] line => `{ ops:, reason: }`.
252
371
  def self.suppress_map(source, file)
253
372
  map = {}
254
373
  Parser.comments(source).grep(Prism::InlineComment).each do |comment|
@@ -261,14 +380,34 @@ module Mutineer
261
380
  ops = nil if ops&.empty?
262
381
  unknown = ops.to_a.reject { |o| MutatorRegistry::ALL.key?(o) }
263
382
  unknown.each do |o|
264
- warn "mutineer: unknown operator #{o.inspect} in #{file}:#{line} " \
383
+ warn "mutineer: unknown operator #{o.inspect} in #{file}:#{line}#{Mutineer.did_you_mean(o, MutatorRegistry::ALL.keys)} " \
265
384
  "(known: #{MutatorRegistry::ALL.keys.join(', ')}); write a reason after --"
266
385
  end
267
- map[line] = ops ? ops.map(&:to_sym).to_set : :all
386
+ reason = comment.slice[/\s--\s*(.*)\z/, 1]&.strip
387
+ reason = nil if reason.nil? || reason.empty?
388
+ map[line] = { ops: ops ? ops.map(&:to_sym).to_set : :all, reason: reason }
268
389
  end
269
390
  map
270
391
  end
271
392
 
393
+ # The operator set for one suppress-map entry. A Hash is the current shape
394
+ # (`ops` plus `reason`). `:all` and a Set remain valid for callers that
395
+ # build the map by hand.
396
+ #
397
+ # @param entry [Hash, Symbol, Set, nil] one line's suppress-map value.
398
+ # @return [Symbol, Set, nil] `:all`, the operator set, or nil.
399
+ def self.line_ops(entry)
400
+ entry.is_a?(Hash) ? entry[:ops] : entry
401
+ end
402
+
403
+ # The reason text on one suppress-map entry, when the entry stores one.
404
+ #
405
+ # @param entry [Hash, Object] one line's suppress-map value.
406
+ # @return [String, nil]
407
+ def self.line_reason(entry)
408
+ entry.is_a?(Hash) ? entry[:reason] : nil
409
+ end
410
+
272
411
  # True when this mutant is suppressed: its line bears a disable-line marker
273
412
  # (bare, or scoped to its operator), OR its new or old-format id is in the
274
413
  # config ignore list. Checked at job-build time so a suppressed mutant is
@@ -279,13 +418,58 @@ module Mutineer
279
418
  def self.suppressed?(operator, line, ids, disabled, ignore_set)
280
419
  return true if Array(ids).any? { |id| ignore_set.include?(id) }
281
420
 
282
- case (entry = disabled[line])
421
+ case line_ops(disabled[line])
283
422
  when :all then true
284
- when Set then entry.include?(operator)
423
+ when Set then line_ops(disabled[line]).include?(operator)
285
424
  else false
286
425
  end
287
426
  end
288
427
 
428
+ # Why an ignored mutant was suppressed. An inline reason wins. Otherwise
429
+ # the reason is the first matching ignore id that has one. Blank text is nil.
430
+ #
431
+ # @param operator [Symbol] the mutant's operator.
432
+ # @param line [Integer] the mutant's line.
433
+ # @param ids [Array<String>] the new id and the old-format id.
434
+ # @param disabled [Hash] the file's suppress map.
435
+ # @param ignore_reasons [Hash{String => String}] id => reason.
436
+ # @return [String, nil]
437
+ def self.suppression_reason(operator, line, ids, disabled, ignore_reasons)
438
+ entry = disabled[line]
439
+ ops = line_ops(entry)
440
+ line_hit = ops == :all || (ops.is_a?(Set) && ops.include?(operator))
441
+ inline = line_hit ? line_reason(entry) : nil
442
+ return inline if inline.is_a?(String) && !inline.strip.empty?
443
+
444
+ Array(ids).each do |candidate|
445
+ text = ignore_reasons[candidate]
446
+ return text if text.is_a?(String) && !text.strip.empty?
447
+ end
448
+ nil
449
+ end
450
+
451
+ # Narrows the jobs and the suppressed (ignored) results to --since when it
452
+ # is set, as the dry run does, so a scoped report lists only mutants on
453
+ # changed lines. Also returns how many mutants there were before narrowing
454
+ # (nil without --since), so the CLI can tell "the changes held nothing to
455
+ # test" from "nothing was mutable at all".
456
+ #
457
+ # @param jobs [Array] (subject, mutation, id) entries to run.
458
+ # @param ignored_results [Array<Mutineer::Result>] suppressed mutants.
459
+ # @param source_map [Hash{String => String}] source text by file.
460
+ # @param config [Mutineer::Config] run configuration.
461
+ # @return [Array(Array, Array<Mutineer::Result>, Integer), Array(Array, Array<Mutineer::Result>, nil)]
462
+ # the jobs to run, the ignored results to report, and the count before --since.
463
+ def self.scope_since(jobs, ignored_results, source_map, config)
464
+ return [jobs, ignored_results, nil] unless config.since
465
+
466
+ # One pass, so git computes the changed lines once.
467
+ ignored_entries = ignored_results.map { |r| [r.subject, r.mutation, r] }
468
+ kept = filter_since(jobs + ignored_entries, source_map, config)
469
+ kept_ignored, kept_jobs = kept.partition { |entry| entry.last.is_a?(Result) }
470
+ [kept_jobs, kept_ignored.map(&:last), jobs.size + ignored_results.size]
471
+ end
472
+
289
473
  # --since: keep only jobs whose mutation lands on a line changed since the git
290
474
  # ref. Composes with coverage selection (it only narrows the job list; each
291
475
  # surviving mutant still goes through Runner.run's coverage check). A file with
@@ -334,17 +518,23 @@ module Mutineer
334
518
  config.sources.map { |f| File.dirname(File.expand_path(f, config.project_root)) }.uniq
335
519
  end
336
520
 
337
- # Removes stale mutant tempfiles from the given directories. The daemon writes a
338
- # differently-named temp, so {DaemonBackend} passes its glob when it has to sweep
339
- # tool-side (nothing boots on an empty run, so the daemon's own sweep never runs).
521
+ # Removes stale mutant tempfiles from the given directories. The default
522
+ # glob matches the files of both modes, so the in-process boot sweep also
523
+ # removes an old daemon file (#287). {DaemonBackend} passes its daemon-only
524
+ # glob when it sweeps tool-side at the end of every daemon run, empty or not.
525
+ # A file another process still owns is left in place: both modes record the
526
+ # creating pid in the name and lock the file while it loads (#295).
340
527
  #
341
528
  # @param dirs [Array<String>] directories to sweep.
342
529
  # @param glob [String] filename pattern to remove.
343
- # @return [void]
344
- def self.sweep_orphans(dirs, glob = "mutineer_mutant*.rb")
345
- dirs.each do |dir|
346
- Dir.glob(File.join(dir, glob)).each do |f|
347
- File.unlink(f) rescue nil # rubocop:disable Style/RescueModifier
530
+ # @return [Array<String>] the files left in place because a process owns them.
531
+ def self.sweep_orphans(dirs, glob = OrphanGuard::TEMP_BASENAME)
532
+ dirs.flat_map do |dir|
533
+ Dir.glob(File.join(dir, glob)).select do |path|
534
+ next true if OrphanGuard.mutant_file_in_use?(path)
535
+
536
+ File.unlink(path) rescue nil # rubocop:disable Style/RescueModifier
537
+ false
348
538
  end
349
539
  end
350
540
  end
@@ -9,6 +9,7 @@ module Mutineer
9
9
  # ["start"] the recorder is armed; once, first
10
10
  # ["pass", file, name, id] a test passed against the mutant
11
11
  # ["kill", file, name, id] a test failed or errored: it killed it
12
+ # ["unavailable", file, name, id] a test ran but its database failed
12
13
  # ["parallel"] the run reached its parallel tests; once
13
14
  # ["skip"] what runs next, a run without --matrix
14
15
  # skips (see below)
@@ -27,20 +28,22 @@ module Mutineer
27
28
  #
28
29
  # Minitest runs its serial test classes first and its `parallelize_me!`
29
30
  # classes after them, and the stop at the first failure of a run without
30
- # `--matrix` skips everything after a serial kill. `parallel` marks where the
31
+ # `--matrix` skips everything after a serial failure. `parallel` marks where the
31
32
  # parallel tests begin, so the parent can tell a kill that the stop would have
32
33
  # followed from one it could not. `cleanup` marks where the tests end and the
33
34
  # suite's own hooks begin (RSpec `after(:suite)`). It decides no verdict
34
35
  # (the `skip` regions do); it only marks a later test line as out of order.
35
36
  #
36
- # After the first serial kill, a run without `--matrix` skips every later
37
+ # After the first serial failure, a run without `--matrix` skips every later
37
38
  # test and every later test class or example group, but it still runs the
38
39
  # rest of the code around the failing test: the remainder of its Minitest
39
40
  # class `run` wrapper, the `after(:all)` hooks of its RSpec groups, and the
40
41
  # suite hooks. The recorder writes `skip` as it enters something that run
41
42
  # would skip and `unskip` as that returns, so a child that exits, crashes or
42
43
  # hangs inside a skipped region is one the plain run would have stopped
43
- # before (see {Isolation.finish}). The pairs nest.
44
+ # before (see {Isolation.finish}). Its first failure decides the verdict:
45
+ # killed for a real kill, error for an unavailable database outcome. The
46
+ # pairs nest. An unavailable result also leaves the row incomplete.
44
47
  #
45
48
  # A row is complete only with `start` and `end`, in order, and no lost line: a
46
49
  # test that exits the process, a crash, an interrupted run or a recorder that
@@ -61,6 +64,9 @@ module Mutineer
61
64
  # Event for a test that failed or errored against the mutant: it killed it.
62
65
  KILL = "kill"
63
66
 
67
+ # Event for a test whose database failed, so it provides no mutation verdict.
68
+ UNAVAILABLE = "unavailable"
69
+
64
70
  # Event that opens a recorded run.
65
71
  START = "start"
66
72
 
@@ -87,17 +93,18 @@ module Mutineer
87
93
 
88
94
  # What one child sent. `killed` and `ran` are sorted, unique
89
95
  # `[file, name, id]` tests (`ran` includes the killers); `lost` counts lines
90
- # that could not be read, including a partial last line. `parallel` and
91
- # `cleanup` say the marker arrived, and `serial_kill` that a test killed
92
- # before the `parallel` marker. `skipping` counts the {SKIP} regions still
96
+ # that could not be read, including a partial last line, and unavailable
97
+ # outcomes. `parallel` and `cleanup` say the marker arrived. `serial_kill`
98
+ # or `serial_error` says which failure came first before `parallel`.
99
+ # `skipping` counts the {SKIP} regions still
93
100
  # open when the stream ended. `invalid` is set by any line out of order.
94
- Report = Struct.new(:killed, :ran, :started, :finished, :parallel, :cleanup, :serial_kill, :skipping, :invalid,
101
+ Report = Struct.new(:killed, :ran, :started, :finished, :parallel, :cleanup, :serial_kill, :serial_error, :skipping, :invalid,
95
102
  :lost, keyword_init: true)
96
103
 
97
104
  # Writes one test line.
98
105
  #
99
106
  # @param io [IO] the write end of the channel.
100
- # @param event [String] {PASS} or {KILL}.
107
+ # @param event [String] {PASS}, {KILL}, or {UNAVAILABLE}.
101
108
  # @param file [String] the file that defines the test.
102
109
  # @param name [String] the test's display name, e.g. `CalculatorTest#test_add`.
103
110
  # @param id [String] what tells the test apart from others with its file and
@@ -169,7 +176,7 @@ module Mutineer
169
176
  # @return [Report]
170
177
  def self.parse(buffer)
171
178
  report = Report.new(killed: [], ran: [], started: false, finished: false, parallel: false, cleanup: false,
172
- serial_kill: false, skipping: 0, invalid: false, lost: 0)
179
+ serial_kill: false, serial_error: false, skipping: 0, invalid: false, lost: 0)
173
180
  buffer.dup.force_encoding(Encoding::UTF_8).each_line do |line|
174
181
  fields = line.end_with?("\n") ? parse_line(line) : nil
175
182
  fields ? apply(report, fields) : report.lost += 1
@@ -211,10 +218,13 @@ module Mutineer
211
218
  report.invalid = true unless open?(report) && !report.cleanup
212
219
  test = fields[1, 3]
213
220
  report.ran << test
214
- return unless fields[0] == KILL
215
-
216
- report.killed << test
217
- report.serial_kill = true unless report.parallel
221
+ if fields[0] == UNAVAILABLE
222
+ report.lost += 1
223
+ report.serial_error = true unless report.parallel || report.serial_kill
224
+ elsif fields[0] == KILL
225
+ report.killed << test
226
+ report.serial_kill = true unless report.parallel || report.serial_error
227
+ end
218
228
  end
219
229
  end
220
230
 
@@ -238,7 +248,7 @@ module Mutineer
238
248
 
239
249
  valid =
240
250
  case fields[0]
241
- when PASS, KILL then fields.size == 4
251
+ when PASS, KILL, UNAVAILABLE then fields.size == 4
242
252
  when START, PARALLEL, SKIP, UNSKIP, CLEANUP, LOST, FINISH then fields.size == 1
243
253
  end
244
254
  fields if valid