evilution 1.5.0 → 1.6.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 (228) hide show
  1. checksums.yaml +4 -4
  2. data/.beads/interactions.jsonl +1 -0
  3. data/.rubocop_todo.yml +30 -0
  4. data/CHANGELOG.md +59 -0
  5. data/README.md +285 -45
  6. data/docs/architecture.md +110 -12
  7. data/docs/ast_pattern_syntax.md +35 -7
  8. data/docs/combined_runs_spike.md +407 -0
  9. data/docs/integrations.md +1 -1
  10. data/docs/isolation.md +63 -2
  11. data/docs/migration-from-mutant.md +16 -11
  12. data/docs/mutant_schemata_spike.md +387 -0
  13. data/docs/operator_yield_study.md +576 -0
  14. data/docs/probe_first_spike.md +264 -0
  15. data/lib/evilution/ast/ancestor_lookup.rb +36 -0
  16. data/lib/evilution/ast/callback_declaration.rb +36 -3
  17. data/lib/evilution/ast/class_body.rb +62 -16
  18. data/lib/evilution/ast/def_region.rb +130 -0
  19. data/lib/evilution/ast/local_declaration.rb +37 -0
  20. data/lib/evilution/ast/parent_index.rb +50 -0
  21. data/lib/evilution/ast/parse_cache.rb +27 -0
  22. data/lib/evilution/ast/parser.rb +8 -3
  23. data/lib/evilution/ast/pattern/filter.rb +53 -12
  24. data/lib/evilution/ast/skip_filter.rb +83 -0
  25. data/lib/evilution/ast/skip_sites/debug_output.rb +67 -0
  26. data/lib/evilution/ast/skip_sites/error_messages.rb +74 -0
  27. data/lib/evilution/ast/skip_sites/logging.rb +93 -0
  28. data/lib/evilution/ast/skip_sites/message.rb +44 -0
  29. data/lib/evilution/ast/skip_sites/nodes.rb +105 -0
  30. data/lib/evilution/ast/skip_sites/presets.rb +31 -0
  31. data/lib/evilution/ast/skip_sites/purity.rb +69 -0
  32. data/lib/evilution/ast/skip_sites/requires.rb +40 -0
  33. data/lib/evilution/ast/skip_sites.rb +9 -0
  34. data/lib/evilution/ast/source_surgeon.rb +20 -2
  35. data/lib/evilution/ast/syntax_check.rb +10 -8
  36. data/lib/evilution/baseline/report.rb +11 -2
  37. data/lib/evilution/baseline.rb +31 -6
  38. data/lib/evilution/cache/keys.rb +140 -0
  39. data/lib/evilution/cache/run_fingerprint.rb +51 -0
  40. data/lib/evilution/cache/skeleton.rb +57 -0
  41. data/lib/evilution/cache/spec_digest.rb +39 -0
  42. data/lib/evilution/cache.rb +145 -42
  43. data/lib/evilution/class_tree.rb +38 -0
  44. data/lib/evilution/cli/command.rb +12 -4
  45. data/lib/evilution/cli/commands/run.rb +12 -3
  46. data/lib/evilution/cli/commands/subjects.rb +5 -2
  47. data/lib/evilution/cli/parser/options_builder.rb +67 -3
  48. data/lib/evilution/cli/printers/environment.rb +34 -4
  49. data/lib/evilution/cli/printers/session_detail.rb +20 -3
  50. data/lib/evilution/compare/categorizer.rb +2 -1
  51. data/lib/evilution/compare/normalizer.rb +3 -2
  52. data/lib/evilution/config/sources.rb +3 -0
  53. data/lib/evilution/config/validators/base.rb +9 -0
  54. data/lib/evilution/config/validators/since.rb +22 -0
  55. data/lib/evilution/config/validators/skip_sites.rb +24 -0
  56. data/lib/evilution/config/validators/timeout_ceiling.rb +10 -0
  57. data/lib/evilution/config/validators/timeout_factor.rb +10 -0
  58. data/lib/evilution/config/validators/timeout_floor.rb +10 -0
  59. data/lib/evilution/config.rb +122 -6
  60. data/lib/evilution/coverage/load_tracker.rb +50 -0
  61. data/lib/evilution/coverage/map.rb +42 -5
  62. data/lib/evilution/coverage/map_builder.rb +123 -22
  63. data/lib/evilution/coverage/map_store.rb +15 -5
  64. data/lib/evilution/coverage/recorder.rb +42 -3
  65. data/lib/evilution/coverage_example_filter.rb +35 -0
  66. data/lib/evilution/equivalent/heuristic/comment_marking.rb +14 -1
  67. data/lib/evilution/example_filter.rb +20 -0
  68. data/lib/evilution/git/changed_lines.rb +103 -0
  69. data/lib/evilution/git/since_scope.rb +77 -0
  70. data/lib/evilution/git/unified_diff.rb +68 -0
  71. data/lib/evilution/infra_crash.rb +26 -0
  72. data/lib/evilution/integration/base.rb +97 -6
  73. data/lib/evilution/integration/crash_detector.rb +46 -6
  74. data/lib/evilution/integration/known_failures.rb +4 -0
  75. data/lib/evilution/integration/loading/body_call_neutralizer.rb +49 -11
  76. data/lib/evilution/integration/loading/callback_equivalence.rb +62 -0
  77. data/lib/evilution/integration/loading/callback_redeclaration.rb +151 -36
  78. data/lib/evilution/integration/loading/concern_redeclaration.rb +16 -12
  79. data/lib/evilution/integration/loading/concern_registry.rb +87 -0
  80. data/lib/evilution/integration/loading/concern_state_cleaner.rb +19 -8
  81. data/lib/evilution/integration/loading/created_modules.rb +56 -0
  82. data/lib/evilution/integration/loading/frozen_namespaces.rb +80 -0
  83. data/lib/evilution/integration/loading/mutation_applier.rb +42 -11
  84. data/lib/evilution/integration/loading/neutralization.rb +41 -0
  85. data/lib/evilution/integration/loading/redefinition_recovery.rb +5 -3
  86. data/lib/evilution/integration/loading/superseded_modules.rb +22 -19
  87. data/lib/evilution/integration/minitest/stop_hook.rb +57 -0
  88. data/lib/evilution/integration/minitest.rb +22 -2
  89. data/lib/evilution/integration/minitest_crash_detector.rb +19 -0
  90. data/lib/evilution/integration/rspec/baseline_runner.rb +3 -2
  91. data/lib/evilution/integration/rspec/crash_detector_lifecycle.rb +6 -1
  92. data/lib/evilution/integration/rspec/example_filter_applier.rb +16 -0
  93. data/lib/evilution/integration/rspec/example_ids.rb +23 -0
  94. data/lib/evilution/integration/rspec/example_order.rb +86 -0
  95. data/lib/evilution/integration/rspec/killer_first.rb +72 -0
  96. data/lib/evilution/integration/rspec/result_builder.rb +7 -0
  97. data/lib/evilution/integration/rspec/state_guard/example_group_descendants.rb +44 -0
  98. data/lib/evilution/integration/rspec/state_guard.rb +2 -2
  99. data/lib/evilution/integration/rspec/target_resolver.rb +42 -0
  100. data/lib/evilution/integration/rspec/test_file_resolver.rb +4 -2
  101. data/lib/evilution/integration/rspec.rb +98 -47
  102. data/lib/evilution/integration/stop_policy.rb +49 -0
  103. data/lib/evilution/integration/test_unit/dispatcher.rb +20 -1
  104. data/lib/evilution/integration/test_unit/subject_class_registry.rb +6 -1
  105. data/lib/evilution/integration/test_unit.rb +28 -2
  106. data/lib/evilution/isolation/fork.rb +58 -13
  107. data/lib/evilution/isolation/in_process.rb +12 -1
  108. data/lib/evilution/line_set.rb +72 -0
  109. data/lib/evilution/mcp/info_tool/actions/environment.rb +12 -16
  110. data/lib/evilution/mcp/info_tool/actions/subjects.rb +3 -5
  111. data/lib/evilution/mcp/info_tool/status_glossary.rb +15 -2
  112. data/lib/evilution/mcp/info_tool.rb +24 -17
  113. data/lib/evilution/mcp/mutate_tool/config_builder.rb +13 -1
  114. data/lib/evilution/mcp/mutate_tool/option_parser.rb +2 -2
  115. data/lib/evilution/mcp/mutate_tool/progress_streamer.rb +1 -1
  116. data/lib/evilution/mcp/mutate_tool/report_trimmer.rb +28 -5
  117. data/lib/evilution/mcp/mutate_tool/survived_enricher.rb +12 -7
  118. data/lib/evilution/mcp/mutate_tool.rb +32 -7
  119. data/lib/evilution/mcp/session_tool.rb +1 -2
  120. data/lib/evilution/mcp.rb +4 -0
  121. data/lib/evilution/mutation/line_diff.rb +111 -0
  122. data/lib/evilution/mutation.rb +17 -13
  123. data/lib/evilution/mutator/base.rb +121 -98
  124. data/lib/evilution/mutator/constant_value_subjects.rb +29 -1
  125. data/lib/evilution/mutator/line_range_filter.rb +27 -0
  126. data/lib/evilution/mutator/load_sources.rb +88 -0
  127. data/lib/evilution/mutator/operator/alias_removal.rb +2 -5
  128. data/lib/evilution/mutator/operator/argument_method_call_replacement.rb +3 -0
  129. data/lib/evilution/mutator/operator/attr_accessor_swap.rb +2 -5
  130. data/lib/evilution/mutator/operator/constant_namespace_strip.rb +3 -3
  131. data/lib/evilution/mutator/operator/freeze_removal.rb +29 -11
  132. data/lib/evilution/mutator/operator/method_call_removal.rb +3 -1
  133. data/lib/evilution/mutator/operator/mixin_removal.rb +2 -5
  134. data/lib/evilution/mutator/operator/superclass_removal.rb +2 -5
  135. data/lib/evilution/mutator/operator/symbol_literal.rb +1 -1
  136. data/lib/evilution/mutator/operator/visibility_removal.rb +2 -5
  137. data/lib/evilution/mutator/registry.rb +11 -5
  138. data/lib/evilution/mutator/restore_source_cache.rb +41 -0
  139. data/lib/evilution/parallel/pool.rb +11 -4
  140. data/lib/evilution/parallel/work_queue/dispatcher/result_stream.rb +40 -0
  141. data/lib/evilution/parallel/work_queue/dispatcher.rb +20 -7
  142. data/lib/evilution/parallel/work_queue/worker/loop.rb +7 -2
  143. data/lib/evilution/parallel/work_queue/worker.rb +10 -3
  144. data/lib/evilution/parallel/work_queue/worker_stat.rb +7 -0
  145. data/lib/evilution/parallel/work_queue.rb +69 -12
  146. data/lib/evilution/parallel_db_warning.rb +39 -12
  147. data/lib/evilution/process_supervisor.rb +22 -2
  148. data/lib/evilution/reporter/cli/item_formatters/coverage_gap.rb +13 -2
  149. data/lib/evilution/reporter/cli/item_formatters/subject_score.rb +5 -1
  150. data/lib/evilution/reporter/cli/line_formatters/mutations.rb +9 -2
  151. data/lib/evilution/reporter/cli/line_formatters/result_line.rb +9 -1
  152. data/lib/evilution/reporter/cli/line_formatters/skipped_sites.rb +14 -0
  153. data/lib/evilution/reporter/cli/line_formatters/uncovered_code.rb +17 -0
  154. data/lib/evilution/reporter/cli/metrics_block.rb +4 -0
  155. data/lib/evilution/reporter/cli/trailer.rb +4 -4
  156. data/lib/evilution/reporter/cli.rb +9 -2
  157. data/lib/evilution/reporter/html/assets/style.css +8 -0
  158. data/lib/evilution/reporter/html/sections/file_section.rb +9 -0
  159. data/lib/evilution/reporter/html/sections/uncovered_details.rb +27 -0
  160. data/lib/evilution/reporter/html/templates/file_section.html.erb +2 -1
  161. data/lib/evilution/reporter/html/templates/summary_cards.html.erb +3 -0
  162. data/lib/evilution/reporter/html/templates/uncovered_details.html.erb +11 -0
  163. data/lib/evilution/reporter/json/mutation_detail.rb +77 -0
  164. data/lib/evilution/reporter/json/subjects.rb +1 -0
  165. data/lib/evilution/reporter/json/timings.rb +42 -0
  166. data/lib/evilution/reporter/json.rb +13 -50
  167. data/lib/evilution/result/coverage_gap.rb +5 -0
  168. data/lib/evilution/result/mutation_result.rb +55 -3
  169. data/lib/evilution/result/mutation_timings.rb +54 -0
  170. data/lib/evilution/result/run_timings.rb +20 -0
  171. data/lib/evilution/result/subject_score.rb +8 -1
  172. data/lib/evilution/result/subject_scorer.rb +5 -3
  173. data/lib/evilution/result/summary.rb +49 -12
  174. data/lib/evilution/runner/baseline_runner.rb +47 -5
  175. data/lib/evilution/runner/diagnostics.rb +0 -13
  176. data/lib/evilution/runner/isolation_resolver.rb +16 -4
  177. data/lib/evilution/runner/mutation_executor/isolated_run.rb +34 -0
  178. data/lib/evilution/runner/mutation_executor/kill_history.rb +66 -0
  179. data/lib/evilution/runner/mutation_executor/mutation_runner.rb +31 -12
  180. data/lib/evilution/runner/mutation_executor/neutralizer/baseline_failed.rb +1 -0
  181. data/lib/evilution/runner/mutation_executor/neutralizer/infra_error.rb +6 -20
  182. data/lib/evilution/runner/mutation_executor/probe_first.rb +99 -0
  183. data/lib/evilution/runner/mutation_executor/probe_first_run.rb +59 -0
  184. data/lib/evilution/runner/mutation_executor/result_cache.rb +45 -14
  185. data/lib/evilution/runner/mutation_executor/result_notifier.rb +1 -1
  186. data/lib/evilution/runner/mutation_executor/result_packer.rb +54 -24
  187. data/lib/evilution/runner/mutation_executor/runless_verdict.rb +39 -0
  188. data/lib/evilution/runner/mutation_executor/strategy/parallel/hinted_items.rb +27 -0
  189. data/lib/evilution/runner/mutation_executor/strategy/parallel/ordered_delivery.rb +97 -0
  190. data/lib/evilution/runner/mutation_executor/strategy/parallel.rb +102 -83
  191. data/lib/evilution/runner/mutation_executor/timed_run.rb +33 -0
  192. data/lib/evilution/runner/mutation_executor/timeout_policy/adaptive.rb +67 -0
  193. data/lib/evilution/runner/mutation_executor/timeout_policy/base.rb +35 -0
  194. data/lib/evilution/runner/mutation_executor/timeout_policy/fixed.rb +17 -0
  195. data/lib/evilution/runner/mutation_executor/timeout_policy.rb +56 -0
  196. data/lib/evilution/runner/mutation_executor.rb +75 -10
  197. data/lib/evilution/runner/mutation_planner.rb +29 -21
  198. data/lib/evilution/runner/phase_clock.rb +22 -0
  199. data/lib/evilution/runner/report_publisher.rb +1 -1
  200. data/lib/evilution/runner/subject_pipeline.rb +18 -8
  201. data/lib/evilution/runner.rb +87 -26
  202. data/lib/evilution/session/diff.rb +9 -2
  203. data/lib/evilution/session/store.rb +17 -2
  204. data/lib/evilution/source_ast_cache.rb +10 -0
  205. data/lib/evilution/spec_ast_cache.rb +15 -0
  206. data/lib/evilution/spec_resolver/constant_references.rb +66 -0
  207. data/lib/evilution/spec_resolver/project_files.rb +50 -0
  208. data/lib/evilution/spec_resolver/sibling_spec_ranker.rb +111 -0
  209. data/lib/evilution/spec_resolver/spec_pattern.rb +19 -0
  210. data/lib/evilution/spec_resolver.rb +76 -65
  211. data/lib/evilution/spec_selector.rb +17 -13
  212. data/lib/evilution/subject.rb +7 -2
  213. data/lib/evilution/version.rb +1 -1
  214. data/lib/evilution/worker_database/copies.rb +73 -0
  215. data/lib/evilution/worker_database/schema.rb +39 -0
  216. data/lib/evilution/worker_database/sqlite_copy.rb +65 -0
  217. data/lib/evilution/worker_database.rb +214 -0
  218. data/schema/evilution.config.schema.json +74 -2
  219. metadata +84 -13
  220. data/lib/evilution/integration/rspec/state_guard/object_space_example_groups.rb +0 -42
  221. data/scripts/benchmark_density +0 -262
  222. data/scripts/benchmark_density.yml +0 -19
  223. data/scripts/canary_manifest.yml +0 -47
  224. data/scripts/compare_mutations +0 -421
  225. data/scripts/compare_mutations.yml +0 -24
  226. data/scripts/compare_targeting +0 -279
  227. data/scripts/compare_targeting.example.yml +0 -24
  228. data/scripts/mutant_json_adapter +0 -227
