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
@@ -8,6 +8,13 @@ require_relative "constant_value_subjects"
8
8
  require_relative "receiver_positions"
9
9
  require_relative "../integration/loading/body_call_neutralizer"
10
10
  require_relative "../ast/heredoc_span"
11
+ require_relative "../ast/parse_cache"
12
+ require_relative "../ast/source_surgeon"
13
+ require_relative "../ast/def_region"
14
+ require_relative "../ast/class_body"
15
+ require_relative "line_range_filter"
16
+ require_relative "restore_source_cache"
17
+ require_relative "load_sources"
11
18
 
12
19
  class Evilution::Mutator::Base < Prism::Visitor
13
20
  include Evilution::Mutator::Primitives
@@ -28,56 +35,53 @@ class Evilution::Mutator::Base < Prism::Visitor
28
35
 
29
36
  attr_reader :mutations
30
37
 
31
- def initialize(**_options)
38
+ # line_ranges: file path => the line range a run asked for in that file;
39
+ # only mutations that change a line of it are built.
40
+ def initialize(line_ranges: {}, **_options)
32
41
  @mutations = []
33
42
  @subject = nil
34
43
  @file_source = nil
44
+ @line_range_filter = Evilution::Mutator::LineRangeFilter.new(line_ranges)
35
45
  @body_call_neutralizer = Evilution::Integration::Loading::BodyCallNeutralizer.new
36
46
  end
37
47
 
38
- def call(subject, filter: nil)
39
- @subject = subject
40
- @file_source = File.read(subject.file_path)
41
- @mutations = []
42
- @filter = filter
48
+ # file_source: the subject's file as read for every operator of the run;
49
+ # read here when not handed over.
50
+ def call(subject, filter: nil, file_source: nil)
51
+ prepare(subject, filter: filter, file_source: file_source)
43
52
  visit(subject.node)
44
53
  @mutations
45
54
  end
46
55
 
47
56
  private
48
57
 
58
+ def prepare(subject, filter:, file_source:)
59
+ @subject = subject
60
+ @file_source = file_source || File.read(subject.file_path)
61
+ @mutations = []
62
+ @filter = filter
63
+ @def_region = nil
64
+ @load_sources = nil
65
+ end
66
+
49
67
  # `skip_unparseable` makes the mutation vanish rather than be recorded when
50
68
  # the surgery does not parse. Off by default: the point operators rely on
51
69
  # unparseable bytes reaching the reporter as their own bucket. The shared
52
70
  # primitives opt in, since a nil-ification or promotion that does not parse
53
71
  # carries no signal.
54
72
  def add_mutation(offset:, length:, replacement:, node:, skip_unparseable: false)
55
- return if @filter && @filter.skip?(node)
56
-
57
- # When the byte range opens a heredoc but stops at its inline anchor,
58
- # extend it to cover the body+terminator. Without this every operator
59
- # whose edit straddles a `<<~MARKER` anchor (argument_removal,
60
- # argument_nil_substitution, method_call_removal, statement_deletion,
61
- # method_body_replacement, block_removal, conditional_branch,
62
- # string_interpolation, ...) emits an orphan-heredoc unparseable.
63
- extended_length = Evilution::AST::HeredocSpan.extend_length(
64
- node: node, offset: offset, length: length
65
- )
66
-
67
- # If the replacement re-references a heredoc anchor (e.g. argument_removal
68
- # rebuilding the args list with a kept `<<~MSG` arg), we cannot safely
69
- # extend the range — doing so would strip the kept heredoc's body without
70
- # putting it back. Skip the mutation rather than emit unparseable bytes.
71
- # When the replacement is heredoc-free (nil, "", a literal, or a non-
72
- # heredoc kept arg), extension cleanly sweeps the orphaned body+terminator.
73
- return if extended_length > length && replacement.match?(HEREDOC_ANCHOR_PATTERN)
73
+ length = heredoc_safe_length(node, offset, length, replacement)
74
+ return unless length
74
75
 
