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,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../ast"
4
+
5
+ # The parent of every node of a tree. Prism nodes do not point at their
6
+ # parent, so the tree is walked once and each node's parent recorded.
7
+ #
8
+ # A node is known by its type and the bytes it spans, not by identity: the
9
+ # nodes an operator mutates come from the subject's own parse of the file,
10
+ # which is not the tree the index was built from. Two nodes of different
11
+ # types can span the same bytes (a body of one statement and the statement),
12
+ # so the type is part of the key. Where a node of one type nests in another
13
+ # of the same type over the same bytes, the outer one's parent is kept, so
14
+ # the walk up never comes back to the key it left.
15
+ class Evilution::AST::ParentIndex
16
+ def initialize(root)
17
+ @parents = {}
18
+ record(root)
19
+ end
20
+
21
+ # The node's ancestors, nearest first, up to the root of the tree. A node
22
+ # the tree does not hold has none.
23
+ def each_ancestor(node)
24
+ parent = @parents[key(node)]
25
+ while parent
26
+ yield parent
27
+ parent = @parents[key(parent)]
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ # Iterative: a deeply nested tree would overflow the stack of a recursive
34
+ # walk.
35
+ def record(root)
36
+ pending = [root]
37
+ until pending.empty?
38
+ node = pending.pop
39
+ node.compact_child_nodes.each do |child|
40
+ @parents[key(child)] ||= node
41
+ pending.push(child)
42
+ end
43
+ end
44
+ end
45
+
46
+ def key(node)
47
+ location = node.location
48
+ [node.class, location.start_offset, location.length]
49
+ end
50
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../ast"
5
+
6
+ # One parse per file for the whole of mutation planning: the operators that
7
+ # look beyond their subject and the ignore filter all want the file's tree.
8
+ # A file is parsed again when its source differs from the one cached for it.
9
+ module Evilution::AST::ParseCache
10
+ Entry = Data.define(:source_hash, :tree)
11
+ private_constant :Entry
12
+
13
+ @trees = {}
14
+
15
+ def self.tree_for(file_path, source)
16
+ entry = @trees[file_path]
17
+ return entry.tree if entry && entry.source_hash == source.hash
18
+
19
+ tree = Prism.parse(source).value
20
+ @trees[file_path] = Entry.new(source_hash: source.hash, tree: tree)
21
+ tree
22
+ end
23
+
24
+ def self.clear!
25
+ @trees = {}
26
+ end
27
+ end
@@ -215,9 +215,13 @@ module Evilution::AST
215
215
  # `Order.validate(:credit_limit)`. The subject spans the declaration and
216
216
  # mutates the callable's body. In a concern's `included` block it is named
217
217
  # after the concern.
218
+ # The options of a declaration -- `validates :for_lift, inclusion: { in:
219
+ # [true] }, if: :lift_round?` -- are a subject named the same way, which
220
+ # mutates its argument list. A condition written out there is mutated as
221
+ # a subject of its own first; the planner drops the copies.
218
222
  def add_callback_subjects(body)
219
- CallbackDeclaration.in_body(body).each do |callable|
220
- add_subject(callable.body, "#{@context.join("::")}.#{callable.label}", :callback, span: callable.declaration)
223
+ [*CallbackDeclaration.in_body(body), *CallbackDeclaration.options_in(body)].each do |part|
224
+ add_subject(part.node, "#{@context.join("::")}.#{part.label}", :callback, span: part.declaration)
221
225
  end
222
226
  end
223
227
 
@@ -233,7 +237,8 @@ module Evilution::AST
233
237
  source: source,
234
238
  node: node,
235
239
  kind: kind,
236
- concern_hook: in_hook?(loc)
240
+ concern_hook: in_hook?(loc),
241
+ byte_range: loc.start_offset...loc.end_offset
237
242
  )
238
243
  end
239
244
 
@@ -1,26 +1,67 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../pattern"
4
+ require_relative "../ancestor_lookup"
4
5
  require_relative "parser"
5
6
 
7
+ # Decides whether a mutation is ignored: it is when a pattern matches the
8
+ # mutated node or any of its ancestors, up to the top of the source the node
9
+ # was parsed from.
6
10
  class Evilution::AST::Pattern::Filter
