evilution 1.5.1 → 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 (227) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +30 -0
  3. data/CHANGELOG.md +49 -0
  4. data/README.md +285 -45
  5. data/docs/architecture.md +110 -12
  6. data/docs/ast_pattern_syntax.md +35 -7
  7. data/docs/combined_runs_spike.md +407 -0
  8. data/docs/integrations.md +1 -1
  9. data/docs/isolation.md +63 -2
  10. data/docs/migration-from-mutant.md +16 -11
  11. data/docs/mutant_schemata_spike.md +387 -0
  12. data/docs/operator_yield_study.md +576 -0
  13. data/docs/probe_first_spike.md +264 -0
  14. data/lib/evilution/ast/ancestor_lookup.rb +36 -0
  15. data/lib/evilution/ast/callback_declaration.rb +36 -3
  16. data/lib/evilution/ast/class_body.rb +62 -16
  17. data/lib/evilution/ast/def_region.rb +130 -0
  18. data/lib/evilution/ast/local_declaration.rb +37 -0
  19. data/lib/evilution/ast/parent_index.rb +50 -0
  20. data/lib/evilution/ast/parse_cache.rb +27 -0
  21. data/lib/evilution/ast/parser.rb +8 -3
  22. data/lib/evilution/ast/pattern/filter.rb +53 -12
  23. data/lib/evilution/ast/skip_filter.rb +83 -0
  24. data/lib/evilution/ast/skip_sites/debug_output.rb +67 -0
  25. data/lib/evilution/ast/skip_sites/error_messages.rb +74 -0
  26. data/lib/evilution/ast/skip_sites/logging.rb +93 -0
  27. data/lib/evilution/ast/skip_sites/message.rb +44 -0
  28. data/lib/evilution/ast/skip_sites/nodes.rb +105 -0
  29. data/lib/evilution/ast/skip_sites/presets.rb +31 -0
  30. data/lib/evilution/ast/skip_sites/purity.rb +69 -0
  31. data/lib/evilution/ast/skip_sites/requires.rb +40 -0
  32. data/lib/evilution/ast/skip_sites.rb +9 -0
  33. data/lib/evilution/ast/source_surgeon.rb +20 -2
  34. data/lib/evilution/ast/syntax_check.rb +10 -8
  35. data/lib/evilution/baseline/report.rb +11 -2
  36. data/lib/evilution/baseline.rb +31 -6
  37. data/lib/evilution/cache/keys.rb +140 -0
  38. data/lib/evilution/cache/run_fingerprint.rb +51 -0
  39. data/lib/evilution/cache/skeleton.rb +57 -0
  40. data/lib/evilution/cache/spec_digest.rb +39 -0
  41. data/lib/evilution/cache.rb +143 -50
  42. data/lib/evilution/class_tree.rb +38 -0
  43. data/lib/evilution/cli/command.rb +12 -4
  44. data/lib/evilution/cli/commands/run.rb +12 -3
  45. data/lib/evilution/cli/commands/subjects.rb +5 -2
  46. data/lib/evilution/cli/parser/options_builder.rb +67 -3
  47. data/lib/evilution/cli/printers/environment.rb +34 -4
  48. data/lib/evilution/cli/printers/session_detail.rb +20 -3
  49. data/lib/evilution/compare/categorizer.rb +2 -1
  50. data/lib/evilution/compare/normalizer.rb +3 -2
  51. data/lib/evilution/config/sources.rb +3 -0
  52. data/lib/evilution/config/validators/base.rb +9 -0
  53. data/lib/evilution/config/validators/since.rb +22 -0
  54. data/lib/evilution/config/validators/skip_sites.rb +24 -0
  55. data/lib/evilution/config/validators/timeout_ceiling.rb +10 -0
  56. data/lib/evilution/config/validators/timeout_factor.rb +10 -0
  57. data/lib/evilution/config/validators/timeout_floor.rb +10 -0
  58. data/lib/evilution/config.rb +122 -6
  59. data/lib/evilution/coverage/load_tracker.rb +50 -0
  60. data/lib/evilution/coverage/map.rb +42 -5
  61. data/lib/evilution/coverage/map_builder.rb +123 -22
  62. data/lib/evilution/coverage/map_store.rb +15 -5
  63. data/lib/evilution/coverage/recorder.rb +42 -3
  64. data/lib/evilution/coverage_example_filter.rb +35 -0
  65. data/lib/evilution/equivalent/heuristic/comment_marking.rb +14 -1
  66. data/lib/evilution/example_filter.rb +20 -0
  67. data/lib/evilution/git/changed_lines.rb +103 -0
  68. data/lib/evilution/git/since_scope.rb +77 -0
  69. data/lib/evilution/git/unified_diff.rb +68 -0
  70. data/lib/evilution/infra_crash.rb +26 -0
  71. data/lib/evilution/integration/base.rb +97 -6
  72. data/lib/evilution/integration/crash_detector.rb +46 -6
  73. data/lib/evilution/integration/known_failures.rb +4 -0
  74. data/lib/evilution/integration/loading/body_call_neutralizer.rb +49 -11
  75. data/lib/evilution/integration/loading/callback_equivalence.rb +62 -0
  76. data/lib/evilution/integration/loading/callback_redeclaration.rb +151 -36
  77. data/lib/evilution/integration/loading/concern_redeclaration.rb +16 -12
  78. data/lib/evilution/integration/loading/concern_registry.rb +87 -0
  79. data/lib/evilution/integration/loading/concern_state_cleaner.rb +19 -8
  80. data/lib/evilution/integration/loading/created_modules.rb +56 -0
  81. data/lib/evilution/integration/loading/frozen_namespaces.rb +80 -0
  82. data/lib/evilution/integration/loading/mutation_applier.rb +42 -11
  83. data/lib/evilution/integration/loading/neutralization.rb +41 -0
  84. data/lib/evilution/integration/loading/redefinition_recovery.rb +5 -3
  85. data/lib/evilution/integration/loading/superseded_modules.rb +22 -19
  86. data/lib/evilution/integration/minitest/stop_hook.rb +57 -0
  87. data/lib/evilution/integration/minitest.rb +22 -2
  88. data/lib/evilution/integration/minitest_crash_detector.rb +19 -0
  89. data/lib/evilution/integration/rspec/baseline_runner.rb +3 -2
  90. data/lib/evilution/integration/rspec/crash_detector_lifecycle.rb +6 -1
  91. data/lib/evilution/integration/rspec/example_filter_applier.rb +16 -0
  92. data/lib/evilution/integration/rspec/example_ids.rb +23 -0
  93. data/lib/evilution/integration/rspec/example_order.rb +86 -0
  94. data/lib/evilution/integration/rspec/killer_first.rb +72 -0
  95. data/lib/evilution/integration/rspec/result_builder.rb +7 -0
  96. data/lib/evilution/integration/rspec/state_guard/example_group_descendants.rb +44 -0
  97. data/lib/evilution/integration/rspec/state_guard.rb +2 -2
  98. data/lib/evilution/integration/rspec/target_resolver.rb +42 -0
  99. data/lib/evilution/integration/rspec/test_file_resolver.rb +4 -2
  100. data/lib/evilution/integration/rspec.rb +98 -47
  101. data/lib/evilution/integration/stop_policy.rb +49 -0
  102. data/lib/evilution/integration/test_unit/dispatcher.rb +20 -1
  103. data/lib/evilution/integration/test_unit/subject_class_registry.rb +6 -1
  104. data/lib/evilution/integration/test_unit.rb +28 -2
  105. data/lib/evilution/isolation/fork.rb +58 -13
  106. data/lib/evilution/isolation/in_process.rb +12 -1
  107. data/lib/evilution/line_set.rb +72 -0
  108. data/lib/evilution/mcp/info_tool/actions/environment.rb +12 -16
  109. data/lib/evilution/mcp/info_tool/actions/subjects.rb +3 -5
  110. data/lib/evilution/mcp/info_tool/status_glossary.rb +15 -2
  111. data/lib/evilution/mcp/info_tool.rb +24 -17
  112. data/lib/evilution/mcp/mutate_tool/config_builder.rb +13 -1
  113. data/lib/evilution/mcp/mutate_tool/option_parser.rb +2 -2
  114. data/lib/evilution/mcp/mutate_tool/progress_streamer.rb +1 -1
  115. data/lib/evilution/mcp/mutate_tool/report_trimmer.rb +28 -5
  116. data/lib/evilution/mcp/mutate_tool/survived_enricher.rb +12 -7
  117. data/lib/evilution/mcp/mutate_tool.rb +32 -7
  118. data/lib/evilution/mcp/session_tool.rb +1 -2
  119. data/lib/evilution/mcp.rb +4 -0
  120. data/lib/evilution/mutation/line_diff.rb +111 -0
  121. data/lib/evilution/mutation.rb +17 -13
  122. data/lib/evilution/mutator/base.rb +121 -98
  123. data/lib/evilution/mutator/constant_value_subjects.rb +29 -1
  124. data/lib/evilution/mutator/line_range_filter.rb +27 -0
  125. data/lib/evilution/mutator/load_sources.rb +88 -0
  126. data/lib/evilution/mutator/operator/alias_removal.rb +2 -5
  127. data/lib/evilution/mutator/operator/argument_method_call_replacement.rb +3 -0
  128. data/lib/evilution/mutator/operator/attr_accessor_swap.rb +2 -5
  129. data/lib/evilution/mutator/operator/constant_namespace_strip.rb +3 -3
  130. data/lib/evilution/mutator/operator/freeze_removal.rb +29 -11
  131. data/lib/evilution/mutator/operator/method_call_removal.rb +3 -1
  132. data/lib/evilution/mutator/operator/mixin_removal.rb +2 -5
  133. data/lib/evilution/mutator/operator/superclass_removal.rb +2 -5
  134. data/lib/evilution/mutator/operator/symbol_literal.rb +1 -1
  135. data/lib/evilution/mutator/operator/visibility_removal.rb +2 -5
  136. data/lib/evilution/mutator/registry.rb +11 -5
  137. data/lib/evilution/mutator/restore_source_cache.rb +41 -0
  138. data/lib/evilution/parallel/pool.rb +11 -4
  139. data/lib/evilution/parallel/work_queue/dispatcher/result_stream.rb +40 -0
  140. data/lib/evilution/parallel/work_queue/dispatcher.rb +20 -7
  141. data/lib/evilution/parallel/work_queue/worker/loop.rb +7 -2
  142. data/lib/evilution/parallel/work_queue/worker.rb +10 -3
  143. data/lib/evilution/parallel/work_queue/worker_stat.rb +7 -0
  144. data/lib/evilution/parallel/work_queue.rb +69 -12
  145. data/lib/evilution/parallel_db_warning.rb +39 -12
  146. data/lib/evilution/process_supervisor.rb +22 -2
  147. data/lib/evilution/reporter/cli/item_formatters/coverage_gap.rb +13 -2
  148. data/lib/evilution/reporter/cli/item_formatters/subject_score.rb +5 -1
  149. data/lib/evilution/reporter/cli/line_formatters/mutations.rb +9 -2
  150. data/lib/evilution/reporter/cli/line_formatters/result_line.rb +9 -1
  151. data/lib/evilution/reporter/cli/line_formatters/skipped_sites.rb +14 -0
  152. data/lib/evilution/reporter/cli/line_formatters/uncovered_code.rb +17 -0
  153. data/lib/evilution/reporter/cli/metrics_block.rb +4 -0
  154. data/lib/evilution/reporter/cli/trailer.rb +4 -4
  155. data/lib/evilution/reporter/cli.rb +9 -2
  156. data/lib/evilution/reporter/html/assets/style.css +8 -0
  157. data/lib/evilution/reporter/html/sections/file_section.rb +9 -0
  158. data/lib/evilution/reporter/html/sections/uncovered_details.rb +27 -0
  159. data/lib/evilution/reporter/html/templates/file_section.html.erb +2 -1
  160. data/lib/evilution/reporter/html/templates/summary_cards.html.erb +3 -0
  161. data/lib/evilution/reporter/html/templates/uncovered_details.html.erb +11 -0
  162. data/lib/evilution/reporter/json/mutation_detail.rb +77 -0
  163. data/lib/evilution/reporter/json/subjects.rb +1 -0
  164. data/lib/evilution/reporter/json/timings.rb +42 -0
  165. data/lib/evilution/reporter/json.rb +13 -50
  166. data/lib/evilution/result/coverage_gap.rb +5 -0
  167. data/lib/evilution/result/mutation_result.rb +55 -3
  168. data/lib/evilution/result/mutation_timings.rb +54 -0
  169. data/lib/evilution/result/run_timings.rb +20 -0
  170. data/lib/evilution/result/subject_score.rb +8 -1
  171. data/lib/evilution/result/subject_scorer.rb +5 -3
  172. data/lib/evilution/result/summary.rb +49 -12
  173. data/lib/evilution/runner/baseline_runner.rb +47 -5
  174. data/lib/evilution/runner/diagnostics.rb +0 -13
  175. data/lib/evilution/runner/isolation_resolver.rb +16 -4
  176. data/lib/evilution/runner/mutation_executor/isolated_run.rb +34 -0
  177. data/lib/evilution/runner/mutation_executor/kill_history.rb +66 -0
  178. data/lib/evilution/runner/mutation_executor/mutation_runner.rb +31 -12
  179. data/lib/evilution/runner/mutation_executor/neutralizer/baseline_failed.rb +1 -0
  180. data/lib/evilution/runner/mutation_executor/neutralizer/infra_error.rb +6 -20
  181. data/lib/evilution/runner/mutation_executor/probe_first.rb +99 -0
  182. data/lib/evilution/runner/mutation_executor/probe_first_run.rb +59 -0
  183. data/lib/evilution/runner/mutation_executor/result_cache.rb +45 -14
  184. data/lib/evilution/runner/mutation_executor/result_notifier.rb +1 -1
  185. data/lib/evilution/runner/mutation_executor/result_packer.rb +54 -24
  186. data/lib/evilution/runner/mutation_executor/runless_verdict.rb +39 -0
  187. data/lib/evilution/runner/mutation_executor/strategy/parallel/hinted_items.rb +27 -0
  188. data/lib/evilution/runner/mutation_executor/strategy/parallel/ordered_delivery.rb +97 -0
  189. data/lib/evilution/runner/mutation_executor/strategy/parallel.rb +102 -83
  190. data/lib/evilution/runner/mutation_executor/timed_run.rb +33 -0
  191. data/lib/evilution/runner/mutation_executor/timeout_policy/adaptive.rb +67 -0
  192. data/lib/evilution/runner/mutation_executor/timeout_policy/base.rb +35 -0
  193. data/lib/evilution/runner/mutation_executor/timeout_policy/fixed.rb +17 -0
  194. data/lib/evilution/runner/mutation_executor/timeout_policy.rb +56 -0
  195. data/lib/evilution/runner/mutation_executor.rb +75 -10
  196. data/lib/evilution/runner/mutation_planner.rb +29 -21
  197. data/lib/evilution/runner/phase_clock.rb +22 -0
  198. data/lib/evilution/runner/report_publisher.rb +1 -1
  199. data/lib/evilution/runner/subject_pipeline.rb +18 -8
  200. data/lib/evilution/runner.rb +87 -26
  201. data/lib/evilution/session/diff.rb +9 -2
  202. data/lib/evilution/session/store.rb +17 -2
  203. data/lib/evilution/source_ast_cache.rb +10 -0
  204. data/lib/evilution/spec_ast_cache.rb +15 -0
  205. data/lib/evilution/spec_resolver/constant_references.rb +66 -0
  206. data/lib/evilution/spec_resolver/project_files.rb +50 -0
  207. data/lib/evilution/spec_resolver/sibling_spec_ranker.rb +111 -0
  208. data/lib/evilution/spec_resolver/spec_pattern.rb +19 -0
  209. data/lib/evilution/spec_resolver.rb +76 -65
  210. data/lib/evilution/spec_selector.rb +17 -13
  211. data/lib/evilution/subject.rb +7 -2
  212. data/lib/evilution/version.rb +1 -1
  213. data/lib/evilution/worker_database/copies.rb +73 -0
  214. data/lib/evilution/worker_database/schema.rb +39 -0
  215. data/lib/evilution/worker_database/sqlite_copy.rb +65 -0
  216. data/lib/evilution/worker_database.rb +214 -0
  217. data/schema/evilution.config.schema.json +74 -2
  218. metadata +84 -13
  219. data/lib/evilution/integration/rspec/state_guard/object_space_example_groups.rb +0 -42
  220. data/scripts/benchmark_density +0 -262
  221. data/scripts/benchmark_density.yml +0 -19
  222. data/scripts/canary_manifest.yml +0 -47
  223. data/scripts/compare_mutations +0 -421
  224. data/scripts/compare_mutations.yml +0 -24
  225. data/scripts/compare_targeting +0 -279
  226. data/scripts/compare_targeting.example.yml +0 -24
  227. data/scripts/mutant_json_adapter +0 -227