75
- length = extended_length
76
+ # Checked before any surgery: a mutation outside the requested lines is
77
+ # never run, so building it would be wasted work.
78
+ lines = changed_lines(node, offset, length)
79
+ return unless @line_range_filter.cover?(@subject.file_path, lines)
76
80
 
77
- surgery = Evilution::AST::SourceSurgeon.apply(
78
- @file_source, offset: offset, length: length, replacement: replacement
79
- )
80
- return if skip_unparseable && surgery.unparseable?
81
+ mutated = Evilution::AST::SourceSurgeon.splice(@file_source, offset: offset, length: length, replacement: replacement)
82
+ surgery = checked_unless_ignored(node, mutated, { offset: offset, length: length, replacement: replacement },
83
+ skip_unparseable)
84
+ return unless surgery
81
85
 
82
86
  slices = slice_affected_lines(
83
87
  mutated_source: surgery.source,
@@ -86,12 +90,65 @@ class Evilution::Mutator::Base < Prism::Visitor
86
90
  replacement_bytesize: replacement.bytesize
87
91
  )
88
92
 
89
- mutation = build_mutation_record(node, surgery, slices, offset)
93
+ mutation = build_mutation_record(node, surgery, slices, offset, lines)
90
94
  @mutations << mutation
91
95
  mutation
92
96
  end
93
97
 
94
- def build_mutation_record(node, surgery, slices, offset)
98
+ # The length the edit takes once a heredoc it opens is swept with it, or nil
99
+ # when it cannot be made.
100
+ #
101
+ # When the byte range opens a heredoc but stops at its inline anchor,
102
+ # extend it to cover the body+terminator. Without this every operator
103
+ # whose edit straddles a `<<~MARKER` anchor (argument_removal,
104
+ # argument_nil_substitution, method_call_removal, statement_deletion,
105
+ # method_body_replacement, block_removal, conditional_branch,
106
+ # string_interpolation, ...) emits an orphan-heredoc unparseable.
107
+ #
108
+ # If the replacement re-references a heredoc anchor (e.g. argument_removal
109
+ # rebuilding the args list with a kept `<<~MSG` arg), we cannot safely
110
+ # extend the range — doing so would strip the kept heredoc's body without
111
+ # putting it back. Skip the mutation rather than emit unparseable bytes.
112
+ # When the replacement is heredoc-free (nil, "", a literal, or a non-
113
+ # heredoc kept arg), extension cleanly sweeps the orphaned body+terminator.
114
+ def heredoc_safe_length(node, offset, length, replacement)
115
+ extended = Evilution::AST::HeredocSpan.extend_length(node: node, offset: offset, length: length)
116
+ extended unless extended > length && replacement.match?(HEREDOC_ANCHOR_PATTERN)
117
+ end
118
+
119
+ # The checked mutation, or nil when it is ignored or vanishes for not
120
+ # parsing. The filter is asked only of a mutation that would be built
121
+ # otherwise -- in the requested lines, its heredoc intact, parseable when it
122
+ # has to be -- so the ignored ones counted are ones the run would have made.
123
+ # The check, a parse, is left until the filter has passed the mutation
124
+ # unless the mutation hangs on it.
125
+ def checked_unless_ignored(node, mutated, edit, skip_unparseable)
126
+ surgery = check(mutated, edit) if skip_unparseable
127
+ return if skip_unparseable && surgery.unparseable?
128
+ return if ignored?(node, mutated, edit[:replacement])
129
+
130
+ surgery || check(mutated, edit)
131
+ end
132
+
133
+ # A mutation inside the subject's method is checked by parsing the method
134
+ # alone when that settles it (see AST::DefRegion); the whole file is
135
+ # parsed otherwise.
136
+ def check(mutated, edit)
137
+ Evilution::AST::SourceSurgeon.check(mutated) { def_region.parse(**edit) }
138
+ end
139
+
140
+ def def_region
141
+ @def_region ||= Evilution::AST::DefRegion.new(@file_source, @subject.node)
142
+ end
143
+
144
+ def ignored?(node, mutated_source, replacement)
145
+ return false unless @filter
146
+
147
+ @filter.skip?(node, file_path: @subject.file_path, source: @file_source, mutated_source: mutated_source,
148
+ replacement: replacement)
149
+ end
150
+
151
+ def build_mutation_record(node, surgery, slices, offset, changed_lines)
95
152
  Evilution::Mutation.new(
96
153
  subject: @subject,
97
154
  operator_name: self.class.operator_name,
@@ -100,7 +157,8 @@ class Evilution::Mutator::Base < Prism::Visitor
100
157
  location: Evilution::Mutation::Location.new(
101
158
  file_path: @subject.file_path,
102
159
  line: node.location.start_line,
103
- column: node.location.start_column
160
+ column: node.location.start_column,
161
+ lines: changed_lines
104
162
  ),
105
163
  parse_status: surgery.status,
106
164
  eval_source: build_eval_source(surgery, offset),
@@ -108,54 +166,40 @@ class Evilution::Mutator::Base < Prism::Visitor
108
166
  )