7
- attr_reader :skipped_count
8
-
9
- def initialize(patterns)
11
+ # ancestor_lookup: shared with other filters asking about the same nodes,
12
+ # so a file's parent index is built once.
13
+ def initialize(patterns, ancestor_lookup: Evilution::AST::AncestorLookup.new)
10
14
  @matchers = patterns.map { |p| Evilution::AST::Pattern::Parser.new(p).parse }
11
- @skipped_count = 0
15
+ @ancestor_lookup = ancestor_lookup
16
+ reset_count!
17
+ end
18
+
19
+ # file_path, source: the file the node was parsed from and its source, where
20
+ # its ancestors are found; without a source only the node is tested. The
21
+ # ancestors are looked up only when the node itself matches no pattern.
22
+ #
23
+ # mutated_source: the file as the mutation leaves it. Two operators can make
24
+ # the same change, which the run makes once, so an ignored mutation is
25
+ # counted once per file and mutated source, however many of its nodes
26
+ # match. Without one each skip counts.
27
+ #
28
+ # replacement: the code the mutation puts in; a pattern skips whatever it
29
+ # is, and it is taken only so this filter can stand in for SkipFilter.
30
+ def skip?(node, file_path: nil, source: nil, mutated_source: nil, replacement: nil)
31
+ return false unless match?(node, file_path: file_path, source: source)
32
+
33
+ @skipped << (mutated_source ? skip_key(file_path, mutated_source) : Object.new)
34
+ true
35
+ end
36
+
37
+ # Whether a pattern matches the node or an ancestor, as #skip? decides it,
38
+ # without counting a skip.
39
+ def match?(node, file_path: nil, source: nil)
40
+ return false if @matchers.empty?
41
+
42
+ matches?(node) || @ancestor_lookup.ancestors(node, file_path, source).any? { |ancestor| matches?(ancestor) }
12
43
  end
13
44
 
14
- def skip?(node)
15
- if @matchers.any? { |m| m.match?(node) }
16
- @skipped_count += 1
17
- true
18
- else
19
- false
20
- end
45
+ # made: the mutations the run makes. One of them can be an ignored mutation
46
+ # made all the same from a node no pattern matches, which is not counted.
47
+ def skipped_count(made: [])
48
+ made_keys = made.to_set { |mutation| skip_key(mutation.file_path, mutation.mutated_source) }
49
+ @skipped.count { |key| !made_keys.include?(key) }
21
50
  end
22
51
 
23
52
  def reset_count!
24
- @skipped_count = 0
53
+ @skipped = Set.new
54
+ end
55
+
56
+ private
57
+
58
+ # The hash stands in for the mutated source, which would keep a copy of the
59
+ # file per ignored mutation.
60
+ def skip_key(file_path, mutated_source)
61
+ [file_path, mutated_source.hash]
62
+ end
63
+
64
+ def matches?(node)
65
+ @matchers.any? { |m| m.match?(node) }
25
66
  end
26
67
  end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../ast"