@@ -18,14 +18,19 @@ require_relative "digest"
18
18
  class Evilution::Coverage::MapStore
19
19
  DEFAULT_ROOT = ".evilution/coverage"
20
20
  CACHE_FILE = "map.json"
21
+ # Bumped when the map gains something an older cache cannot hold (the
22
+ # unexecuted lines and spec files, the files the build loaded): such a cache
23
+ # is rebuilt, not read.
24
+ FORMAT = 3
21
25
 
22
26
  def initialize(root: DEFAULT_ROOT, digest: Evilution::Coverage::Digest.new)
23
27
  @root = root
24
28
  @digest = digest
25
29
  end
26
30
 
31
+ # Keyed on the source files and on every file the map's build loaded.
27
32
  def save(map, source_files)
28
- payload = { "digests" => digests_for(source_files), "map" => map.to_h }
33
+ payload = { "format" => FORMAT, "digests" => digests_for(source_files | map.loaded_files), "map" => map.to_h }
29
34
  FileUtils.mkdir_p(@root)
30
35
  File.write(cache_path, JSON.generate(payload))
31
36
  end
@@ -41,13 +46,15 @@ class Evilution::Coverage::MapStore
41
46
 
42
47
  # Source files whose on-disk content no longer matches the cache (changed,
43
48
  # deleted, or never cached) -- the caller rebuilds these. Every file is stale