109
167
  end
110
168
 
111
- # Pre-compute the worker's eval target so per-iter Prism re-parse cost
112
- # stays out of the hot path. For parseable mutations we strip class/module
113
- # body calls that are known to be non-idempotent (registries etc.); for
114
- # unparseable bytes we fall through so the syntax validator can reject
115
- # them at apply time. Passing the subject's file path lets the neutralizer
116
- # skip files the parent never preloaded — those are lazy plugin files whose
117
- # DSL calls are still needed for the child fork's first-time load.
118
- # A scope or aasm subject's mutation sits inside a class-body declaration,
119
- # which must run again for the mutated body to replace the loaded one, so
120
- # that one declaration is kept.
121
- def build_eval_source(surgery, offset)
122
- return surgery.source unless surgery.ok?
123
-
124
- @body_call_neutralizer.call(surgery.source, file_path: @subject.file_path, **redeclaration(offset))
169
+ # The original lines the replaced bytes sit on; an insertion changes the
170
+ # line it is made on. Counted from the node rather than the top of the file,
171
+ # so only the bytes between the node and the edit are scanned.
172
+ def changed_lines(node, offset, length)
173
+ first = line_at(offset, node)
174
+ last = first + line_breaks_in(offset, [length - 1, 0].max)
175
+ first..last
125
176
  end
126
177
 
127
- # The original file, evaluated after a mutation's tests to put back what
128
- # the mutation changed. Without it an in-process run would leave the last
129
- # mutation of a file applied while the files after it are mutated, failing
130
- # their tests for them. Its class-body calls are neutralized like the eval
131
- # source's; a scope, aasm or callback subject keeps its own declaration, so
132
- # the mutated callable is declared over again as written.
133
- def build_restore_source(offset)
134
- return self.class.original_restore_source(@subject.file_path, @file_source, @body_call_neutralizer) unless redeclared_subject?
135
-
136
- @body_call_neutralizer.call(@file_source, file_path: @subject.file_path, **redeclaration(offset))
178
+ # Prism knows the node's line; the edit's is found by counting the line
179
+ # breaks between the two, forwards or backwards.
180
+ def line_at(offset, node)
181
+ from, to = [offset, node.start_offset].minmax
182
+ node.start_line + ((offset <=> node.start_offset) * line_breaks_in(from, to - from))
137
183
  end
138
184
 
139
- # Subjects whose body is written inside a class-body declaration and only
140
- # takes effect when that declaration runs again.
141
- REDECLARED_KINDS = %i[scope aasm callback].freeze
142
- private_constant :REDECLARED_KINDS
185
+ # Counted on the raw bytes: a byte range can start or end inside a multibyte
186
+ # character, and counting such a slice as UTF-8 raises.
187
+ def line_breaks_in(offset, length)
188
+ @file_source.byteslice(offset, length).b.count("\n")
189
+ end
143
190
 
