saboteur 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1024 -0
  3. data/README.md +199 -952
  4. data/exe/saboteur +34 -7
  5. data/lib/saboteur/baseline/workflow.rb +2 -1
  6. data/lib/saboteur/baseline_health.rb +40 -6
  7. data/lib/saboteur/cli/exit_status.rb +80 -4
  8. data/lib/saboteur/cli/help_options.rb +17 -0
  9. data/lib/saboteur/cli/history_command.rb +135 -0
  10. data/lib/saboteur/cli/presets.rb +40 -0
  11. data/lib/saboteur/cli/run_budget_options.rb +37 -0
  12. data/lib/saboteur/cli/run_reporting.rb +74 -5
  13. data/lib/saboteur/cli/selection_options.rb +31 -0
  14. data/lib/saboteur/cli/shard_options.rb +17 -0
  15. data/lib/saboteur/cli/source_set.rb +53 -0
  16. data/lib/saboteur/cli/survivors_command.rb +157 -0
  17. data/lib/saboteur/cli.rb +136 -98
  18. data/lib/saboteur/config.rb +163 -15
  19. data/lib/saboteur/config_file.rb +8 -0
  20. data/lib/saboteur/constant_reads.rb +182 -0
  21. data/lib/saboteur/constant_subject.rb +70 -0
  22. data/lib/saboteur/constant_subject_finder.rb +281 -0
  23. data/lib/saboteur/coverage_reads.rb +29 -0
  24. data/lib/saboteur/crash_report.rb +154 -0
  25. data/lib/saboteur/custom_map.rb +3 -1
  26. data/lib/saboteur/dirty_tree_warning.rb +128 -0
  27. data/lib/saboteur/doctor.rb +49 -1
  28. data/lib/saboteur/engine/baseline_guard.rb +136 -14
  29. data/lib/saboteur/engine/boot.rb +16 -1
  30. data/lib/saboteur/engine/canary.rb +41 -10
  31. data/lib/saboteur/engine/candidate_selection.rb +28 -1
  32. data/lib/saboteur/engine/candidates.rb +43 -3
  33. data/lib/saboteur/engine/coverage_guard.rb +29 -0
  34. data/lib/saboteur/engine/coverage_store.rb +79 -15
  35. data/lib/saboteur/engine/defined_order_prefix.rb +43 -0
  36. data/lib/saboteur/engine/diagnostics.rb +41 -2
  37. data/lib/saboteur/engine/diff_scope.rb +24 -5
  38. data/lib/saboteur/engine/distribution.rb +134 -8
  39. data/lib/saboteur/engine/early_stop_recording.rb +47 -0
  40. data/lib/saboteur/engine/gated_run.rb +35 -0
  41. data/lib/saboteur/engine/load_coverage.rb +45 -0
  42. data/lib/saboteur/engine/out_of_scope_guard.rb +21 -0
  43. data/lib/saboteur/engine/pipelines.rb +107 -0
  44. data/lib/saboteur/engine/result_cache.rb +67 -9
  45. data/lib/saboteur/engine/run_budget.rb +90 -0
  46. data/lib/saboteur/engine/run_fingerprint.rb +4 -0
  47. data/lib/saboteur/engine/sandbox_policy.rb +56 -11
  48. data/lib/saboteur/engine/scheduler.rb +222 -20
  49. data/lib/saboteur/engine/selection_announcement.rb +54 -0
  50. data/lib/saboteur/engine/shard.rb +69 -0
  51. data/lib/saboteur/engine/subject_selection.rb +373 -19
  52. data/lib/saboteur/engine/suite_files.rb +3 -1
  53. data/lib/saboteur/engine/test_guess.rb +438 -25
  54. data/lib/saboteur/engine/thread_warnings.rb +53 -0
  55. data/lib/saboteur/engine/unchanged_syntax.rb +17 -2
  56. data/lib/saboteur/engine/unscoped_suite_warning.rb +41 -0
  57. data/lib/saboteur/engine/verdict_check.rb +54 -18
  58. data/lib/saboteur/engine.rb +202 -158
  59. data/lib/saboteur/execution/external_runner.rb +471 -0
  60. data/lib/saboteur/execution/marker_sweep.rb +79 -0
  61. data/lib/saboteur/execution/peak_memory.rb +28 -0
  62. data/lib/saboteur/execution/process_table.rb +12 -0
  63. data/lib/saboteur/execution/process_tree.rb +65 -5
  64. data/lib/saboteur/execution/protocol.rb +2 -1
  65. data/lib/saboteur/execution/reevaluator.rb +68 -12
  66. data/lib/saboteur/execution/runner.rb +225 -24
  67. data/lib/saboteur/execution/sandbox.rb +21 -3
  68. data/lib/saboteur/execution/seatbelt.rb +226 -0
  69. data/lib/saboteur/execution/supervisor.rb +45 -13
  70. data/lib/saboteur/execution/worker_pool.rb +110 -8
  71. data/lib/saboteur/frameworks/sqlite_memory_database.rb +49 -0
  72. data/lib/saboteur/gates.rb +43 -2
  73. data/lib/saboteur/help.rb +108 -13
  74. data/lib/saboteur/ignore_list.rb +40 -7
  75. data/lib/saboteur/install.rb +4 -3
  76. data/lib/saboteur/integration/base.rb +7 -2
  77. data/lib/saboteur/integration/rspec.rb +175 -9
  78. data/lib/saboteur/mutation_source/argument_removal.rb +1 -1
  79. data/lib/saboteur/mutation_source/builtin.rb +44 -14
  80. data/lib/saboteur/mutation_source/chain_removal.rb +1 -1
  81. data/lib/saboteur/mutation_source/condition_negation.rb +1 -1
  82. data/lib/saboteur/mutation_source/constant_literals.rb +66 -0
  83. data/lib/saboteur/mutation_source/element_removal.rb +73 -0
  84. data/lib/saboteur/mutation_source/endpoint_replacement.rb +1 -1
  85. data/lib/saboteur/mutation_source/entry_edits.rb +44 -0
  86. data/lib/saboteur/mutation_source/filter_replacement.rb +1 -1
  87. data/lib/saboteur/mutation_source/hazard_guard.rb +129 -3
  88. data/lib/saboteur/mutation_source/interpolation_removal.rb +41 -0
  89. data/lib/saboteur/mutation_source/literal_replacement.rb +1 -1
  90. data/lib/saboteur/mutation_source/method_swap.rb +193 -0
  91. data/lib/saboteur/mutation_source/mutation.rb +5 -3
  92. data/lib/saboteur/mutation_source/operand_replacement.rb +54 -0
  93. data/lib/saboteur/mutation_source/operator_assignment.rb +1 -1
  94. data/lib/saboteur/mutation_source/operator_examples.rb +6 -1
  95. data/lib/saboteur/mutation_source/operator_registry.rb +7 -2
  96. data/lib/saboteur/mutation_source/operator_visit.rb +39 -0
  97. data/lib/saboteur/mutation_source/pair_removal.rb +13 -31
  98. data/lib/saboteur/mutation_source/range_replacement.rb +1 -1
  99. data/lib/saboteur/mutation_source/regex_replacement.rb +93 -0
  100. data/lib/saboteur/preview.rb +122 -13
  101. data/lib/saboteur/refinement_activations.rb +60 -0
  102. data/lib/saboteur/reporter/agent.rb +9 -1
  103. data/lib/saboteur/reporter/agent_commands.rb +47 -15
  104. data/lib/saboteur/reporter/agent_incomplete.rb +38 -2
  105. data/lib/saboteur/reporter/agent_summary.rb +4 -3
  106. data/lib/saboteur/reporter/findings.rb +5 -2
  107. data/lib/saboteur/reporter/history_policy.rb +11 -0
  108. data/lib/saboteur/reporter/history_reader.rb +47 -0
  109. data/lib/saboteur/reporter/html.rb +26 -4
  110. data/lib/saboteur/reporter/mutation_text.rb +6 -0
  111. data/lib/saboteur/reporter/next_steps.rb +14 -4
  112. data/lib/saboteur/reporter/score_history.rb +5 -1
  113. data/lib/saboteur/reporter/stryker.rb +40 -7
  114. data/lib/saboteur/reporter/survivor_classification.rb +101 -0
  115. data/lib/saboteur/reporter/survivor_hints.rb +3 -0
  116. data/lib/saboteur/reporter/survivor_list.rb +15 -9
  117. data/lib/saboteur/reporter/terminal.rb +74 -17
  118. data/lib/saboteur/result.rb +38 -7
  119. data/lib/saboteur/selection/coverage_map.rb +107 -11
  120. data/lib/saboteur/selection/dependency_shield.rb +80 -0
  121. data/lib/saboteur/selection/external_test_map.rb +27 -0
  122. data/lib/saboteur/selection/git_diff.rb +105 -13
  123. data/lib/saboteur/selection/layout_only_lines.rb +80 -0
  124. data/lib/saboteur/selection/recorder.rb +22 -4
  125. data/lib/saboteur/selection/source_scope.rb +22 -0
  126. data/lib/saboteur/selection/untracked_changes.rb +126 -0
  127. data/lib/saboteur/subject.rb +53 -7
  128. data/lib/saboteur/subject_finder.rb +173 -29
  129. data/lib/saboteur/text.rb +42 -0
  130. data/lib/saboteur/version.rb +1 -1
  131. data/lib/saboteur/working_tree.rb +38 -15
  132. data/lib/saboteur.rb +29 -1
  133. metadata +50 -2
