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
@@ -118,10 +118,18 @@ module Mutineer
118
118
  # that are the same ({.same?}): a validator with equivalent options, a
119
119
  # lambda that captured equivalent values, a block from the same place in
120
120
  # the code. They pair only when there are as many old ones as new ones
121
- # ({.exact_pairs}). An entry left over, such as a `validates` whose options come
122
- # from a method the mutant changed, pairs by {.key} only when no concern
123
- # entry could be the one it repeats ({.by_key}). Otherwise both copies stay,
124
- # as before this fix.
121
+ # ({.exact_pairs}). An entry left over pairs by {.key}. The key is the
122
+ # callback kind, the filter, and its `if:` / `unless:` conditions. A
123
+ # `&:name` proc's filter is that symbol. A block's filter is its place
124
+ # and its code, not a value it captured. A validator's filter is its
125
+ # class, its attributes and its option names, not values a method the
126
+ # mutant changed can return. A concern entry that differs on any of
127
+ # those is not a candidate, so the class body's repeat can pair. One
128
+ # that is the same on all of them still shares the key, and the pair is
129
+ # kept only when the counts match, so a guess cannot drop the concern's
130
+ # entry. Otherwise both copies stay (#413). Full reload (#389) builds
131
+ # the class once, which is what can separate those identical entries.
132
+ # Redefine does not run the class body again, so it never adds the copy.
125
133
  #
126
134
  # ActiveSupport gives a class a new chain object each time a callback is
127
135
  # added or skipped, so a class whose chain objects all stay the same was
@@ -151,12 +159,13 @@ module Mutineer
151
159
  reloaded = files.map { |file| real_path(file) }
152
160
  # Each class's entries before the load, copied only for the classes asked.
153
161
  old = Hash.new { |copies, klass| copies[klass] = before.fetch(klass, {}).transform_values(&:to_a) }
154
- pairs = changed.flat_map { |klass| replacements(klass, old, reloaded) }
155
- return if pairs.empty?
156
-
157
- swap = {}.compare_by_identity
158
- pairs.each { |old_entry, new_entry| swap[old_entry] = new_entry }
159
- restore(changed, old, swap)
162
+ by_chain = {}
163
+ changed.each do |klass|
164
+ replacements(klass, old, reloaded).each do |name, pairs|
165
+ by_chain[[klass, name]] = pairs
166
+ end
167
+ end
168
+ restore(changed, old, by_chain) unless by_chain.empty?
160
169
  end
161
170
 
162
171
  # @param now [Hash{Symbol => Object}] a class's chains after the load.
@@ -168,24 +177,57 @@ module Mutineer
168
177
 
169
178
  # Pairs each entry the load added to `klass`'s own chains with the old
170
179
  # entry it repeats: first with old entries that are the same
171
- # ({.exact_pairs}), then by key.
180
+ # ({.exact_pairs}), then by key. A mutant can change a block's body, so
181
+ # the code no longer matches. Leftovers that share a place and have
182
+ # equal counts pair without the code ({.by_key} with `code: false`).
183
+ # A concern that shares only that place still makes the counts differ.
184
+ # Last, a callback that names a method pairs with the one it displaced
185
+ # ({.displaced_pairs}), even when its conditions changed.
172
186
  #
173
187
  # @param klass [Class] a class whose chains the load changed.
174
188
  # @param old [Hash{Class => Hash{Symbol => Array}}] each class's entries before the load.
175
189
  # @param reloaded [Array<String>] real paths of the source and mutant files.
176
- # @return [Array<Array(Object, Object)>] each old entry and the new entry that replaces it.
190
+ # @return [Hash{Symbol => Array<Array(Object, Object)>}] for each chain
191
+ # name, each old entry and the new entry that replaces it.
177
192
  def self.replacements(klass, old, reloaded)
178
193
  parent = klass.superclass
179
194
  parent_now = parent.respond_to?(:__callbacks) ? parent.__callbacks : {}
180
195
  parent_old = old[parent]
181
- klass.__callbacks.flat_map do |name, chain|
196
+ pairs = {}
197
+ klass.__callbacks.each do |name, chain|
182
198
  before = old[klass].fetch(name, [])