@@ -0,0 +1,264 @@
1
+ # Probe-first per subject: spike report
2
+
3
+ Status: **opt-in prototype** (`--probe-first` / `probe_first: true`, off by
4
+ default). Decision at the end.
5
+
6
+ ## Idea
7
+
8
+ Run one sentinel mutant per subject before the others: the method's whole body
9
+ replaced by a bare `raise` (`method_body_to_raise`). If even that survives, no
10
+ selected test calls the method on a path it checks, and the subject's other
11
+ mutants (10–50 per method) would almost all survive too, after a full run and a
12
+ [survivor confirmation](../README.md#survivor-confirmation) run each. Report them
13
+ as survivors implied by the sentinel, without running them.
14
+
15
+ The same idea one level down: run `statement_deletion` of each statement first,
16
+ and when deleting the statement survives, skip the other mutants inside it.
17
+
18
+ ## Prototype
19
+
20
+ - `Runner::MutationExecutor::ProbeFirst` picks the sentinel (the
21
+ `method_body_to_raise` mutant reported at the subject's own line), records
22
+ its verdict and answers which mutants it implies. A sentinel that survived or
23
+ is `uncovered` implies; killed, timeout, error, neutral do not. Subjects
24
+ without a sentinel (empty body, a body that is a single `raise`, constants,
25
+ class-body declarations) are never skipped. Nor is a method with a `rescue`
26
+ or `ensure` clause of its own: `method_body_to_raise` replaces only the
27
+ statements before the clause and keeps it, and the clause can catch the
28
+ `raise` (`rescue StandardError`) or return over it (`ensure` with a
29
+ `return`), so the method returns normally and the mutant survives while the
30
+ tests check the method's result. Such a mutant is not taken as a sentinel.
31
+ - `Runner::MutationExecutor::ProbeFirstRun` runs two batches through the
32
+ unchanged sequential or parallel strategy: all sentinels, then the rest. One
33
+ notifier spans both, so progress, `--fail-fast` and the logs see one run;
34
+ results come back in the original order.
35
+ - `RunlessVerdict` reports an implied mutant `survived` with
36
+ `implied_by: "method_body_to_raise"` (JSON field), without a run, the same way
37
+ it already reports `unparseable` and `uncovered`. Implied survivors are not
38
+ cached and get no confirmation run. `probe_first` joins the incremental
39
+ cache's run fingerprint.
40
+ - The statement level was measured by simulation only (below); it was not
41
+ built, because the simulation already rules it out.
42
+
43
+ ## Method
44
+
45
+ Corpus: the `scripts/benchmark_perf.yml` targets, on scratchpad copies of
46
+ `/tmp/ev-canaries`, `--jobs 2` (Redmine `--jobs 1`), one run each:
47
+
48
+ | Target | File | Tests |
49
+ |---|---|---|
50
+ | dry-monads | `lib/dry/monads/list.rb` | `spec/unit/list_spec.rb` (RSpec) |
51
+ | httparty | `lib/httparty.rb` | `spec/httparty_spec.rb` (RSpec) |
52
+ | grape | `lib/grape/declared_params_handler.rb` | `spec/grape/endpoint/declared_spec.rb` (RSpec) |
53
+ | i18n | `lib/i18n/backend/base.rb` | `test/i18n_test.rb` (Minitest) |
54
+ | redmine | `app/models/tracker.rb` | `test/unit/tracker_test.rb` (Minitest) |
55
+ | httparty, fixed | as httparty, with the cwd-relative fixture path in the spec made absolute | |
56
+ | dry-monads `maybe.rb` (extra) | `lib/dry/monads/maybe.rb` | `spec/unit/maybe_spec.rb` (RSpec) |
57
+
58
+ Per target: a full run (the verdicts every other number is checked against),
59
+ the same with `--probe-first`, and for the RSpec targets both again with
60
+ `--example-targeting coverage` (where `uncovered` applies). Mutants are matched
61
+ across runs by file, line, operator and diff.
62
+
63
+ - **Divergence**: implied survivors that the full run killed or timed out.
64
+ - **Time saved**: the summed `duration` of the implied mutants in the full run
65
+ (`killtime` saved); wall time from runs with the flag off and on alternated
66
+ (below), since the machine was shared with another long benchmark and
67
+ single runs taken at different times differed by up to 2x with nothing
68
+ implied at all.
69
+ - The statement level is simulated on the full run: a surviving
70
+ `statement_deletion` covering lines a..b implies every other mutant reported
71
+ on a..b.
72
+
73
+ ### Corpus artifacts
74
+
75
+ - **httparty**: `spec/httparty_spec.rb:226` opens `spec/fixtures/tiny.gif` by a
76
+ cwd-relative path, which raises `Errno::ENOENT` in the per-mutation sandbox.
77
+ Every mutant run against that example is "killed" (1412/1412 in the full
78
+ run), so every sentinel is killed and probe-first implies nothing. The
79
+ "httparty, fixed" copy makes the path absolute; its numbers are the ones that
80
+ count.
81
+ - **dry-monads `list.rb`**: re-evaluating the mutated file re-runs class-body
82
+ lines that cannot run twice, so every mutant is killed (0 survivors in every
83
+ run). Probe-first implies nothing there either.
84
+
85
+ ## Results: subject level (`method_body_to_raise`)
86
+
87
+ Lexical targeting (the default). "Killtime saved" is the summed duration, in
88
+ the full run, of the mutants the probe-first run reported without running
89
+ them.
90
+
91
+ | Target | Mutants | Sentinels survived | Implied survivors | Of those, killed in the full run | Killtime saved |
92
+ |---|---|---|---|---|---|
93
+ | i18n | 1837 | 9 / 18 | 520 | **0** | 22.9 s of 79.1 s (29%) |
94
+ | httparty, fixed | 1412 | 17 / 62 | 206 | **0** | 37.5 s of 226.1 s (17%) |
95
+ | dry-monads `maybe.rb` (extra) | 611 | 11 / 35 | 114 | **0** | 15.0 s of 96.6 s (16%) |
96
+ | redmine | 500 | 1 / 12 | 3 | **0** | 1.9 s of 240.4 s (1%) |
97
+ | grape | 693 | 0 / 11 | 0 | – | 0 |
98
+ | **Total** (without the artifact rows below) | 5053 | 38 / 138 | **843** | **0** | |
99
+ | httparty (artifact) | 1412 | 0 / 62 | 0 | – | 0 |
100
+ | dry-monads `list.rb` (artifact) | 923 | 0 / 35 | 0 | – | 0 |
101
+
102
+ `maybe.rb` was added because `list.rb`, the corpus' dry-monads target, kills
103
+ every mutant (see artifacts). The artifact rows are left out of the total:
104
+ the "httparty (artifact)" row is the same file as "httparty, fixed", and every
105
+ sentinel of both artifact rows is "killed" by the artifact, not by a test.
106
+ The implied sets of the probe-first runs agree with a simulation on the full
107
+ runs (httparty 206 = 206, i18n 520 vs 522, `maybe.rb` 114 vs 107; the
108
+ simulation guesses each subject's span from line numbers).
109
+
110
+ The i18n row is the probe-first run's with one method taken out: the runs were
111
+ taken before methods with their own `rescue` or `ensure` were excluded from
112
+ probing (see Prototype). Of the 39 sentinels that survived then, one belonged
113
+ to such a method, i18n's `translate_localization_format` (`rescue
114
+ MissingTranslationData => e`); its rescue is too narrow to catch the
115
+ sentinel's `RuntimeError`, and its 351 implied mutants all survived the full
116
+ run too, so leaving it unprobed costs those 351 skips (871 implied, 37.2 s of
117
+ killtime, before) and loses no kill. The other 38 surviving sentinels belong
118
+ to methods without a rescue or ensure clause, and so does every sentinel of
119
+ the other targets.
120
+
121
+ No implied survivor was killed in any full run: **divergence 0 of 843**. The
122
+ case that would diverge -- a test that rescues the sentinel's `RuntimeError`
123
+ around the call, or a matcher that expects some error, while a value mutant of
124
+ the same method is still caught -- did not occur in this corpus.
125
+
126
+ ### Overlap with `uncovered` (coverage targeting)
127
+
128
+ Both detect code no test exercises. With `--example-targeting coverage` (RSpec
129
+ only), `uncovered` already takes every mutant whose changed lines never ran,
130
+ so the sentinel itself is usually `uncovered` and what probe-first adds is the
131
+ mutants on lines that ran at load time: the `def` line (default argument
132
+ values, `optional_parameter_to_required`, `block_param_removal`) of a method
133
+ nobody calls, and lines coverage cannot vouch for.
134
+
135
+ | Target | `uncovered` | Probe-first implied on top | Their full-run verdict | Killtime saved on top |
136
+ |---|---|---|---|---|
137
+ | httparty, fixed | 209 | 21 | 21 survived | 3.9 s (1.7%) |
138
+ | dry-monads `maybe.rb` | 49 | 67 | 67 survived | 8.9 s (9.2%) |
139
+ | grape | 0 | 0 | – | 0 |
140
+ | httparty (artifact) | 209 | 21 | 21 "killed" (artifact) | – |
141
+ | dry-monads `list.rb` (artifact) | 33 | 3 | 3 "killed" (artifact) | – |
142
+
143
+ On the artifact targets the 24 implied mutants on top of `uncovered` are the
144
+ `def` lines of methods coverage shows are never called; the full run "kills"
145
+ them only through the artifacts (the fixture crash, the class-body re-run),
146
+ the same way it "kills" their `uncovered` neighbours. On the fixed httparty
147
+ copy the same 21 mutants survive.
148
+
149
+ So with coverage targeting probe-first adds little (2-9% of killtime). Its
150
+ value is where `uncovered` cannot reach: **Minitest and test-unit** (i18n and
151
+ redmine above, 29% and 1% of killtime), and RSpec runs that keep lexical
152
+ targeting.
153
+
154
+ ## Results: statement level (`statement_deletion`), simulated
155
+
156
+ | Target | Implied | Of those, killed in the full run |
157
+ |---|---|---|
158
+ | i18n | 818 | 60 (7%) |
159
+ | httparty, fixed | 92 | 16 (17%) |
160
+ | dry-monads `maybe.rb` | 53 | 11 (21%) |
161
+ | grape | 47 | 9 (19%) |
162
+ | redmine | 49 | 19 (39%) |
163
+
164
+ Deleting a statement that survives does not mean the statement is not
165
+ executed. The typical case is a guard: `raise ArgumentError, "..." unless
166
+ fields.is_a?(Array)` or `return if arg.blank?` can be deleted because no test
167
+ passes the bad input, but negating its condition makes it fire on good input
168
+ and is killed at once. Statement-level probing would hide real kills on every
169
+ target: **no-go**.
170
+
171
+ ## Wall time
172
+
173
+ Same evilution tree, `--probe-first` off and on alternated, two runs each
174
+ (seconds; the machine was shared with another benchmark, so single runs vary
175
+ by about 10%):
176
+
177
+ | Target | Off | On | Change |
178
+ |---|---|---|---|
179
+ | i18n, `--jobs 2` | 47.1, 39.1 | 31.4, 27.7 | **-31%** |
180
+ | i18n, `--jobs 1` | 88.6, 85.7 | 53.2, 49.4 | **-41%** |
181
+ | httparty, fixed, `--jobs 2` | 141.3, 127.3 | 114.4, 118.2 | **-13%** |
182
+ | grape, `--jobs 2` (nothing implied) | 65.9, 70.8 | 69.8, 69.7 | none (noise) |
183
+
184
+ The i18n rows were taken again once methods with their own `rescue` or
185
+ `ensure` were no longer probed (520 implied survivors in every "on" run; the
186
+ earlier runs, with 871 implied, gave -48% at `--jobs 2` and -50% at
187
+ `--jobs 1`). The wall time saved follows the killtime saved, and a little
188
+ more: an implied survivor also skips its survivor confirmation run. Verdicts
189
+ and scores were identical in every pair. (An interleaved
190
+ `scripts/benchmark_perf` run of the `HEAD` export against the worktree with
191
+ `--probe-first`, three repeats, before that change, gave the same picture:
192
+ i18n -43% / -51%, httparty fixed -14%, 0 lost kills; it also
193
+ showed grape 14% faster with nothing implied, which the same-tree pair above
194
+ does not, so that run's deltas carry a difference between the two trees and
195
+ the table above is the one to read.)
196
+
197
+ Where nothing is implied (grape, the fixture project, redmine) probe-first
198
+ costs nothing measurable: the sentinels are mutants the run had anyway, and
199
+ only their order changes.
200
+
201
+ ## Parallel runs (`--jobs`)
202
+
203
+ The prototype runs two batches, so a parallel run has a barrier: once the
204
+ sentinel queue is empty, workers idle until the last sentinel is done, and the
205
+ second batch starts a fresh pool.
206
+
207
+ - Measured at `--jobs 2`: idle time at the barrier (worker wall minus busy
208
+ time in the first pool) was 0.02–0.24 s per worker; the sentinel batch took
209
+ 0.3 s (i18n), 1.1 s (grape), 2.8 s (`maybe.rb`), 5.3 s (httparty) of
210
+ 22–101 s of execution; starting the second pool, about 0.04 s. Under 1% of
211
+ wall in every case, and i18n saves the same share at `--jobs 1` (no
212
+ barrier) as at `--jobs 2`.
213
+ - The barrier is bounded by one sentinel per worker, so it grows with
214
+ `--jobs` and with slow sentinels: the worst case is a sentinel that runs
215
+ into its time limit (an infinite loop the body-raise cannot cause, but a
216
+ slow spec file can), which holds every other worker for that long. A run
217
+ with fewer subjects than workers also leaves workers unused in the first
218
+ batch.
219
+ - The alternative without a barrier is one streaming queue with the
220
+ sentinels first, the decision made when the pool hands a mutant out (as
221
+ killer hints already are, `Strategy::Parallel::HintedItems`), and the
222
+ runless partition moved from before the run to dispatch time. A mutant
223
+ handed out while its sentinel is still running would then simply run
224
+ (correct, just not skipped): at most `jobs - 1` per subject, for the last
225
+ subjects only. Not built: the measured barrier is too small to pay for
226
+ moving the partition.
227
+ - A side effect: the sentinels' kills reach the kill history before the
228
+ other mutants of their subject run, so killer-first ordering has a killer
229
+ for every exercised subject from the start. No gain could be measured on
230
+ grape.
231
+
232
+ ## Decision
233
+
234
+ - **Statement level (`statement_deletion` as sentinel): no-go.** 7–39% of the
235
+ mutants it would skip are killed by a full run, on every target.
236
+ - **Subject level (`method_body_to_raise` as sentinel): go, as an opt-in.**
237
+ Divergence 0 of 843 implied survivors. On the targets with untested
238
+ methods worth the name it saves 13–41% of wall time (i18n -31% at
239
+ `--jobs 2`, -41% at `--jobs 1`; httparty -13%) and 16–29% of killtime
240
+ (`maybe.rb` 16%, i18n 29%); it costs nothing measurable
241
+ where there are none. It
242
+ stays behind `--probe-first` / `probe_first: true`, marked experimental in
243
+ the README, and joins the incremental cache's run fingerprint.
244
+ - **On by default: not yet.** Only four of seven targets had a single
245
+ unexercised method, so 0 divergences is evidence from few subjects (38
246
+ sentinels that survived); the case that would diverge (a caller that
247
+ rescues the sentinel's error) is common in real code and absent here
248
+ (a method that rescues it itself is not probed, see Prototype); an
249
+ implied survivor has no `test_command` or confirmation run of its own; and
250
+ with RSpec coverage targeting `uncovered` already reports most of the same
251
+ mutants on httparty (185 of 206, 90%) but under half on `maybe.rb` (47 of
252
+ 114, 41%). Revisit after a stability-matrix round run with
253
+ `--probe-first` on, comparing its verdicts against a full run: if the
254
+ divergence stays at 0, make it the default for runs without coverage
255
+ targeting (Minitest and test-unit first, where nothing else skips untested
256
+ code).
257
+
258
+ ## Reproducing
259
+
260
+ Full runs and probe-first runs: `scripts/benchmark_perf` with one target per
261
+ setting (the `--probe-first` and `--example-targeting coverage` flags in the
262
+ target's `args`). Divergence: match the `implied_by` entries of the
263
+ probe-first run's JSON to the full run's by file, line, operator and diff,
264
+ and read the full run's verdict.
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../ast"
4
+ require_relative "parent_index"
5
+ require_relative "parse_cache"
6
+
7
+ # The ancestors of a node within the file it was parsed from, found in the
8
+ # file's tree from the shared parse cache.
9
+ #
10
+ # Only the last source's index is kept: the mutations of a file are made one
11
+ # after the other, and an index per file would hold every file's tree for
12
+ # the whole run. The operators each read the file anew, so an equal source
13
+ # is taken over, and the next check against it compares no bytes.
14
+ class Evilution::AST::AncestorLookup
15
+ NONE = [].freeze
16
+ private_constant :NONE
17
+
18
+ # Nearest first, up to the top of the source; none without a source.
19
+ def ancestors(node, file_path, source)
20
+ return NONE if source.nil?
21
+
22
+ parent_index(file_path, source).enum_for(:each_ancestor, node)
23
+ end
24
+
25
+ private
26
+
27
+ def parent_index(file_path, source)
28
+ unless source.equal?(@indexed_source)
29
+ unless source == @indexed_source
30
+ @parent_index = Evilution::AST::ParentIndex.new(Evilution::AST::ParseCache.tree_for(file_path, source))
31
+ end
32
+ @indexed_source = source
33
+ end
34
+ @parent_index
35
+ end
36
+ end
@@ -16,13 +16,26 @@ require_relative "literal_callable"
16
16
  # when it is a block or a lambda. A condition or callback named by a symbol
17
17
  # is a method, and a subject of its own already.
18
18
  #
19
+ # So are the options of a declaration: `inclusion: { in: [true] }` and
20
+ # `if: :lift_round?` above decide what the validation checks and when, as
21
+ # much as a condition written out does.
22
+ #
19
23
  # Declarations are recognised by name -- `validate`, `validates`,
20
24
  # `validates_*`, `before_*`, `after_*`, `around_*` -- and only receiver-less,
21
25
  # directly in a class body: that is the call the mutated file re-runs.
22
26
  module Evilution::AST::CallbackDeclaration
23
27
  # body: the node holding the callable's body. declaration: the call it is
24
28
  # written in. label: how the declaration reads, for naming the subject.
25
- Callable = Data.define(:body, :declaration, :label)
29
+ Callable = Data.define(:body, :declaration, :label) do
30
+ # What mutations go into.
31
+ def node = body
32
+ end
33
+
34
+ # arguments: the declaration's argument list, options and all.
35
+ Options = Data.define(:arguments, :declaration, :label) do
36
+ # What mutations go into.
37
+ def node = arguments
38
+ end
26
39
 
27
40
  NAME = /\A(?:validate|validates|validates_\w+|(?:before|after|around)_\w+)\z/
28
41
  CONDITIONS = %w[if unless].freeze
@@ -31,10 +44,24 @@ module Evilution::AST::CallbackDeclaration
31
44
  # The callables of every callback declaration among a class body's direct
32
45
  # statements.
33
46
  def self.in_body(body)
47
+ declarations_in(body).flat_map { |declaration| callables(declaration) }
48
+ end
49
+
50
+ # The declarations among a class body's direct statements that are given
51
+ # options -- `key: value` pairs, written without braces -- with their
52
+ # argument lists. Options passed only as a double splat are not written out
53
+ # here, and a hash in braces is a positional argument, not options.
54
+ def self.options_in(body)
55
+ declarations_in(body).select { |declaration| options?(declaration) }.map do |declaration|
56
+ Options.new(arguments: declaration.arguments, declaration: declaration, label: label_of(declaration))
57
+ end
58
+ end
59
+
60
+ def self.declarations_in(body)
34
61
  statements = body.is_a?(Prism::BeginNode) ? body.statements : body
35
62
  return [] unless statements.is_a?(Prism::StatementsNode)
36
63
 
37
- statements.body.select { |node| match?(node) }.flat_map { |declaration| callables(declaration) }
64
+ statements.body.select { |node| match?(node) }
38
65
  end
39
66
 
40
67
  # Whether node is a callback declaration that can be run again on its own.
@@ -77,6 +104,11 @@ module Evilution::AST::CallbackDeclaration
77
104
  pair.is_a?(Prism::AssocNode) && pair.key.is_a?(Prism::SymbolNode) && CONDITIONS.include?(pair.key.unescaped)
78
105
  end
79
106
 
107
+ def self.options?(declaration)
108
+ options = arguments_of(declaration).last
109
+ options.is_a?(Prism::KeywordHashNode) && options.elements.any?(Prism::AssocNode)
110
+ end
111
+
80
112
  def self.arguments_of(call)
81
113
  call.arguments ? call.arguments.arguments : []
82
114
  end
@@ -87,5 +119,6 @@ module Evilution::AST::CallbackDeclaration
87
119
  node.compact_child_nodes.any? { |child| heredoc?(child) }
88
120
  end
89
121
 
90
- private_class_method :callables, :label_of, :bodies_of, :condition_bodies, :condition?, :arguments_of, :heredoc?
122
+ private_class_method :declarations_in, :options?, :callables, :label_of, :bodies_of,
123
+ :condition_bodies, :condition?, :arguments_of, :heredoc?
91
124
  end
@@ -2,21 +2,28 @@
2
2
 
3
3
  require "prism"
4
4
  require_relative "../ast"
5
+ require_relative "value_object_definition"
5
6
 
6
- # The body of a class, module or `class << self`: the code written outside
7
- # every method, where no method subject reaches it.
7
+ # The body of a class, module or `class << self`, or the block of a
8
+ # value-object definition (`Data.define(:a) do ... end`), which is the body of
9
+ # the class it defines: the code written outside every method, where no method
10
+ # subject reaches it.
8
11
  #
9
12
  # Operators that mutate such code attribute their mutants to one method, so
10
13
  # each mutant has exactly one subject. That method is the scope's anchor: the
11
14
  # first method written in the scope, in source order. Blocks of the body
12
15
  # (`included do ... end`) are searched, nested scopes are not: their methods
13
- # anchor the inner scope. A scope with no method of its own borrows the anchor
14
- # of the first singleton class in its body that has one, so a class whose
15
- # methods all sit in `class << self` is still reached.
16
+ # anchor the inner scope, and their code is attributed to it.
17
+ #
18
+ # A scope with no method of its own borrows the anchor of the first singleton
19
+ # class in its body that has one, so a class whose methods all sit in
20
+ # `class << self` is still reached. It never borrows from a value-object
21
+ # block: those methods belong to the class the block defines, and a mutant
22
+ # labelled with one of them would point at code it does not contain. A scope
23
+ # left without an anchor is listed by .unanchored_in, for operators whose
24
+ # code has a subject of its own to fall back on (a constant assignment).
16
25
  class Evilution::AST::ClassBody
17
26
  SCOPES = [Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode].freeze
18
- # Nodes whose children belong to something else: a method, or another scope.
19
- OPAQUE = [Prism::DefNode, *SCOPES].freeze
20
27
 
21
28
  # The location that closes each kind of conditional or loop.
22
29
  CLOSINGS = {
@@ -33,28 +40,59 @@ class Evilution::AST::ClassBody
33
40
  # first. A method anchors its own scope, and with it any enclosing scope
34
41
  # that borrows from it.
35
42
  def self.anchored_at(tree, line)
36
- scopes_in(tree).map { |scope| new(scope) }.select { |body| body.anchor_line == line }
43
+ bodies_in(tree).select { |body| body.anchor_line == line }
44
+ end
45
+
46
+ # The bodies with no anchor, outermost first: no method of their own, and
47
+ # none to borrow.
48
+ def self.unanchored_in(tree)
49
+ bodies_in(tree).select { |body| body.anchor_line.nil? }
50
+ end
51
+
52
+ # Every body of the tree, outermost first. The operators ask this of a
53
+ # file's tree once for each of its subjects, so the bodies of the last tree
54
+ # walked are kept until another is asked about or .clear! is called.
55
+ def self.bodies_in(tree)
56
+ return @bodies if @tree.equal?(tree)
57
+
58
+ @bodies = scopes_in(tree).map { |scope| new(scope) }.freeze
59
+ @tree = tree
60
+ @bodies
61
+ end
62
+
63
+ def self.clear!
64
+ @tree = nil
65
+ @bodies = nil
37
66
  end
38
67
 
39
68
  def self.scopes_in(node)
40
69
  node.compact_child_nodes.flat_map do |child|
41
70
  nested = scopes_in(child)
42
- SCOPES.include?(child.class) ? [child, *nested] : nested
71
+ scope?(node, child) ? [child, *nested] : nested
43
72
  end
44
73
  end
45
74
 
46
- private_class_method :scopes_in
75
+ # Whether the child of the node opens a scope of its own.
76
+ def self.scope?(node, child)
77
+ SCOPES.include?(child.class) || (child.is_a?(Prism::BlockNode) && Evilution::AST::ValueObjectDefinition.match?(node))
78
+ end
79
+
80
+ # Whether the child of the node belongs to something else: a method, or
81
+ # another scope.
82
+ def self.opaque?(node, child)
83
+ child.is_a?(Prism::DefNode) || scope?(node, child)
84
+ end
85
+
86
+ private_class_method :scopes_in, :bodies_in
47
87
 
48
88
  def initialize(node)
49
89
  @node = node
50
90
  end
51
91
 
52
92
  def anchor_line
53
- first_method = contents.find { |child| child.is_a?(Prism::DefNode) }
54
- return first_method.location.start_line if first_method
93
+ return @anchor_line if defined?(@anchor_line)
55
94
 
56
- singleton_classes = contents.grep(Prism::SingletonClassNode)
57
- singleton_classes.filter_map { |scope| self.class.new(scope).anchor_line }.first
95
+ @anchor_line = find_anchor_line
58
96
  end
59
97
 
60
98
  # The statements of the body the block selects, each one removable: cut
@@ -70,6 +108,14 @@ class Evilution::AST::ClassBody
70
108
 
71
109
  private
72
110
 
111
+ def find_anchor_line
112
+ first_method = contents.find { |child| child.is_a?(Prism::DefNode) }
113
+ return first_method.location.start_line if first_method
114
+
115
+ singleton_classes = contents.grep(Prism::SingletonClassNode)
116
+ singleton_classes.filter_map { |scope| self.class.new(scope).anchor_line }.first
117
+ end
118
+
73
119
  # Every node written in the scope, in source order, down to the methods and
74
120
  # nested scopes, which are listed but not entered. The scope's own header
75
121
  # (its name, its superclass expression) is not part of the body.
@@ -79,7 +125,7 @@ class Evilution::AST::ClassBody
79
125
 
80
126
  def statements_in(node, removable:)
81
127
  node.compact_child_nodes.flat_map do |child|
82
- next [] if OPAQUE.include?(child.class)
128
+ next [] if self.class.opaque?(node, child)
83
129
 
84
130
  nested = statements_in(child, removable: child.is_a?(Prism::StatementsNode) && !modifier?(node))
85
131
  removable ? [child, *nested] : nested
@@ -95,7 +141,7 @@ class Evilution::AST::ClassBody
95
141
 
96
142
  def descend(node)
97
143
  node.compact_child_nodes.flat_map do |child|
98
- OPAQUE.include?(child.class) ? [child] : [child, *descend(child)]
144
+ self.class.opaque?(node, child) ? [child] : [child, *descend(child)]
99
145
  end
100
146
  end
101
147
  end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../ast"
5
+ require_relative "syntax_check"
6
+
7
+ # Checks a mutation made inside a method by parsing the method alone rather
8
+ # than the whole file around it.
9
+ #
10
+ # A file is parsed whole to tell whether a mutation of it is still Ruby,
11
+ # which costs per mutation what the file is long. A `def ... end` closes the
12
+ # scope its body is read in: what the body may hold, and how its names read,
13
+ # does not depend on the code around it. So when the original file parses
14
+ # and the mutated def parses on its own as one `def ... end` spanning all of
15
+ # its text, the mutated file parses too, the def standing where the original
16
+ # stood.
17
+ #
18
+ # Where that does not follow, or might not, #parse is nil and the caller
19
+ # parses the whole file as before, so no mutation the whole file rejects is
20
+ # ever taken:
21
+ # - an endless def, which the text after it can extend;
22
+ # - a def on a receiver other than self, which is read in the outer scope;
23
+ # - a def whose line holds anything but names before it (`private def`),
24
+ # which could open a heredoc whose body runs through the def;
25
+ # - a file that names its encoding, which changes what its strings may hold;
26
+ # - an original def that does not parse alone, such as one opening a heredoc
27
+ # whose body comes after its `end`: a mutation can drop the opener and
28
+ # parse alone while the body left behind breaks the file;
29
+ # - a mutation that reaches outside the def.
30
+ class Evilution::AST::DefRegion
31
+ # node: the mutated def; source: the text it was parsed from, the def's
32
+ # own with the names before it on its line blanked, so its columns stay as
33
+ # they were; span: the offsets the original def takes up in the file;
34
+ # offset, length, replacement: the mutation.
35
+ Region = Data.define(:node, :source, :span, :offset, :length, :replacement)
36
+
37
+ NAMES_BEFORE_DEF = /\A[ \t]*(?:[a-z_][a-zA-Z0-9_]*[ \t]+)*\z/n
38
+ ENCODING_COMMENT = /coding/i
39
+ private_constant :NAMES_BEFORE_DEF, :ENCODING_COMMENT
40
+
41
+ def initialize(source, node, prism_interpreter: Evilution::AST::SyntaxCheck::PRISM_INTERPRETER)
42
+ @source = source
43
+ @node = node
44
+ @prism_interpreter = prism_interpreter
45
+ end
46
+
47
+ # The mutated def's Region when the def proves the mutated file parses,
48
+ # nil otherwise.
49
+ def parse(offset:, length:, replacement:)
50
+ return unless covers?(offset, length) && checkable?
51
+
52
+ text = mutated_text(offset, length, replacement)
53
+ node = lone_def(text)
54
+ return unless node
55
+
56
+ Region.new(node: node, source: text, span: @node.start_offset..@node.end_offset,
57
+ offset: offset, length: length, replacement: replacement)
58
+ end
59
+
60
+ private
61
+
62
+ def covers?(offset, length)
63
+ @node.is_a?(Prism::DefNode) && offset >= @node.start_offset && offset + length <= @node.end_offset
64
+ end
65
+
66
+ def checkable?
67
+ return @checkable if defined?(@checkable)
68
+
69
+ @checkable = closed_def?(@node) && in_place? && names_before? && !names_encoding? &&
70
+ !lone_def(mutated_text(@node.start_offset, 0, "")).nil?
71
+ end
72
+
73
+ # The def that text holds as its one statement, spanning all of it; nil
74
+ # when text does not parse or holds anything else.
75
+ def lone_def(text)
76
+ result = Prism.parse(text)
77
+ return unless result.success?
78
+
79
+ statements = result.value.statements.body
80
+ statements.first if statements.length == 1 && whole_def?(statements.first, text)
81
+ end
82
+
83
+ def closed_def?(node)
84
+ !node.end_keyword_loc.nil? && (node.receiver.nil? || node.receiver.is_a?(Prism::SelfNode))
85
+ end
86
+
87
+ # The node is the def written at its offsets in this source.
88
+ def in_place?
89
+ @source.byteslice(@node.start_offset, @node.end_offset - @node.start_offset) == @node.slice
90
+ end
91
+
92
+ def names_before?
93
+ prefix.match?(NAMES_BEFORE_DEF)
94
+ end
95
+
96
+ # Ruby reads an encoding comment on the first line, or on the second after
97
+ # a shebang.
98
+ def names_encoding?
99
+ bytes = @source.b
100
+ first_break = bytes.index("\n")
101
+ second_break = first_break && bytes.index("\n", first_break + 1)
102
+ bytes.byteslice(0, second_break || bytes.bytesize).match?(ENCODING_COMMENT)
103
+ end
104
+
105
+ def prefix
106
+ @prefix ||= begin
107
+ bytes = @source.b
108
+ line_break = @node.start_offset.zero? ? nil : bytes.rindex("\n", @node.start_offset - 1)
109
+ line_start = line_break ? line_break + 1 : 0
110
+ bytes.byteslice(line_start, @node.start_offset - line_start)
111
+ end
112
+ end
113
+
114
+ def mutated_text(offset, length, replacement)
115
+ text = " " * prefix.bytesize
116
+ text << bytes_between(@node.start_offset, offset) << replacement.b
117
+ text << bytes_between(offset + length, @node.end_offset)
118
+ text.force_encoding(@source.encoding)
119
+ end
120
+
121
+ def bytes_between(from, to)
122
+ @source.b.byteslice(from, to - from)
123
+ end
124
+
125
+ def whole_def?(node, text)
126
+ node.is_a?(Prism::DefNode) && closed_def?(node) &&
127
+ node.start_offset == prefix.bytesize && node.end_offset == text.bytesize &&
128
+ (@prism_interpreter || Evilution::AST::SyntaxCheck.parse_y_accepts?(text))
129
+ end
130
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../ast"
6
+
7
+ # Whether Ruby source assigns a local variable. Ruby decides while parsing
8
+ # which names are locals: once an assignment to `factor` has been read, a later
9
+ # `factor` reads the local, even when the assignment never ran. A change to
10
+ # such an assignment can therefore change what other lines do, though its own
11
+ # line never runs.
12
+ #
13
+ # The source can be lines cut out of a file, which need not parse on their
14
+ # own; Prism still builds a tree for what it can. With no source at all the
15
+ # answer is yes, the one that asks for a test run.
16
+ class Evilution::AST::LocalDeclaration
17
+ DECLARING = [
18
+ Prism::LocalVariableWriteNode, Prism::LocalVariableOperatorWriteNode,
19
+ Prism::LocalVariableOrWriteNode, Prism::LocalVariableAndWriteNode,
20
+ Prism::LocalVariableTargetNode
21
+ ].freeze
22
+ private_constant :DECLARING
23
+
24
+ def in?(source)
25
+ return true if source.nil?
26
+
27
+ declares?(Prism.parse(source).value)
28
+ end
29
+
30
+ private
31
+
32
+ def declares?(node)
33
+ return true if DECLARING.any? { |type| node.is_a?(type) }
34
+
35
+ node.compact_child_nodes.any? { |child| declares?(child) }
36
+ end
37
+ end