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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +733 -0
  3. data/README.md +85 -50
  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 +540 -59
  8. data/lib/mutineer/command_redact.rb +38 -0
  9. data/lib/mutineer/concern_blocks.rb +95 -24
  10. data/lib/mutineer/config.rb +112 -6
  11. data/lib/mutineer/coverage_map.rb +1345 -156
  12. data/lib/mutineer/daemon_backend.rb +41 -18
  13. data/lib/mutineer/daemon_client.rb +104 -21
  14. data/lib/mutineer/daemon_server.rb +85 -14
  15. data/lib/mutineer/database_urls.rb +189 -0
  16. data/lib/mutineer/external_backend.rb +93 -6
  17. data/lib/mutineer/fs_path.rb +125 -0
  18. data/lib/mutineer/isolation.rb +380 -25
  19. data/lib/mutineer/job_plan.rb +352 -41
  20. data/lib/mutineer/minitest_integration/file_order.rb +56 -0
  21. data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
  22. data/lib/mutineer/minitest_integration.rb +26 -58
  23. data/lib/mutineer/mutant_id.rb +4 -1
  24. data/lib/mutineer/mutation.rb +10 -4
  25. data/lib/mutineer/mutator_registry.rb +6 -3
  26. data/lib/mutineer/mutators/collection_method.rb +195 -6
  27. data/lib/mutineer/mutators/literal_mutation.rb +33 -3
  28. data/lib/mutineer/mutators/range_literal.rb +78 -29
  29. data/lib/mutineer/mutators/rescue_body.rb +126 -0
  30. data/lib/mutineer/mutators/return_nil.rb +23 -6
  31. data/lib/mutineer/output_filter.rb +451 -0
  32. data/lib/mutineer/pairing.rb +463 -31
  33. data/lib/mutineer/parser.rb +53 -0
  34. data/lib/mutineer/project_path.rb +15 -8
  35. data/lib/mutineer/rails_worker_db.rb +366 -6
  36. data/lib/mutineer/reach_probe.rb +229 -0
  37. data/lib/mutineer/redaction.rb +301 -41
  38. data/lib/mutineer/reload_guards.rb +985 -0
  39. data/lib/mutineer/reporter.rb +467 -58
  40. data/lib/mutineer/result.rb +6 -7
  41. data/lib/mutineer/run_seed.rb +16 -0
  42. data/lib/mutineer/runner.rb +194 -55
  43. data/lib/mutineer/source_bytes.rb +187 -0
  44. data/lib/mutineer/statement_lines.rb +108 -14
  45. data/lib/mutineer/test_runners/rspec.rb +118 -10
  46. data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
  47. data/lib/mutineer/test_runners.rb +3 -2
  48. data/lib/mutineer/triage.rb +55 -10
  49. data/lib/mutineer/version.rb +1 -1
  50. data/lib/mutineer.rb +1 -0
  51. data/skills/mutineer/SKILL.md +12 -4
  52. metadata +13 -1
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "outer_reporter"
4
+ require_relative "../child_cause"
4
5
  require_relative "../redaction"
5
6
 
6
7
  module Mutineer
@@ -12,10 +13,13 @@ module Mutineer
12
13
  #
13
14
  # Only the first failed result on the outer reporter counts (see
14
15
  # {OuterReporter}). A run that stops at its first failure ends there, so a
15
- # full run and a `--matrix` run give the same verdict. One limit: the tests
16
- # of a `parallelize_me!` class record in the order their threads finish, so
17
- # a mutant that makes one parallel test hit a database error and another
18
- # fail an assertion can score `error` in one run and `killed` in the next.
16
+ # full run and a `--matrix` run give the same verdict. "First" follows
17
+ # Minitest's run order. Serial classes finish before `parallelize_me!`
18
+ # classes. Inside a parallel class the order is the queue order, the same
19
+ # order `runnable_methods` produced, not the order the worker threads
20
+ # finish (#347). A mutant that makes one parallel test hit a database
21
+ # error and another fail an assertion therefore scores the same way on
22
+ # every run.
19
23
  # Stdlib only: the app-side daemon loads this file without Prism. An