183
199
  added = without(without(chain.to_a, before), parent_now[name].to_a)
184
- next [] if added.empty?
200
+ next if added.empty?
185
201
 
186
202
  pool = without(before, parent_old.fetch(name, []))
187
203
  exact = exact_pairs(added, pool, reloaded)
188
- exact + by_key(without(added, exact.map(&:last)), pool, reloaded)
204
+ rest = without(added, exact.map(&:last))
205
+ keyed = by_key(rest, pool, reloaded)
206
+ loose = by_key(without(rest, keyed.map(&:last)), without(pool, keyed.map(&:first)), reloaded, code: false)
207
+ matched = exact + keyed + loose
208
+ matched += displaced_pairs(without(added, matched.map(&:last)), without(pool, matched.map(&:first)), chain.to_a)
209
+ pairs[name] = matched unless matched.empty?
210
+ end
211
+ pairs
212
+ end
213
+
214
+ # Pairs a new callback that names a method with the old one it
215
+ # displaced. Rails keeps one callback per method name and kind in a
216
+ # chain, whatever its `if:` and `unless:`, so the old one is gone from
217
+ # the chain. A reload that changes a condition makes the {.key}
218
+ # differ (#413), but the pair is still certain. {.restored} needs it to
219
+ # rebuild a subclass skip from the reloaded callback (#440).
220
+ #
221
+ # @param added [Array] new entries nothing else paired.
222
+ # @param pool [Array] old entries nothing else paired.
223
+ # @param now [Array] the chain's entries after the load.
224
+ # @return [Array<Array(Object, Object)>] each old entry and the new entry that replaces it.
225
+ def self.displaced_pairs(added, pool, now)
226
+ present = identity_set(now)
227
+ gone = pool.reject { |entry| present.include?(entry) }
228
+ added.filter_map do |new_entry|
229
+ olds = gone.select { |entry| displaced?(entry, new_entry) }
230
+ [olds.first, new_entry] if olds.size == 1
189
231
  end
190
232
  end
191
233
 
@@ -223,10 +265,12 @@ module Mutineer
223
265
  # @param added [Array] new entries with no exact match.
224
266
  # @param pool [Array] old entries no exact match took.
225
267
  # @param reloaded [Array<String>] real paths of the source and mutant files.
268
+ # @param code [Boolean] when false, a block's compiled body is left out
269
+ # of the key, so a mutant of that body still pairs.
226
270
  # @return [Array<Array(Object, Object)>] each old entry and the new entry that replaces it.
227
- def self.by_key(added, pool, reloaded)
228
- old_by_key = pool.group_by { |entry| key(entry, reloaded) }
229
- added.group_by { |entry| key(entry, reloaded) }.flat_map do |k, new_entries|
271
+ def self.by_key(added, pool, reloaded, code: true)
272
+ old_by_key = pool.group_by { |entry| key(entry, reloaded, code: code) }
273
+ added.group_by { |entry| key(entry, reloaded, code: code) }.flat_map do |k, new_entries|
230
274
  old_entries = old_by_key.fetch(k, [])
231
275
  old_entries.size == new_entries.size ? old_entries.zip(new_entries) : []
232
276
  end
@@ -238,9 +282,10 @@ module Mutineer
238
282
  #
239
283
  # @param changed [Array<Class>] the classes whose chains the load changed.
240
284
  # @param old [Hash{Class => Hash{Symbol => Array}}] each class's entries before the load.
241
- # @param swap [Hash{Object => Object}] each old entry to its new entry.
285
+ # @param by_chain [Hash{Array(Class, Symbol) => Array<Array(Object, Object)>}]
286
+ # each class and chain name, with the old and new entries paired there.
242
287
  # @return [void]
243
- def self.restore(changed, old, swap)
288
+ def self.restore(changed, old, by_chain)
244
289
  seen = {}.compare_by_identity
245
290
  changed.each do |klass|
246
291
  klass.__callbacks.each do |name, chain|
@@ -248,16 +293,19 @@ module Mutineer
248
293
 
249
294
  seen[chain] = true