@@ -1,6 +1,8 @@
1
1
  # SPDX-License-Identifier: MIT
2
2
  # frozen_string_literal: true
3
3
 
4
+ require "delegate"
5
+
4
6
  module Saboteur
5
7
  module Integration
6
8
  # Loads spec files once in the parent. In a child, narrows the already
@@ -8,20 +10,35 @@ module Saboteur
8
10
  # reporter, so nothing is printed and no status file is written.
9
11
  class RSpec < Base
10
12
  def setup
11
- require "rspec/core"
13
+ require_framework
12
14
  ::RSpec::Core::Runner.disable_autorun!
13
15
  @runner = ::RSpec::Core::Runner.new(configuration_options)
14
- @runner.setup($stderr, $stdout)
16
+ load_output = SetupOutputCapture.new($stdout)
17
+ begin
18
+ @runner.setup($stderr, load_output)
19
+ rescue SystemExit => e
20
+ # A spec file calling exit at load makes RSpec print "While
21
+ # loading <file> ..." (which names the file) and re-raise.
22
+ # Uncaught, the parent exits with the spec's status: exit(0)
23
+ # would end the run silently green (#411).
24
+ raise Error, "a spec file called exit while loading (status #{e.status})"
25
+ end
15
26
  configuration.order = "rand:#{config.fetch('seed', 1)}"