20
24
  # unknown Minitest shape arms nothing, and the run keeps its 0 or 1.
21
25
  module InTestDatabaseError
@@ -44,6 +48,10 @@ module Mutineer
44
48
  # The lock errors in {NAMES}, which get their own stderr hint.
45
49
  LOCK_ERROR_NAMES = %w[ActiveRecord::Deadlocked ActiveRecord::LockWaitTimeout].freeze
46
50
 
51
+ # Guards the parallel-queue index. The main thread writes it as it
52
+ # enqueues, and a worker thread reads it when that test is recorded.
53
+ QUEUE_LOCK = Mutex.new
54
+
47
55
  # Prepended on `Minitest::CompositeReporter`.
48
56
  module RecordFirstFailure
49
57
  # Records the result, then lets the module keep the first failure.
@@ -56,13 +64,55 @@ module Mutineer
56
64
  end
57
65
  end
58
66
 
67
+ # Prepended on `Minitest::Test`. The worker that runs a test is the
68
+ # worker that records it, but the result does not carry the class
69
+ # object. This notes the test's place in the outer queue and attaches
70
+ # that place to the result. Two anonymous classes that inherit one
71
+ # method stay distinct, because the place is stored on the class object
72
+ # (review of #453).
73
+ module StampQueueIndex
74
+ # Notes this test's place, runs it, and attaches the place to the result.
75
+ # A nested {run} keeps its own place.
76
+ #
77
+ # @return [Minitest::Result]
78
+ def run
79
+ InTestDatabaseError.stamp(self)
80
+ result = super
81
+ InTestDatabaseError.attach(result)
82
+ result
83
+ end
84
+ end
85
+
86
+ # Prepended on `Minitest::Parallel::Executor`. Records each job as the
87
+ # main thread enqueues it, which is Minitest's run order for a
88
+ # `parallelize_me!` class. The workers then finish in another order.
89
+ module NoteQueueOrder
90
+ # Records the job, then queues it.
91
+ #
92
+ # @param work [Array, Object] `[klass, method_name, reporter]`, or
93
+ # whatever else the executor was given.
94
+ # @return [Object] whatever `Minitest::Parallel::Executor#<<` returns.
95
+ def <<(work)
96
+ InTestDatabaseError.note_queued(work)
97
+ super
98
+ end
99
+ end
100
+
59
101
  class << self
60
- # The first failed result's database error, false when that result
61
- # failed for another reason, or nil before any failure.
102
+ # The first failed serial result's database error, false when that
103
+ # result failed for another reason, or nil before any serial failure.
104
+ # Parallel results are kept separately, in queue order.
62
105
  #
63
106
  # @return [Exception, false, nil]
64
107
  attr_accessor :first_failure
65
108
 
109
+ # Parallel failures as `[queue index, exception or false]`, in the
110
+ # order they were recorded. The earliest index decides when no serial
111
+ # test failed.
112
+ #
113
+ # @return [Array<Array>]
114
+ attr_accessor :parallel_failures
115
+
66
116
  # Installs the hooks and arms the module for this process.
67
117
  #
68
118
  # @return [Boolean] false when the Minitest shape is unknown.
@@ -75,7 +125,8 @@ module Mutineer
75
125
  OuterReporter.register(self)
76
126
  self.armed_pid = Process.pid
77
127
  self.armed_reporter = nil
78
- self.first_failure = nil
128
+ reset_failures!
129
+ hook_queue_order
79
130
  true
80
131
  end
81
132
 
@@ -85,40 +136,58 @@ module Mutineer
85
136
  def disarm!
86
137
  self.armed_pid = nil
87
138
  self.armed_reporter = nil
88
- self.first_failure = nil
139
+ reset_failures!
89
140
  end
90
141
 
