mutineer 1.8.0 → 1.8.2

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.
@@ -0,0 +1,564 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set" # loads on its own only from Ruby 3.2; the daemon runs on the app's Ruby
4
+
5
+ module Mutineer
6
+ # Lets the reload strategy run a Rails class body a second time.
7
+ #
8
+ # Reload `load`s the mutant file over a class that is already defined, so the
9
+ # class body runs again. Two Rails macros refuse that:
10
+ #
11
+ # - `enum` raises because the enum's methods already exist (#378).
12
+ # - `skip_callback` (and `skip_before_action`, which calls it) raises because
13
+ # the first run already removed the callback (#384).
14
+ #
15
+ # While {.during_load} runs, an `enum` the class had before the load skips
16
+ # its conflict check, and `skip_callback` does not raise for a callback the
17
+ # first run already removed (see {CallbackReskipping}).
18
+ # The original file ran the same macros once with the full checks, so the
19
+ # second run repeats work Rails already accepted. Outside the load, for
20
+ # example while the tests run, both macros check as usual.
21
+ #
22
+ # {.during_load} also loads the original file first when the app autoloads it
23
+ # and nothing has loaded it yet (#383). Ruby would do that anyway when the
24
+ # mutant reopens the constant, but inside the load, after
25
+ # {ConcernBlocks.forget}, so the concern's original `included` block would
26
+ # come back and the mutant's block would raise `MultipleIncludedBlocks`.
27
+ #
28
+ # A second run of the class body also adds each block callback, lambda
29
+ # callback and validator again. Rails replaces a repeated callback only when
30
+ # it names a method. So {.during_load} removes the original's copy of each
31
+ # one the load added again (see {RepeatedCallbacks}, #390). Otherwise a
32
+ # `validates` or a `before_save do ... end` would run twice in every mutant
33
+ # of the file, and a test that counts errors or side effects would give a
34
+ # false kill.
35
+ #
36
+ # Call it only in a forked child that loads the mutant: it prepends onto
37
+ # Rails modules in that process. Without Rails it only runs the block.
38
+ #
39
+ # Stdlib-only, so the app-side daemon can load it.
40
+ module ReloadGuards
41
+ # Skips the conflict check for an enum the class defined before the mutant
42
+ # load began, during that load only.
43
+ module EnumRedeclaration
44
+ private
45
+
46
+ # @param enum_name [Symbol, String] the enum being declared.
47
+ # @return [void]
48
+ def detect_enum_conflict!(enum_name, *)
49
+ return if ReloadGuards.loading? && ReloadGuards.enum_existed?(self, enum_name)
50
+
51
+ super
52
+ end
53
+ end
54
+
55
+ # Makes a repeated skip a no-op, during a mutant load only. The class body
56
+ # outside methods is the original's, and the original's first run already
57
+ # removed the callback from this class, so a skip there is a repeat. It
58
+ # counts as one when each filter is a method the class has (a callback the
59
+ # parent or an included concern installed) or a callback the parent still
60
+ # has. A skip that names neither keeps Rails' check and still raises
61
+ # (scored `error`), so a typo is not hidden.
62
+ module CallbackReskipping
63
+ # @param name [Symbol] the callback chain, such as `:save`.
64
+ # @param filter_list [Array] the kind, the filters, and options.
65
+ # @return [void]
66
+ def skip_callback(name, *filter_list, &block)
67
+ return super unless ReloadGuards.loading?
68
+
69
+ options = filter_list.last.is_a?(Hash) ? filter_list.pop : {}
70
+ options = { raise: false }.merge(options) if ReloadGuards.repeated_skip?(self, name, *filter_list)
71
+ super(name, *filter_list, options, &block)
72
+ end
73
+ end
74
+
75
+ # True while a mutant file loads.
76
+ #
77
+ # @return [Boolean]
78
+ def self.loading?
79
+ @loading == true
80
+ end
81
+
82
+ # Loads the original file if the app autoloads it, then runs the block that
83
+ # loads the mutant with the guards on. After the block, removes the
84
+ # original's copy of each callback and validator the load added again.
85
+ #
86
+ # @param source_file [String] the file the mutant replaces.
87
+ # @param mutant_path [String, nil] the file the block loads.
88
+ # @yield the mutant's load.
89
+ # @return [Object] what the block returns.
90
+ def self.during_load(source_file, mutant_path = nil)
91
+ load_original(source_file)
92
+ install!
93
+ before = RepeatedCallbacks.snapshot
94
+ @enums_before_load = {}
95
+ @loading = true
96
+ result = yield
97
+ RepeatedCallbacks.remove(before, [source_file, mutant_path].compact)
98
+ result
99
+ ensure
100
+ @loading = false
101
+ end
102
+
103
+ # Puts back one copy of each callback and validator that a second run of a
104
+ # class body added again (#390).
105
+ #
106
+ # Each class body the load runs again appends its block callbacks, lambda
107
+ # callbacks and validators a second time. Rails drops the old copy only of
108
+ # a callback that names a method, and appends the new one at the end. For
109
+ # each class whose chains the load changed, {.replacements} pairs each
110
+ # entry the load added to the class's own chain with the class's own entry
111
+ # from before the load that it repeats. {.restore} then puts each new entry
112
+ # where its old entry was and drops the old one, in every changed chain and
113
+ # in `_validators`. So each macro runs once, in its original order.
114
+ #
115
+ # An included concern adds its entries to the class, and the reload does
116
+ # not run its block again, so it adds no new copy of them. Its entries can
117
+ # look like the class body's own, so entries pair first with old entries
118
+ # that are the same ({.same?}): a validator with equivalent options, a
119
+ # lambda that captured equivalent values, a block from the same place in
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.
125
+ #
126
+ # ActiveSupport gives a class a new chain object each time a callback is
127
+ # added or skipped, so a class whose chain objects all stay the same was
128
+ # not changed by the load and is skipped.
129
+ module RepeatedCallbacks
130
+ # Every loaded class with ActiveSupport callbacks, with its chain objects.
131
+ #
132
+ # @return [Hash{Class => Hash{Symbol => Object}}] empty without ActiveSupport.
133
+ def self.snapshot
134
+ return {} unless ReloadGuards.callbacks_loaded?
135
+
136
+ # Asks the module: a class can define its own `<` (`extend Comparable`).
137
+ classes = ObjectSpace.each_object(Class).select { |klass| ::ActiveSupport::Callbacks > klass }
138
+ classes.to_h { |klass| [klass, klass.__callbacks.dup] }
139
+ end
140
+
141
+ # Keeps one copy of each entry the load added again.
142
+ #
143
+ # @param before [Hash] the {.snapshot} from before the load.
144
+ # @param files [Array<String>] the source file and the mutant file. A
145
+ # block from either one counts as a block from the same file.
146
+ # @return [void]
147
+ def self.remove(before, files)
148
+ changed = before.keys.reject { |klass| same_chains?(klass.__callbacks, before[klass]) }
149
+ return if changed.empty?
150
+
151
+ reloaded = files.map { |file| real_path(file) }
152
+ # Each class's entries before the load, copied only for the classes asked.
153
+ 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)
160
+ end
161
+
162
+ # @param now [Hash{Symbol => Object}] a class's chains after the load.
163
+ # @param before [Hash{Symbol => Object}] its chains before the load.
164
+ # @return [Boolean] true when every chain is the same object as before.
165
+ def self.same_chains?(now, before)
166
+ now.size == before.size && now.all? { |name, chain| before[name].equal?(chain) }
167
+ end
168
+
169
+ # Pairs each entry the load added to `klass`'s own chains with the old
170
+ # entry it repeats: first with old entries that are the same
171
+ # ({.exact_pairs}), then by key.
172
+ #
173
+ # @param klass [Class] a class whose chains the load changed.
174
+ # @param old [Hash{Class => Hash{Symbol => Array}}] each class's entries before the load.
175
+ # @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.
177
+ def self.replacements(klass, old, reloaded)
178
+ parent = klass.superclass
179
+ parent_now = parent.respond_to?(:__callbacks) ? parent.__callbacks : {}
180
+ parent_old = old[parent]
181
+ klass.__callbacks.flat_map do |name, chain|
182
+ before = old[klass].fetch(name, [])
183
+ added = without(without(chain.to_a, before), parent_now[name].to_a)
184
+ next [] if added.empty?
185
+
186
+ pool = without(before, parent_old.fetch(name, []))
187
+ exact = exact_pairs(added, pool, reloaded)
188
+ exact + by_key(without(added, exact.map(&:last)), pool, reloaded)
189
+ end
190
+ end
191
+
192
+ # Pairs new entries with the old entries that are the same ({.same?}),
193
+ # in order, but only when there are as many old ones as new ones. The
194
+ # class body can add a callback once only (`@done ||= ...`), or a
195
+ # concern can hold a look-alike, so a different count keeps both
196
+ # copies, as before #390. Removes the paired old entries from `pool`.
197
+ #
198
+ # @param added [Array] the entries the load added.
199
+ # @param pool [Array] the old entries that could be repeated.
200
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
201
+ # @return [Array<Array(Object, Object)>] each old entry and the new entry that replaces it.
202
+ def self.exact_pairs(added, pool, reloaded)
203
+ seen = {}
204
+ rest = added
205
+ pairs = []
206
+ until rest.empty?
207
+ first = rest.first
208
+ group, rest = rest.partition { |entry| same?(entry, first, reloaded, seen) }
209
+ olds = pool.select { |entry| same?(entry, first, reloaded, seen) }
210
+ next unless olds.size == group.size
211
+
212
+ pool.replace(without(pool, olds))
213
+ pairs.concat(olds.zip(group))
214
+ end
215
+ pairs
216
+ end
217
+
218
+ # Pairs the entries no exact match took, by {.key}, but only for a key
219
+ # with as many old entries left as new ones. More old entries means an
220
+ # included concern added some of them, and a guess could drop the
221
+ # concern's entry. Those keep both copies, as before #390.
222
+ #
223
+ # @param added [Array] new entries with no exact match.
224
+ # @param pool [Array] old entries no exact match took.
225
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
226
+ # @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|
230
+ old_entries = old_by_key.fetch(k, [])
231
+ old_entries.size == new_entries.size ? old_entries.zip(new_entries) : []
232
+ end
233
+ end
234
+
235
+ # Rebuilds each changed chain in its order from before the load, with
236
+ # each old entry swapped for its new entry, and entries the load added
237
+ # unpaired at the end. Swaps each validator in `_validators` the same way.
238
+ #
239
+ # @param changed [Array<Class>] the classes whose chains the load changed.
240
+ # @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.
242
+ # @return [void]
243
+ def self.restore(changed, old, swap)
244
+ seen = {}.compare_by_identity
245
+ changed.each do |klass|
246
+ klass.__callbacks.each do |name, chain|
247
+ next if seen[chain]
248
+
249
+ seen[chain] = true
250
+ now = chain.to_a
251
+ order = reorder(old[klass].fetch(name, []), now, swap)
252
+ next if order.size == now.size && order.zip(now).all? { |a, b| a.equal?(b) }
253
+
254
+ chain.clear
255
+ chain.append(*order)
256
+ end
257
+ end
258
+ # A validator is the filter of its `validate` callback.
259
+ validators = {}.compare_by_identity
260
+ swap.each { |old_entry, new_entry| validators[old_entry.filter] = new_entry.filter }
261
+ lists = changed.select { |klass| klass.respond_to?(:_validators) }.flat_map { |klass| klass._validators.values }
262
+ lists.uniq(&:object_id).each do |list|
263
+ list.map! { |validator| validators.fetch(validator, validator) }
264
+ list.uniq!(&:object_id)
265
+ end
266
+ end
267
+
268
+ # @param old [Array] the chain's entries before the load.
269
+ # @param now [Array] its entries after the load.
270
+ # @param swap [Hash{Object => Object}] each old entry to its new entry.
271
+ # @return [Array] `old` with each new entry swapped in, then the other new entries.
272
+ def self.reorder(old, now, swap)
273
+ present = identity_set(now)
274
+ order = old.filter_map do |entry|
275
+ new_entry = swap[entry]
276
+ new_entry && present.include?(new_entry) ? new_entry : (entry if present.include?(entry))
277
+ end
278
+ order + without(now, order).reject { |entry| swap.key?(entry) }
279
+ end
280
+
281
+ # True when two entries come from the same macro call with the same
282
+ # arguments: the same kind, and filters and `if:`/`unless:` conditions
283
+ # that are {.equivalent?}. A concern and the class body can add one
284
+ # callback class under different conditions; those are different entries.
285
+ #
286
+ # @param old_entry [Object] an ActiveSupport callback from before the load.
287
+ # @param new_entry [Object] one the load added.
288
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
289
+ # @param seen [Hash] answers already found ({.equivalent?}).
290
+ # @return [Boolean]
291
+ def self.same?(old_entry, new_entry, reloaded, seen = {})
292
+ old_entry.kind == new_entry.kind && equivalent?(old_entry.filter, new_entry.filter, reloaded, 0, seen) &&
293
+ equivalent?(conditions(old_entry), conditions(new_entry), reloaded, 0, seen)
294
+ end
295
+
296
+ # @param callback [Object] an ActiveSupport callback.
297
+ # @return [Array] its `if:` and `unless:` conditions, as ActiveSupport
298
+ # keeps them (empty lists when it keeps none).
299
+ def self.conditions(callback)
300
+ %i[@if @unless].map { |name| callback.instance_variable_get(name) || [] }
301
+ end
302
+
303
+ # How deep {.equivalent?} looks into objects before it gives up.
304
+ EQUIVALENT_DEPTH = 6
305
+
306
+ # True when two values would make the same callback or validator. Each
307
+ # run of a class body builds new blocks, so a block matches another
308
+ # from the same place in the code ({.block_key}) that captured
309
+ # equivalent values. Hashes and arrays match element by element. A class
310
+ # or module matches only itself. Other objects, such as a validator or a
311
+ # callback object, match when they are equal, or of one class with
312
+ # equivalent instance variables. Past {EQUIVALENT_DEPTH} levels nothing
313
+ # matches, so a deep object pairs only by key ({.by_key}).
314
+ #
315
+ # A pair already being compared, such as a lambda that captures itself
316
+ # (Rails's `belongs_to touch:` lambda does), counts as a match: nothing
317
+ # else in it differs. Each answer is kept in `seen`, so a value many
318
+ # blocks capture is compared once. A value that cannot be compared, such
319
+ # as the proxy `with_options` yields, matches nothing, so both copies stay.
320
+ #
321
+ # @param left [Object]
322
+ # @param right [Object]
323
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
324
+ # @param depth [Integer] levels already compared.
325
+ # @param seen [Hash{Array => Boolean, Symbol}] each pair's answer, or
326
+ # `:open` while it is being compared, by object id.
327
+ # @return [Boolean]
328
+ def self.equivalent?(left, right, reloaded, depth = 0, seen = {})
329
+ return true if SAME_OBJECT.bind_call(left, right)
330
+
331
+ pair = [left.__id__, right.__id__]
332
+ return seen[pair] != false if seen.key?(pair)
333
+ return false if depth > EQUIVALENT_DEPTH || !INSTANCE_OF.bind_call(left, KERNEL_CLASS.bind_call(right))
334
+
335
+ seen[pair] = :open
336
+ same = ->(a, b) { equivalent?(a, b, reloaded, depth + 1, seen) }
337
+ seen[pair] = case left
338
+ when Module then false
339
+ when Proc then same_block?(left, right, reloaded, same)
340
+ when Hash then left.keys == right.keys && left.all? { |k, v| same.(v, right[k]) }
341
+ when Array then left.size == right.size && left.zip(right).all? { |a, b| same.(a, b) }
342
+ else left == right || same_state?(left, right, same)
343
+ end
344
+ rescue StandardError
345
+ seen[pair] = false if pair
346
+ false
347
+ end
348
+
349
+ # `BasicObject#equal?`, unbound, so a proxy cannot answer for its target.
350
+ SAME_OBJECT = ::BasicObject.instance_method(:equal?)
351
+
352
+ # `Kernel#instance_of?`, unbound, for the same reason.
353
+ INSTANCE_OF = ::Kernel.instance_method(:instance_of?)
354
+
355
+ # `Kernel#class`, unbound, for the same reason.
356
+ KERNEL_CLASS = ::Kernel.instance_method(:class)
357
+
358
+ # @param left [Object]
359
+ # @param right [Object] an object of the same class.
360
+ # @param same [Proc] compares two values one level deeper.
361
+ # @return [Boolean] true when the instance variables both have are
362
+ # equivalent. One of them can hold a value the other has not cached
363
+ # yet, such as a Rails reflection's memoized lookups, so the others are
364
+ # skipped.
365
+ def self.same_state?(left, right, same)
366
+ names = left.instance_variables & right.instance_variables
367
+ return false if names.empty?
368
+
369
+ names.all? { |name| same.(left.instance_variable_get(name), right.instance_variable_get(name)) }
370
+ end
371
+
372
+ # @param left [Proc]
373
+ # @param right [Proc]
374
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
375
+ # @param same [Proc] compares two values one level deeper.
376
+ # @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.
379
+ def self.same_block?(left, right, reloaded, same)
380
+ return false unless left.source_location && right.source_location
381
+
382
+ block_key(left, reloaded) == block_key(right, reloaded) && same_captures?(left, right, same)
383
+ end
384
+
385
+ # @param left [Proc]
386
+ # @param right [Proc] a block from the same place in the code.
387
+ # @param same [Proc] compares two values one level deeper.
388
+ # @return [Boolean] true when both captured equivalent values. The list
389
+ # itself is not a level, so blocks that capture each other in a loop
390
+ # reach the pair already being compared before the depth limit.
391
+ def self.same_captures?(left, right, same)
392
+ mine = captured(left)
393
+ theirs = captured(right)
394
+ return false unless mine && theirs && mine.size == theirs.size
395
+
396
+ mine.zip(theirs).all? { |a, b| same.(a, b) }
397
+ end
398
+
399
+ # The block's receiver and the values it captured, or nil when Ruby
400
+ # cannot show them. The receiver tells apart `method(:x).to_proc` of
401
+ # two objects; a class body's blocks share the class as receiver.
402
+ #
403
+ # @param block [Proc]
404
+ # @return [Array, nil]
405
+ def self.captured(block)
406
+ scope = block.binding
407
+ [scope.receiver] + scope.local_variables.map { |name| scope.local_variable_get(name) }
408
+ rescue ArgumentError
409
+ nil
410
+ end
411
+
412
+ # Where a block sits in the code: the file and Ruby's label for it, such
413
+ # 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.
416
+ #
417
+ # @param block [Proc]
418
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
419
+ # @return [Array]
420
+ def self.block_key(block, reloaded)
421
+ file, line = block.source_location
422
+ # CRuby only; elsewhere blocks match by file and line alone.
423
+ label = RubyVM::InstructionSequence.of(block)&.label if defined?(::RubyVM::InstructionSequence)
424
+ reloaded.include?(file && real_path(file)) ? [:reloaded, label] : [file, line, label]
425
+ end
426
+
427
+ # 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.
431
+ #
432
+ # @param callback [Object] an ActiveSupport callback.
433
+ # @param reloaded [Array<String>] real paths of the source and mutant files.
434
+ # @return [Array]
435
+ def self.key(callback, reloaded)
436
+ filter = callback.filter
437
+ filter_key = case filter
438
+ when Symbol, Module then filter
439
+ when Proc then block_key(filter, reloaded)
440
+ else [filter.class, filter.respond_to?(:attributes) ? filter.attributes : nil]
441
+ end
442
+ [callback.kind, filter_key]
443
+ end
444
+
445
+ # @param list [Array]
446
+ # @param others [Array]
447
+ # @return [Array] the entries of `list` that are not in `others`, by identity.
448
+ def self.without(list, others)
449
+ others = identity_set(others)
450
+ list.reject { |entry| others.include?(entry) }
451
+ end
452
+
453
+ # A set that compares by identity. Building it first keeps entries that
454
+ # are equal by value but not the same object.
455
+ #
456
+ # @param list [Array]
457
+ # @return [Set]
458
+ def self.identity_set(list)
459
+ Set.new.compare_by_identity.merge(list)
460
+ end
461
+
462
+ # @param file [String]
463
+ # @return [String] the real path, so a symlinked path still matches.
464
+ def self.real_path(file)
465
+ (@real_paths ||= {})[file] ||= begin
466
+ File.realpath(file)
467
+ rescue SystemCallError
468
+ File.expand_path(file)
469
+ end
470
+ end
471
+ end
472
+
473
+ # Whether `klass` had the enum before this load. Rails records a new enum
474
+ # after its first conflict check, so the answer is read once, at the first
475
+ # check of each enum, and kept for the rest of the load.
476
+ #
477
+ # @param klass [Class] the model declaring the enum.
478
+ # @param enum_name [Symbol, String] the enum being declared.
479
+ # @return [Boolean]
480
+ def self.enum_existed?(klass, enum_name)
481
+ key = [klass, enum_name.to_s]
482
+ @enums_before_load.fetch(key) { @enums_before_load[key] = klass.defined_enums.key?(key.last) }
483
+ end
484
+
485
+ # The callback kinds `skip_callback` takes before its filters.
486
+ CALLBACK_KINDS = %i[before after around].freeze
487
+
488
+ # True when every filter of this skip is a method `klass` has, or a callback
489
+ # of that kind on chain `name` that the superclass still has. The kind is
490
+ # optional and defaults to `:before`, as in Rails. With no filters there is
491
+ # nothing to compare, so false.
492
+ #
493
+ # @param klass [Class] the class calling `skip_callback`.
494
+ # @param name [Symbol] the callback chain, such as `:save`.
495
+ # @param args [Array] the optional kind, then the filters.
496
+ # @return [Boolean]
497
+ def self.repeated_skip?(klass, name, *args)
498
+ kind = CALLBACK_KINDS.include?(args.first) ? args.shift : :before
499
+ return false if args.empty?
500
+
501
+ parent = klass.superclass
502
+ chain = parent.respond_to?(:__callbacks) ? parent.__callbacks[name] : nil
503
+ args.all? do |filter|
504
+ (filter.is_a?(Symbol) && (klass.method_defined?(filter) || klass.private_method_defined?(filter))) ||
505
+ chain&.any? { |callback| callback.kind == kind && callback.filter == filter }
506
+ end
507
+ end
508
+
509
+ # Prepends the guards once, onto the Rails modules this process has loaded.
510
+ # A module Rails only set to autoload stays unloaded, so the mutant run
511
+ # sees the same Ruby as the clean run.
512
+ #
513
+ # @return [void]
514
+ def self.install!
515
+ if defined?(::ActiveRecord::Enum) && !::ActiveRecord.autoload?(:Enum)
516
+ prepend_once(::ActiveRecord::Enum, EnumRedeclaration)
517
+ end
518
+ return unless callbacks_loaded?
519
+
520
+ prepend_once(::ActiveSupport::Callbacks::ClassMethods, CallbackReskipping)
521
+ end
522
+
523
+ # `defined?` does not load an autoloaded constant, but using it does.
524
+ #
525
+ # @return [Boolean] true when `ActiveSupport::Callbacks` is loaded, not
526
+ # only set to autoload.
527
+ def self.callbacks_loaded?
528
+ !!defined?(::ActiveSupport::Callbacks) && !::ActiveSupport.autoload?(:Callbacks)
529
+ end
530
+
531
+ # @param target [Module] the Rails module.
532
+ # @param guard [Module] the guard to put in front of it.
533
+ # @return [void]
534
+ def self.prepend_once(target, guard)
535
+ target.prepend(guard) unless target.ancestors.include?(guard)
536
+ end
537
+
538
+ # Asks each Rails autoloader for the constant `source_file` defines. A
539
+ # loader that does not manage the file raises `Zeitwerk::Error`; the next
540
+ # one is asked. A managed file that does not define the constant Zeitwerk
541
+ # expects raises `Zeitwerk::NameError`, a `NameError`; the mutant load goes
542
+ # on as before. A file already loaded only costs a constant lookup.
543
+ # Autoload roots are real paths, so the loaders get the real path.
544
+ #
545
+ # @param source_file [String] the file the mutant replaces.
546
+ # @return [void]
547
+ def self.load_original(source_file)
548
+ return unless defined?(::Rails) && ::Rails.respond_to?(:autoloaders) && defined?(::Zeitwerk::Error)
549
+ # A library can load railties with no Rails app; `Rails.autoloaders` then raises.
550
+ return unless ::Rails.respond_to?(:application) && ::Rails.application
551
+
552
+ ::Rails.autoloaders.each do |loader|
553
+ next unless loader.respond_to?(:load_file)
554
+
555
+ begin
556
+ loader.load_file(RepeatedCallbacks.real_path(source_file))
557
+ return
558
+ rescue ::Zeitwerk::Error, ::NameError
559
+ next
560
+ end
561
+ end
562
+ end
563
+ end
564
+ end
@@ -139,7 +139,8 @@ module Mutineer
139
139
  if @agg.skipped_invalid_count.positive?