16
- raise Error, "rspec quit while loading spec files" if world.wants_to_quit
27
+ raise Error, load_failure_message(load_output.failed_files) if quit_while_loading?(world)
17
28
 
18
29
  refuse_unintended_focus
19
30
  end
20
31
 
21
- # RSpec only adds its default "spec" path when $0 is `rspec`.
32
+ # RSpec only adds its default path when $0 is `rspec`, so saboteur adds
33
+ # it when RSpec's own parse of the args, .rspec and SPEC_OPTS names no
34
+ # file or directory to run. Parsing, not a look at the tokens, keeps an
35
+ # option value such as `--require spec_helper` from passing as a path,
36
+ # and a --default-path from .rspec wins over "spec".
22
37
  def rspec_args
38
+ require_framework
23
39
  args = Array(config.fetch("rspec_args", []))
24
- args.any? { |arg| !arg.start_with?("-") } ? args : args + ["spec"]
40
+ options = ::RSpec::Core::ConfigurationOptions.new(args).options
41
+ Array(options[:files_or_directories_to_run]).empty? ? args + [options[:default_path] || "spec"] : args
25
42
  end
26
43
 
27
44
  # RSpec's own parser reads rspec_args and .rspec, so option values such
@@ -34,6 +51,48 @@ module Saboteur
34
51
  end
35
52
  end
36
53
 
54
+ # The spec files the configured suite runs, as UTF-8 paths relative to
55
+ # root (a file outside it stays absolute), by
56
+ # RSpec's own rules: .rspec, rspec_args, the default path, --pattern
57
+ # and --exclude-pattern. RSpec applies the patterns only to the
58
+ # directories it scans, never to a file named explicitly, so a file
59
+ # saboteur names as a test must come from this list (#307). Nothing is
60
+ # loaded and nothing global changes: only the file-selection options
61
+ # reach a fresh Configuration, so --require, -I and formatters are
62
+ # ignored. A pattern set in Ruby (RSpec.configure in a spec helper) is
63
+ # not seen. RSpec resolves patterns against the working directory, so
64
+ # this reads from the root.
65
+ def configured_spec_files(root)
66
+ require_framework
67
+ from_directory(root) do
68
+ options = ::RSpec::Core::ConfigurationOptions.new(rspec_args).options
69
+ configuration = ::RSpec::Core::Configuration.new
70
+ %i[default_path pattern exclude_pattern].each do |name|
71
+ configuration.public_send(:"#{name}=", options[name]) if options[name]
72
+ end
73
+ configuration.files_or_directories_to_run = options[:files_or_directories_to_run]
74
+ # RSpec expands from the working directory, which can spell the
75
+ # root differently from its realpath (/var and /private/var).
76
+ prefix = Text.utf8_identity(File.join(Dir.pwd, ""))
77
+ configuration.files_to_run.map { |file| Text.utf8_identity(file).delete_prefix(prefix) }
78
+ end
79
+ end
80
+
81
+ # A missing rspec-core is the target's setup error, not a saboteur
82
+ # defect (#482): refuse with the remedy instead of crashing into the
83
+ # internal-error report. Only the framework's own LoadError converts -
84
+ # matched on the failed feature, not the message, so a missing
85
+ # rspec/core_ext does not pose as rspec/core; a LoadError the app's
86
+ # files raise keeps the crash route.
87
+ def require_framework
88
+ require "rspec/core"
89
+ rescue LoadError => e
90
+ raise unless e.path == "rspec/core"
91
+
92
+ raise Error, "rspec is not in the bundle: the RSpec integration needs the rspec-core gem. " \
93
+ "Add it to the bundle's test group (or point c.integration at the suite's framework)"
94
+ end
95
+
37
96
  def all_tests
