evilution 1.1.0 → 1.2.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 (65) hide show
  1. checksums.yaml +4 -4
  2. data/.beads/interactions.jsonl +20 -0
  3. data/CHANGELOG.md +39 -0
  4. data/README.md +141 -11
  5. data/docs/architecture.md +20 -5
  6. data/docs/isolation.md +3 -4
  7. data/exe/evil +8 -1
  8. data/exe/evilution +8 -1
  9. data/lib/evilution/ast/local_reads.rb +43 -0
  10. data/lib/evilution/cli/exit_guard.rb +59 -0
  11. data/lib/evilution/cli/parser/options_builder.rb +1 -0
  12. data/lib/evilution/cli.rb +1 -0
  13. data/lib/evilution/config.rb +3 -2
  14. data/lib/evilution/diagnostic.rb +22 -0
  15. data/lib/evilution/integration/loading/concern_state_cleaner.rb +20 -4
  16. data/lib/evilution/integration/loading/reeval_warning_filter.rb +72 -0
  17. data/lib/evilution/integration/loading/source_evaluator.rb +4 -1
  18. data/lib/evilution/integration/minitest.rb +2 -1
  19. data/lib/evilution/integration/rspec/crash_detector_lifecycle.rb +9 -1
  20. data/lib/evilution/integration/rspec/state_guard/configuration_streams.rb +4 -1
  21. data/lib/evilution/integration/rspec/unresolved_spec_warner.rb +2 -1
  22. data/lib/evilution/integration/rspec.rb +48 -2
  23. data/lib/evilution/integration/test_unit/test_file_resolver.rb +2 -1
  24. data/lib/evilution/isolation/fork.rb +29 -8
  25. data/lib/evilution/mutator/operator/block_destructuring_expansion.rb +85 -0
  26. data/lib/evilution/mutator/operator/block_parameter_drop.rb +96 -0
  27. data/lib/evilution/mutator/operator/forwarding_super_to_explicit.rb +71 -0
  28. data/lib/evilution/mutator/operator/method_body_replacement.rb +10 -1
  29. data/lib/evilution/mutator/operator/method_body_to_raise.rb +59 -0
  30. data/lib/evilution/mutator/operator/method_body_to_super.rb +150 -0
  31. data/lib/evilution/mutator/operator/optional_default_injection.rb +71 -0
  32. data/lib/evilution/mutator/operator/optional_parameter_to_required.rb +41 -0
  33. data/lib/evilution/mutator/operator/typed_default_return.rb +84 -0
  34. data/lib/evilution/mutator/registry.rb +8 -0
  35. data/lib/evilution/process_supervisor.rb +20 -8
  36. data/lib/evilution/reporter/cli/item_formatters/neutral_group.rb +23 -0
  37. data/lib/evilution/reporter/cli/item_formatters/subject_score.rb +42 -0
  38. data/lib/evilution/reporter/cli/item_formatters/subject_score_group.rb +16 -0
  39. data/lib/evilution/reporter/cli/line_formatters/infra_retry_notice.rb +19 -0
  40. data/lib/evilution/reporter/cli/line_formatters/result_line.rb +27 -3
  41. data/lib/evilution/reporter/cli/line_formatters/score.rb +19 -1
  42. data/lib/evilution/reporter/cli/line_formatters/unresolved_targets.rb +35 -0
  43. data/lib/evilution/reporter/cli/metrics_block.rb +4 -0
  44. data/lib/evilution/reporter/cli/trailer.rb +11 -7
  45. data/lib/evilution/reporter/cli.rb +20 -4
  46. data/lib/evilution/reporter/json/subjects.rb +29 -0
  47. data/lib/evilution/reporter/json.rb +23 -1
  48. data/lib/evilution/result/mutation_result.rb +3 -2
  49. data/lib/evilution/result/neutral_reason.rb +35 -0
  50. data/lib/evilution/result/subject_score.rb +28 -0
  51. data/lib/evilution/result/subject_scorer.rb +37 -0
  52. data/lib/evilution/result/summary.rb +44 -2
  53. data/lib/evilution/runner/canary.rb +52 -5
  54. data/lib/evilution/runner/mutation_executor/infra_retry.rb +54 -0
  55. data/lib/evilution/runner/mutation_executor/neutralizer/baseline_failed.rb +9 -3
  56. data/lib/evilution/runner/mutation_executor/neutralizer/infra_error.rb +18 -1
  57. data/lib/evilution/runner/mutation_executor/result_cache.rb +11 -0
  58. data/lib/evilution/runner/mutation_executor/strategy/parallel.rb +19 -1
  59. data/lib/evilution/runner/mutation_executor.rb +32 -4
  60. data/lib/evilution/runner/report_publisher.rb +31 -9
  61. data/lib/evilution/runner/target_spec_audit.rb +43 -0
  62. data/lib/evilution/runner.rb +10 -1
  63. data/lib/evilution/version.rb +1 -1
  64. data/lib/evilution.rb +12 -0
  65. metadata +25 -2