140
140
  message << " A mutant skipped (invalid) did not re-parse after mutation. If its source file " \
141
141
  "parses on its own, that is a mutineer bug: please report it at " \
142
- "https://github.com/davidteren/mutineer/issues."
142
+ "https://github.com/davidteren/mutineer/issues, with the source file and " \
143
+ "these versions: Ruby #{RUBY_VERSION}, Prism #{Prism::VERSION}."
143
144
  end
144
145
  err.puts message
145
146
  end
@@ -264,7 +264,7 @@ module Mutineer
264
264
  # 3x the clean run, floor 30s, ceiling 300s: a heuristic. The floor covers
265
265
  # a fast suite; the ceiling bounds a hung mutant (infinite loop) so a
266
266
  # handful cannot stall a serial run for ~45min on a slow suite.
267
- smoke_elapsed, check_summary = ExternalBackend.smoke_check!(config.test_command, abs_tests)
267
+ smoke_elapsed, check_summary, random_order = ExternalBackend.smoke_check!(config.test_command, abs_tests)
268
268
  timeout = [[smoke_elapsed * 3, 30].max, 300].min.ceil
269
269
  # #292: the summary check needs a summary line to look for. Without one
270
270
  # on the clean run, keep the exit-code rule and say what that costs.
@@ -274,6 +274,22 @@ module Mutineer
274
274
  "so the crash check is off: an exit 1 counts as a kill even when the suite crashed " \