38
97
  groups = world.ordered_example_groups.flat_map(&:descendants)
39
98
  groups.flat_map { |group| world.filtered_examples[group] }
@@ -55,7 +114,8 @@ module Saboteur
55
114
  passed = execute(listener)
56
115
  RunResult.new(passed: passed && listener.failed.empty?, failed_ids: listener.failed,
57
116
  executed_ids: listener.executed, runtime: listener.runtime,
58
- failure_classes: listener.failure_classes, failure_messages: listener.failure_messages)
117
+ failure_classes: listener.failure_classes, failure_messages: listener.failure_messages,
118
+ failure_details: listener.failure_details)
59
119
  end
60
120
 
61
121
  def run_all(recorder)
@@ -71,11 +131,41 @@ module Saboteur
71
131
  passed = execute(listener, log_phase: true)
72
132
  RunResult.new(passed: passed && listener.failed.empty?, failed_ids: listener.failed,
73
133
  executed_ids: listener.executed, runtime: listener.runtime,
74
- failure_classes: listener.failure_classes, failure_messages: listener.failure_messages)
134
+ failure_classes: listener.failure_classes, failure_messages: listener.failure_messages,
135
+ failure_details: listener.failure_details)
75
136
  end
76
137
 
138
+ SHOWN_LOAD_FAILURES = 5
139
+
77
140
  private
78
141
 
142
+ # rspec-core 3.11 added rspec_is_quitting (SyntaxError and SystemExit
143
+ # at load quit through it, not through wants_to_quit). The gemspec
144
+ # does not pin rspec, so a target app can carry an older rspec-core;
145
+ # ask before reading the newer flag (#378).
146
+ def quit_while_loading?(world)
147
+ world.wants_to_quit || (world.respond_to?(:rspec_is_quitting) && world.rspec_is_quitting)
148
+ end
149
+
150
+ # The quit reason RSpec keeps is only a flag; the failing files it
151
+ # printed are what a user needs. Name them and the way out: a repo
152
+ # whose spec tree cannot load whole (an engine's dummy app, an
153
+ # env-dependent integration spec) must narrow what loads (#378).
154
+ def load_failure_message(failed_files)
155
+ message = +"rspec quit while loading spec files"
156
+ if failed_files.any?
157
+ shown = failed_files.first(SHOWN_LOAD_FAILURES)
158
+ rest = failed_files.size - shown.size
159
+ message << ": #{failed_files.size} failed to load:"
160
+ shown.each { |file| message << "\n #{file}" }
161
+ message << "\n ... and #{rest} more" if rest.positive?
162
+ end
163
+ message << "\nExclude the broken subtrees in config/saboteur.rb, e.g. " \
164
+ 'c.rspec_args = ["--exclude-pattern", "spec/dummy/**/*_spec.rb"], or name the ' \
165
+ "suite with --test. Rails engines with an embedded dummy app (spec/dummy) need " \
166
+ 'this; see README "Rails engines and dummy apps".'
167
+ end
168
+
79
169
  def refuse_unintended_focus
80
170
  return if config["allow_focus"]
81
171
 
@@ -94,6 +184,12 @@ module Saboteur
94
184
  options.options[:full_description] ? keys + [:full_description] : keys
95
185
  end
96
186
 
187
+ # A real run already starts in the root; only a caller elsewhere (a
188
+ # spec) changes directory, and only for the block.
189
+ def from_directory(root, &)
190
+ File.realpath(Dir.pwd) == File.realpath(root) ? yield : Dir.chdir(root, &)
191
+ end
192
+
97
193
  def world
98
194
  ::RSpec.world
99
195
  end
@@ -181,12 +277,50 @@ module Saboteur
181
277
  end
182
278
  end
183
279
 