44
- # when there is no cache at all.
49
+ # when there is no cache at all. The files the cache was keyed on are checked
50
+ # too, though the caller does not name them: a change to a file the build
51
+ # loaded makes the cache stale as well.
45
52
  def stale_files(source_files)
46
53
  payload = read_payload
47
54
  return source_files.dup unless payload
48
55
 
49
56
  cached_digests = payload["digests"] || {}
50
- source_files.reject { |file| fresh?(file, cached_digests) }
57
+ (source_files | cached_digests.keys).reject { |file| fresh?(file, cached_digests) }
51
58
  end
52
59
 
53
60
  private
@@ -61,8 +68,10 @@ class Evilution::Coverage::MapStore
61
68
  index = (raw_map["index"] || {}).slice(*fresh_files)
62
69
  built = (raw_map["built_files"] || []) & fresh_files
63
70
  executed = (raw_map["executed_lines"] || {}).slice(*fresh_files)
71
+ missed = raw_map["missed_lines"].slice(*fresh_files)
64
72
  Evilution::Coverage::Map.from_h(
65
- "index" => index, "built_files" => built, "executed_lines" => executed
73
+ "index" => index, "built_files" => built, "executed_lines" => executed,
74
+ "missed_lines" => missed, "spec_files" => raw_map["spec_files"], "loaded_files" => raw_map["loaded_files"]
66
75
  )