144
- # So is anything written in a concern's hook block, a method included: the
145
- # block has to run again on the classes that took the concern already.
146
- def redeclared_subject?
147
- REDECLARED_KINDS.include?(@subject.kind) || @subject.concern_hook?
191
+ # The source a worker evaluates for the mutation, and the one it evaluates
192
+ # afterwards to put the file back (see LoadSources).
193
+ def build_eval_source(surgery, offset)
194
+ load_sources.eval_source(surgery, offset)
148
195
  end
149
196
 
150
- # What the neutralizer needs to keep a redeclared subject's declaration:
151
- # where the mutation is, and the lines the declaration takes up in the
152
- # file as it was loaded -- which is where the callbacks it registered then
153
- # say they come from, whatever the mutation does to its length.
154
- def redeclaration(offset)
155
- return {} unless redeclared_subject?
197
+ def build_restore_source(offset)
198
+ load_sources.restore_source(offset)
199
+ end
156
200
 
157
- first = @subject.line_number
158
- { keep_offset: offset, keep_lines: first..(first + @subject.source.count("\n")) }
201
+ def load_sources
202
+ @load_sources ||= Evilution::Mutator::LoadSources.new(@subject, @file_source, @body_call_neutralizer)
159
203
  end
160
204
 
161
205
  NEWLINE_BYTE = 10
@@ -206,34 +250,13 @@ class Evilution::Mutator::Base < Prism::Visitor
206
250
  %i[method scope aasm callback]
207
251
  end
208
252
 
209
- @parse_cache = {}
210
-
211
253
  def self.parsed_tree_for(file_path, file_source)
212
- cache = Evilution::Mutator::Base.instance_variable_get(:@parse_cache)
213
- entry = cache[file_path]
214
- return entry[:tree] if entry && entry[:source_hash] == file_source.hash
215
-
216
- tree = Prism.parse(file_source).value
217
- cache[file_path] = { source_hash: file_source.hash, tree: tree }
218
- tree
219
- end
220
-
221
- @restore_cache = {}
222
-
223
- # The restore source of a subject with no declaration of its own to keep is
224
- # the same for every mutation of the file: neutralized once and shared.
225
- def self.original_restore_source(file_path, file_source, neutralizer)
226
- cache = Evilution::Mutator::Base.instance_variable_get(:@restore_cache)
227
- entry = cache[file_path]
228
- return entry[:source] if entry && entry[:source_hash] == file_source.hash
229
-
230
- source = neutralizer.call(file_source, file_path: file_path).freeze
231
- cache[file_path] = { source_hash: file_source.hash, source: source }
232
- source
254
+ Evilution::AST::ParseCache.tree_for(file_path, file_source)
233
255
  end
234
256
 
235
257
  def self.clear_parse_cache!
236
- Evilution::Mutator::Base.instance_variable_set(:@parse_cache, {})
237
- Evilution::Mutator::Base.instance_variable_set(:@restore_cache, {})
258
+ Evilution::AST::ParseCache.clear!
259
+ Evilution::AST::ClassBody.clear!
260
+ Evilution::Mutator::RestoreSourceCache.clear!
238
261
  end
239
262
  end
@@ -5,9 +5,37 @@ require_relative "../mutator"
5
5
  # Extended by the operators that mutate a value wherever it is written, so
6
6
  # that they also run on the value of a constant assignment: a literal in
7
7
  # `LIMIT = 10` or in `DEFAULTS = { size: 1 }` is as much a literal as one in
8
- # a method body.
8
+ # a method body, and so is a call in `LIMIT = ENV["LIMIT"].to_i`.
9
+ #
10
+ # A method defined in the value (`Handler = Class.new do def call ... end`)
11
+ # is a subject of its own, so the operator leaves it to that subject rather
12
+ # than report its mutations against the constant.
9
13
  module Evilution::Mutator::ConstantValueSubjects
14
+ def self.extended(operator)
15
+ operator.include(Visits)
16
+ end
17
+
10
18
  def subject_kinds
11
19
  super + %i[constant_write]
12
20
  end