280
+ # RSpec prints each load failure ("An error occurred while loading
281
+ # ...") to the output stream during setup and only marks
282
+ # world.wants_to_quit. Capture the stream while passing everything
283
+ # through, so the tool error can name the files (#378).
284
+ class SetupOutputCapture < SimpleDelegator
285
+ LOAD_FAILURE = [
286
+ /^An error occurred while loading (\S+?)\.?(?: - Did you mean\?)?$/,
287
+ # SyntaxError and SystemExit use their own wording (#378).
288
+ /^While loading (\S+?) an? `(?:raise SyntaxError|exit)`/
289
+ ].freeze
290
+
291
+ def initialize(io)
292
+ super
293
+ @captured = +""
294
+ end
295
+
296
+ def write(text)
297
+ @captured << text
298
+ super
299
+ end
300
+
301
+ def print(*args)
302
+ @captured << args.join
303
+ super
304
+ end
305
+
306
+ def puts(*args)
307
+ @captured << args.flatten.join("\n") << "\n"
308
+ super
309
+ end
310
+
311
+ def failed_files
312
+ LOAD_FAILURE.flat_map { |pattern| @captured.scan(pattern) }.flatten
313
+ end
314
+ end
315
+
184
316
  class Listener
185
317
  EVENTS = %i[example_started example_finished example_failed].freeze
186
318
 
187
319
  MESSAGE_LIMIT = 200
320
+ DETAIL_LIMIT = 2_000
321
+ BACKTRACE_LIMIT = 5
188
322
 
189
- attr_reader :executed, :failed, :failure_classes, :failure_messages
323
+ attr_reader :executed, :failed, :failure_classes, :failure_messages, :failure_details
190
324
  attr_accessor :runtime
191
325
 
192
326
  def initialize(recorder = nil)
@@ -195,6 +329,7 @@ module Saboteur
195
329
  @failed = []
196
330
  @failure_classes = {}
197
331
  @failure_messages = {}
332
+ @failure_details = {}
198
333
  @runtime = 0.0
199
334
  end
200
335
 
@@ -213,12 +348,43 @@ module Saboteur
213
348
  exception = notification.example.execution_result.exception
214
349
  @failure_classes[notification.example.id] = exception&.class&.name
215
350
  @failure_messages[notification.example.id] = first_line(exception&.message)
351
+ @failure_details[notification.example.id] = detail_for(exception)
216
352
  end
217
353
 
218
354
  private
219
355
 
356
+ # The full failure, class and first frames: a neutral re-run that
357
+ # fails needs enough context to explain why (the compact first line
358
+ # alone reads as just "expected: 0").
359
+ def detail_for(exception)
360
+ return nil unless exception
361
+
362
+ message = Text.utf8("#{exception.class.name}: #{exception.message}")
363
+ ([message] + relevant_frames(exception)).join("\n")[0, DETAIL_LIMIT]
364
+ rescue StandardError
365
+ exception.message.to_s.dump[0, DETAIL_LIMIT]
366
+ end
367
+
368
+ # An expectation failure leads with rspec's own frames (notify_failure,
369
+ # handle_failure); under the cap they would crowd out the spec line.
370
+ # Keep the project frames; fall back to the raw ones when rspec itself
371
+ # raised, so the detail never comes back empty.
372
+ def relevant_frames(exception)
373
+ frames = Array(exception.backtrace).map { |frame| Text.utf8(frame.to_s) }
374
+ project = frames.reject { |frame| frame.include?("/gems/rspec") }
375
+ (project.empty? ? frames : project).first(BACKTRACE_LIMIT)
376
+ end
377
+
220
378
  def first_line(message)
221
- message.to_s.lines.map(&:strip).find { |line| !line.empty? }.to_s[0, MESSAGE_LIMIT]
379
+ # Exception messages can carry bytes invalid under the locale encoding
380
+ # (e.g. a path with raw bytes); scrub before splitting, or .lines/.strip
381
+ # raises Encoding::CompatibilityError and the run reports error (CI #272).
382
+ Text.utf8(message.to_s).lines.map(&:strip).find { |line| !line.empty? }.to_s[0, MESSAGE_LIMIT]
383
+ rescue StandardError
384
+ # Text.utf8 is mutable code: when the mutant under test breaks it,
385
+ # the raise must not flip the run to error - the recorded failure
386
+ # is what classifies the kill. dump is ASCII-safe for any bytes.
387
+ message.to_s.dump[0, MESSAGE_LIMIT]
222
388
  end
223
389
  end
224
390
  end
@@ -23,7 +23,7 @@ module Saboteur
23
23
  def mutations_for(subject)
24
24
  found = []
25
25
  tokens = Prism.lex(subject.file_source).value.map(&:first)
26
- visit(subject.node.body, subject, found, tokens)
26
+ visit(subject.body_node, subject, found, tokens)
27
27
  found
28
28
  end
29
29
 
@@ -8,11 +8,16 @@ require_relative "literal_replacement"
8
8
  require_relative "argument_removal"
9
9
  require_relative "chain_removal"
10
10
  require_relative "pair_removal"
11
+ require_relative "element_removal"
11
12
  require_relative "endpoint_replacement"
12
13
  require_relative "operator_registry"
13
14
  require_relative "range_replacement"
14
15
  require_relative "operator_assignment"