275
275
  "before its tests ran. Use the default Minitest or RSpec progress output to turn it on."
276
276
  end
277
+ # #338: each mutant run picks its own order, so an order-dependent
278
+ # suite can fail a mutant run for a reason the mutant did not cause.
279
+ if random_order
280
+ runner, seed = random_order
281
+ fix = if runner == :minitest
282
+ "set SEED=#{seed} before the mutineer command (SEED=#{seed} mutineer run ...), " \
283
+ "not inside --test-command"
284
+ else
285
+ # SPEC_OPTS is read after the command line, so it would undo the fix.
286
+ "add --order defined to the rspec command in --test-command and keep " \
287
+ "--order rand out of SPEC_OPTS"
288
+ end
289
+ warn "[mutineer] the clean run of --test-command printed a #{runner == :minitest ? 'Minitest' : 'RSpec'} " \
290
+ "seed (#{seed}) and Mutineer found no fixed seed, so each mutant run may use a new test " \
291
+ "order and an order-dependent suite can give a false kill. To fix the order, #{fix}."
292
+ end
277
293
 
278
294
  results = []
279
295
  progress = Progress.new(jobs.size)
@@ -304,7 +320,7 @@ module Mutineer
304
320
  def self.run_external(subject, mutation, command, abs_tests, timeout:, verbose:, check_summary:)