21
+
22
+ module Visits
23
+ def visit_def_node(node)
24
+ super unless constant_value?
25
+ end
26
+
27
+ private
28
+
29
+ def constant_value?
30
+ @subject.kind == :constant_write
31
+ end
32
+
33
+ # The `freeze` of a constant's value is freeze_removal's to drop: under
34
+ # that name, and against the method anchoring the class body. A call
35
+ # operator running on the constant would otherwise make the same bytes
36
+ # first and take them over in dedup.
37
+ def constant_value_freeze?(call)
38
+ constant_value? && call.name == :freeze && call.arguments.nil?
39
+ end
40
+ end
13
41
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../mutator"
4
+ require_relative "../line_set"
5
+
6
+ # Whether a mutation changes a line of the range asked for in its file.
7
+ # Subjects are selected when they overlap a range, but a subject can carry
8
+ # mutations of lines outside it -- class-body code is attributed to the
9
+ # scope's first method, a constant's subject spans its whole block -- and
10
+ # those are not what was asked for.
11
+ #
12
+ # A mutation is covered when any line it changes falls in the range, so one
13
+ # that starts above the range and reaches into it still counts. Files without
14
+ # a range cover every mutation. A file's range is a Range or an
15
+ # Evilution::LineSet of several, such as the hunks changed since a git ref.
16
+ class Evilution::Mutator::LineRangeFilter
17
+ def initialize(line_ranges)
18
+ @line_ranges = line_ranges
19
+ end
20
+
21
+ def cover?(file_path, lines)
22
+ range = @line_ranges[file_path]
23
+ return true unless range
24
+
25
+ Evilution::LineSet.wrap(range).overlap?(lines.first, lines.last)
26
+ end
27
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../mutator"
4
+ require_relative "restore_source_cache"
5
+
6
+ # The two sources a worker evaluates for a mutation of a subject's file: the
7
+ # mutated file, to load the mutation, and the original, to put the file back
8
+ # after the mutation's tests. Both have their class-body calls neutralized
9
+ # (see Integration::Loading::BodyCallNeutralizer).
10
+ class Evilution::Mutator::LoadSources
11
+ # Subjects whose body is written inside a class-body declaration and only
12
+ # takes effect when that declaration runs again.
13
+ REDECLARED_KINDS = %i[scope aasm callback].freeze
14
+ private_constant :REDECLARED_KINDS
15
+
16
+ def initialize(subject, file_source, neutralizer)
17
+ @subject = subject
18
+ @file_source = file_source
19
+ @neutralizer = neutralizer
20
+ end
21
+
22
+ # Pre-compute the worker's eval target so per-iter Prism re-parse cost
23
+ # stays out of the hot path. For parseable mutations we strip class/module
24
+ # body calls that are known to be non-idempotent (registries etc.); for
25
+ # unparseable bytes we fall through so the syntax validator can reject
26
+ # them at apply time. Passing the subject's file path lets the neutralizer
27
+ # skip files the parent never preloaded — those are lazy plugin files whose
28
+ # DSL calls are still needed for the child fork's first-time load.
29
+ # A scope or aasm subject's mutation sits inside a class-body declaration,
30
+ # which must run again for the mutated body to replace the loaded one, so
31
+ # that one declaration is kept.
32
+ def eval_source(surgery, offset)
33
+ return surgery.source unless surgery.ok?
34
+
35
+ carried_eval_source(surgery) ||
36
+ @neutralizer.call(surgery.source, file_path: @subject.file_path, **redeclaration(offset))
37
+ end
38
+
39
+ # The original file, evaluated after a mutation's tests to put back what
40
+ # the mutation changed. Without it an in-process run would leave the last
41
+ # mutation of a file applied while the files after it are mutated, failing
42
+ # their tests for them. Its class-body calls are neutralized like the eval
43
+ # source's; a scope, aasm or callback subject keeps its own declaration, so
44
+ # the mutated callable is declared over again as written.
45
+ def restore_source(offset)
46
+ return neutralization.source unless redeclared_subject?
47
+
48
+ @neutralizer.call(@file_source, file_path: @subject.file_path, **redeclaration(offset))
49
+ end
50
+
51
+ private
52
+
53
+ # A mutation the method alone was parsed for (see AST::DefRegion) leaves
54
+ # the rest of the file as it was, so its class-body calls are the
55
+ # original's: unless one of them holds the method, or the mutated method
56
+ # holds a class body of its own, the original's neutralization with the
57
+ # mutation spliced in is what neutralizing the mutated file would give,
58
+ # without parsing it.
59
+ def carried_eval_source(surgery)
60
+ region = surgery.region
61
+ return if region.nil? || redeclared_subject? || !@neutralizer.blanks_nothing_in?(region.node)
62
+
63
+ neutralization.carry(region.span, offset: region.offset, length: region.length,
64
+ replacement: region.replacement, mutated: surgery.source)
65
+ end
66
+
67
+ def neutralization
68
+ Evilution::Mutator::RestoreSourceCache.neutralization_for(@subject.file_path, @file_source, @neutralizer)
69
+ end
70
+
71
+ # So is anything written in a concern's hook block, a method included: the
72
+ # block has to run again on the classes that took the concern already.
73
+ def redeclared_subject?
74
+ REDECLARED_KINDS.include?(@subject.kind) || @subject.concern_hook?
75
+ end
76
+
77
+ # What the neutralizer needs to keep a redeclared subject's declaration:
78
+ # where the mutation is, the lines the declaration takes up in the file as
79
+ # it was loaded -- which is where the callbacks it registered then say they
80
+ # come from, whatever the mutation does to its length -- and the
81
+ # declaration as written there.
82
+ def redeclaration(offset)
83
+ return {} unless redeclared_subject?
84
+
85
+ first = @subject.line_number
86
+ { keep_offset: offset, keep_lines: first..(first + @subject.source.count("\n")), keep_source: @subject.source }
87
+ end
88
+ end
@@ -19,11 +19,8 @@ require_relative "../../ast/class_body"
19
19
  # generic call operators, and global-variable aliases (`alias $new $old`)