250
295
  now = chain.to_a
251
- order = reorder(old[klass].fetch(name, []), now, swap)
296
+ order = reorder(old[klass].fetch(name, []), now, swap_for(klass, name, by_chain))
252
297
  next if order.size == now.size && order.zip(now).all? { |a, b| a.equal?(b) }
253
298
 
254
299
  chain.clear
255
300
  chain.append(*order)
256
301
  end
257
302
  end
258
- # A validator is the filter of its `validate` callback.
303
+ # A validator is the filter of its `validate` callback. The same
304
+ # validator object can sit on a subclass, so every pair counts here.
259
305
  validators = {}.compare_by_identity
260
- swap.each { |old_entry, new_entry| validators[old_entry.filter] = new_entry.filter }
306
+ by_chain.each_value do |pairs|
307
+ pairs.each { |old_entry, new_entry| validators[old_entry.filter] = new_entry.filter }
308
+ end
261
309
  lists = changed.select { |klass| klass.respond_to?(:_validators) }.flat_map { |klass| klass._validators.values }
262
310
  lists.uniq(&:object_id).each do |list|
263
311
  list.map! { |validator| validators.fetch(validator, validator) }
@@ -265,17 +313,228 @@ module Mutineer
265
313
  end
266
314
  end
267
315
 
316
+ # A paired new entry whose old entry this chain did not have came from an
317
+ # ancestor's class body, for this same chain name. Rails appends a
318
+ # parent's callback to every loaded subclass, so it also reached a
319
+ # subclass that skipped it (#414). Such an entry is dropped. A
320
+ # replacement on another chain name, or on a class that is not an
321
+ # ancestor, is not such an entry: that chain keeps it. When Rails
322
+ # dropped the subclass's own copy as a duplicate, such as the
323
+ # conditional copy that a `skip_callback ... if:` leaves, that copy
324
+ # comes back. A copy that still holds its ancestor's conditions from
325
+ # before this load is rebuilt from the reloaded callback, so a mutant
326
+ # of those conditions reaches the subclass (#440). Reload and the
327
+ # daemon run this path. Redefine does not re-run the class body.
328
+ #
268
329
  # @param old [Array] the chain's entries before the load.
269
330
  # @param now [Array] its entries after the load.
270
331
  # @param swap [Hash{Object => Object}] each old entry to its new entry.
271
332
  # @return [Array] `old` with each new entry swapped in, then the other new entries.
272
333
  def self.reorder(old, now, swap)
273
334
  present = identity_set(now)
335
+ paired = identity_set(swap.values)
336
+ kept = identity_set(old.filter_map { |entry| swap[entry] })
337
+ stray = now.select { |entry| paired.include?(entry) && !kept.include?(entry) }
274
338
  order = old.filter_map do |entry|
275
339
  new_entry = swap[entry]