67
76
  end
68
77
 
@@ -76,7 +85,8 @@ class Evilution::Coverage::MapStore
76
85
  def read_payload
77
86
  return nil unless File.file?(cache_path)
78
87
 
79
- JSON.parse(File.read(cache_path))
88
+ payload = JSON.parse(File.read(cache_path))
89
+ payload.is_a?(Hash) && payload["format"] == FORMAT ? payload : nil
80
90
  rescue JSON::ParserError, Errno::ENOENT
81
91
  nil
82
92
  end
@@ -8,9 +8,13 @@ require_relative "map"
8
8
  # coverage_source is injected for testability; in production it is
9
9
  # -> { ::Coverage.peek_result }.
10
10
  class Evilution::Coverage::Recorder
11
- def initialize(target_files:, coverage_source: -> { ::Coverage.peek_result })
11
+ # prior_counts: line counts taken before the build forked. Ruby starts a
12
+ # forked child's counts at zero, so the lines a file ran while it loaded in
13
+ # the parent (a preload) are known only from these.
14
+ def initialize(target_files:, coverage_source: -> { ::Coverage.peek_result }, prior_counts: {})
12
15
  @target_files = target_files.to_a
13
16
  @coverage_source = coverage_source
17
+ @prior_counts = prior_counts
14
18
  @index = Hash.new { |h, file| h[file] = Hash.new { |g, line| g[line] = [] } }
15
19
  @executed = Hash.new { |h, file| h[file] = [] }
16
20
  end
@@ -23,11 +27,21 @@ class Evilution::Coverage::Recorder
23
27
  result
24
28
  end
25
29
 
26
- def to_map(built_files:)
30
+ # vouched_files: the target files whose coverage the build can stand behind
31
+ # (see MapBuilder). For those, the end of the build is read once more: what
32
+ # ran after the last example counts as executed, and the executable lines
33
+ # still at zero are recorded as never run.
34
+ # loaded_files: the project files the build loaded (see Map).
35
+ def to_map(built_files:, vouched_files: [], spec_files: [], loaded_files: [])
36
+ record_prior
37
+ missed = record_end_of_build(vouched_files)
27
38
  Evilution::Coverage::Map.new(
28
39
  index: materialize(@index),
29
40
  built_files: built_files,
30
- executed_lines: @executed.transform_values(&:uniq)
41
+ executed_lines: @executed.transform_values(&:uniq),
42
+ missed_lines: missed,
43
+ spec_files: spec_files,
44
+ loaded_files: loaded_files
31
45
  )
32
46
  end
33
47
 