@@ -55,6 +55,7 @@ class Evilution::CLI::Parser::OptionsBuilder
55
55
 
56
56
  def add_spec_filter_options(opts)
57
57
  opts.on("--min-score FLOAT", Float, "Minimum mutation score to pass") { |s| @options[:min_score] = s }
58
+ opts.on("--output FILE", "Write the report to FILE instead of stdout") { |f| @options[:output] = f }
58
59
  opts.on("--spec FILES", Array, "Spec files to run (comma-separated)") { |f| @options[:spec_files] = f }
59
60
  opts.on("--spec-dir DIR", "Include all specs in DIR") { |d| expand_spec_dir(d) }
60
61
  opts.on("--spec-pattern GLOB",
data/lib/evilution/cli.rb CHANGED
@@ -18,6 +18,7 @@ require_relative "cli/commands/session_diff"
18
18
  require_relative "cli/commands/session_gc"
19
19
  require_relative "cli/commands/compare"
20
20
  require_relative "cli/commands/run"
21
+ require_relative "cli/exit_guard"
21
22
 
22
23
  # @api private
23
24
  class Evilution::CLI
@@ -17,7 +17,7 @@ class Evilution::Config
17
17
  save_session: false, line_ranges: {}, spec_files: [], ignore_patterns: [],
18
18
  show_disabled: false, baseline_session: nil, skip_heredoc_literals: false,
19
19
  related_specs_heuristic: false, fallback_to_full_suite: false, preload: nil,
20
- spec_mappings: {}, spec_pattern: nil, example_targeting: true,
20
+ output: nil, spec_mappings: {}, spec_pattern: nil, example_targeting: true,
21
21
  example_targeting_fallback: :full_file, example_targeting_strategy: :lexical,
22
22
  example_targeting_cache: { max_files: 50, max_blocks: 10_000 },
23
23
  quiet_children: false, quiet_children_dir: "tmp/evilution_children",
@@ -25,7 +25,7 @@ class Evilution::Config
25
25
  }.freeze
26
26
 
27
27
  attr_reader :target_files, :schema_version, :timeout, :format,
28
- :target, :min_score, :integration, :verbose, :quiet,
28
+ :target, :min_score, :integration, :verbose, :quiet, :output,
29
29
  :jobs, :fail_fast, :baseline, :isolation, :incremental, :suggest_tests,
30
30
  :progress, :save_session, :line_ranges, :spec_files, :hooks,
31
31
  :ignore_patterns, :show_disabled, :baseline_session,
@@ -243,6 +243,7 @@ class Evilution::Config
243
243
  format: :to_sym.to_proc,
244
244
  target: nil,
245
245
  min_score: :to_f.to_proc,
246
+ output: nil,
246
247
  verbose: nil,
247
248
  quiet: nil,