276
- new_entry && present.include?(new_entry) ? new_entry : (entry if present.include?(entry))
340
+ next new_entry if new_entry && present.include?(new_entry)
341
+ next entry if present.include?(entry)
342
+
343
+ other = merge_stray(entry, stray, swap)
344
+ if other
345
+ restored(entry, other, swap)
346
+ elsif stray.any? { |candidate| displaced?(entry, candidate) }
347
+ entry
348
+ end
349
+ end
350
+ order + without(now, order).reject { |entry| swap.key?(entry) || paired.include?(entry) }
351
+ end
352
+
353
+ # The callback to put back when Rails dropped `entry` as a duplicate of
354
+ # `stray`. `skip_callback` with `if:` or `unless:` builds `entry` by
355
+ # copying the parent's condition objects and appending the subclass's
356
+ # own. Putting that object back would keep the parent's conditions from
357
+ # before the reload (#440). When those objects are still the parent's
358
+ # previous ones, this rebuilds `entry` from `stray` through
359
+ # `merge_conditional_options`, the same merge `skip_callback` uses: a
360
+ # subclass `if:` is stored on `unless`, and a subclass `unless:` on
361
+ # `if`. A copy that does not hold those objects is the subclass's own
362
+ # callback and stays. A parent that had no conditions has no objects to
363
+ # match, so its copy stays too (#414): a subclass callback of the same
364
+ # name is not rewritten. A list of only symbols, or other interned
365
+ # objects, is not evidence either. An independent callback can name the
366
+ # same symbol. A copied Proc is evidence, and that Proc is carried.
367
+ # The old copy also stays when `stray` still has those same condition
368
+ # objects.
369
+ #
370
+ # @param entry [Object] the subclass callback Rails dropped.
371
+ # @param stray [Object] the ancestor callback appended in its place.
372
+ # @param swap [Hash{Object => Object}] each old entry to its new entry.
373
+ # @return [Object] `entry`, or the rebuilt callback.
374
+ def self.restored(entry, stray, swap)
375
+ parent = merge_source(entry, stray, swap)
376
+ return entry unless parent
377
+ return entry if same_conditions?(stray, parent)
378
+ return entry unless stray.respond_to?(:merge_conditional_options)
379
+
380
+ extra_if, extra_unless = extra_conditions(entry, parent)
381
+ stray.merge_conditional_options(
382
+ chain_view(stray), if_option: extra_unless, unless_option: extra_if
383
+ )
384
+ end
385
+
386
+ # The nearest old callback that `swap` maps to `stray` and that {restored}
387
+ # can show `entry` was merged from. Ancestors are inserted furthest
388
+ # first, so the last match is the nearest class that reloaded `stray`.
389
+ #
390
+ # @param entry [Object] the subclass callback Rails dropped.
391
+ # @param stray [Object] the ancestor callback appended in its place.
392
+ # @param swap [Hash{Object => Object}] each old entry to its new entry.
393
+ # @return [Object, nil] that old callback, or nil when `entry` is not its merge.
394
+ def self.merge_source(entry, stray, swap)
395
+ found = nil
396
+ swap.each do |old_entry, new_entry|
397
+ found = old_entry if new_entry.equal?(stray) && merged_from?(entry, old_entry)
277
398
  end