15
16
  require_relative "condition_negation"
17
+ require_relative "method_swap"
18
+ require_relative "operand_replacement"
19
+ require_relative "interpolation_removal"
20
+ require_relative "regex_replacement"
16
21
 
17
22
  module Saboteur
18
23
  module MutationSource
@@ -61,11 +66,16 @@ module Saboteur
61
66
  @literal_replacement = LiteralReplacement.new(arid: arid)
62
67
  @argument_removal = ArgumentRemoval.new(arid: arid)
63
68
  @pair_removal = PairRemoval.new(arid: arid)
69
+ @element_removal = ElementRemoval.new(arid: arid)
64
70
  @chain_removal = ChainRemoval.new(arid: arid)
65
71
  @endpoint_replacement = EndpointReplacement.new(arid: arid)
66
72
  @range_replacement = RangeReplacement.new(arid: arid)
67
73
  @operator_assignment = OperatorAssignment.new(arid: arid)
68
74
  @condition_negation = ConditionNegation.new(arid: arid)
75
+ @method_swap = MethodSwap.new(arid: arid)
76
+ @operand_replacement = OperandReplacement.new(arid: arid)
77
+ @interpolation_removal = InterpolationRemoval.new(arid: arid)
78
+ @regex_replacement = RegexReplacement.new(arid: arid)
69
79
  end
70
80
 
71
81
  def name
@@ -73,25 +83,26 @@ module Saboteur
73
83
  end
74
84
 
75
85
  def mutations_for(subject)
76
- body = subject.node.body
86
+ body = subject.body_node
77
87
  @subject = subject
78
88
  @found = []
79
89
  coarse(body)
80
90
  visit(body)
81
- @found.concat(@filter_replacement.mutations_for(subject))
82
- @found.concat(@literal_replacement.mutations_for(subject))
83
- @found.concat(@argument_removal.mutations_for(subject))
84
- @found.concat(@pair_removal.mutations_for(subject))
85
- @found.concat(@chain_removal.mutations_for(subject))
86
- @found.concat(@endpoint_replacement.mutations_for(subject))
87
- @found.concat(@range_replacement.mutations_for(subject))
88
- @found.concat(@operator_assignment.mutations_for(subject))
89
- @found.concat(@condition_negation.mutations_for(subject))
91
+ @found.concat(delegated.flat_map { |source| source.mutations_for(subject) })
90
92
  @found.select { |mutation| @operators.include?(mutation.operator) }
91
93
  ensure
92
94
  @subject = nil
93
95
  end
94
96
 
97
+ # The single-purpose sources run after the shared walk, in registration
98
+ # order; each filters its own operator through the same policy below.
99
+ def delegated
100
+ [@filter_replacement, @literal_replacement, @argument_removal, @pair_removal, @element_removal,
101
+ @chain_removal,
102
+ @endpoint_replacement, @range_replacement, @operator_assignment, @condition_negation,
103
+ @method_swap, @operand_replacement, @interpolation_removal, @regex_replacement]
104
+ end
105
+
95
106
  private
96
107
 
97
108
  # --- coarse ---------------------------------------------------------
@@ -111,7 +122,7 @@ module Saboteur
111
122
 
112
123
  # Nothing but logging: replacing it changes nothing a test can see.
113
124
  def nothing_to_remove?(statements)
114
- statements.body.all? { |statement| arid?(statement) }
125
+ statements.body.all? { |statement| arid_statement?(statement) }
115
126
  end
116
127
 
117
128
  # A raise that the method's own rescue catches is absorbed by design. It
@@ -150,7 +161,7 @@ module Saboteur
150
161
  def visit_statements(node)
151
162
  statements = node.body
152
163
  statements.each do |statement|
153
- next if arid?(statement)
164
+ next if arid_statement?(statement)
154
165
 
155
166
  removable_here = statements.drop(1).any? && removable?(statement)
156
167
  add("statement_removal", statement, "nil") if removable_here
@@ -185,9 +196,12 @@ module Saboteur
185
196
  add("safe_navigation", node.call_operator_loc, ".") if node.safe_navigation?
186
197
  end
187
198
 
199
+ # Only the infix spelling is binary: an operator call has no call
200
+ # operator, and its receiver, message, single argument and lack of a
201
+ # block all follow from that (a block binds to the last operand's own
202
+ # call, never to the operator).
188
203
  def binary?(node)
189
- node.receiver && node.message_loc && node.call_operator_loc.nil? &&
190
- node.arguments&.arguments&.size == 1 && node.block.nil?
204
+ node.call_operator_loc.nil?
191
205
  end
192
206
 
193
207
  def numeric_operands?(node)
@@ -247,6 +261,22 @@ module Saboteur
247
261
  @arid.any? { |pattern| pattern.match?(source) }