248
249
  baseline: nil,
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../evilution"
4
+
5
+ # Evilution's own advisory messages -- "no matching spec found", and the like.
6
+ #
7
+ # These deliberately bypass Kernel#warn. That routes through Warning.warn,
8
+ # which a project is free to replace: the `warning` gem with a raising handler
9
+ # turns every Ruby warning into an exception, and both dry-schema and
10
+ # dry-monads configure exactly that in their spec_helper. Under such a project
11
+ # our advisory message would be promoted to a fatal error, so a mutation we
12
+ # meant to classify :unresolved would be reported :error instead -- 239 of
13
+ # dry-monads' 240 mutations, in the run that found this.
14
+ #
15
+ # A message about how evilution is behaving must never be able to fail a
16
+ # user's mutation. Writing straight to the stream keeps it a diagnostic.
17
+ # EV-df7u / GH #1588.
18
+ module Evilution::Diagnostic
19
+ def self.warn(message, stream: $stderr)
20
+ stream.puts(message)
21
+ end
22
+ end
@@ -11,6 +11,22 @@ require_relative "../../load_path/subpath_resolver"
11
11
  class Evilution::Integration::Loading::ConcernStateCleaner
12
12
  IVARS = %i[@_included_block @_prepended_block].freeze
13
13
 
14
+ # ObjectSpace hands us every Module in the VM, including ones that answer no
15
+ # message honestly. ActiveSupport::Deprecation::DeprecatedConstantProxy
16
+ # subclasses Module and undefines all of its instance methods, so a plain
17
+ # `mod.singleton_class` lands in its method_missing and emits the
18
+ # deprecation; where the deprecator's behaviour is :raise (grape's
19
+ # spec_helper sets that), the sweep blows up on a module it merely walked
20
+ # past. Calling through UnboundMethods bypasses the object's own method
21
+ # table, so a proxy is never given the chance to intercept -- and this holds
22
+ # for any method_missing-based wrapper, not just ActiveSupport's.
23
+ # EV-v2rc / GH #1581.
24
+ SINGLETON_CLASS = Object.instance_method(:singleton_class)
25
+ IVAR_DEFINED = Object.instance_method(:instance_variable_defined?)
26
+ IVAR_GET = Object.instance_method(:instance_variable_get)
27
+ REMOVE_IVAR = Object.instance_method(:remove_instance_variable)
28
+ private_constant :SINGLETON_CLASS, :IVAR_DEFINED, :IVAR_GET, :REMOVE_IVAR
29
+
14
30
  def initialize(subpath_resolver: Evilution::LoadPath::SubpathResolver.new)
15
31
  @subpath_resolver = subpath_resolver
16
32
  end
@@ -22,7 +38,7 @@ class Evilution::Integration::Loading::ConcernStateCleaner
22
38
  subpath = @subpath_resolver.call(file_path)
23
39
 
24
40
  ObjectSpace.each_object(Module) do |mod|
25
- next unless mod.singleton_class.ancestors.include?(ActiveSupport::Concern)
41
+ next unless SINGLETON_CLASS.bind_call(mod).ancestors.include?(ActiveSupport::Concern)
26
42
 
27
43
  clear_concern_ivars(mod, absolute, subpath)
28
44
  end
@@ -32,14 +48,14 @@ class Evilution::Integration::Loading::ConcernStateCleaner
32
48
 
33
49
  def clear_concern_ivars(mod, absolute, subpath)
34
50
  IVARS.each do |ivar|
35
- next unless mod.instance_variable_defined?(ivar)
51
+ next unless IVAR_DEFINED.bind_call(mod, ivar)
36
52
 
37
- block = mod.instance_variable_get(ivar)
53
+ block = IVAR_GET.bind_call(mod, ivar)
38
54
  block_file = block.source_location&.first
39
55
  next unless block_file
40
56
 
41
57
  expanded = File.expand_path(block_file)
42
- mod.remove_instance_variable(ivar) if source_matches?(expanded, absolute, subpath)
58
+ REMOVE_IVAR.bind_call(mod, ivar) if source_matches?(expanded, absolute, subpath)
43
59
  end
44
60
  end