278
- order + without(now, order).reject { |entry| swap.key?(entry) }
399
+ found
400
+ end
401
+
402
+ # The reloaded callback `entry` was merged from, when several ancestors
403
+ # appended a callback of the same name. The first of those is not always
404
+ # the one. {displaced?} only compares the method name and the kind.
405
+ #
406
+ # @param entry [Object] the subclass callback Rails dropped.
407
+ # @param stray [Array] new callbacks this chain did not have.
408
+ # @param swap [Hash{Object => Object}] each old entry to its new entry.
409
+ # @return [Object, nil] the stray to rebuild from, or nil.
410
+ def self.merge_stray(entry, stray, swap)
411
+ stray.find { |candidate| displaced?(entry, candidate) && merge_source(entry, candidate, swap) }
412
+ end
413
+
414
+ # @param entry [Object] a callback left by `skip_callback`.
415
+ # @param parent [Object] the ancestor callback it was merged from.
416
+ # @return [Boolean] true when `entry`'s conditions still start with
417
+ # `parent`'s condition objects, and one of those objects is not
418
+ # interned. `skip_callback` copies those objects. A parent with no
419
+ # conditions matches every copy, so that is not evidence. A symbol is
420
+ # the same object wherever it is written, so it is not evidence
421
+ # either. A symbol-only parent condition stays on the old copy.
422
+ # No issue tracks that limit yet.
423
+ def self.merged_from?(entry, parent)
424
+ entry_if, entry_unless = conditions(entry)
425
+ parent_if, parent_unless = conditions(parent)
426
+ return false if parent_if.empty? && parent_unless.empty?
427
+ return false unless prefix?(entry_if, parent_if) && prefix?(entry_unless, parent_unless)
428
+
429
+ (parent_if + parent_unless).any? { |item| !interned_condition?(item) }
430
+ end
431
+
432
+ # @param object [Object] one `if:` or `unless:` condition.
433
+ # @return [Boolean] true for nil, true, false, a Symbol, or an Integer.
434
+ # Those are the same object wherever they are written. A Proc is not.
435
+ def self.interned_condition?(object)
436
+ object.nil? || same_object?(object, true) || same_object?(object, false) ||
437
+ object.is_a?(Symbol) || object.is_a?(Integer)
438
+ rescue StandardError
439
+ false
440
+ end
441
+
442
+ # @param left [Object] a callback.
443
+ # @param right [Object] another callback.
444
+ # @return [Boolean] true when both condition lists hold the same objects,
445
+ # in order.
446
+ def self.same_conditions?(left, right)
447
+ left_if, left_unless = conditions(left)
448
+ right_if, right_unless = conditions(right)
449
+ same_list?(left_if, right_if) && same_list?(left_unless, right_unless)
450
+ end
451
+
452
+ # The conditions `skip_callback` appended after `parent`'s. {merged_from?}
453
+ # has already required `parent`'s conditions to be a prefix.
454
+ #
455
+ # @param entry [Object] the merged subclass callback.
456
+ # @param parent [Object] the ancestor callback it was merged from.
457
+ # @return [Array(Array, Array)] the extra `if` conditions and the extra
458
+ # `unless` conditions.
459
+ def self.extra_conditions(entry, parent)
460
+ entry_if, entry_unless = conditions(entry)
461
+ parent_if, parent_unless = conditions(parent)
462
+ [entry_if.drop(parent_if.size), entry_unless.drop(parent_unless.size)]
463
+ end
464
+
465
+ # The `name` and `config` `ActiveSupport::Callbacks::Callback.build` reads
466
+ # off a chain. {chain_view} builds one from a callback.
467
+ ChainView = Struct.new(:name, :config)
468
+
469
+ # @param callback [Object] an ActiveSupport callback.
470
+ # @return [ChainView] that callback's chain name and config.
471
+ def self.chain_view(callback)
472
+ ChainView.new(callback.name, callback.chain_config)
473
+ end
474
+
475
+ # @param list [Array]
476
+ # @param parent [Array]
477
+ # @return [Boolean] true when `list` and `parent` are the same objects, in order.
478
+ def self.same_list?(list, parent)
479
+ list.size == parent.size && prefix?(list, parent)
480
+ end
481
+
482
+ # @param list [Array]
483
+ # @param parent [Array] the objects `list` must start with.
484
+ # @return [Boolean] true when each of `parent` is that object of `list`.
485
+ # A shorter `list` does not start with `parent`.
486
+ def self.prefix?(list, parent)
487
+ parent.each_with_index.all? { |item, index| same_object?(list[index], item) }
488
+ end
489
+
490
+ # `BasicObject#equal?`, so a proxy condition cannot answer for its target.
491
+ # A condition that cannot be compared does not count as the parent's.
492
+ #
493
+ # @param left [Object]
494
+ # @param right [Object]
495
+ # @return [Boolean]
496
+ def self.same_object?(left, right)
497
+ SAME_OBJECT.bind_call(left, right)
498
+ rescue StandardError
499
+ false
500
+ end
501
+
502
+ # Pairs for `name` on `klass` and on each of its ancestors. Rails appends
503
+ # one reloaded callback onto descendants of that class, and only onto
504
+ # this chain name. A pair from any other chain must not drop an entry
505
+ # here.
506
+ #
507
+ # @param klass [Class] the class whose chain is being rebuilt.
508
+ # @param name [Symbol] the chain name, such as `:save`.
509
+ # @param by_chain [Hash{Array(Class, Symbol) => Array<Array(Object, Object)>}]
510
+ # see {restore}.
511
+ # @return [Hash{Object => Object}] each old entry to its new entry.
512
+ def self.swap_for(klass, name, by_chain)
513
+ swap = {}.compare_by_identity
514
+ owners(klass).each do |owner|
515
+ by_chain[[owner, name]]&.each { |old_entry, new_entry| swap[old_entry] = new_entry }
516
+ end
517
+ swap
518
+ end
519
+
520
+ # @param klass [Class]
521
+ # @return [Array<Class>] `klass` and its ancestors, furthest first.
522
+ def self.owners(klass)
523
+ owners = []
524
+ current = klass
525
+ while current.is_a?(Class)
526
+ owners << current
527
+ current = current.superclass
528
+ end
529
+ owners.reverse
530
+ end
531
+
532
+ # @param entry [Object] an old ActiveSupport callback.
533
+ # @param other [Object] a new one.
534
+ # @return [Boolean] true when Rails drops `entry` on appending `other`:
535
+ # both name the same method, with the same kind.
536
+ def self.displaced?(entry, other)
537
+ entry.filter.is_a?(Symbol) && entry.kind == other.kind && entry.filter == other.filter
279
538
  end