@@ -59,6 +73,31 @@ class Evilution::Coverage::Recorder
59
73
  end
60
74
  end
61
75
 
76
+ def record_prior
77
+ @target_files.each do |file|
78
+ counts = line_counts(@prior_counts[file])
79
+ record_executed(file, counts) if counts
80
+ end
81
+ end
82
+
83
+ def record_end_of_build(files)
84
+ final = snapshot
85
+ files.each_with_object({}) do |file, missed|
86
+ counts = line_counts(final[file])
87
+ next unless counts
88
+
89
+ record_executed(file, counts)
90
+ missed[file] = unexecuted_lines(counts)
91
+ end
92
+ end
93
+
94
+ # nil marks a line that is not executable; zero, one that never ran in the
95
+ # build's child. A line that ran before the fork is in the executed lines,
96
+ # which Map#uncovered? lets win.
97
+ def unexecuted_lines(counts)
98
+ counts.each_with_index.filter_map { |count, idx| idx + 1 if count.is_a?(Integer) && count.zero? }
99
+ end
100
+
62
101
  # Credit example_location with every line whose execution count rose between
63
102
  # the before/after snapshots (a newly-executed, executable line).
64
103
  def record_increases(file, before_counts, after_counts, example_location)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "version"
4
4
  require_relative "coverage/map"
5
+ require_relative "ast/local_declaration"
5
6
 
6
7
  # Per-mutation example targeting backed by a real line-coverage Map (EV-ndjd).
7
8
  # Honours the same contract as the lexical Evilution::ExampleFilter --
@@ -27,6 +28,7 @@ class Evilution::CoverageExampleFilter
27
28
  @map = map
28
29
  @lexical = lexical
29
30
  @project_root = project_root.to_s
31
+ @local_declaration = Evilution::AST::LocalDeclaration.new
30
32
  end
31
33
 
32
34
  def call(mutation, spec_paths)
@@ -38,4 +40,37 @@ class Evilution::CoverageExampleFilter
38
40
 
39
41
  @lexical.call(mutation, spec_paths)
40
42
  end
43
+
44
+ # Whether no example of these spec files, and no file load, executes any
45
+ # line the mutation changes -- so running it could only end in a survivor.
46
+ # The lines it changes, not the line it is reported at: a method body's
47
+ # mutation is reported at its `def`, which runs while the file loads. Only a
48
+ # map built from every one of the spec files can say so; anything else (a
49
+ # spec file the build did not run, the whole spec dir, a file the map cannot
50
+ # vouch for) answers no and the mutation runs.
51
+ #
52
+ # A mutation that adds or removes a local variable assignment runs too:
53
+ # Ruby settles what a name means while parsing, so the change reaches lines
54
+ # that do run (see Evilution::AST::LocalDeclaration).
55
+ def uncovered?(mutation, spec_paths)
56
+ return false if spec_paths.nil?
57
+
58
+ specs = spec_paths.map { |path| File.expand_path(path, @project_root) }
59
+ return false unless @map.built_from?(specs)
60
+
61
+ file = File.expand_path(mutation.file_path, @project_root)
62
+ @map.uncovered?(file, mutation.changed_lines) && !declares_local?(mutation)
63
+ end
64
+
65
+ # The map is built before this filter is; the lexical filter it falls back
66
+ # on parses lazily, and is what a forked child would otherwise fill.
67
+ def warm(mutation, spec_paths)
68
+ @lexical.warm(mutation, spec_paths)
69
+ end
70
+
71
+ private
72
+
73
+ def declares_local?(mutation)
74
+ @local_declaration.in?(mutation.original_slice) || @local_declaration.in?(mutation.mutated_slice)
75
+ end
41
76
  end
@@ -7,7 +7,7 @@ class Evilution::Equivalent::Heuristic::CommentMarking
7
7
 
8
8
  def match?(mutation)
9
9
  source = mutation.original_source
10
- return false unless source
10
+ return false unless source && marked?(source)
11
11
 
12
12
  lines = source.lines
13
13
  line_index = mutation.line - 1
@@ -18,4 +18,17 @@ class Evilution::Equivalent::Heuristic::CommentMarking
18
18
 
19
19
  false
20
20
  end
21
+
22
+ private
23
+
24
+ # Whether the marker is anywhere in the file: when it is not, no line of
25
+ # it is looked at, sparing the split of the file into lines per mutation.
26
+ # Mutations come file by file, so the answer for the last file is kept. A
27
+ # marker found here may still run across a line break; the lines decide.
28
+ def marked?(source)
29
+ return @marked if @source.equal?(source)
30
+
31
+ @source = source
32
+ @marked = source.match?(MARKER)
33
+ end
21
34
  end
@@ -31,8 +31,28 @@ class Evilution::ExampleFilter
31
31
  locations.sort
32
32
  end
33
33
 
34
+ # Parses what #call would for this mutation and spec files, so the caches
35
+ # hold it before any per-mutation process is forked: a forked child keeps
36
+ # what its parent cached, and loses what it adds itself. Only what the
37
+ # bounded caches hold without evicting is parsed; the rest is left for the
38
+ # child that needs it. A file that does not parse is left for #call to
39
+ # report when a mutation needs it.
40
+ def warm(mutation, spec_paths)
41
+ source = mutation.original_source
42
+ return if source.nil?
43
+
44
+ @source_cache.preload(source) unless @source_cache.nil?
45
+ spec_paths.each { |path| warm_spec(path) }
46
+ end
47
+
34
48
  private
35
49
 
50
+ def warm_spec(path)
51
+ @cache.preload(path)
52
+ rescue Evilution::ParseError
53
+ nil
54
+ end
55
+
36
56
  def fallback_result(spec_paths)