91
- # Keeps the first failed result recorded on the outer reporter. Minitest
92
- # records from several threads only under its own reporter lock. A
93
- # result it cannot read counts as an ordinary failure, so a fault here
94
- # keeps the old verdict and never turns a kill into an error.
142
+ # Keeps the failures recorded on the outer reporter. A serial failure
143
+ # is kept only when it is the first one. A `parallelize_me!` failure
144
+ # is kept with its queue index, so the earliest queued test decides
145
+ # even when another thread finishes first (#347). Minitest records
146
+ # from several threads only under its own reporter lock. A result it
147
+ # cannot read counts as an ordinary failure, so a fault here keeps the
148
+ # old verdict and never turns a kill into an error.
95
149
  #
96
150
  # @param reporter [Minitest::CompositeReporter] the recording reporter.
97
151
  # @param result [Minitest::Result] the result of one test.
98
152
  # @return [void]
99
153
  def record(reporter, result)
154
+ # Taken before the early returns. A pass still has a place, and a
155
+ # result this run did not attach (a test recording on its own
156
+ # reporter) has none.
157
+ index = stored_index(result)
100
158
  return if result.passed? || result.skipped?
101
- return unless first_failure.nil? && outer_reporter?(reporter)
159
+ return unless outer_reporter?(reporter)
102
160
 
103
- self.first_failure = database_error(result.failures.first) || false
161
+ outcome = database_error(result.failures.first) || false
162
+ # Nil is a serial test, or a parallel test the queue hook did not
163
+ # see. Zero is the first queued test, so it must not be treated as
164
+ # missing. The database error is kept either way: a missing place
165
+ # does not turn it into an ordinary failure.
166
+ if index.nil?
167
+ self.first_failure = outcome if first_failure.nil?
168
+ else
169
+ parallel_failures << [index, outcome]
170
+ end
104
171
  rescue StandardError
105
- self.first_failure = false
172
+ self.first_failure = false if first_failure.nil?
106
173
  end
107
174
 
108
175
  # The run's exit code: 2 instead of 1 when the armed run's first failure
109
176
  # was a database error. Then it also writes one line on stderr that
110
- # names the error. `STDERR`, not `warn`: a test can leave `$stderr` as
177
+ # names the error, and one cause line on the armed {ChildCause} pipe
178
+ # (#417 / R7). `STDERR`, not `warn`: a test can leave `$stderr` as
111
179
  # a `StringIO`. The flush matters because both fork boundaries end with
112
180
  # `exit!`.
113
181
  #
114
182
  # @param code [Integer] 0 or 1 from the run.
115
183
  # @return [Integer] `code`, or 2.
116
184
  def exit_code(code)
117
- error = first_failure if armed_here?
185
+ error = deciding_error if armed_here?
118
186
  return code unless code == 1 && error
119
187
 
120
188
  STDERR.puts(line(error))
121
189
  STDERR.flush
190
+ ChildCause.write_text("a test failed with a database error: #{ChildCause.line(error)}")
122
191
  2
123
192
  end
124
193
 
@@ -139,13 +208,168 @@ module Mutineer
139
208
  return unless failure.is_a?(::Minitest::UnexpectedError)
140
209
 
141
210
  error = failure.respond_to?(:error) ? failure.error : failure.exception
142
- return if loaded(CODE_ERROR_NAMES).any? { |klass| error.is_a?(klass) }
211
+ error if database_exception?(error)
212
+ end
213
+
214
+ # True when `error` is one of {NAMES} and not one of {CODE_ERROR_NAMES}.
215
+ # Shared with the RSpec runner, which has no `Minitest::UnexpectedError`
216
+ # wrapper (#347).
217
+ #
218
+ # @param error [Exception, nil]
219
+ # @return [Boolean]
220
+ def database_exception?(error)
221
+ return false if error.nil?
222
+ return false if loaded(CODE_ERROR_NAMES).any? { |klass| error.is_a?(klass) }
143
223
 