248
262
  end
249
263
 
264
+ # An arid call is noise only in itself. A block on that call is real
265
+ # work (a notifier wrapping a computation), so the statement still
266
+ # mutates and keeps its body out of nothing_to_remove?; the walk
267
+ # descends into the block through the normal visit below.
268
+ def arid_statement?(statement)
269
+ return false unless arid?(statement)
270
+
271
+ block = statement.is_a?(Prism::CallNode) ? statement.block : nil
272
+ body = block.is_a?(Prism::BlockNode) ? block.body : nil
273
+ # A do-block with rescue/ensure parses its body as a BeginNode, not
274
+ # a StatementsNode (#464): its clauses are real work, not noise.
275
+ return false if body && !body.is_a?(Prism::StatementsNode)
276
+
277
+ body.nil? || body.body.all? { |inner| arid_statement?(inner) }
278
+ end
279
+
250
280
  # node_or_location: a Prism node or location (both have offsets and slice).
251
281
  def add(operator, node_or_location, replacement, coarse: false)
252
282
  location = node_or_location
@@ -33,7 +33,7 @@ module Saboteur
33
33
 
34
34
  def mutations_for(subject)
35
35
  found = []
36
- visit(subject.node.body, subject, found)
36
+ visit(subject.body_node, subject, found)
37
37
  found
38
38
  end
39
39
 
@@ -19,7 +19,7 @@ module Saboteur
19
19
 
20
20
  def mutations_for(subject)
21
21
  found = []
22
- visit(subject.node.body, subject, found)
22
+ visit(subject.body_node, subject, found)
23
23
  found
24
24
  end
25
25
 
@@ -0,0 +1,66 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require "prism"
5
+ require_relative "../text"
6
+ require_relative "base"
7
+ require_relative "pair_removal"
8
+ require_relative "element_removal"
9
+ require_relative "operator_registry"
10
+
11
+ module Saboteur
12
+ module MutationSource
13
+ # Mutations for a constant hash or array literal (a ConstantSubject,
14
+ # issue #306): pair_removal on the hash's entries, element_removal on
15
+ # the array's elements. The other operators stay method-only. Every
16
+ # entry is proposed here; in --since mode the diff scope narrows the
17
+ # proposals to the changed entries, and a configured operator exclusion
18
+ # applies as usual.
19
+ class ConstantLiterals < Base
20
+ def initialize(operators: [], exclude_operators: [], arid: [])
21
+ super()
22
+ @operators = OperatorRegistry.select(operators, exclude_operators)
23
+ @arid = arid.map { |pattern| Regexp.new(pattern) }
24
+ @pair_removal = PairRemoval.new
25
+ @element_removal = ElementRemoval.new
26
+ end
27
+
28
+ def name
29
+ "builtin"
30
+ end
31
+
32
+ def mutations_for(subject)
33
+ literal = subject.literal_node
34
+ found = []
35
+ if literal.is_a?(Prism::HashNode) && @operators.include?("pair_removal")
36
+ found.concat(@pair_removal.mutations_for(subject, root: literal))
37
+ end
38
+ if literal.is_a?(Prism::ArrayNode) && @operators.include?("element_removal")
39
+ found.concat(@element_removal.mutations_for(subject, root: literal))
40
+ end
41
+ found.reject { |mutation| arid?(subject, mutation) }
42
+ end
43
+
44
+ private
45
+
46
+ # An arid entry (logging or metrics config) mutates nothing a test
47
+ # can see - the same rule the operators apply to statements inside a
48
+ # method, here at entry level because a constant has no statements.
49
+ def arid?(subject, mutation)
50
+ mutation.edits.any? do |edit|
51
+ # file_source is a binread: retag losslessly before the UTF-8
52
+ # pattern match, and keep the mutant when the bytes still cannot
53
+ # be judged - a crash here would hide it instead.
54
+ slice = Text.utf8_identity(subject.file_source[edit.start_byte...edit.end_byte])
55
+ @arid.any? { |pattern| safe_match?(pattern, slice) }
56
+ end
57
+ end
58
+
59
+ def safe_match?(pattern, slice)
60
+ pattern.match?(slice)
61
+ rescue Encoding::CompatibilityError, ArgumentError
62
+ false
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,73 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require "prism"
5
+ require_relative "base"
6
+ require_relative "entry_edits"
7
+
8
+ module Saboteur
9
+ module MutationSource
10
+ # Deletes one element from an array literal.
11
+ class ElementRemoval < Base
12
+ include EntryEdits
13
+
14
+ # Nested scopes (a def, a class) are separate subjects; their arrays
15
+ # are not part of the enclosing subject.
16
+ BOUNDARIES = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode,
17
+ Prism::ParametersNode, Prism::BlockParametersNode].freeze
18
+
19
+ def initialize(arid: []) # rubocop:disable Lint/MissingSuper
20
+ @arid = arid.map { |pattern| Regexp.new(pattern) }
21
+ end
22
+
23
+ def name
24
+ "element_removal"
25
+ end
26
+
27
+ # root: the node to walk instead of the subject's method body (a
28
+ # constant's literal, see MutationSource::ConstantLiterals).
29
+ def mutations_for(subject, root: nil)
30
+ found = []
31
+ tokens = Prism.lex(subject.file_source).value.map(&:first)
32
+ visit(root || subject.body_node, subject, found, tokens)
33
+ found
34
+ end
35
+
36
+ private
37
+
38
+ def visit(node, subject, found, tokens)
39
+ return if node.nil? || BOUNDARIES.any? { |type| node.is_a?(type) }
40
+
41
+ if node.is_a?(Prism::StatementsNode)
42
+ # An arid statement (logging, metrics) mutates nothing a test can
43
+ # see; leave its arrays alone, like the other removal operators.
44
+ node.body.each do |child|
45
+ visit(child, subject, found, tokens) unless @arid.any? { |p| p.match?(child.slice) }
46
+ end
47
+ return
48
+ end
49
+ remove_elements(node, subject, found, tokens) if node.is_a?(Prism::ArrayNode)
50
+ node.compact_child_nodes.each { |child| visit(child, subject, found, tokens) }
51
+ end
52
+
53
+ def remove_elements(node, subject, found, tokens)
54
+ elements = node.elements
55
+ elements.each_with_index do |element, index|
56
+ # A splat's separator handling is fragile (`[*A, 2]` vs `[2, *A]`)
57
+ # and removing one can silently change what expands where; leave
58
+ # splats intact, like pair_removal does.
59
+ next if heredoc?(element) || element.is_a?(Prism::SplatNode)
60
+
61
+ edits = [delete_entry(subject, element.location)]
62
+ separator = entry_separator(elements, index, ending_for(node), tokens)
63
+ edits << delete_entry(subject, separator) if separator
64
+ found << Mutation.new(subject_id: subject.id, operator: name, edits: edits, source: name)
65
+ end
66
+ end
67
+
68
+ def ending_for(node)
69
+ node.closing_loc&.start_offset || node.location.end_offset
70
+ end
71
+ end
72
+ end
73
+ end
@@ -23,7 +23,7 @@ module Saboteur
23
23
 