20
20
  # are not method surface.
21
21
  class Evilution::Mutator::Operator::AliasRemoval < Evilution::Mutator::Base
22
- def call(subject, filter: nil)
23
- @subject = subject
24
- @file_source = File.read(subject.file_path)
25
- @mutations = []
26
- @filter = filter
22
+ def call(subject, filter: nil, file_source: nil)
23
+ prepare(subject, filter: filter, file_source: file_source)
27
24
 
28
25
  declarations_for(subject).each { |declaration| remove(declaration) }
29
26
  @mutations
@@ -14,6 +14,8 @@ require_relative "../operator"
14
14
  # - hash values inside HashNode / KeywordHashNode (incl. inside call args)
15
15
  # - array elements inside ArrayNode
16
16
  class Evilution::Mutator::Operator::ArgumentMethodCallReplacement < Evilution::Mutator::Base
17
+ extend Evilution::Mutator::ConstantValueSubjects
18
+
17
19
  def visit_call_node(node)
18
20
  node.arguments.arguments.each { |arg| try_replace(arg) } if node.arguments
19
21
 
@@ -48,6 +50,7 @@ class Evilution::Mutator::Operator::ArgumentMethodCallReplacement < Evilution::M
48
50
  def try_replace(value)
49
51
  return unless value.is_a?(Prism::CallNode)
50
52
  return unless value.receiver
53
+ return if constant_value_freeze?(value)
51
54
 