45
61
 
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../loading"
4
+
5
+ # Suppresses exactly the Ruby warnings that re-evaluating a file is guaranteed
6
+ # to produce, and nothing else.
7
+ #
8
+ # Applying a mutation re-evaluates the file, which redefines its methods and
9
+ # reassigns its constants. Ruby reports that in verbose mode, and a project
10
+ # that also promotes warnings to exceptions -- dry-schema's `.rspec` carries
11
+ # `--warnings` while its spec_helper installs `Warning.process { |w| raise w }`
12
+ # -- fails every mutation on a message describing evilution's mechanism rather
13
+ # than the code under test.
14
+ #
15
+ # The blunt fix, `$VERBOSE = nil` around the eval, costs too much: the
16
+ # evaluated file then sees a different $VERBOSE than a normal load would (code
17
+ # branching on it takes the other path), and genuine warnings introduced BY a
18
+ # mutation are swallowed along with ours -- losing a kill that a raising
19
+ # handler would otherwise hand us. So the filter matches the specific
20
+ # mechanism messages instead and delegates everything else to whatever handler
21
+ # the project installed.
22
+ #
23
+ # EV-df7u / GH #1588.
24
+ class Evilution::Integration::Loading::ReevalWarningFilter
25
+ MECHANISM_WARNINGS = Regexp.union(
26
+ /method redefined; discarding old /,
27
+ /already initialized constant /,
28
+ /previous definition of .+ was here/
29
+ ).freeze
30
+
31
+ ACTIVE_KEY = :__evilution_reeval_warning_filter_active
32
+
33
+ # Prepended to Warning's singleton, so it sits AHEAD of any handler the
34
+ # project extended in (the `warning` gem extends Warning::Processor), and
35
+ # `super` still reaches that handler for everything it does not drop.
36
+ module Filter
37
+ def warn(message, *args, **kwargs)
38
+ filter = Evilution::Integration::Loading::ReevalWarningFilter
39
+ return if filter.active? && filter.mechanism_warning?(message)
40
+
41
+ super
42
+ end
43
+ end
44
+
45
+ class << self
46
+ def suppress
47
+ install
48
+ previous = Thread.current[ACTIVE_KEY]
49
+ Thread.current[ACTIVE_KEY] = true
50
+ yield
51
+ ensure
52
+ Thread.current[ACTIVE_KEY] = previous
53
+ end
54
+
55
+ def active?
56
+ Thread.current[ACTIVE_KEY] ? true : false
57
+ end
58
+
59
+ def mechanism_warning?(message)
60
+ MECHANISM_WARNINGS.match?(message)
61
+ end
62
+
63
+ # Idempotent: prepending twice would run the filter twice per warning.
64
+ # `target` exists so a test can install into a throwaway module -- once
65
+ # Warning's singleton has the filter there is no way to take it back off.
66
+ def install(target = Warning.singleton_class)
67
+ return if target.ancestors.include?(Filter)
68
+
69
+ target.prepend(Filter)
70
+ end
71
+ end
72
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../loading"
4
+ require_relative "reeval_warning_filter"
4
5
 
5
6
  # Evaluate source with __FILE__ set to the absolute original path so that
6
7
  # `require_relative` and `__dir__` resolve against the real source tree, where
@@ -17,6 +18,8 @@ class Evilution::Integration::Loading::SourceEvaluator
17
18
  # GH #1278), anchor the eval __FILE__ against PROJECT_ROOT so siblings
18
19
  # `require_relative` can find each other from the real source tree.
19
20
  absolute = File.expand_path(file_path, Evilution.project_base_dir)
20
- eval(source, TOPLEVEL_BINDING, absolute, 1)
21
+ Evilution::Integration::Loading::ReevalWarningFilter.suppress do
22
+ eval(source, TOPLEVEL_BINDING, absolute, 1)
23
+ end
21
24
  end
22
25
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "stringio"
4
4
  require_relative "base"
5
+ require_relative "../diagnostic"
5
6
  require_relative "minitest_crash_detector"
6
7
  require_relative "loading/test_load_path"
7
8
  require_relative "../spec_resolver"
@@ -276,7 +277,7 @@ class Evilution::Integration::Minitest < Evilution::Integration::Base
276
277
  return if @warned_files.include?(file_path)
277
278
 
278
279
  @warned_files << file_path
279
- warn unresolved_test_message(file_path)
280
+ Evilution::Diagnostic.warn(unresolved_test_message(file_path))
280
281
  end
281
282
 
282
283
  # Name both recovery paths when skipping :unresolved; when already falling
@@ -10,8 +10,16 @@ class Evilution::Integration::RSpec::CrashDetectorLifecycle
10
10
  @detector.reset
11
11
  else
12
12
  @detector = Evilution::Integration::CrashDetector.new(StringIO.new)