280
539
 
281
540
  # True when two entries come from the same macro call with the same
@@ -361,7 +620,8 @@ module Mutineer
361
620
  # @return [Boolean] true when the instance variables both have are
362
621
  # equivalent. One of them can hold a value the other has not cached
363
622
  # yet, such as a Rails reflection's memoized lookups, so the others are
364
- # skipped.
623
+ # skipped. No shared variables is not a match: a symbol or a number
624
+ # has none, and those compare by value.
365
625
  def self.same_state?(left, right, same)
366
626
  names = left.instance_variables & right.instance_variables
367
627
  return false if names.empty?
@@ -374,9 +634,14 @@ module Mutineer
374
634
  # @param reloaded [Array<String>] real paths of the source and mutant files.
375
635
  # @param same [Proc] compares two values one level deeper.
376
636
  # @return [Boolean] true for blocks from the same place in the code that
377
- # captured equivalent values. A block with no source, such as
378
- # `&:save` or `method(:x).to_proc`, matches only itself.
637
+ # captured equivalent values. A `&:name` proc has no source and
638
+ # captures nothing; it matches another for the same symbol (#413).
639
+ # `method(:x).to_proc` still matches only through its source and the
640
+ # receiver it captured.
379
641
  def self.same_block?(left, right, reloaded, same)
642
+ left_symbol = symbol_proc_text(left)
643
+ right_symbol = symbol_proc_text(right)
644
+ return left_symbol == right_symbol if left_symbol || right_symbol
380
645
  return false unless left.source_location && right.source_location
381
646
 
382
647
  block_key(left, reloaded) == block_key(right, reloaded) && same_captures?(left, right, same)
@@ -411,35 +676,191 @@ module Mutineer
411
676
 
412
677
  # Where a block sits in the code: the file and Ruby's label for it, such
413
678
  # as "block in <class:Parcel>", plus the line for a file the load did
414
- # not run. The source file and the mutant file count as one file, and
415
- # their lines are left out, because a mutant can move lines.
679
+ # not run, plus the block's code ({.block_code}). The source file and
680
+ # the mutant file count as one file, and their lines are left out,
681
+ # because a mutant can move lines. Two blocks that share a label and
682
+ # differ in code are different callbacks (#413). A `&:name` proc is
683
+ # the symbol Ruby prints for it, so `:normalize` does not share a key
684
+ # with `:touch`.
416
685
  #
417
686
  # @param block [Proc]
418
687
  # @param reloaded [Array<String>] real paths of the source and mutant files.
688
+ # @param code [Boolean] when false, the compiled body is left out. A
689
+ # mutant of that body then shares the key of the block it replaced.
419
690
  # @return [Array]
420
- def self.block_key(block, reloaded)
691
+ def self.block_key(block, reloaded, code: true)
692
+ symbol = symbol_proc_text(block)
693
+ return [:symbol, symbol] if symbol
694
+
421
695
  file, line = block.source_location
422
696
  # CRuby only; elsewhere blocks match by file and line alone.
423
697
  label = RubyVM::InstructionSequence.of(block)&.label if defined?(::RubyVM::InstructionSequence)