305
321
  source = File.read(subject.file, encoding: "UTF-8")
306
322
  mutated = mutation.apply(source)
307
- return Result.skipped if Parser.parse_string(mutated).errors.any?
323
+ return Result.skipped if Parser.invalid?(mutated)
308
324
 
309
325
  FileSwap.with(subject.file, mutated) do
310
326
  ExternalBackend.run(command, abs_tests, timeout: timeout, verbose: verbose, check_summary: check_summary)
@@ -365,7 +381,7 @@ module Mutineer
365
381
  mutated = mutation.apply(source)
366
382
 
367
383
  # Validity rule: a mutant that does not re-parse is skipped before forking.
368
- return Result.skipped if Parser.parse_string(mutated).errors.any?
384
+ return Result.skipped if Parser.invalid?(mutated)
369
385
 
370
386
  # Coverage selection (both standalone and boot mode): a mutation on a line
371
387
  # no test exercises is :no_coverage (no fork); otherwise exactly the
@@ -391,8 +407,7 @@ module Mutineer
391
407
  elsif subject&.reload_hook_call
392
408
  # #297: a def in a concern's hook block reaches each class that has it.
393
409
  code, hook_def = JobPlan.hook_mutant(mutated, mutation, subject, source)
394
- Isolation.apply_whole_file(code, source_file)
395
- ConcernBlocks.place_hook_def(source_file: source_file, **hook_def)
410
+ Isolation.apply_whole_file(code, source_file, hook_def: hook_def)
396
411
  else
397
412
  Isolation.apply_whole_file(mutated, source_file)
398
413
  end
@@ -31,7 +31,7 @@ module Mutineer
31
31
  end
32
32
  begin
33
33
  parser.parse!(argv)
34
- rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
34
+ rescue OptionParser::ParseError => e
35
35
  err.puts "mutineer: #{e.message}"
36
36
  return 2
37
37
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Mutineer
4
4
  # Current Mutineer release version.
5
- VERSION = "1.8.0"
5
+ VERSION = "1.8.2"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mutineer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Teren
@@ -109,6 +109,7 @@ files:
109
109
  - lib/mutineer/rails_run_env.rb
110
110
  - lib/mutineer/rails_worker_db.rb
111
111
  - lib/mutineer/redaction.rb
112
+ - lib/mutineer/reload_guards.rb
112
113
  - lib/mutineer/reporter.rb
113
114
  - lib/mutineer/result.rb
114
115
  - lib/mutineer/runner.rb