144
- error if loaded(NAMES).any? { |klass| error.is_a?(klass) }
224
+ loaded(NAMES).any? { |klass| error.is_a?(klass) }
225
+ end
226
+
227
+ # Writes the stderr line for `error` and flushes it. The same line
228
+ # {exit_code} writes. `STDERR`, not `warn`: a test can leave `$stderr`
229
+ # as a `StringIO`. Shared with the RSpec runner (#347).
230
+ #
231
+ # @param error [Exception] the database error.
232
+ # @return [void]
233
+ def warn_database_error(error)
234
+ STDERR.puts(line(error))
235
+ STDERR.flush
236
+ end
237
+
238
+ # Remembers where a parallel job sits in the outer run's queue. Called
239
+ # on the main thread, before a worker can run the job. A job for
240
+ # another reporter is a test running a parallel class itself; its
241
+ # order is not the run's, so it is ignored (review of #453). Each
242
+ # enqueue is its own place, keyed by the class object, so two
243
+ # anonymous classes that inherit one method do not share a place.
244
+ # Never raises: a fault leaves that test without an index, and {record}
245
+ # then keeps the outcome without a place.
246
+ #
247
+ # @param work [Object] the value passed to `Minitest::Parallel::Executor#<<`.
248
+ # @return [void]
249
+ def note_queued(work)
250
+ return unless work.is_a?(Array)
251
+
252
+ klass, method_name, reporter = work
253
+ return unless klass.is_a?(Module) && method_name
254
+ return if other_reporter?(reporter)
255
+
256
+ QUEUE_LOCK.synchronize do
257
+ index = @next_queue_index
258
+ @next_queue_index += 1
259
+ (queue_positions[[klass.object_id, method_name.to_s]] ||= []) << index
260
+ end
261
+ rescue StandardError
262
+ nil
263
+ end
264
+
265
+ # Takes this test's place from the outer queue and pushes it on the
266
+ # thread's stack. A nested {StampQueueIndex#run} pushes its own place
267
+ # and pops it in {attach}, so the outer place stays put.
268
+ #
269
+ # @param test [Minitest::Runnable]
270
+ # @return [void]
271
+ def stamp(test)
272
+ index = QUEUE_LOCK.synchronize do
273
+ queue_positions[[test.class.object_id, test.name.to_s]]&.shift
274
+ end
275
+ (Thread.current[:mutineer_db_queue_stack] ||= []) << index
276
+ rescue StandardError
277
+ (Thread.current[:mutineer_db_queue_stack] ||= []) << nil
278
+ end
279
+
280
+ # Pops the place {stamp} pushed for this run and keeps it for `result`.
281
+ #
282
+ # @param result [Minitest::Result, nil]
283
+ # @return [void]
284
+ def attach(result)
285
+ index = Thread.current[:mutineer_db_queue_stack]&.pop
286
+ return unless result
287
+
288
+ QUEUE_LOCK.synchronize { result_indexes[result.object_id] = index }
289
+ rescue StandardError
290
+ nil
145
291
  end
146
292
 
147
293
  private
148
294
 