37
57
  case @fallback
38
58
  when :full_file then spec_paths
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
5
+ require_relative "../git"
6
+ require_relative "../line_set"
7
+ require_relative "unified_diff"
8
+
9
+ # The lines changed since a git ref, per file: what a pull request changes.
10
+ #
11
+ # The diff runs from the merge base of the ref and HEAD -- what `ref...HEAD`
12
+ # compares -- so that what the ref gained after the branch left it does not
13
+ # count, to the working tree, so that changes not yet committed do. Files git
14
+ # does not track yet count whole; those it ignores do not count. Deleted files
15
+ # have no lines left to mutate. A rename counts only its edited lines, under
16
+ # the new name.
17
+ #
18
+ # Lines that were only deleted leave no line of their own behind; the lines
19
+ # either side of the gap count instead (Evilution::Git::UnifiedDiff), so that
20
+ # a method whose only change is a deleted line is still mutated where that
21
+ # line was.
22
+ #
23
+ # Paths are relative to the current directory, as file arguments are, and
24
+ # files outside it are left out.
25
+ class Evilution::Git::ChangedLines
26
+ # Pinned against the user's git config: Evilution::Git::UnifiedDiff reads
27
+ # the new file's name after a `b/` prefix, colours, external diff tools and
28
+ # text conversion would break that read or shift its lines, rename
29
+ # detection decides whether a renamed file counts whole, and inter-hunk
30
+ # context would join nearby hunks, unchanged lines between them included.
31
+ DIFF_ARGS = %w[
32
+ diff -U0 --inter-hunk-context=0 --no-color --no-ext-diff --no-textconv --relative --find-renames
33
+ --diff-filter=ACMRT --dst-prefix=b/
34
+ ].freeze
35
+ private_constant :DIFF_ARGS
36
+
37
+ # GIT_DIFF_OPTS sets the context lines of a diff over -U0; unset, so that
38
+ # only changed lines come back.
39
+ GIT_ENV = { "GIT_DIFF_OPTS" => nil }.freeze
40
+ private_constant :GIT_ENV
41
+
42
+ def initialize(ref)
43
+ @ref = ref
44
+ end
45
+
46
+ # Returns { path => Evilution::LineSet } for every file with a changed line.
47
+ def call
48
+ ensure_repository
49
+ base = merge_base
50
+ changed = Evilution::Git::UnifiedDiff.parse(git(*DIFF_ARGS, base, "--"))
51
+ untracked.each { |path| changed[path] = Evilution::LineSet.new([1..Float::INFINITY]) }
52
+ changed
53
+ end
54
+
55
+ private
56
+
57
+ attr_reader :ref
58
+
59
+ def ensure_repository
60
+ return if run("rev-parse", "--is-inside-work-tree").last.success?
61
+
62
+ raise Evilution::ConfigError, "--since needs a git repository: #{Dir.pwd} is not inside one"
63
+ end
64
+
65
+ # A ref starting with a dash reaches rev-parse as an option it rejects, so
66
+ # it is reported unknown before merge-base could read it as one.
67
+ def merge_base
68
+ commit = "#{ref}^{commit}"
69
+ known = run("rev-parse", "--verify", "--quiet", commit).last.success?
70
+ raise Evilution::ConfigError, "--since: unknown git ref '#{ref}'" unless known
71
+
72
+ output, status = run("merge-base", commit, "HEAD")
73
+ return output.strip if status.success?
74
+
75
+ raise Evilution::ConfigError,
76
+ "--since: no merge base between #{ref} and HEAD. In a shallow clone, fetch the history " \
77
+ "both share (e.g. actions/checkout with fetch-depth: 0, or git fetch --unshallow)"
78
+ end
79
+
80
+ def untracked
81
+ git("ls-files", "--others", "--exclude-standard", "-z").split("\0").map { |path| utf8(path) }
82
+ end
83
+
84
+ def utf8(text)
85
+ text.dup.force_encoding(Encoding::UTF_8)
86
+ end
87
+
88
+ def git(*args)
89
+ output, status = run(*args)
90
+ return output if status.success?
91
+
92
+ raise Evilution::ConfigError, "--since: git #{args.first} failed: #{output.strip}"
93
+ end
94
+
95
+ # Output is read as bytes: a diff holds whatever the files hold, which need
96
+ # not be valid UTF-8.
97
+ def run(*)
98
+ output, error, status = Open3.capture3(GIT_ENV, "git", "-c", "core.quotePath=false", *, binmode: true)
99
+ [output.empty? ? error : output, status]
100
+ rescue SystemCallError => e
101
+ raise Evilution::ConfigError, "--since needs git, which could not be run: #{e.message}"
102
+ end
103
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../git"
4
+ require_relative "../line_set"
5
+ require_relative "changed_files"
6
+ require_relative "changed_lines"
7
+
8
+ # Limits the files and line ranges of a run to the lines changed since a git
9
+ # ref (--since REF), handing back the same files and ranges a command line
10
+ # would give, for the rest of the run to treat as it treats those.
11
+ #
12
+ # Without file arguments, every changed Ruby file under lib/ or app/ is taken,
13
+ # as the run without arguments takes the git-changed files. With them, the
14
+ # files are only narrowed: a file given that did not change drops out, and a
15
+ # file's line range keeps only its changed lines. Every file kept has a line
16
+ # set, so a file never runs whole by having none.
17
+ class Evilution::Git::SinceScope
18
+ Paths = Data.define(:files, :ranges)
19
+
20
+ # ref: the git ref, or nil for no --since, which leaves files and
21
+ # line_ranges as given.
22
+ #
23
+ # A file given must exist and be a file: one that did not change drops out
24
+ # of the run, so a misspelled path or a directory would otherwise pass
25
+ # without anything checked.
26
+ def self.apply(ref, files:, line_ranges:)
27
+ return Paths.new(files: files, ranges: line_ranges) if ref.nil?
28
+
29
+ files.each { |file| check_file(file) }
30
+ new(Evilution::Git::ChangedLines.new(ref).call).call(files: files, line_ranges: line_ranges)
31
+ end
32
+
33
+ def self.check_file(file)
34
+ return if File.file?(file)
35
+ raise Evilution::ConfigError, "file not found: #{file}" unless File.exist?(file)
36
+
37
+ raise Evilution::ConfigError,
38
+ "#{file} is a directory; --since takes files, or no file arguments for every changed file under lib/ and app/"
39
+ end
40
+ private_class_method :check_file
41
+
42
+ # changed: { path => Evilution::LineSet }, as Evilution::Git::ChangedLines
43
+ # returns it.
44
+ def initialize(changed)
45
+ @changed = changed
46
+ end
47
+
48
+ def call(files:, line_ranges:)
49
+ files.empty? ? changed_sources : given_files(files, line_ranges)
50
+ end
51
+
52
+ private
53
+
54
+ attr_reader :changed
55
+
56
+ def changed_sources
57
+ picked = changed.select { |path, _| source_file?(path) }.sort.to_h
58
+ Paths.new(files: picked.keys, ranges: picked)
59
+ end
60
+
61
+ def source_file?(path)
62
+ path.end_with?(".rb") && Evilution::Git::ChangedFiles::SOURCE_PREFIXES.any? { |prefix| path.start_with?(prefix) }
63
+ end
64
+
65
+ def given_files(files, line_ranges)
66
+ by_full_path = changed.transform_keys { |path| File.expand_path(path) }
67
+ ranges = {}
68
+ files.each do |file|
69
+ lines = by_full_path[File.expand_path(file)]
70
+ next if lines.nil?
71
+
72
+ lines &= line_ranges[file] if line_ranges.key?(file)
73
+ ranges[file] = lines unless lines.empty?
74
+ end
75
+ Paths.new(files: ranges.keys, ranges: ranges)
76
+ end
77
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../git"
4
+ require_relative "../line_set"
5
+
6
+ # Reads the new-file lines each hunk of `git diff -U0` output touches, per
7
+ # file: { path => Evilution::LineSet }. Expects git's default `b/` prefix on
8
+ # the new file's name.
9
+ #
10
+ # A line starting with `diff --git ` or `@@ ` is always a header: every
11
+ # content line of a hunk starts with `+`, `-`, a space or `\`. A file's `+++`
12
+ # line is read only between its `diff --git` line and its first hunk, where an
13
+ # added line that happens to start with `++` cannot be.
14
+ #
15
+ # Lines that were only deleted leave no line of their own behind; the lines
16
+ # either side of the gap count instead.
17
+ class Evilution::Git::UnifiedDiff
18
+ HUNK_HEADER = /\A@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/
19
+ ESCAPES = { "a" => "\a", "b" => "\b", "t" => "\t", "n" => "\n", "v" => "\v", "f" => "\f", "r" => "\r" }.freeze
20
+ private_constant :HUNK_HEADER, :ESCAPES
21
+
22
+ def self.parse(output)
23
+ new.parse(output)
24
+ end
25
+
26
+ def parse(output)
27
+ @changed = Hash.new { |changed, path| changed[path] = [] }
28
+ @path = nil
29
+ @in_header = false
30
+ output.each_line(chomp: true) { |line| read(line) }
31
+ @changed.transform_values { |ranges| Evilution::LineSet.new(ranges) }
32
+ end
33
+
34
+ private
35
+
36
+ def read(line)
37
+ if line.start_with?("diff --git ")
38
+ @path = nil
39
+ @in_header = true
40
+ elsif @in_header && line.start_with?("+++ ")
41
+ @path = new_path(line.delete_prefix("+++ "))
42
+ elsif (hunk = HUNK_HEADER.match(line))
43
+ read_hunk(Integer(hunk[1]), hunk[2] ? Integer(hunk[2]) : 1)
44
+ end
45
+ end
46
+
47
+ def read_hunk(start, count)
48
+ @in_header = false
49
+ return if @path.nil?
50
+
51
+ @changed[@path] << (count.zero? ? ([start, 1].max..(start + 1)) : (start..(start + count - 1)))
52
+ end
53
+
54
+ # `+++ b/path`, quoted C-style when the path holds a quote, a backslash or a
55
+ # control character; a path holding a space ends in a tab instead.
56
+ def new_path(text)
57
+ text = text.chomp("\t")
58
+ text = unquote(text) if text.start_with?('"')
59
+ text.delete_prefix("b/").dup.force_encoding(Encoding::UTF_8)
60
+ end
61
+
62
+ def unquote(text)
63
+ text[1...-1].b.gsub(/\\(?:([0-7]{3})|(.))/m) do
64
+ octal = Regexp.last_match(1)
65
+ octal ? octal.to_i(8).chr : ESCAPES.fetch(Regexp.last_match(2), Regexp.last_match(2))
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "version"
4
+
5
+ # The crash classes that come from the test infrastructure (a timed-out or
6
+ # locked database, a slow CI) rather than from the mutation, so a test that
7
+ # crashes on one says nothing about the mutation.
8
+ #
9
+ # Shared by the integrations, which must not stop a run on such a crash, and
10
+ # by the runner, which neutralises and retries mutations killed by one. Needs
11
+ # only the top-level namespace: the integrations load it while evilution
12
+ # itself is still loading.
13
+ module Evilution::InfraCrash
14
+ CLASSES = %w[
15
+ Timeout::Error
16
+ ActiveRecord::StatementTimeout
17
+ ActiveRecord::Deadlocked
18
+ ActiveRecord::ConnectionTimeoutError
19
+ ActiveRecord::LockWaitTimeout
20
+ SQLite3::BusyException
21
+ ].freeze
22
+
23
+ def self.class_name?(error_class)
24
+ CLASSES.include?(error_class)
25
+ end
26
+ end
@@ -17,20 +17,107 @@ class Evilution::Integration::Base
17
17
  @mutation_applier = mutation_applier