13
- ::RSpec.configuration.add_formatter(@detector)
14
13
  end
15
14
  @detector
16
15
  end
16
+
17
+ # The run rebuilds RSpec's formatter loader so its formatters are bound to the
18
+ # run's own streams (EV-m6xc / GH #1627), which drops any formatter registered
19
+ # before it — the detector included. It is registered per run instead.
20
+ def register
21
+ detector = current
22
+ ::RSpec.configuration.add_formatter(detector)
23
+ detector
24
+ end
17
25
  end
@@ -18,7 +18,10 @@ require_relative "internals"
18
18
  # writing the ivars directly: configuration#output_stream= is guarded (it warns
19
19
  # and no-ops once a reporter exists), so the public setter cannot put it back.
20
20
  class Evilution::Integration::RSpec::StateGuard::ConfigurationStreams
21
- IVARS = %i[@color_mode @output_stream @error_stream].freeze
21
+ # - @reporter and @formatter_loader -- both hold the stream they were built
22
+ # with, so the run drops them to rebuild against its own
23
+ # (EV-m6xc / GH #1627); the host's are restored here.
24
+ IVARS = %i[@color_mode @output_stream @error_stream @reporter @formatter_loader].freeze
22
25
 
23
26
  def snapshot
24
27
  config = ::RSpec.configuration
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../rspec"
4
+ require_relative "../../diagnostic"
4
5
 
5
6
  class Evilution::Integration::RSpec::UnresolvedSpecWarner
6
7
  def initialize
@@ -11,7 +12,7 @@ class Evilution::Integration::RSpec::UnresolvedSpecWarner
11
12
  return if @warned.include?(file_path)
12
13
 
13
14
  @warned << file_path
14
- warn message(file_path, fallback_to_full_suite)
15
+ Evilution::Diagnostic.warn(message(file_path, fallback_to_full_suite))
15
16
  end
16
17
 
17
18
  private
@@ -70,6 +70,26 @@ class Evilution::Integration::RSpec < Evilution::Integration::Base
70
70
  targets = @example_filter_applier.call(mutation, files)
71
71
  return @result_builder.unresolved_example(mutation) if targets.nil?
72
72
 
73
+ result = run_targets(targets)
74
+ return result unless confirm_survivor?(result, targets, files)
75
+
76
+ run_targets(files)
77
+ end
78
+
79
+ # Targeting runs a subset of the examples in a spec file, chosen by name. A
80
+ # mutation that subset does not catch looks like a survivor whether or not the
81
+ # rest of the file would have caught it, and a survivor nobody can reproduce
82
+ # is worse than a missed kill: it sends the reader to write a test that is
83
+ # already there (EV-f8h3 / GH #1624).
84
+ #
85
+ # So a survivor is re-run against the whole resolved file, and that run is the
86
+ # one reported. Only survivors pay for it, and only where the subset was
87
+ # actually narrower than the file.
88
+ def confirm_survivor?(result, targets, files)
89
+ result[:passed] && targets != files
90
+ end
91
+
92
+ def run_targets(targets)
73
93
  args = ["--format", "progress", "--no-color", "--order", "defined", *resolve_targets(targets)]
74
94
  command = "rspec #{args.join(" ")}"
75
95
 
@@ -115,10 +135,13 @@ class Evilution::Integration::RSpec < Evilution::Integration::Base
115
135
  end
116
136
 
117
137
  def execute_run(args, command)
118
- detector = @crash_detector_lifecycle.current
119
138
  snapshot = @state_guard.snapshot
120
139
  begin
121
- status = ::RSpec::Core::Runner.run(args, StringIO.new, StringIO.new)
140
+ out = StringIO.new
141
+ err = StringIO.new
142
+ claim_rspec_streams(out, err)
143
+ detector = @crash_detector_lifecycle.register
144
+ status = ::RSpec::Core::Runner.run(args, err, out)
122
145
  @result_builder.from_run(status, command, detector, examples_loaded:)
123
146
  rescue StandardError => e
124
147
  { passed: false, error: e.message, test_command: command }
@@ -127,6 +150,29 @@ class Evilution::Integration::RSpec < Evilution::Integration::Base
127
150
  end
128
151
  end