295
+ # The database error that decides the run, false when the first failure
296
+ # was ordinary, or nil when nothing failed. A serial failure wins: those
297
+ # classes run before any parallel class. Otherwise the parallel failure
298
+ # with the smallest queue index wins.
299
+ #
300
+ # @return [Exception, false, nil]
301
+ def deciding_error
302
+ return first_failure unless first_failure.nil?
303
+
304
+ earliest = parallel_failures.min_by { |index, _outcome| index }
305
+ earliest&.last
306
+ end
307
+
308
+ # Clears the failures and the queue index for a new run.
309
+ #
310
+ # @return [void]
311
+ def reset_failures!
312
+ self.first_failure = nil
313
+ self.parallel_failures = []
314
+ QUEUE_LOCK.synchronize do
315
+ @queue_positions = {}
316
+ @next_queue_index = 0
317
+ @result_indexes = {}
318
+ end
319
+ end
320
+
321
+ # Installs {NoteQueueOrder} when this Minitest has a parallel executor,
322
+ # and {StampQueueIndex} so a result can carry its place. An older shape
323
+ # without one keeps the record-order rule.
324
+ #
325
+ # @return [void]
326
+ def hook_queue_order
327
+ OuterReporter.prepend_once(::Minitest::Test, StampQueueIndex)
328
+ OuterReporter.prepend_once(::Minitest::Parallel::Executor, NoteQueueOrder)
329
+ rescue NameError
330
+ nil
331
+ end
332
+
333
+ # True when `reporter` belongs to a nested run, not the outer one.
334
+ # False when the outer reporter is not known yet, so the jobs that
335
+ # build the run's queue are still recorded.
336
+ #
337
+ # @param reporter [Minitest::CompositeReporter, nil]
338
+ # @return [Boolean]
339
+ def other_reporter?(reporter)
340
+ mine = armed_reporter
341
+ !!(mine && reporter && !reporter.equal?(mine))
342
+ end
343
+
344
+ # The place {attach} stored for `result`, or nil when this run did not
345
+ # attach one. Removes it so a later record of another result cannot
346
+ # see it.
347
+ #
348
+ # @param result [Object, nil]
349
+ # @return [Integer, nil]
350
+ def stored_index(result)
351
+ return if result.nil?
352
+
353
+ QUEUE_LOCK.synchronize { result_indexes.delete(result.object_id) }
354
+ rescue StandardError
355
+ nil
356
+ end
357
+
358
+ # Places still waiting for their test to start, keyed by class object
359
+ # id and method name. Each enqueue appends. {stamp} shifts the next one.
360
+ #
361
+ # @return [Hash{Array => Array<Integer>}]
362
+ def queue_positions
363
+ @queue_positions ||= {}
364
+ end
365
+
366
+ # Places attached to results, keyed by the result's object id.
367
+ #
368
+ # @return [Hash{Integer => Integer, nil}]
369
+ def result_indexes
370
+ @result_indexes ||= {}
371
+ end
372
+
149
373
  # The named classes this process has loaded.
150
374
  #
151
375
  # @param names [Array<String>] constant names.
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "fs_path"
4
+ require_relative "run_seed"
5
+ require_relative "minitest_integration/file_order"
3
6
  require_relative "minitest_integration/stop_at_first_failure"
4
7
  require_relative "minitest_integration/kill_recorder"
5
8
  require_relative "minitest_integration/in_test_database_error"
@@ -22,16 +25,19 @@ module Mutineer
22
25
  # Active Record connection or lock error (#296, {InTestDatabaseError}).
23
26
  class MinitestIntegration
24
27
  # The seed of every Minitest run Mutineer starts, unless `SEED` is set:
25
- # coverage capture, the clean checks and the mutant runs (#282). A file's
26
- # clean check then runs its tests in the order a mutant run of that file
27
- # uses. Several covering files can still differ from the combined clean
28
- # check: a smaller class list shuffles differently. With the stop,
29
- # the test order can decide `killed` vs `timeout`; a fixed order keeps the
30
- # verdict stable for a `--baseline` gate. A `--matrix` run uses the same
31
- # order, so its first failure comes at the same point as the stop's.
28
+ # coverage capture, the clean checks and the mutant runs (#282). The same
29
+ # value is {RunSeed::PINNED_SEED}, which a random RSpec order uses too.
30
+ # Within one file, class order depends only on this seed, so adding
31
+ # another file does not reshuffle it. Files run in the order that run
32
+ # was given. A mutant run's file list is coverage order (paired, then
33
+ # cheapest); the combined clean check uses the order the tests were
34
+ # given. With the stop, the test order can decide `killed` vs `timeout`;
35
+ # a fixed order keeps the verdict stable for a `--baseline` gate. A
36
+ # `--matrix` run uses the same order, so its first failure comes at the
37
+ # same point as the stop's.
32
38
  #
33
39
  # @api private
34
- PINNED_SEED = 1
40
+ PINNED_SEED = RunSeed::PINNED_SEED
35
41
 
36
42
  # The arguments that give `Minitest.run` the shared seed. With `SEED` set,