18
18
  end
19
19
 
20
+ # Fills whatever the integration caches per source or test file for the
21
+ # given mutations. Called in the parent before any mutation runs, so forked
22
+ # children inherit the caches instead of each filling its own and losing it.
23
+ def warm_targeting(_mutations)
24
+ nil
25
+ end
26
+
27
+ # Whether no test executes any line the mutation changes, so it need not
28
+ # run. Only an integration backed by line coverage can tell.
29
+ def uncovered?(_mutation)
30
+ false
31
+ end
32
+
33
+ # The ids of the tests that killed the next mutation's neighbours earlier in
34
+ # the run, the most telling first. An integration that stops at the first
35
+ # kill and can choose the order of its tests runs these first; the others
36
+ # have nothing to do with them.
37
+ def prioritize(_test_ids)
38
+ nil
39
+ end
40
+
41
+ # Whether the next run tries its tests in an order of the integration's own
42
+ # choosing rather than the one they are defined in. Asked in the parent,
43
+ # before the run.
44
+ def reorders?
45
+ false
46
+ end
47
+
48
+ # Runs the block with every run in it trying the tests in their defined
49
+ # order. A forked child inherits it.
50
+ def in_defined_order
51
+ yield
52
+ end
53
+
54
+ # Fills what loading a mutated file looks up on the heap (see
55
+ # Loading::MutationApplier#warm). Called in the parent before any mutation
56
+ # runs, for the same reason as #warm_targeting.
57
+ def warm_loading
58
+ @mutation_applier.warm
59
+ nil
60
+ end
61
+
62
+ # The timing methods are taken as method objects before the mutation is
63
+ # applied. When the file under mutation is this one, applying it replaces
64
+ # them, and a mutant of the code that builds the result would otherwise
65
+ # decide its own verdict. A method object keeps the definition the call
66
+ # started with.
20
67
  def call(mutation)