424
- reloaded.include?(file && real_path(file)) ? [:reloaded, label] : [file, line, label]
698
+ compiled = block_code(block, reloaded) if code
699
+ reloaded.include?(file && real_path(file)) ? [:reloaded, label, compiled] : [file, line, label, compiled]
700
+ end
701
+
702
+ # CRuby prints `Symbol#to_proc` as `#<Proc:0xaddr(&:name) (lambda)>`.
703
+ # The text after `&` is the symbol's own inspect. The cache holds 67
704
+ # of these procs, so a later `&:other` can make the next `&:name` a
705
+ # new object; the text still matches (#413).
706
+ SYMBOL_PROC = /\A#<Proc:0x[0-9a-fA-F]+\(&(.+)\) \(lambda\)>\z/
707
+
708
+ # @param block [Proc]
709
+ # @return [String, nil] the symbol text, such as `":normalize"`, or nil
710
+ # when `block` is not a `Symbol#to_proc`.
711
+ def self.symbol_proc_text(block)
712
+ SYMBOL_PROC.match(block.to_s)&.[](1) if block.is_a?(::Proc)
713
+ end
714
+
715
+ # Magic `InstructionSequence#to_a` starts with. A different format has
716
+ # no code identity, so the block matches by file and label only.
717
+ ISEQ_FORMAT = "YARVInstructionSequence/SimpleDataFormat"
718
+
719
+ # Fields of an instruction sequence that stay the same when the same
720
+ # block is compiled again. Node ids and code locations change on every
721
+ # load, so they are left out.
722
+ ISEQ_MISC_KEYS = %i[arg_size local_size stack_max parser].freeze
723
+
724
+ # The block's code, with locations removed, or nil when Ruby cannot
725
+ # show it. A value the block captured is not part of the code, so a
726
+ # new object, a time or a random value still pairs (#413). `__FILE__`
727
+ # is the source path in one load and the mutant path in the other;
728
+ # both become the same placeholder. A `rescue` or `ensure` keeps a
729
+ # nested sequence in the catch table; that sequence is normalized
730
+ # too, or the two copies would not pair.
731
+ #
732
+ # @param block [Proc]
733
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
734
+ # @return [Array, nil]
735
+ def self.block_code(block, reloaded)
736
+ return unless defined?(::RubyVM::InstructionSequence)
737
+
738
+ iseq = ::RubyVM::InstructionSequence.of(block)
739
+ return unless iseq
740
+
741
+ data = iseq.to_a
742
+ return unless data.is_a?(Array) && data[0] == ISEQ_FORMAT && data[4].is_a?(Hash)
743
+
744
+ file, = block.source_location
745
+ paths = [file, *reloaded].compact.flat_map { |path| [path, real_path(path)] }.uniq
746
+ code_identity(data, paths)
747
+ rescue StandardError
748
+ nil
749
+ end
750
+
751
+ # @param data [Array] one `InstructionSequence#to_a` array.
752
+ # @param paths [Array<String>] paths to treat as the reloaded file.
753
+ # @return [Array]
754
+ def self.code_identity(data, paths)
755
+ misc = ISEQ_MISC_KEYS.to_h { |key| [key, data[4][key]] }
756
+ [data[5], misc, data[9], data[10], data[11], strip_code(data[12], paths), strip_code(data[13], paths)]
757
+ end
758
+
759
+ # @param node [Object] one node of an instruction sequence.
760
+ # @param paths [Array<String>] paths to treat as the reloaded file.
761
+ # @return [Object] `node` with line numbers and those paths removed.
762
+ # A bare integer in an instruction list is a line number, including
763
+ # one in the middle of a `rescue` or `ensure` body. An integer that
764
+ # is an operand, such as `[:getlocal_WC_0, 3]`, stays.
765
+ def self.strip_code(node, paths)
766
+ case node
767
+ when Array
768
+ if iseq_node?(node)
769
+ code_identity(node, paths)
770
+ elsif instruction_body?(node)
771
+ node.filter_map { |child| strip_code(child, paths) unless child.is_a?(Integer) }
772
+ else
773
+ node.map { |child| strip_code(child, paths) }
774
+ end
775
+ when String
776
+ paths.include?(node) ? :reloaded_path : node
777
+ when Hash
778
+ node.to_h { |key, value| [strip_code(key, paths), strip_code(value, paths)] }
779
+ else
780
+ node
781
+ end
782
+ end
783
+
784
+ # A real instruction sequence, not an array a callback happened to
785
+ # build. The magic string alone is not enough: the node also has the
786
+ # misc hash, a label, and a bytecode array. Anything else stays an
787
+ # array, so it cannot blank the block's code identity.
788
+ #
789
+ # @param node [Array]
790
+ # @return [Boolean]
791
+ def self.iseq_node?(node)
792
+ node[0] == ISEQ_FORMAT && node[4].is_a?(Hash) && node[5].is_a?(String) && node[13].is_a?(Array)
793
+ end
794
+
795
+ # @param node [Array] one array from an instruction sequence.
796
+ # @return [Boolean] true when `node` is a list of instructions rather
797
+ # than one instruction's operands.
798
+ def self.instruction_body?(node)
799
+ node.any? { |child| child.is_a?(Symbol) && child.to_s.start_with?("RUBY_EVENT_") }
425
800
  end