52
55
  add_mutation(
53
56
  offset: value.location.start_offset,
@@ -25,11 +25,8 @@ class Evilution::Mutator::Operator::AttrAccessorSwap < Evilution::Mutator::Base
25
25
  NARROW = %i[attr_reader attr_writer].freeze
26
26
  DECLARATIONS = [*NARROW, :attr_accessor].freeze
27
27
 
28
- def call(subject, filter: nil)
29
- @subject = subject
30
- @file_source = File.read(subject.file_path)
31
- @mutations = []
32
- @filter = filter
28
+ def call(subject, filter: nil, file_source: nil)
29
+ prepare(subject, filter: filter, file_source: file_source)
33
30
 
34
31
  declarations_for(subject).each do |declaration|
35
32
  widen(declaration) if NARROW.include?(declaration.name)
@@ -15,9 +15,9 @@ require_relative "../operator"
15
15
  #
16
16
  # A top-level path (`::LIMIT`) has no namespace to strip. The target of an
17
17
  # operator write (`Config::LIMIT ||= x`) is left alone: bare, `LIMIT ||= x`
18
- # is a dynamic constant assignment inside a method. Prism lets it through,
19
- # where parse.y rejects it with a SyntaxError, so whether the mutant exists
20
- # would depend on the parser the interpreter compiles with.
18
+ # is a dynamic constant assignment inside a method. Prism before 2.0 lets it
19
+ # through, where parse.y rejects it with a SyntaxError, so whether the mutant
20
+ # exists would depend on the parser and the Prism version in use.
21
21
  class Evilution::Mutator::Operator::ConstantNamespaceStrip < Evilution::Mutator::Base
22
22
  def call(subject, **)
23
23
  @write_targets = Set.new
@@ -12,10 +12,13 @@ require_relative "../../ast/class_body"
12
12
  # shows that it has to stay as it is.
13
13
  #
14
14
  # A constant is defined in the class body, outside every method, where no
15
- # subject reaches it. The mutation is attributed to the method anchoring the
16
- # enclosing body (see Evilution::AST::ClassBody); a top-level constant has no
17
- # body to be attributed to. A `freeze` inside a method is left to the generic
18
- # call operators, which already drop it.
15
+ # method subject reaches it. The mutation is attributed to the method
16
+ # anchoring the enclosing body (see Evilution::AST::ClassBody). A body with no
17
+ # anchor -- a class whose only methods are in a `Data.define` block -- has
18
+ # its constants mutated under their own constant-assignment subjects instead,
19
+ # which hold the line. A top-level constant has no body and is left alone. A
20
+ # `freeze` inside a method is left to the generic call operators, which
21
+ # already drop it.
19
22
  class Evilution::Mutator::Operator::FreezeRemoval < Evilution::Mutator::Base
20
23
  CONSTANT_WRITES = [Prism::ConstantWriteNode, Prism::ConstantPathWriteNode, Prism::ConstantOrWriteNode].freeze
21
24
 
@@ -30,11 +33,12 @@ class Evilution::Mutator::Operator::FreezeRemoval < Evilution::Mutator::Base
30
33
  FROZEN_STRING_LITERALS = /^[ \t]*#.*frozen[_-]string[_-]literal:[ \t]*true/i
31
34
  private_constant :CONSTANT_WRITES, :ALWAYS_FROZEN, :LEADING_COMMENTS, :FROZEN_STRING_LITERALS
32
35
 
33
- def call(subject, filter: nil)
34
- @subject = subject
35
- @file_source = File.read(subject.file_path)
36
- @mutations = []
37
- @filter = filter
36
+ def self.subject_kinds
37
+ super + %i[constant_write]
38
+ end
39
+
40
+ def call(subject, filter: nil, file_source: nil)
41
+ prepare(subject, filter: filter, file_source: file_source)
38
42
 
39
43
  constant_writes_for(subject).each { |write| freezes_in(write).each { |freeze_call| remove(freeze_call) } }
40
44
  @mutations
@@ -53,15 +57,29 @@ class Evilution::Mutator::Operator::FreezeRemoval < Evilution::Mutator::Base
53
57
 
54
58
  def constant_writes_for(subject)
55
59
  tree = self.class.parsed_tree_for(subject.file_path, @file_source)
60
+ return unanchored_writes(tree, subject.node) if subject.kind == :constant_write
56
61
 
57
62
  Evilution::AST::ClassBody.anchored_at(tree, subject.line_number).flat_map do |body|
58
63
  body.declarations { |node| CONSTANT_WRITES.include?(node.class) }
59
64
  end
60
65
  end
61
66
 
62
- # Every `freeze` call under the node, outermost first.
67
+ # The subject's own assignment, when it is written in a body no method
68
+ # anchors; under an anchor it is that method's to mutate.
69
+ def unanchored_writes(tree, write)
70
+ offset = write.location.start_offset
71
+
72
+ Evilution::AST::ClassBody.unanchored_in(tree).flat_map do |body|
73
+ body.declarations { |node| CONSTANT_WRITES.include?(node.class) && node.location.start_offset == offset }
74
+ end
75
+ end
76
+
77
+ # Every `freeze` call under the node, outermost first. What a method or a
78
+ # nested scope holds (the block of `Data.define`) is not the node's own.
63
79
  def freezes_in(node)
64
- nested = node.compact_child_nodes.flat_map { |child| freezes_in(child) }
80
+ nested = node.compact_child_nodes.flat_map do |child|
81
+ Evilution::AST::ClassBody.opaque?(node, child) ? [] : freezes_in(child)
82
+ end
65
83
  removable_freeze?(node) ? [node, *nested] : nested
66
84
  end
67
85
 
@@ -3,8 +3,10 @@
3
3
  require_relative "../operator"
4
4
 
5
5
  class Evilution::Mutator::Operator::MethodCallRemoval < Evilution::Mutator::Base
6
+ extend Evilution::Mutator::ConstantValueSubjects
7
+
6
8
  def visit_call_node(node)
7
- if node.receiver
9
+ if node.receiver && !constant_value_freeze?(node)
8
10
  add_mutation(
9
11
  offset: node.location.start_offset,
10
12
  length: node.location.length,
@@ -15,11 +15,8 @@ require_relative "../../ast/class_body"
15
15
  class Evilution::Mutator::Operator::MixinRemoval < Evilution::Mutator::Base
16
16
  MIXIN_METHODS = %i[include extend prepend].freeze
17
17
 
18
- def call(subject, filter: nil)
19
- @subject = subject
20
- @file_source = File.read(subject.file_path)
21
- @mutations = []
22
- @filter = filter
18
+ def call(subject, filter: nil, file_source: nil)
19
+ prepare(subject, filter: filter, file_source: file_source)
23
20
 
24
21
  mixin_calls_for(subject).each { |call_node| emit_mixin_removal(call_node) }
25
22
  @mutations
@@ -11,11 +11,8 @@ require_relative "../../ast/class_body"
11
11
  # The definition line belongs to no method, so the mutant is attributed to
12
12
  # the method anchoring the class body (see Evilution::AST::ClassBody).
13
13
  class Evilution::Mutator::Operator::SuperclassRemoval < Evilution::Mutator::Base
14
- def call(subject, filter: nil)
15
- @subject = subject
16
- @file_source = File.read(subject.file_path)
17
- @mutations = []
18
- @filter = filter
14
+ def call(subject, filter: nil, file_source: nil)
15
+ prepare(subject, filter: filter, file_source: file_source)
19
16
 
20
17
  classes_with_superclass_for(subject).each do |class_node|
21
18
  offset, length = superclass_range(class_node)
@@ -6,7 +6,7 @@ require_relative "../../ast/round_half_mode"
6
6
  class Evilution::Mutator::Operator::SymbolLiteral < Evilution::Mutator::Base
7
7
  extend Evilution::Mutator::ConstantValueSubjects
8
8
 
9
- def call(subject, filter: nil)
9
+ def call(subject, **)
10
10
  @round_half_modes = []
11
11
  super
12
12
  end
@@ -26,11 +26,8 @@ class Evilution::Mutator::Operator::VisibilityRemoval < Evilution::Mutator::Base
26
26
  # runs with or without the declaration, so it stays.
27
27
  WRAPPED = [Prism::DefNode, Prism::CallNode].freeze
28
28
 
29
- def call(subject, filter: nil)
30
- @subject = subject
31
- @file_source = File.read(subject.file_path)
32
- @mutations = []
33
- @filter = filter
29
+ def call(subject, filter: nil, file_source: nil)
30
+ prepare(subject, filter: filter, file_source: file_source)
34
31
 
35
32
  declarations_for(subject).each { |declaration| remove(declaration) }
36
33
  @mutations