21
68
  ensure_framework_loaded
22
- fire_hook(:mutation_insert_pre, mutation: mutation, file_path: mutation.file_path)
23
- load_error = @mutation_applier.call(mutation)
24
- return load_error if load_error
69
+ fire_insert_hook(:mutation_insert_pre, mutation)
70
+ timed = timer
71
+ attach_timings = method(:with_timings)
72
+ load_error, apply = timed.call { @mutation_applier.call(mutation) }
73
+ return attach_timings.call(load_error, apply: apply) if load_error
25
74
 
26
- fire_hook(:mutation_insert_post, mutation: mutation, file_path: mutation.file_path)
27
- Evilution.while_running_tests { run_tests(mutation) }
75
+ fire_insert_hook(:mutation_insert_post, mutation)
76
+ result, tests = timed.call { Evilution.while_running_tests { run_tests(mutation) } }
77
+ attach_timings.call(result, apply: apply, tests: tests)
28
78
  ensure
29
- @mutation_applier.restore(mutation) unless restored_by_applier?(load_error)
79
+ @mutation_applier.restore(mutation) if restore_needed?(load_error)
30
80
  end
31
81
 
32
82
  private
33
83
 
84
+ # A process that ends with its mutation exits right after reporting it, so
85
+ # nothing would run against the restored file; evaluating it again would
86
+ # only delay the report.
87
+ def restore_needed?(load_error)
88
+ return false if Evilution.single_mutation_process?
89
+
90
+ !restored_by_applier?(load_error)
91
+ end
92
+
93
+ # An integration that re-runs a survivor reports that run's time as
94
+ # `timings: { confirmation: }`; the rest of the test time is the first run.
95
+ def with_timings(result, apply:, tests: nil)
96
+ return result unless result.is_a?(Hash)
97
+
98
+ reported = result[:timings].is_a?(Hash) ? result[:timings] : {}
99
+ confirmation = reported[:confirmation]
100
+ timings = { apply: apply }
101
+ timings[:test_run] = tests - (confirmation || 0.0) if tests
102
+ timings[:confirmation] = confirmation if confirmation
103
+ result.merge(timings: timings)
104
+ end
105
+
106
+ # A callable returning the block's value and the seconds it took, bound to
107
+ # the clock as it is now.
108
+ def timer
109
+ clock = method(:monotonic_now)
110
+ lambda do |&block|
111
+ started = clock.call
112
+ value = block.call
113
+ [value, clock.call - started]
114
+ end
115
+ end
116
+
117
+ def monotonic_now
118
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
119
+ end
120
+
34
121
  # The applier evaluates the original to tell that a mutant broke the load of
35
122
  # its file; after that the file is as it was, and need not be restored again.
36
123
  def restored_by_applier?(load_error)
@@ -55,6 +142,10 @@ class Evilution::Integration::Base
55
142
  raise NotImplementedError, "#{self.class}#reset_state must be implemented"
56
143
  end
57
144
 
145
+ def fire_insert_hook(event, mutation)
146
+ fire_hook(event, mutation: mutation, file_path: mutation.file_path)
147
+ end
148
+
58
149
  def fire_hook(event, **payload)
59
150
  @hooks.fire(event, **payload) if @hooks
60
151
  end