4
+ require_relative "ancestor_lookup"
5
+ require_relative "pattern/filter"
6
+ require_relative "skip_sites/presets"
7
+
8
+ # Decides whether a mutation is skipped during generation, and counts why:
9
+ # an `ignore_patterns` pattern matches its node or an ancestor (see
10
+ # Pattern::Filter), or its node lies in a site of a `skip_sites` preset
11
+ # (see SkipSites::Presets). The two share one parent index per file.
12
+ #
13
+ # A skipped mutation is counted once per file and mutated source, under the
14
+ # first reason it was skipped for: patterns before presets, presets in
15
+ # their attribution order.
16
+ class Evilution::AST::SkipFilter
17
+ PATTERNS = :ignore_patterns
18
+ RAISE = /\braise\b/
19
+ private_constant :PATTERNS, :RAISE
20
+
21
+ # nil when the config skips nothing.
22
+ def self.for(config)
23
+ patterns = config.ignore_patterns
24
+ sites = config.skip_sites
25
+ return nil if patterns.empty? && sites.empty?
26
+
27
+ new(patterns: patterns, sites: sites)
28
+ end
29
+
30
+ # patterns: ignore_patterns strings; sites: preset names (Symbols).
31
+ def initialize(patterns:, sites:)
32
+ @ancestor_lookup = Evilution::AST::AncestorLookup.new
33
+ @patterns = Evilution::AST::Pattern::Filter.new(patterns, ancestor_lookup: @ancestor_lookup)
34
+ @presets = Evilution::AST::SkipSites::Presets.build(sites)
35
+ reset_count!
36
+ end
37
+
38
+ # file_path, source: the file the node was parsed from and its source;
39
+ # without a source only the node itself is tested. mutated_source: the
40
+ # file as the mutation leaves it, the identity a skip is counted by;
41
+ # without one each skip counts. replacement: the code the mutation puts
42
+ # in; one that raises makes the code fail where it reported, so no preset
43
+ # skips it.
44
+ def skip?(node, file_path: nil, source: nil, mutated_source: nil, replacement: nil)
45
+ reason = reason_for(node, file_path, source, replacement)
46
+ return false unless reason
47
+
48
+ @skipped[mutated_source ? [file_path, mutated_source.hash] : Object.new] ||= reason
49
+ true
50
+ end
51
+
52
+ # made: the mutations the run makes. One of them can be a skipped mutation
53
+ # made all the same from a node nothing skips, which is not counted.
54
+ def skipped_count(made: [])
55
+ counted(made).length
56
+ end
57
+
58
+ # The skips per preset, e.g. { logging: 3 }; presets that skipped nothing
59
+ # are left out.
60
+ def skipped_by_site(made: [])
61
+ counted(made).reject { |reason| reason == PATTERNS }.tally
62
+ end
63
+
64
+ def reset_count!
65
+ @skipped = {}
66
+ end
67
+
68
+ private
69
+
70
+ def reason_for(node, file_path, source, replacement)
71
+ return PATTERNS if @patterns.match?(node, file_path: file_path, source: source)
72
+ return nil if @presets.empty? || (replacement && replacement.match?(RAISE))
73
+
74
+ chain = [node, *@ancestor_lookup.ancestors(node, file_path, source)]
75
+ preset = @presets.find { |candidate| candidate.covers?(chain) }
76
+ preset && preset.name
77
+ end
78
+
79
+ def counted(made)
80
+ made_keys = made.to_set { |mutation| [mutation.file_path, mutation.mutated_source.hash] }
81
+ @skipped.filter_map { |key, reason| reason unless made_keys.include?(key) }
82
+ end
83
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../skip_sites"
5
+ require_relative "nodes"
6
+ require_relative "message"
7
+
8
+ # Console output: `puts`, `print`, `printf`, `warn`, `p` and `pp` sent to no
9
+ # receiver or to Kernel, and `puts` / `print` / `printf` / `write` sent to
10
+ # `$stdout`, `$stderr`, `STDOUT` or `STDERR`. Output that is the program's
11
+ # job -- a CLI printing its result -- is console output all the same, which
12
+ # is why this preset stands apart from logging.
13
+ #
14
+ # `p` and `pp` return what they print and a stream's `write` the number of
15
+ # bytes written, so they are sites only where their value goes unused (see
16
+ # Nodes.value_dropped?): as the last statement of a method, or inside an
17
+ # expression, a changed argument changes the value. The others return nil
18
+ # whatever they print.
19
+ #
20
+ # Within a site, mutations are skipped by the same rule as for log lines
21
+ # (see Logging): the call itself and its receiver only where its value goes
22
+ # unused.
23
+ class Evilution::AST::SkipSites::DebugOutput
24
+ PRINTERS = %i[puts print printf warn].freeze
25
+ VALUE_PRINTERS = %i[p pp].freeze
26
+ STREAM_PRINTERS = %i[puts print printf].freeze
27
+ STREAM_WRITERS = %i[write].freeze
28
+ STREAMS = %w[stdout stderr STDOUT STDERR].freeze
29
+ private_constant :PRINTERS, :VALUE_PRINTERS, :STREAM_PRINTERS, :STREAM_WRITERS, :STREAMS
30
+
31
+ def name
32
+ :debug_output
33
+ end
34
+
35
+ # chain: the mutated node, then its ancestors, nearest first.
36
+ def covers?(chain)
37
+ chain.each_index.any? { |index| site?(chain, index) && covered_in_site?(chain, index) }
38
+ end
39
+
40
+ private
41
+
42
+ def site?(chain, index)
43
+ node = chain[index]
44
+ return false unless node.is_a?(Prism::CallNode)
45
+ return true if Evilution::AST::SkipSites::Nodes.kernel_call?(node, PRINTERS) || stream_call?(node, STREAM_PRINTERS)
46
+
47
+ value_printer?(node) && Evilution::AST::SkipSites::Nodes.value_dropped?(chain, index)
48
+ end
49
+
50
+ def value_printer?(call)
51
+ Evilution::AST::SkipSites::Nodes.kernel_call?(call, VALUE_PRINTERS) || stream_call?(call, STREAM_WRITERS)
52
+ end
53
+
54
+ def stream_call?(call, names)
55
+ receiver = call.receiver
56
+ return false unless names.include?(call.name)
57
+
58
+ stream = receiver.is_a?(Prism::GlobalVariableReadNode) || receiver.is_a?(Prism::ConstantReadNode)
59
+ stream && STREAMS.include?(Evilution::AST::SkipSites::Nodes.receiver_name(receiver))
60
+ end
61
+
62
+ def covered_in_site?(chain, site_index)
63
+ call = chain[site_index]
64
+ parts = call.arguments ? call.arguments.arguments : []
65
+ Evilution::AST::SkipSites::Message.covered_in_call(chain, site_index, parts)
66
+ end
67
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../skip_sites"
5
+ require_relative "nodes"
6
+ require_relative "message"
7
+
8
+ # The message of a raised error. The raise itself, the class raised and the
9
+ # condition guarding the raise decide behaviour and are never covered.
10
+ #
11
+ # A site is `raise` or `fail` sent to no receiver or to Kernel. Its message
12
+ # is the second positional argument (`raise Error, "msg"`), or the only one
13
+ # when that is a string (`raise "msg"`); when the error is built in place
14
+ # (`raise Error.new("msg")`, `.exception`, on a constant), its string
15
+ # arguments are messages, while its other arguments may be data the error
16
+ # keeps. Keywords (`cause:`) and the backtrace argument are not messages; a
17
+ # splat leaves the positions unknown, and nothing is covered.
18
+ #
19
+ # A mutation inside a message is skipped when it changes the message alone:
20
+ # literal text of the message, or anything inside a message that only reads
21
+ # (see Purity). In `"left: #{queue.pop}"` the text is covered and the call
22
+ # is not. A message given alone is not covered as a whole: replacing it
23
+ # (`raise nil`) changes the class raised, as `raise` takes its only argument
24
+ # for the error when it is not a string.
25
+ class Evilution::AST::SkipSites::ErrorMessages
26
+ RAISES = %i[raise fail].freeze
27
+ CONSTRUCTORS = %i[new exception].freeze
28
+ private_constant :RAISES, :CONSTRUCTORS
29
+
30
+ def name
31
+ :error_messages
32
+ end
33
+
34
+ # chain: the mutated node, then its ancestors, nearest first.
35
+ def covers?(chain)
36
+ chain.each_with_index.any? do |node, index|
37
+ Evilution::AST::SkipSites::Nodes.kernel_call?(node, RAISES) && !error_argument?(node, chain.first) &&
38
+ Evilution::AST::SkipSites::Message.covered_in(chain, index, messages(node))
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ # The first positional argument is what `raise` makes the error of: the
45
+ # class, or a message given alone. Replacing it as a whole changes the
46
+ # class raised.
47
+ def error_argument?(raise_call, node)
48
+ positional = Evilution::AST::SkipSites::Nodes.positional_arguments(raise_call)
49
+ !positional.nil? && Evilution::AST::SkipSites::Nodes.same?(positional.first, node)
50
+ end
51
+
52
+ def messages(raise_call)
53
+ positional = Evilution::AST::SkipSites::Nodes.positional_arguments(raise_call)
54
+ return [] if positional.nil?
55
+ return [positional[1]] if positional.length > 1
56
+
57
+ only = positional.first
58
+ return [only] if Evilution::AST::SkipSites::Nodes.string?(only)
59
+
60
+ constructed_messages(only)
61
+ end
62
+
63
+ def constructed_messages(node)
64
+ return [] unless constructor_call?(node)
65
+
66
+ positional = Evilution::AST::SkipSites::Nodes.positional_arguments(node) || []
67
+ positional.select { |argument| Evilution::AST::SkipSites::Nodes.string?(argument) }
68
+ end
69
+
70
+ def constructor_call?(node)
71
+ node.is_a?(Prism::CallNode) && CONSTRUCTORS.include?(node.name) &&
72
+ (node.receiver.is_a?(Prism::ConstantReadNode) || node.receiver.is_a?(Prism::ConstantPathNode))
73
+ end
74
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../skip_sites"
5
+ require_relative "nodes"
6
+ require_relative "message"
7
+
8
+ # Log lines and deprecation warnings: what the code reports about its work,
9
+ # not the work.
10
+ #
11
+ # A site is a call of a log level (`debug info warn error fatal unknown add
12
+ # log`) on a receiver named `logger` -- `logger`, `@logger`, `LOGGER`,
13
+ # `Rails.logger`, `config.logger` -- or of `warn` / `deprecation_warning` on
14
+ # a deprecator (`ActiveSupport::Deprecation`, `ActiveSupport.deprecator`,
15
+ # `Rails.deprecator`, `deprecator`), or `Kernel#warn` with `category:
16
+ # :deprecated`. The condition guarding a log line is not part of it, nor is
17
+ # a predicate such as `logger.debug?`.
18
+ #
19
+ # A mutation inside a site is skipped when it changes the message alone:
20
+ # literal text of the message, or anything inside an argument (or the block)
21
+ # that only reads. A mutation of the call itself, its receiver, or code
22
+ # between the two -- deleting the line, say -- is skipped when every
23
+ # argument only reads and the call's value goes unused (see
24
+ # Nodes.value_dropped?); were an argument to change state
25
+ # (`logger.info(queue.pop)`), deleting the line would change more than the
26
+ # log, and were the value used (`logger.info(msg) && save`), so would
27
+ # replacing the call.
28
+ class Evilution::AST::SkipSites::Logging
29
+ LEVELS = %i[debug info warn error fatal unknown add log].freeze
30
+ DEPRECATION_WARNINGS = %i[warn deprecation_warning].freeze
31
+ WARN = %i[warn].freeze
32
+ private_constant :LEVELS, :DEPRECATION_WARNINGS, :WARN
33
+
34
+ def name
35
+ :logging
36
+ end
37
+
38
+ # chain: the mutated node, then its ancestors, nearest first.
39
+ def covers?(chain)
40
+ chain.each_with_index.any? { |node, index| site?(node) && covered_in_site?(chain, index) }
41
+ end
42
+
43
+ private
44
+
45
+ def site?(node)
46
+ return false unless node.is_a?(Prism::CallNode)
47
+
48
+ log_line?(node) || deprecation_warning?(node) || deprecated_category_warn?(node)
49
+ end
50
+
51
+ def log_line?(call)
52
+ LEVELS.include?(call.name) && named?(call.receiver, "logger", "LOGGER")
53
+ end
54
+
55
+ def deprecation_warning?(call)
56
+ DEPRECATION_WARNINGS.include?(call.name) && named?(call.receiver, "deprecator", "Deprecation")
57
+ end
58
+
59
+ def deprecated_category_warn?(call)
60
+ Evilution::AST::SkipSites::Nodes.kernel_call?(call, WARN) && deprecated_category?(call.arguments)
61
+ end
62
+
63
+ def deprecated_category?(arguments)
64
+ return false unless arguments
65
+
66
+ arguments.arguments.grep(Prism::KeywordHashNode).any? do |hash|
67
+ hash.elements.any? { |element| category_deprecated?(element) }
68
+ end
69
+ end
70
+
71
+ def category_deprecated?(element)
72
+ element.is_a?(Prism::AssocNode) &&
73
+ symbol_named?(element.key, "category") && symbol_named?(element.value, "deprecated")
74
+ end
75
+
76
+ def symbol_named?(node, name)
77
+ node.is_a?(Prism::SymbolNode) && node.unescaped == name
78
+ end
79
+
80
+ # A receiver goes by no name when there is none.
81
+ def named?(receiver, *names)
82
+ names.include?(Evilution::AST::SkipSites::Nodes.receiver_name(receiver))
83
+ end
84
+
85
+ def covered_in_site?(chain, site_index)
86
+ Evilution::AST::SkipSites::Message.covered_in_call(chain, site_index, message_parts(chain[site_index]))
87
+ end
88
+
89
+ def message_parts(call)
90
+ arguments = call.arguments ? call.arguments.arguments : []
91
+ call.block ? arguments + [call.block] : arguments
92
+ end
93
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../skip_sites"
5
+ require_relative "nodes"
6
+ require_relative "purity"
7
+
8
+ # Where a mutation sits relative to the parts of a site that only make up a
9
+ # message (a log line's arguments, a raised error's message).
10
+ module Evilution::AST::SkipSites::Message
11
+ module_function
12
+
13
+ # chain: the mutated node, then its ancestors, nearest first; the site is
14
+ # chain[site_index]. nil when the node lies in none of the parts. Otherwise
15
+ # whether mutating it changes the message alone: it is literal text of the
16
+ # message, outside any interpolated code, or the part it lies in only
17
+ # reads (see Purity).
18
+ def covered_in(chain, site_index, parts)
19
+ index = (0...site_index).find { |position| parts.any? { |part| Evilution::AST::SkipSites::Nodes.same?(chain[position], part) } }
20
+ return nil unless index
21
+
22
+ literal_text?(chain, index) || Evilution::AST::SkipSites::Purity.pure?(chain[index])
23
+ end
24
+
25
+ # For a site whose call does nothing but report: a mutation outside every
26
+ # part -- of the call itself, say, deleting it, or of its receiver -- is
27
+ # covered when every part only reads and the call's value goes unused
28
+ # (see Nodes.value_dropped?), since dropping the call then drops nothing
29
+ # but the report.
30
+ def covered_in_call(chain, site_index, parts)
31
+ covered = covered_in(chain, site_index, parts)
32
+ return covered unless covered.nil?
33
+
34
+ Evilution::AST::SkipSites::Nodes.value_dropped?(chain, site_index) &&
35
+ parts.all? { |part| Evilution::AST::SkipSites::Purity.pure?(part) }
36
+ end
37
+
38
+ # The text of a string, or of a string between interpolations: a mutation
39
+ # of it changes no value but the message.
40
+ def literal_text?(chain, part_index)
41
+ chain.first.is_a?(Prism::StringNode) &&
42
+ chain[1..part_index].all?(Prism::InterpolatedStringNode)
43
+ end
44
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../skip_sites"
5
+
6
+ # Questions the presets ask of single nodes.
7
+ module Evilution::AST::SkipSites::Nodes
8
+ SIGILS = /\A(?:@@|@|\$)/
9
+
10
+ # Parents whose value is the value of the branch taken.
11
+ PASSING_ON = [
12
+ Prism::IfNode, Prism::UnlessNode, Prism::ElseNode, Prism::CaseNode, Prism::CaseMatchNode, Prism::WhenNode,
13
+ Prism::InNode, Prism::BeginNode, Prism::RescueNode, Prism::ParenthesesNode
14
+ ].freeze
15
+ # The children of those parents that are branches, not conditions.
16
+ BRANCHES = [
17
+ Prism::StatementsNode, Prism::ElseNode, Prism::IfNode, Prism::UnlessNode, Prism::WhenNode, Prism::InNode,
18
+ Prism::RescueNode
19
+ ].freeze
20
+ # Parents that never use the value of their statements.
21
+ DISCARDING = [Prism::ProgramNode, Prism::EnsureNode, Prism::WhileNode, Prism::UntilNode, Prism::ForNode].freeze
22
+ private_constant :SIGILS, :PASSING_ON, :BRANCHES, :DISCARDING
23
+
24
+ module_function
25
+
26
+ # Two nodes stand for the same code when they are of one type and span the
27
+ # same bytes: the node a mutation changes comes from its subject's parse of
28
+ # the file, its ancestors from another parse of the same source.
29
+ def same?(node, other)
30
+ node.instance_of?(other.class) &&
31
+ node.location.start_offset == other.location.start_offset &&
32
+ node.location.length == other.location.length
33
+ end
34
+
35
+ # A call of one of the names, sent to no receiver or to Kernel.
36
+ def kernel_call?(node, names)
37
+ return false unless node.is_a?(Prism::CallNode) && names.include?(node.name)
38
+
39
+ receiver = node.receiver
40
+ receiver.nil? || constant_named?(receiver, :Kernel)
41
+ end
42
+
43
+ def constant_named?(node, name)
44
+ (node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)) && node.name == name
45
+ end
46
+
47
+ # The name a receiver goes by, without sigils: `logger`, `@logger`,
48
+ # `$logger`, `LOGGER`, `App::LOGGER` and `Rails.logger` all go by theirs;
49
+ # anything else (no receiver, a literal, a call with arguments) by none.
50
+ def receiver_name(node)
51
+ case node
52
+ when Prism::CallNode
53
+ node.arguments || node.block ? nil : node.name.to_s
54
+ when Prism::LocalVariableReadNode, Prism::InstanceVariableReadNode, Prism::ClassVariableReadNode,
55
+ Prism::GlobalVariableReadNode, Prism::ConstantReadNode, Prism::ConstantPathNode
56
+ node.name.to_s.sub(SIGILS, "")
57
+ end
58
+ end
59
+
60
+ # The positional arguments of a call: its arguments without keywords, or
61
+ # nil when a splat or a forwarded `...` makes their positions unknown.
62
+ def positional_arguments(call)
63
+ arguments = call.arguments ? call.arguments.arguments : []
64
+ positional = arguments.grep_v(Prism::KeywordHashNode)
65
+ return nil if positional.any? { |argument| argument.is_a?(Prism::SplatNode) || argument.is_a?(Prism::ForwardingArgumentsNode) }
66
+
67
+ positional
68
+ end
69
+
70
+ def string?(node)
71
+ node.is_a?(Prism::StringNode) || node.is_a?(Prism::InterpolatedStringNode)
72
+ end
73
+
74
+ # chain: a node, then its ancestors, nearest first. Whether the value of
75
+ # chain[index] goes unused: it is a statement followed by another one, or
76
+ # the last statement of a body whose value is itself unused -- a branch of
77
+ # a conditional or a `begin` whose value goes unused, an `ensure` clause, a
78
+ # loop body, the top of the file. The last statement of a method or a
79
+ # block gives the value it returns, and anything else -- an assignment, an
80
+ # operand, an argument, a condition -- uses it. Without the ancestors
81
+ # nothing is known, and the value counts as used.
82
+ def value_dropped?(chain, index)
83
+ child = chain[index]
84
+ chain.drop(index + 1).each do |parent|
85
+ verdict = value_dropped_in(parent, child)
86
+ return verdict unless verdict.nil?
87
+
88
+ child = parent
89
+ end
90
+ false
91
+ end
92
+
93
+ # true or false when the parent settles it, nil when its own value decides.
94
+ def value_dropped_in(parent, child)
95
+ return (same?(parent.body.last, child) ? nil : true) if parent.is_a?(Prism::StatementsNode)
96
+ return nil if passes_on?(parent, child)
97
+
98
+ child.is_a?(Prism::StatementsNode) && DISCARDING.any? { |type| parent.is_a?(type) }
99
+ end
100
+
101
+ # A condition is never of a branch's type: Ruby wants it in parentheses.
102
+ def passes_on?(parent, child)
103
+ PASSING_ON.any? { |type| parent.is_a?(type) } && BRANCHES.any? { |type| child.is_a?(type) }
104
+ end
105
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../skip_sites"
4
+ require_relative "logging"
5
+ require_relative "debug_output"
6
+ require_relative "error_messages"
7
+ require_relative "requires"
8
+
9
+ # The presets by name. Each answers #covers?(chain): whether the node a
10
+ # mutation changes, followed by its ancestors nearest first, lies inside one
11
+ # of its sites in a way that only changes what the site reports.
12
+ #
13
+ # Listed in the order a skipped mutation is attributed: one that two presets
14
+ # cover is counted under the first.
15
+ module Evilution::AST::SkipSites::Presets
16
+ ALL = {
17
+ logging: Evilution::AST::SkipSites::Logging,
18
+ debug_output: Evilution::AST::SkipSites::DebugOutput,
19
+ error_messages: Evilution::AST::SkipSites::ErrorMessages,
20
+ requires: Evilution::AST::SkipSites::Requires
21
+ }.freeze
22
+
23
+ def self.names
24
+ ALL.keys
25
+ end
26
+
27
+ # names: Symbols from .names; the presets come back in attribution order.
28
+ def self.build(names)
29
+ ALL.filter_map { |name, preset| preset.new if names.include?(name) }
30
+ end
31
+ end