426
801
 
427
802
  # What two entries share when they are copies of one macro call, apart
428
- # from its arguments. A callback that names a method matches the same
429
- # name. A block or lambda matches by {.block_key}. A validator matches by
430
- # class and attributes.
803
+ # from values a mutant can change. A callback that names a method
804
+ # matches the same name. A block or lambda matches by {.block_key}.
805
+ # A validator matches by class, attributes and option names, not option
806
+ # values. `if:` and `unless:` are part of the key, so the same filter
807
+ # under a different condition is a different callback (#413).
431
808
  #
432
809
  # @param callback [Object] an ActiveSupport callback.
433
810
  # @param reloaded [Array<String>] real paths of the source and mutant files.
811
+ # @param code [Boolean] see {block_key}.
434
812
  # @return [Array]
435
- def self.key(callback, reloaded)
813
+ def self.key(callback, reloaded, code: true)
436
814
  filter = callback.filter
437
815
  filter_key = case filter
438
816
  when Symbol, Module then filter
439
- when Proc then block_key(filter, reloaded)
440
- else [filter.class, filter.respond_to?(:attributes) ? filter.attributes : nil]
817
+ when Proc then block_key(filter, reloaded, code: code)
818
+ else object_key(filter)
441
819
  end
442
- [callback.kind, filter_key]
820
+ [callback.kind, filter_key, condition_key(callback, reloaded, code: code)]
821
+ end
822
+
823
+ # @param filter [Object] a callback filter that is not a symbol, module or proc.
824
+ # @return [Array] its class, attributes and option names.
825
+ def self.object_key(filter)
826
+ attributes = filter.attributes if filter.respond_to?(:attributes)
827
+ shape = option_shape(filter.options) if filter.respond_to?(:options)
828
+ [filter.class, attributes, shape]
829
+ end
830
+
831
+ # Option names, nested, without the values. A method the mutant changed
832
+ # can return a different value; the names in the class body stay.
833
+ #
834
+ # @param value [Object] a validator's options, or a nested value.
835
+ # @return [Array, nil] nil when `value` is not a Hash.
836
+ def self.option_shape(value)
837
+ return unless value.is_a?(Hash)
838
+
839
+ value.keys.sort_by(&:to_s).map { |key| [key, option_shape(value[key])] }
840
+ end
841
+
842
+ # @param callback [Object] an ActiveSupport callback.
843
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
844
+ # @param code [Boolean] see {block_key}.
845
+ # @return [Array] its `if:` and `unless:` conditions, as keys. A proc
846
+ # condition uses {.block_key}, so a captured value that changes on
847
+ # each load does not hide the repeat.
848
+ def self.condition_key(callback, reloaded, code: true)
849
+ conditions(callback).map do |list|
850
+ Array(list).map { |item| condition_item_key(item, reloaded, code: code) }
851
+ end
852
+ end
853
+
854
+ # @param item [Object] one `if:` or `unless:` condition.
855
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
856
+ # @param code [Boolean] see {block_key}.
857
+ # @return [Object]
858
+ def self.condition_item_key(item, reloaded, code: true)
859
+ case item
860
+ when Proc then block_key(item, reloaded, code: code)
861
+ when Symbol, Module, Numeric, String, TrueClass, FalseClass, NilClass then item
862
+ else item.class
863
+ end
443
864
  end
444
865
 
445
866
  # @param list [Array]