37
43
  # none: Minitest reads `SEED` itself.
@@ -72,6 +78,7 @@ module Mutineer
72
78
 
73
79
  begin
74
80
  require "minitest"
81
+ FsPath.install_minitest_path_filter!
75
82
  rescue LoadError
76
83
  raise Mutineer::FrameworkUnavailable,
77
84
  "minitest is not available — add minitest to the project under test, " \
@@ -92,20 +99,21 @@ module Mutineer
92
99
  # Each test class's file position: the file that first defined it.
93
100
  rank = {}
94
101
  Array(test_files).each_with_index do |f, i|
95
- load f
102
+ # Match Dir.pwd's encoding so Minitest can shorten a failure path (#346).
103
+ load FsPath.filesystem(f)
96
104
  Minitest::Runnable.runnables.each { |klass| rank[klass] ||= i }
97
105
  end
98
106
 
99
- armed =
100
- if record_to
101
- KillRecorder.arm!(record_to, Minitest::Runnable.runnables)
102
- elsif stop_at_first_failure
103
- StopAtFirstFailure.arm!(Minitest::Runnable.runnables)
104
- end
107
+ if record_to
108
+ KillRecorder.arm!(record_to, Minitest::Runnable.runnables)
109
+ elsif stop_at_first_failure
110
+ StopAtFirstFailure.arm!(Minitest::Runnable.runnables)
111
+ end
105
112
  # An unknown Minitest shape arms no hook and gets a full run, in the
106
- # same seeded order.
107
- keep_file_order(rank) if armed
108
- # Every run, on its own line: `armed` decides the file order (#296).
113
+ # same seeded file order as a clean check (#282).
114
+ keep_file_order(rank)
115
+ # On its own line, not inside the stop or matrix branch. Folding it
116
+ # in would tie the database-error hook to those hooks (#296).
109
117
  InTestDatabaseError.arm!
110
118
  # No silencing here: the fork boundary that calls this method has already
111
119
  # pointed stdout at File::NULL (see ChildStdout).
@@ -123,45 +131,5 @@ module Mutineer
123
131
  InTestDatabaseError.disarm!
124
132
  end
125
133
 
126
- # Runs the test classes in the order of their files (#203). Minitest
127
- # shuffles the classes with the seed, then runs the serial classes before
128
- # the parallel (`parallelize_me!`) ones. A stable sort by file position
129
- # after that shuffle puts the files in the given order and keeps the
130
- # seeded order within one file. The serial-then-parallel split is
131
- # unchanged. Only the child's own class list gets {FileOrder}.
132
- #
133
- # @api private
134
- # @param rank [Hash{Class => Integer}] each class's file position.
135
- # @return [void]
136
- def self.keep_file_order(rank)
137
- FileOrder.rank = rank
138
- ::Minitest::Runnable.runnables.extend(FileOrder)
139
- end
140
-
141
- # Extends the list of test classes: its `shuffle` keeps the file order.
142
- module FileOrder
143
- class << self
144
- # Each test class's file position.
145
- #
146
- # @return [Hash{Class => Integer}, nil]
147
- attr_accessor :rank
148
- end
149
-
150
- # The seeded shuffle, then a stable sort by file position.
151
- #
152
- # @return [Array<Class>]
153
- def shuffle(...)
154
- rank = FileOrder.rank || {}
155
- super.sort_by.with_index { |klass, i| [rank.fetch(klass, rank.size), i] }
156
- end
157
-
158
- # Minitest 5.15 and older drop empty classes before the shuffle, so the
159
- # filtered list keeps the file order too.
160
- #
161
- # @return [Array<Class>]
162
- def reject(...)
163
- super.extend(FileOrder)
164
- end
165
- end
166
134
  end
167
135
  end
@@ -110,7 +110,10 @@ module Mutineer
110
110
  # @param parts [Array] the values that make up the id.
111
111
  # @return [String] a 12-character hex id.
112
112
  def digest(parts)
