mutineer 1.8.1 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +733 -0
- data/README.md +85 -50
- data/lib/mutineer/changed_lines.rb +2 -1
- data/lib/mutineer/child_cause.rb +149 -0
- data/lib/mutineer/child_stdout.rb +12 -1
- data/lib/mutineer/cli.rb +540 -59
- data/lib/mutineer/command_redact.rb +38 -0
- data/lib/mutineer/concern_blocks.rb +95 -24
- data/lib/mutineer/config.rb +112 -6
- data/lib/mutineer/coverage_map.rb +1345 -156
- data/lib/mutineer/daemon_backend.rb +41 -18
- data/lib/mutineer/daemon_client.rb +104 -21
- data/lib/mutineer/daemon_server.rb +85 -14
- data/lib/mutineer/database_urls.rb +189 -0
- data/lib/mutineer/external_backend.rb +93 -6
- data/lib/mutineer/fs_path.rb +125 -0
- data/lib/mutineer/isolation.rb +380 -25
- data/lib/mutineer/job_plan.rb +352 -41
- data/lib/mutineer/minitest_integration/file_order.rb +56 -0
- data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
- data/lib/mutineer/minitest_integration.rb +26 -58
- data/lib/mutineer/mutant_id.rb +4 -1
- data/lib/mutineer/mutation.rb +10 -4
- data/lib/mutineer/mutator_registry.rb +6 -3
- data/lib/mutineer/mutators/collection_method.rb +195 -6
- data/lib/mutineer/mutators/literal_mutation.rb +33 -3
- data/lib/mutineer/mutators/range_literal.rb +78 -29
- data/lib/mutineer/mutators/rescue_body.rb +126 -0
- data/lib/mutineer/mutators/return_nil.rb +23 -6
- data/lib/mutineer/output_filter.rb +451 -0
- data/lib/mutineer/pairing.rb +463 -31
- data/lib/mutineer/parser.rb +53 -0
- data/lib/mutineer/project_path.rb +15 -8
- data/lib/mutineer/rails_worker_db.rb +366 -6
- data/lib/mutineer/reach_probe.rb +229 -0
- data/lib/mutineer/redaction.rb +301 -41
- data/lib/mutineer/reload_guards.rb +985 -0
- data/lib/mutineer/reporter.rb +467 -58
- data/lib/mutineer/result.rb +6 -7
- data/lib/mutineer/run_seed.rb +16 -0
- data/lib/mutineer/runner.rb +194 -55
- data/lib/mutineer/source_bytes.rb +187 -0
- data/lib/mutineer/statement_lines.rb +108 -14
- data/lib/mutineer/test_runners/rspec.rb +118 -10
- data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
- data/lib/mutineer/test_runners.rb +3 -2
- data/lib/mutineer/triage.rb +55 -10
- data/lib/mutineer/version.rb +1 -1
- data/lib/mutineer.rb +1 -0
- data/skills/mutineer/SKILL.md +12 -4
- metadata +13 -1
|
@@ -0,0 +1,985 @@
|
|
|
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 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.
|
|
133
|
+
#
|
|
134
|
+
# ActiveSupport gives a class a new chain object each time a callback is
|
|
135
|
+
# added or skipped, so a class whose chain objects all stay the same was
|
|
136
|
+
# not changed by the load and is skipped.
|
|
137
|
+
module RepeatedCallbacks
|
|
138
|
+
# Every loaded class with ActiveSupport callbacks, with its chain objects.
|
|
139
|
+
#
|
|
140
|
+
# @return [Hash{Class => Hash{Symbol => Object}}] empty without ActiveSupport.
|
|
141
|
+
def self.snapshot
|
|
142
|
+
return {} unless ReloadGuards.callbacks_loaded?
|
|
143
|
+
|
|
144
|
+
# Asks the module: a class can define its own `<` (`extend Comparable`).
|
|
145
|
+
classes = ObjectSpace.each_object(Class).select { |klass| ::ActiveSupport::Callbacks > klass }
|
|
146
|
+
classes.to_h { |klass| [klass, klass.__callbacks.dup] }
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Keeps one copy of each entry the load added again.
|
|
150
|
+
#
|
|
151
|
+
# @param before [Hash] the {.snapshot} from before the load.
|
|
152
|
+
# @param files [Array<String>] the source file and the mutant file. A
|
|
153
|
+
# block from either one counts as a block from the same file.
|
|
154
|
+
# @return [void]
|
|
155
|
+
def self.remove(before, files)
|
|
156
|
+
changed = before.keys.reject { |klass| same_chains?(klass.__callbacks, before[klass]) }
|
|
157
|
+
return if changed.empty?
|
|
158
|
+
|
|
159
|
+
reloaded = files.map { |file| real_path(file) }
|
|
160
|
+
# Each class's entries before the load, copied only for the classes asked.
|
|
161
|
+
old = Hash.new { |copies, klass| copies[klass] = before.fetch(klass, {}).transform_values(&:to_a) }
|
|
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?
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# @param now [Hash{Symbol => Object}] a class's chains after the load.
|
|
172
|
+
# @param before [Hash{Symbol => Object}] its chains before the load.
|
|
173
|
+
# @return [Boolean] true when every chain is the same object as before.
|
|
174
|
+
def self.same_chains?(now, before)
|
|
175
|
+
now.size == before.size && now.all? { |name, chain| before[name].equal?(chain) }
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Pairs each entry the load added to `klass`'s own chains with the old
|
|
179
|
+
# entry it repeats: first with old entries that are the same
|
|
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.
|
|
186
|
+
#
|
|
187
|
+
# @param klass [Class] a class whose chains the load changed.
|
|
188
|
+
# @param old [Hash{Class => Hash{Symbol => Array}}] each class's entries before the load.
|
|
189
|
+
# @param reloaded [Array<String>] real paths of the source and mutant files.
|
|
190
|
+
# @return [Hash{Symbol => Array<Array(Object, Object)>}] for each chain
|
|
191
|
+
# name, each old entry and the new entry that replaces it.
|
|
192
|
+
def self.replacements(klass, old, reloaded)
|
|
193
|
+
parent = klass.superclass
|
|
194
|
+
parent_now = parent.respond_to?(:__callbacks) ? parent.__callbacks : {}
|
|
195
|
+
parent_old = old[parent]
|
|
196
|
+
pairs = {}
|
|
197
|
+
klass.__callbacks.each do |name, chain|
|
|
198
|
+
before = old[klass].fetch(name, [])
|
|
199
|
+
added = without(without(chain.to_a, before), parent_now[name].to_a)
|
|
200
|
+
next if added.empty?
|
|
201
|
+
|
|
202
|
+
pool = without(before, parent_old.fetch(name, []))
|
|
203
|
+
exact = exact_pairs(added, 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
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Pairs new entries with the old entries that are the same ({.same?}),
|
|
235
|
+
# in order, but only when there are as many old ones as new ones. The
|
|
236
|
+
# class body can add a callback once only (`@done ||= ...`), or a
|
|
237
|
+
# concern can hold a look-alike, so a different count keeps both
|
|
238
|
+
# copies, as before #390. Removes the paired old entries from `pool`.
|
|
239
|
+
#
|
|
240
|
+
# @param added [Array] the entries the load added.
|
|
241
|
+
# @param pool [Array] the old entries that could be repeated.
|
|
242
|
+
# @param reloaded [Array<String>] real paths of the source and mutant files.
|
|
243
|
+
# @return [Array<Array(Object, Object)>] each old entry and the new entry that replaces it.
|
|
244
|
+
def self.exact_pairs(added, pool, reloaded)
|
|
245
|
+
seen = {}
|
|
246
|
+
rest = added
|
|
247
|
+
pairs = []
|
|
248
|
+
until rest.empty?
|
|
249
|
+
first = rest.first
|
|
250
|
+
group, rest = rest.partition { |entry| same?(entry, first, reloaded, seen) }
|
|
251
|
+
olds = pool.select { |entry| same?(entry, first, reloaded, seen) }
|
|
252
|
+
next unless olds.size == group.size
|
|
253
|
+
|
|
254
|
+
pool.replace(without(pool, olds))
|
|
255
|
+
pairs.concat(olds.zip(group))
|
|
256
|
+
end
|
|
257
|
+
pairs
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Pairs the entries no exact match took, by {.key}, but only for a key
|
|
261
|
+
# with as many old entries left as new ones. More old entries means an
|
|
262
|
+
# included concern added some of them, and a guess could drop the
|
|
263
|
+
# concern's entry. Those keep both copies, as before #390.
|
|
264
|
+
#
|
|
265
|
+
# @param added [Array] new entries with no exact match.
|
|
266
|
+
# @param pool [Array] old entries no exact match took.
|
|
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.
|
|
270
|
+
# @return [Array<Array(Object, Object)>] each old entry and the new entry that replaces it.
|
|
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|
|
|
274
|
+
old_entries = old_by_key.fetch(k, [])
|
|
275
|
+
old_entries.size == new_entries.size ? old_entries.zip(new_entries) : []
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Rebuilds each changed chain in its order from before the load, with
|
|
280
|
+
# each old entry swapped for its new entry, and entries the load added
|
|
281
|
+
# unpaired at the end. Swaps each validator in `_validators` the same way.
|
|
282
|
+
#
|
|
283
|
+
# @param changed [Array<Class>] the classes whose chains the load changed.
|
|
284
|
+
# @param old [Hash{Class => Hash{Symbol => Array}}] each class's entries before the load.
|
|
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.
|
|
287
|
+
# @return [void]
|
|
288
|
+
def self.restore(changed, old, by_chain)
|
|
289
|
+
seen = {}.compare_by_identity
|
|
290
|
+
changed.each do |klass|
|
|
291
|
+
klass.__callbacks.each do |name, chain|
|
|
292
|
+
next if seen[chain]
|
|
293
|
+
|
|
294
|
+
seen[chain] = true
|
|
295
|
+
now = chain.to_a
|
|
296
|
+
order = reorder(old[klass].fetch(name, []), now, swap_for(klass, name, by_chain))
|
|
297
|
+
next if order.size == now.size && order.zip(now).all? { |a, b| a.equal?(b) }
|
|
298
|
+
|
|
299
|
+
chain.clear
|
|
300
|
+
chain.append(*order)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
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.
|
|
305
|
+
validators = {}.compare_by_identity
|
|
306
|
+
by_chain.each_value do |pairs|
|
|
307
|
+
pairs.each { |old_entry, new_entry| validators[old_entry.filter] = new_entry.filter }
|
|
308
|
+
end
|
|
309
|
+
lists = changed.select { |klass| klass.respond_to?(:_validators) }.flat_map { |klass| klass._validators.values }
|
|
310
|
+
lists.uniq(&:object_id).each do |list|
|
|
311
|
+
list.map! { |validator| validators.fetch(validator, validator) }
|
|
312
|
+
list.uniq!(&:object_id)
|
|
313
|
+
end
|
|
314
|
+
end
|
|
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
|
+
#
|
|
329
|
+
# @param old [Array] the chain's entries before the load.
|
|
330
|
+
# @param now [Array] its entries after the load.
|
|
331
|
+
# @param swap [Hash{Object => Object}] each old entry to its new entry.
|
|
332
|
+
# @return [Array] `old` with each new entry swapped in, then the other new entries.
|
|
333
|
+
def self.reorder(old, now, swap)
|
|
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) }
|
|
338
|
+
order = old.filter_map do |entry|
|
|
339
|
+
new_entry = swap[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)
|
|
398
|
+
end
|
|
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
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
# True when two entries come from the same macro call with the same
|
|
541
|
+
# arguments: the same kind, and filters and `if:`/`unless:` conditions
|
|
542
|
+
# that are {.equivalent?}. A concern and the class body can add one
|
|
543
|
+
# callback class under different conditions; those are different entries.
|
|
544
|
+
#
|
|
545
|
+
# @param old_entry [Object] an ActiveSupport callback from before the load.
|
|
546
|
+
# @param new_entry [Object] one the load added.
|
|
547
|
+
# @param reloaded [Array<String>] real paths of the source and mutant files.
|
|
548
|
+
# @param seen [Hash] answers already found ({.equivalent?}).
|
|
549
|
+
# @return [Boolean]
|
|
550
|
+
def self.same?(old_entry, new_entry, reloaded, seen = {})
|
|
551
|
+
old_entry.kind == new_entry.kind && equivalent?(old_entry.filter, new_entry.filter, reloaded, 0, seen) &&
|
|
552
|
+
equivalent?(conditions(old_entry), conditions(new_entry), reloaded, 0, seen)
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
# @param callback [Object] an ActiveSupport callback.
|
|
556
|
+
# @return [Array] its `if:` and `unless:` conditions, as ActiveSupport
|
|
557
|
+
# keeps them (empty lists when it keeps none).
|
|
558
|
+
def self.conditions(callback)
|
|
559
|
+
%i[@if @unless].map { |name| callback.instance_variable_get(name) || [] }
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
# How deep {.equivalent?} looks into objects before it gives up.
|
|
563
|
+
EQUIVALENT_DEPTH = 6
|
|
564
|
+
|
|
565
|
+
# True when two values would make the same callback or validator. Each
|
|
566
|
+
# run of a class body builds new blocks, so a block matches another
|
|
567
|
+
# from the same place in the code ({.block_key}) that captured
|
|
568
|
+
# equivalent values. Hashes and arrays match element by element. A class
|
|
569
|
+
# or module matches only itself. Other objects, such as a validator or a
|
|
570
|
+
# callback object, match when they are equal, or of one class with
|
|
571
|
+
# equivalent instance variables. Past {EQUIVALENT_DEPTH} levels nothing
|
|
572
|
+
# matches, so a deep object pairs only by key ({.by_key}).
|
|
573
|
+
#
|
|
574
|
+
# A pair already being compared, such as a lambda that captures itself
|
|
575
|
+
# (Rails's `belongs_to touch:` lambda does), counts as a match: nothing
|
|
576
|
+
# else in it differs. Each answer is kept in `seen`, so a value many
|
|
577
|
+
# blocks capture is compared once. A value that cannot be compared, such
|
|
578
|
+
# as the proxy `with_options` yields, matches nothing, so both copies stay.
|
|
579
|
+
#
|
|
580
|
+
# @param left [Object]
|
|
581
|
+
# @param right [Object]
|
|
582
|
+
# @param reloaded [Array<String>] real paths of the source and mutant files.
|
|
583
|
+
# @param depth [Integer] levels already compared.
|
|
584
|
+
# @param seen [Hash{Array => Boolean, Symbol}] each pair's answer, or
|
|
585
|
+
# `:open` while it is being compared, by object id.
|
|
586
|
+
# @return [Boolean]
|
|
587
|
+
def self.equivalent?(left, right, reloaded, depth = 0, seen = {})
|
|
588
|
+
return true if SAME_OBJECT.bind_call(left, right)
|
|
589
|
+
|
|
590
|
+
pair = [left.__id__, right.__id__]
|
|
591
|
+
return seen[pair] != false if seen.key?(pair)
|
|
592
|
+
return false if depth > EQUIVALENT_DEPTH || !INSTANCE_OF.bind_call(left, KERNEL_CLASS.bind_call(right))
|
|
593
|
+
|
|
594
|
+
seen[pair] = :open
|
|
595
|
+
same = ->(a, b) { equivalent?(a, b, reloaded, depth + 1, seen) }
|
|
596
|
+
seen[pair] = case left
|
|
597
|
+
when Module then false
|
|
598
|
+
when Proc then same_block?(left, right, reloaded, same)
|
|
599
|
+
when Hash then left.keys == right.keys && left.all? { |k, v| same.(v, right[k]) }
|
|
600
|
+
when Array then left.size == right.size && left.zip(right).all? { |a, b| same.(a, b) }
|
|
601
|
+
else left == right || same_state?(left, right, same)
|
|
602
|
+
end
|
|
603
|
+
rescue StandardError
|
|
604
|
+
seen[pair] = false if pair
|
|
605
|
+
false
|
|
606
|
+
end
|
|
607
|
+
|
|
608
|
+
# `BasicObject#equal?`, unbound, so a proxy cannot answer for its target.
|
|
609
|
+
SAME_OBJECT = ::BasicObject.instance_method(:equal?)
|
|
610
|
+
|
|
611
|
+
# `Kernel#instance_of?`, unbound, for the same reason.
|
|
612
|
+
INSTANCE_OF = ::Kernel.instance_method(:instance_of?)
|
|
613
|
+
|
|
614
|
+
# `Kernel#class`, unbound, for the same reason.
|
|
615
|
+
KERNEL_CLASS = ::Kernel.instance_method(:class)
|
|
616
|
+
|
|
617
|
+
# @param left [Object]
|
|
618
|
+
# @param right [Object] an object of the same class.
|
|
619
|
+
# @param same [Proc] compares two values one level deeper.
|
|
620
|
+
# @return [Boolean] true when the instance variables both have are
|
|
621
|
+
# equivalent. One of them can hold a value the other has not cached
|
|
622
|
+
# yet, such as a Rails reflection's memoized lookups, so the others are
|
|
623
|
+
# skipped. No shared variables is not a match: a symbol or a number
|
|
624
|
+
# has none, and those compare by value.
|
|
625
|
+
def self.same_state?(left, right, same)
|
|
626
|
+
names = left.instance_variables & right.instance_variables
|
|
627
|
+
return false if names.empty?
|
|
628
|
+
|
|
629
|
+
names.all? { |name| same.(left.instance_variable_get(name), right.instance_variable_get(name)) }
|
|
630
|
+
end
|
|
631
|
+
|
|
632
|
+
# @param left [Proc]
|
|
633
|
+
# @param right [Proc]
|
|
634
|
+
# @param reloaded [Array<String>] real paths of the source and mutant files.
|
|
635
|
+
# @param same [Proc] compares two values one level deeper.
|
|
636
|
+
# @return [Boolean] true for blocks from the same place in the code that
|
|
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.
|
|
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
|
|
645
|
+
return false unless left.source_location && right.source_location
|
|
646
|
+
|
|
647
|
+
block_key(left, reloaded) == block_key(right, reloaded) && same_captures?(left, right, same)
|
|
648
|
+
end
|
|
649
|
+
|
|
650
|
+
# @param left [Proc]
|
|
651
|
+
# @param right [Proc] a block from the same place in the code.
|
|
652
|
+
# @param same [Proc] compares two values one level deeper.
|
|
653
|
+
# @return [Boolean] true when both captured equivalent values. The list
|
|
654
|
+
# itself is not a level, so blocks that capture each other in a loop
|
|
655
|
+
# reach the pair already being compared before the depth limit.
|
|
656
|
+
def self.same_captures?(left, right, same)
|
|
657
|
+
mine = captured(left)
|
|
658
|
+
theirs = captured(right)
|
|
659
|
+
return false unless mine && theirs && mine.size == theirs.size
|
|
660
|
+
|
|
661
|
+
mine.zip(theirs).all? { |a, b| same.(a, b) }
|
|
662
|
+
end
|
|
663
|
+
|
|
664
|
+
# The block's receiver and the values it captured, or nil when Ruby
|
|
665
|
+
# cannot show them. The receiver tells apart `method(:x).to_proc` of
|
|
666
|
+
# two objects; a class body's blocks share the class as receiver.
|
|
667
|
+
#
|
|
668
|
+
# @param block [Proc]
|
|
669
|
+
# @return [Array, nil]
|
|
670
|
+
def self.captured(block)
|
|
671
|
+
scope = block.binding
|
|
672
|
+
[scope.receiver] + scope.local_variables.map { |name| scope.local_variable_get(name) }
|
|
673
|
+
rescue ArgumentError
|
|
674
|
+
nil
|
|
675
|
+
end
|
|
676
|
+
|
|
677
|
+
# Where a block sits in the code: the file and Ruby's label for it, such
|
|
678
|
+
# as "block in <class:Parcel>", plus the line for a file the load did
|
|
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`.
|
|
685
|
+
#
|
|
686
|
+
# @param block [Proc]
|
|
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.
|
|
690
|
+
# @return [Array]
|
|
691
|
+
def self.block_key(block, reloaded, code: true)
|
|
692
|
+
symbol = symbol_proc_text(block)
|
|
693
|
+
return [:symbol, symbol] if symbol
|
|
694
|
+
|
|
695
|
+
file, line = block.source_location
|
|
696
|
+
# CRuby only; elsewhere blocks match by file and line alone.
|
|
697
|
+
label = RubyVM::InstructionSequence.of(block)&.label if defined?(::RubyVM::InstructionSequence)
|
|
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_") }
|
|
800
|
+
end
|
|
801
|
+
|
|
802
|
+
# What two entries share when they are copies of one macro call, apart
|
|
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).
|
|
808
|
+
#
|
|
809
|
+
# @param callback [Object] an ActiveSupport callback.
|
|
810
|
+
# @param reloaded [Array<String>] real paths of the source and mutant files.
|
|
811
|
+
# @param code [Boolean] see {block_key}.
|
|
812
|
+
# @return [Array]
|
|
813
|
+
def self.key(callback, reloaded, code: true)
|
|
814
|
+
filter = callback.filter
|
|
815
|
+
filter_key = case filter
|
|
816
|
+
when Symbol, Module then filter
|
|
817
|
+
when Proc then block_key(filter, reloaded, code: code)
|
|
818
|
+
else object_key(filter)
|
|
819
|
+
end
|
|
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
|
|
864
|
+
end
|
|
865
|
+
|
|
866
|
+
# @param list [Array]
|
|
867
|
+
# @param others [Array]
|
|
868
|
+
# @return [Array] the entries of `list` that are not in `others`, by identity.
|
|
869
|
+
def self.without(list, others)
|
|
870
|
+
others = identity_set(others)
|
|
871
|
+
list.reject { |entry| others.include?(entry) }
|
|
872
|
+
end
|
|
873
|
+
|
|
874
|
+
# A set that compares by identity. Building it first keeps entries that
|
|
875
|
+
# are equal by value but not the same object.
|
|
876
|
+
#
|
|
877
|
+
# @param list [Array]
|
|
878
|
+
# @return [Set]
|
|
879
|
+
def self.identity_set(list)
|
|
880
|
+
Set.new.compare_by_identity.merge(list)
|
|
881
|
+
end
|
|
882
|
+
|
|
883
|
+
# @param file [String]
|
|
884
|
+
# @return [String] the real path, so a symlinked path still matches.
|
|
885
|
+
def self.real_path(file)
|
|
886
|
+
(@real_paths ||= {})[file] ||= begin
|
|
887
|
+
File.realpath(file)
|
|
888
|
+
rescue SystemCallError
|
|
889
|
+
File.expand_path(file)
|
|
890
|
+
end
|
|
891
|
+
end
|
|
892
|
+
end
|
|
893
|
+
|
|
894
|
+
# Whether `klass` had the enum before this load. Rails records a new enum
|
|
895
|
+
# after its first conflict check, so the answer is read once, at the first
|
|
896
|
+
# check of each enum, and kept for the rest of the load.
|
|
897
|
+
#
|
|
898
|
+
# @param klass [Class] the model declaring the enum.
|
|
899
|
+
# @param enum_name [Symbol, String] the enum being declared.
|
|
900
|
+
# @return [Boolean]
|
|
901
|
+
def self.enum_existed?(klass, enum_name)
|
|
902
|
+
key = [klass, enum_name.to_s]
|
|
903
|
+
@enums_before_load.fetch(key) { @enums_before_load[key] = klass.defined_enums.key?(key.last) }
|
|
904
|
+
end
|
|
905
|
+
|
|
906
|
+
# The callback kinds `skip_callback` takes before its filters.
|
|
907
|
+
CALLBACK_KINDS = %i[before after around].freeze
|
|
908
|
+
|
|
909
|
+
# True when every filter of this skip is a method `klass` has, or a callback
|
|
910
|
+
# of that kind on chain `name` that the superclass still has. The kind is
|
|
911
|
+
# optional and defaults to `:before`, as in Rails. With no filters there is
|
|
912
|
+
# nothing to compare, so false.
|
|
913
|
+
#
|
|
914
|
+
# @param klass [Class] the class calling `skip_callback`.
|
|
915
|
+
# @param name [Symbol] the callback chain, such as `:save`.
|
|
916
|
+
# @param args [Array] the optional kind, then the filters.
|
|
917
|
+
# @return [Boolean]
|
|
918
|
+
def self.repeated_skip?(klass, name, *args)
|
|
919
|
+
kind = CALLBACK_KINDS.include?(args.first) ? args.shift : :before
|
|
920
|
+
return false if args.empty?
|
|
921
|
+
|
|
922
|
+
parent = klass.superclass
|
|
923
|
+
chain = parent.respond_to?(:__callbacks) ? parent.__callbacks[name] : nil
|
|
924
|
+
args.all? do |filter|
|
|
925
|
+
(filter.is_a?(Symbol) && (klass.method_defined?(filter) || klass.private_method_defined?(filter))) ||
|
|
926
|
+
chain&.any? { |callback| callback.kind == kind && callback.filter == filter }
|
|
927
|
+
end
|
|
928
|
+
end
|
|
929
|
+
|
|
930
|
+
# Prepends the guards once, onto the Rails modules this process has loaded.
|
|
931
|
+
# A module Rails only set to autoload stays unloaded, so the mutant run
|
|
932
|
+
# sees the same Ruby as the clean run.
|
|
933
|
+
#
|
|
934
|
+
# @return [void]
|
|
935
|
+
def self.install!
|
|
936
|
+
if defined?(::ActiveRecord::Enum) && !::ActiveRecord.autoload?(:Enum)
|
|
937
|
+
prepend_once(::ActiveRecord::Enum, EnumRedeclaration)
|
|
938
|
+
end
|
|
939
|
+
return unless callbacks_loaded?
|
|
940
|
+
|
|
941
|
+
prepend_once(::ActiveSupport::Callbacks::ClassMethods, CallbackReskipping)
|
|
942
|
+
end
|
|
943
|
+
|
|
944
|
+
# `defined?` does not load an autoloaded constant, but using it does.
|
|
945
|
+
#
|
|
946
|
+
# @return [Boolean] true when `ActiveSupport::Callbacks` is loaded, not
|
|
947
|
+
# only set to autoload.
|
|
948
|
+
def self.callbacks_loaded?
|
|
949
|
+
!!defined?(::ActiveSupport::Callbacks) && !::ActiveSupport.autoload?(:Callbacks)
|
|
950
|
+
end
|
|
951
|
+
|
|
952
|
+
# @param target [Module] the Rails module.
|
|
953
|
+
# @param guard [Module] the guard to put in front of it.
|
|
954
|
+
# @return [void]
|
|
955
|
+
def self.prepend_once(target, guard)
|
|
956
|
+
target.prepend(guard) unless target.ancestors.include?(guard)
|
|
957
|
+
end
|
|
958
|
+
|
|
959
|
+
# Asks each Rails autoloader for the constant `source_file` defines. A
|
|
960
|
+
# loader that does not manage the file raises `Zeitwerk::Error`; the next
|
|
961
|
+
# one is asked. A managed file that does not define the constant Zeitwerk
|
|
962
|
+
# expects raises `Zeitwerk::NameError`, a `NameError`; the mutant load goes
|
|
963
|
+
# on as before. A file already loaded only costs a constant lookup.
|
|
964
|
+
# Autoload roots are real paths, so the loaders get the real path.
|
|
965
|
+
#
|
|
966
|
+
# @param source_file [String] the file the mutant replaces.
|
|
967
|
+
# @return [void]
|
|
968
|
+
def self.load_original(source_file)
|
|
969
|
+
return unless defined?(::Rails) && ::Rails.respond_to?(:autoloaders) && defined?(::Zeitwerk::Error)
|
|
970
|
+
# A library can load railties with no Rails app; `Rails.autoloaders` then raises.
|
|
971
|
+
return unless ::Rails.respond_to?(:application) && ::Rails.application
|
|
972
|
+
|
|
973
|
+
::Rails.autoloaders.each do |loader|
|
|
974
|
+
next unless loader.respond_to?(:load_file)
|
|
975
|
+
|
|
976
|
+
begin
|
|
977
|
+
loader.load_file(RepeatedCallbacks.real_path(source_file))
|
|
978
|
+
return
|
|
979
|
+
rescue ::Zeitwerk::Error, ::NameError
|
|
980
|
+
next
|
|
981
|
+
end
|
|
982
|
+
end
|
|
983
|
+
end
|
|
984
|
+
end
|
|
985
|
+
end
|