24
24
  def mutations_for(subject)
25
25
  found = []
26
- visit(subject.node.body, subject, found)
26
+ visit(subject.body_node, subject, found)
27
27
  found
28
28
  end
29
29
 
@@ -0,0 +1,44 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ module Saboteur
5
+ module MutationSource
6
+ # The byte edits that remove one entry of a comma-separated literal (a
7
+ # hash pair or an array element): the entry itself plus one comma, so
8
+ # the rest of the literal still parses.
9
+ module EntryEdits
10
+ # Removes the entry's bytes.
11
+ def delete_entry(subject, location)
12
+ Edit.new(file: subject.file, start_byte: location.start_offset,
13
+ end_byte: location.end_offset, replacement: "")
14
+ end
15
+
16
+ # The comma after the entry, else the comma before it. `before` is the
17
+ # node preceding the whole entry list (a call's last positional
18
+ # argument before its keywords) when there is one. An entry with no
19
+ # separator - the only entry, or whitespace-separated as in %w[] -
20
+ # needs no second edit.
21
+ def entry_separator(entries, index, ending, tokens, before: nil)
22
+ entry = entries[index]
23
+ after = comma_between(entry.location.end_offset, ending, tokens)
24
+ return after if after
25
+
26
+ previous = entries[0...index].last || before
27
+ comma_between(previous.location.end_offset, entry.location.start_offset, tokens) if previous
28
+ end
29
+
30
+ def comma_between(first, last, tokens)
31
+ tokens.find do |token|
32
+ token.type == :COMMA && token.location.start_offset >= first && token.location.end_offset <= last
33
+ end&.location
34
+ end
35
+
36
+ # A heredoc's body lives on later lines: removing the entry that opens
37
+ # it would leave the body behind, so such entries are left alone.
38
+ def heredoc?(node)
39
+ (node.respond_to?(:opening_loc) && node.opening_loc&.slice&.start_with?("<<")) ||
40
+ node.compact_child_nodes.any? { |child| heredoc?(child) }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -23,7 +23,7 @@ module Saboteur
23
23
 
24
24
  def mutations_for(subject)
25
25
  found = []
26
- visit(subject.node.body, subject, found)
26
+ visit(subject.body_node, subject, found)
27
27
  found
28
28
  end
29
29