129
152
 
153
+ # RSpec redirects its own output to the streams it is handed only when the
154
+ # stream its configuration already holds is the current `$stdout`. In-process
155
+ # isolation swaps `$stdout` for a null IO around every mutation, and a
156
+ # configuration built before that swap — which is what `--preload` produces,
157
+ # since the project's spec_helper calls RSpec.configure — still holds the real
158
+ # stdout. The two no longer match, RSpec keeps its own stream, and the run's
159
+ # output lands on the real stdout ahead of the report (EV-m6xc / GH #1627).
160
+ #
161
+ # So the streams are claimed outright rather than offered. The ivars are
162
+ # written directly because `output_stream=` is guarded once a reporter exists.
163
+ # The reporter and its formatters are dropped along with them: both hold the
164
+ # stream they were built with, so leaving them in place would keep writing
165
+ # wherever the host pointed. StateGuard snapshots all four ivars and puts the
166
+ # host's back afterwards.
167
+ def claim_rspec_streams(out, err)
168
+ config = ::RSpec.configuration
169
+ config.instance_variable_set(:@output_stream, out)
170
+ config.instance_variable_set(:@error_stream, err)
171
+ %i[@reporter @formatter_loader].each do |ivar|
172
+ config.remove_instance_variable(ivar) if config.instance_variable_defined?(ivar)
173
+ end
174
+ end
175
+
130
176
  # Count of examples loaded into RSpec.world by this run. Used to distinguish
131
177
  # "test failed → mutation killed" (positive count, nonzero status) from
132
178
  # "spec file failed to load / RSpec returned nonzero with nothing observed"
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../test_unit"
4
+ require_relative "../../diagnostic"
4
5
 
5
6
  # Resolves the list of test files to load for a given mutation. Encapsulates
6
7
  # the explicit-override path, spec-selector lookup, fallback glob, and the
@@ -41,7 +42,7 @@ class Evilution::Integration::TestUnit::TestFileResolver
41
42
  return if @warned_files.include?(file_path)
42
43
 
43
44
  @warned_files << file_path
44
- warn unresolved_message(file_path)
45
+ Evilution::Diagnostic.warn(unresolved_message(file_path))
45
46
  end
46
47
 
47
48
  # Name both recovery paths when skipping :unresolved; when already falling
@@ -142,9 +142,9 @@ class Evilution::Isolation::Fork
142
142
  # Child exited. Drain any final payload that arrived between
143
143
  # wait_readable timeout and the reap (race) before declaring empty.
144
144
  final = read_payload(read_io, Process.clock_gettime(Process::CLOCK_MONOTONIC) + 0.1)
145
- return decode_payload(final) if final
145
+ return decode_payload(final, handle) if final
146
146
 
147
- return empty_result
147
+ return empty_result(handle)
148
148
  end
149
149
  end
150
150
 
@@ -161,11 +161,13 @@ class Evilution::Isolation::Fork
161
161
 
162
162
  if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
163
163
  @supervisor.terminate(handle, grace: GRACE_PERIOD)
164
- break
164
+ # The status now records OUR signal, so it cannot be reported as the
165
+ # child's own cause of death.
166
+ return decode_payload(payload, handle, terminated: true)
165
167
  end
166
168
  sleep 0.05
167
169
  end
168
- decode_payload(payload)
170
+ decode_payload(payload, handle)
169
171
  end
170
172
 
171
173
  def read_payload(read_io, deadline)
@@ -201,14 +203,33 @@ class Evilution::Isolation::Fork
201
203
  buf
202
204
  end
203
205
 
204
- def decode_payload(data)
205
- return empty_result if data.nil? || data.empty?
206
+ def decode_payload(data, handle = nil, terminated: false)
207
+ return empty_result(handle, terminated: terminated) if data.nil? || data.empty?
206
208
 
207
209
  { timeout: false }.merge(Marshal.load(data))
208
210
  end
209
211
 