113
- Digest::SHA256.hexdigest(parts.join("\x00"))[0, 12]
113
+ # Each part keeps its own bytes. A UTF-8 path joined with an EUC-JP
114
+ # token would raise, and an ASCII token's bytes do not change (#346).
115
+ blob = parts.map { |part| part.to_s.b }.join("\x00".b)
116
+ Digest::SHA256.hexdigest(blob)[0, 12]
114
117
  end
115
118
 
116
119
  # Extracts the exact code being mutated, whitespace-collapsed — the same
@@ -13,15 +13,21 @@ module Mutineer
13
13
  # @param source [String] original source text.
14
14
  # @return [String] mutated source text.
15
15
  def apply(source)
16
- source.byteslice(0, start_offset) + replacement + source.byteslice(end_offset..)
16
+ # An ASCII replacement is retagged so it concatenates with a source
17
+ # whose magic comment names another encoding (EUC-JP, Shift_JIS).
18
+ # The bytes do not change, so the file Ruby loads stays in that encoding.
19
+ repl = replacement
20
+ repl = repl.dup.force_encoding(source.encoding) if repl.ascii_only? && repl.encoding != source.encoding
21
+ source.byteslice(0, start_offset) + repl + source.byteslice(end_offset..)
17
22
  end
18
23
 
19
- # Checks whether the mutated source still parses cleanly.
24
+ # Checks whether the running Ruby can still load the mutated source (see
25
+ # {Parser.invalid?}).
20
26
  #
21
27
  # @param source [String] original source text.
22
- # @return [Boolean] true when the mutated source re-parses without errors.
28
+ # @return [Boolean] true when the mutated source re-parses.
23
29
  def valid?(source)
24
- Parser.parse_string(apply(source)).errors.empty?
30
+ !Parser.invalid?(apply(source))
25
31
  end
26
32
  end
27
33
  end
@@ -21,6 +21,7 @@ require_relative "mutators/operand_removal"
21
21
  require_relative "mutators/array_literal"
22
22
  require_relative "mutators/condition_forcing"
23
23
  require_relative "mutators/operator_assignment"
24
+ require_relative "mutators/rescue_body"
24
25
 
25
26
  module Mutineer
26
27
  # Maps operator names to operator classes.
@@ -53,7 +54,8 @@ module Mutineer
53
54
  "array_literal" => Mutators::ArrayLiteral,
54
55
  "condition_true" => Mutators::ConditionTrue,
55
56
  "condition_false" => Mutators::ConditionFalse,
56
- "operator_assignment" => Mutators::OperatorAssignment
57
+ "operator_assignment" => Mutators::OperatorAssignment,
58
+ "rescue_body" => Mutators::RescueBody
57
59
  }.freeze
58
60
 
59
61
  # The default Tier-1 operator set.
@@ -62,7 +64,7 @@ module Mutineer
62
64
  TIER2_NAMES = %w[return_nil literal_mutation condition_negation string_literal regex collection_method
63
65
  safe_navigation range negation_removal
64
66
  chain_link call_to_receiver operand_removal array_literal
65
- condition_true condition_false operator_assignment].freeze
67
+ condition_true condition_false operator_assignment rescue_body].freeze
66
68
 
67
69
  # Short human-readable descriptions for each operator.
68
70
  DESCRIPTIONS = {
@@ -86,7 +88,8 @@ module Mutineer
86
88
  "array_literal" => "[a, b] -> []",
87
89
  "condition_true" => "if/elsif/unless/ternary/modifier/case-in guard condition -> true",
88
90
  "condition_false" => "if/elsif/unless/ternary/modifier/case-in guard condition -> false",
89
- "operator_assignment" => "+= <-> -=, *= <-> /=, %= -> *=, **= -> *= (not ||=, &&=)"
91
+ "operator_assignment" => "+= <-> -=, *= <-> /=, %= -> *=, **= -> *= (not ||=, &&=)",
92
+ "rescue_body" => "rescue clause body -> nil, x rescue y -> x rescue nil"
90
93
  }.freeze
91
94
 
92
95
  # Resolves operator names to classes.