210
- def empty_result
211
- { timeout: false, passed: false, error: "empty result from child" }
212
+ def empty_result(handle, terminated: false)
213
+ { timeout: false, passed: false, error: child_death_reason(handle, terminated: terminated) }
214
+ end
215
+
216
+ # A child that wrote no payload has usually left its stdout/stderr capture
217
+ # empty too, so its termination status is the only evidence of what happened.
218
+ # Reporting it separates a segfault from an OOM kill from a clean early exit,
219
+ # all of which reach this path identically.
220
+ def child_death_reason(handle, terminated: false)
221
+ return "child was terminated after the reap deadline without writing a result" if terminated
222
+
223
+ status = handle.nil? ? nil : handle.status
224
+ return "empty result from child" unless status.is_a?(Process::Status)
225
+ return "child exited #{status.exitstatus} without writing a result" unless status.signaled?
226
+
227
+ "child died on #{signal_name(status.termsig)} without writing a result"
228
+ end
229
+
230
+ def signal_name(termsig)
231
+ name = Signal.signame(termsig)
232
+ name.nil? ? "signal #{termsig}" : "SIG#{name}"
212
233
  end
213
234
 
214
235
  def timeout_result(handle)
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../operator"
6
+
7
+ # Flatten a destructuring group in a block's parameter list:
8
+ # `pairs.each_with_index { |(key, value), index| ... }` becomes
9
+ # `pairs.each_with_index { |key, value, index| ... }`.
10
+ #
11
+ # The two forms bind differently once the group has a sibling: given `[1, 2]`
12
+ # and `3`, the grouped form binds key=1, value=2, index=3, while the flat form
13
+ # binds key=[1, 2], value=3, index=nil. A survivor means nothing asserts the
14
+ # shape of what the yielder hands over.
15
+ #
16
+ # A lone group is left alone. With one parameter and a single yielded array —
17
+ # what `each` over pairs, and `Hash#each`, actually yield — both forms bind the
18
+ # same values, so the mutation would be behaviour-preserving and survive every
19
+ # suite. The forms do diverge there for a yielder that passes several values,
20
+ # but which one a block is handed cannot be known from the source, and a
21
+ # guaranteed survivor on the common idiom is the worse trade.
22
+ #
23
+ # Methods and lambdas are left alone as well: both bind by arity, so flattening
24
+ # a group in their signatures raises ArgumentError at every call site instead of
25
+ # changing how arguments are distributed.
26
+ class Evilution::Mutator::Operator::BlockDestructuringExpansion < Evilution::Mutator::Base
27
+ def visit_block_node(node)
28
+ expand_groups(node)
29
+ super
30
+ end
31
+
32
+ private
33
+
34
+ def expand_groups(node)
35
+ parameters = node.parameters
36
+ return unless parameters.is_a?(Prism::BlockParametersNode)
37
+
38
+ inner = parameters.parameters
39
+ return unless inner.is_a?(Prism::ParametersNode)
40
+ return unless siblings?(inner)
41
+
42
+ groups(inner).each { |group| flatten_group(group) }
43
+ end
44
+
45
+ # Both lists can hold a destructuring group: `|(a, b), c|` puts it in
46
+ # requireds, `|*rest, (a, b)|` in posts.
47
+ def groups(parameters)
48
+ [*parameters.requireds, *parameters.posts].grep(Prism::MultiTargetNode)
49
+ end
50
+
51
+ def siblings?(parameters)
52
+ parameter_count(parameters) > 1
53
+ end
54
+
55
+ # Block-local variables (`|a; tmp|`) are not parameters and so are not
56
+ # siblings — they change nothing about how arguments bind.
57
+ def parameter_count(parameters)
58
+ [
59
+ *parameters.requireds,
60
+ *parameters.optionals,
61
+ parameters.rest,
62
+ *parameters.posts,
63
+ *parameters.keywords,
64
+ parameters.keyword_rest,
65
+ parameters.block
66
+ ].compact.length
67
+ end
68
+
69
+ # Only the group's own parentheses go; anything nested inside keeps its own.
70
+ # A group written in a block's parameter list always carries them — the
71
+ # parenthesis-free MultiTargetNode belongs to multiple assignment, which is
72
+ # not reachable from here.
73
+ def flatten_group(group)
74
+ lparen = group.lparen_loc
75
+ rparen = group.rparen_loc
76
+
77
+ inner_start = lparen.start_offset + lparen.length
78
+ add_mutation(
79
+ offset: group.location.start_offset,
80
+ length: group.location.length,
81
+ replacement: byteslice_source(inner_start, rparen.start_offset - inner_start),
82
+ node: group
83
+ )
84
+ end
85
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../operator"
6
+
7
+ # Drop the single parameter of a block: `users.each { |u| touch(u) }` becomes
8
+ # `users.each { touch(u) }`.
9
+ #
10
+ # A block tolerates being handed more arguments than it declares, so the mutant
11
+ # runs until the body reads the dropped name, where it raises NameError. A
12
+ # survivor therefore means the block never ran on a path the suite asserts —
13
+ # the collection was empty, or the call was stubbed away.
14
+ #
15
+ # Not to be confused with BlockParamRemoval, which removes a method's `&block`
16
+ # parameter from its signature.
17
+ #
18
+ # Two shapes are deliberately left alone. A parameter the body never reads makes
19
+ # the mutation behaviour-preserving, so it would survive every suite and report
20
+ # a coverage gap that is not there. A lambda checks its arity, so dropping its
21
+ # parameter raises ArgumentError on every call, whatever the body does.
22
+ class Evilution::Mutator::Operator::BlockParameterDrop < Evilution::Mutator::Base
23
+ def visit_block_node(node)
24
+ drop_parameter(node)
25
+ super
26
+ end
27
+
28
+ private
29
+
30
+ def drop_parameter(node)
31
+ return unless droppable?(node)
32
+
33
+ location = node.parameters.location
34
+ leading = leading_space(location)
35
+
36
+ add_mutation(
37
+ offset: location.start_offset - leading,
38
+ length: location.length + leading,
39
+ replacement: "",
40
+ node: node
41
+ )
42
+ end
43
+
44
+ def droppable?(node)
45
+ parameters = node.parameters
46
+ return false unless parameters.is_a?(Prism::BlockParametersNode)
47
+ return false unless parameters.locals.empty?
48
+
49
+ names = single_parameter_names(parameters.parameters)
50
+ names.any? { |name| body_reads?(node.body, name) }
51
+ end
52
+
53
+ # The space the block keyword leaves in front of the parameters goes with
54
+ # them, so `{ |u| body }` mutates to `{ body }` and `do |u|` to `do` rather
55
+ # than leaving a double space or a trailing one.
56
+ def leading_space(location)
57
+ preceding = @file_source.byteslice(location.start_offset - 1, 1)
58
+ preceding == " " ? 1 : 0
59
+ end
60
+
61
+ # The names bound by a lone required parameter, which is either a plain name
62
+ # or a destructuring target (`|(key, value)|`). Anything else — a second
63
+ # parameter, an optional, a rest, a keyword, a block pass, or the implicit
64
+ # rest Prism reports for the trailing comma in `|key,|` — means this is not a
65
+ # single-parameter block, and numbered parameters carry no node to remove.
66
+ def single_parameter_names(parameters)
67
+ return [] unless parameters.is_a?(Prism::ParametersNode)
68
+ return [] unless parameters.requireds.length == 1
69
+ return [] unless other_parameters_absent?(parameters)
70
+
71
+ parameter_names(parameters.requireds.first)
72
+ end
73
+
74
+ # Post-required parameters are not checked: block syntax only produces them
75
+ # after a rest parameter, which is rejected here anyway.
76
+ def other_parameters_absent?(parameters)
77
+ parameters.optionals.empty? && parameters.keywords.empty? &&
78
+ parameters.rest.nil? && parameters.keyword_rest.nil? && parameters.block.nil?
79
+ end
80
+
81
+ def parameter_names(node)
82
+ case node
83
+ when Prism::RequiredParameterNode then [node.name.to_s]
84
+ when Prism::MultiTargetNode then node.lefts.flat_map { |child| parameter_names(child) }
85
+ else []
86
+ end
87
+ end
88
+
89
+ # An underscore-prefixed name announces a parameter that is not meant to be
90
+ # read, so it is treated as unused even where the body happens to mention it.
91
+ def body_reads?(body, name)
92
+ return false if name.start_with?("_")
93
+
94
+ Evilution::AST::LocalReads.new.call(body, name)
95
+ end
96
+ end