saboteur 0.7.0 → 0.8.1

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 (114) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +772 -0
  3. data/README.md +221 -1072
  4. data/lib/saboteur/attribute_finder.rb +113 -0
  5. data/lib/saboteur/cli/exit_status.rb +32 -1
  6. data/lib/saboteur/cli/help_options.rb +2 -1
  7. data/lib/saboteur/cli/history_command.rb +135 -0
  8. data/lib/saboteur/cli/presets.rb +47 -3
  9. data/lib/saboteur/cli/run_budget_options.rb +3 -1
  10. data/lib/saboteur/cli/selection_options.rb +31 -0
  11. data/lib/saboteur/cli/shard_options.rb +17 -0
  12. data/lib/saboteur/cli/source_set.rb +35 -0
  13. data/lib/saboteur/cli/survivors_command.rb +157 -0
  14. data/lib/saboteur/cli.rb +74 -55
  15. data/lib/saboteur/config.rb +102 -6
  16. data/lib/saboteur/config_file.rb +19 -7
  17. data/lib/saboteur/constant_reads.rb +182 -0
  18. data/lib/saboteur/constant_subject.rb +70 -0
  19. data/lib/saboteur/constant_subject_finder.rb +281 -0
  20. data/lib/saboteur/coverage_reads.rb +29 -0
  21. data/lib/saboteur/custom_map.rb +12 -1
  22. data/lib/saboteur/dirty_tree_warning.rb +130 -0
  23. data/lib/saboteur/doctor.rb +13 -0
  24. data/lib/saboteur/engine/baseline_guard.rb +135 -21
  25. data/lib/saboteur/engine/boot.rb +11 -3
  26. data/lib/saboteur/engine/bytecode_equivalence.rb +17 -1
  27. data/lib/saboteur/engine/candidate_selection.rb +20 -0
  28. data/lib/saboteur/engine/candidates.rb +43 -3
  29. data/lib/saboteur/engine/coverage_store.rb +58 -14
  30. data/lib/saboteur/engine/defined_order_prefix.rb +43 -0
  31. data/lib/saboteur/engine/diagnostics.rb +19 -1
  32. data/lib/saboteur/engine/distribution.rb +10 -0
  33. data/lib/saboteur/engine/early_stop_recording.rb +3 -2
  34. data/lib/saboteur/engine/gated_run.rb +37 -0
  35. data/lib/saboteur/engine/load_coverage.rb +63 -0
  36. data/lib/saboteur/engine/out_of_scope_guard.rb +21 -0
  37. data/lib/saboteur/engine/pipelines.rb +115 -0
  38. data/lib/saboteur/engine/result_cache.rb +65 -8
  39. data/lib/saboteur/engine/run_budget.rb +32 -2
  40. data/lib/saboteur/engine/sandbox_policy.rb +59 -3
  41. data/lib/saboteur/engine/scheduler.rb +131 -17
  42. data/lib/saboteur/engine/selection_announcement.rb +59 -0
  43. data/lib/saboteur/engine/shard.rb +69 -0
  44. data/lib/saboteur/engine/subject_selection.rb +380 -21
  45. data/lib/saboteur/engine/test_guess.rb +407 -27
  46. data/lib/saboteur/engine/unchanged_syntax.rb +17 -2
  47. data/lib/saboteur/engine.rb +103 -129
  48. data/lib/saboteur/execution/external_runner.rb +489 -0
  49. data/lib/saboteur/execution/fiddle_loader.rb +34 -0
  50. data/lib/saboteur/execution/marker_sweep.rb +80 -0
  51. data/lib/saboteur/execution/peak_memory.rb +28 -0
  52. data/lib/saboteur/execution/process_table.rb +19 -3
  53. data/lib/saboteur/execution/process_tree.rb +65 -5
  54. data/lib/saboteur/execution/reevaluator.rb +75 -12
  55. data/lib/saboteur/execution/runner.rb +46 -7
  56. data/lib/saboteur/execution/sandbox.rb +25 -6
  57. data/lib/saboteur/execution/seatbelt.rb +233 -0
  58. data/lib/saboteur/execution/supervisor.rb +29 -11
  59. data/lib/saboteur/execution/test_command_shape.rb +149 -0
  60. data/lib/saboteur/execution/worker_pool.rb +23 -1
  61. data/lib/saboteur/frameworks/sqlite_memory_database.rb +79 -0
  62. data/lib/saboteur/gates.rb +43 -2
  63. data/lib/saboteur/help.rb +60 -9
  64. data/lib/saboteur/ignore_list.rb +29 -6
  65. data/lib/saboteur/integration/rspec.rb +223 -5
  66. data/lib/saboteur/mutation_source/argument_removal.rb +1 -1
  67. data/lib/saboteur/mutation_source/argument_swap.rb +54 -0
  68. data/lib/saboteur/mutation_source/builtin.rb +28 -5
  69. data/lib/saboteur/mutation_source/chain_removal.rb +1 -1
  70. data/lib/saboteur/mutation_source/condition_negation.rb +1 -1
  71. data/lib/saboteur/mutation_source/constant_literals.rb +66 -0
  72. data/lib/saboteur/mutation_source/element_removal.rb +73 -0
  73. data/lib/saboteur/mutation_source/endpoint_replacement.rb +1 -1
  74. data/lib/saboteur/mutation_source/entry_edits.rb +44 -0
  75. data/lib/saboteur/mutation_source/filter_replacement.rb +1 -1
  76. data/lib/saboteur/mutation_source/hazard_guard.rb +189 -0
  77. data/lib/saboteur/mutation_source/literal_replacement.rb +1 -1
  78. data/lib/saboteur/mutation_source/method_swap.rb +148 -0
  79. data/lib/saboteur/mutation_source/operator_assignment.rb +1 -1
  80. data/lib/saboteur/mutation_source/operator_examples.rb +4 -1
  81. data/lib/saboteur/mutation_source/operator_registry.rb +5 -2
  82. data/lib/saboteur/mutation_source/operator_visit.rb +1 -1
  83. data/lib/saboteur/mutation_source/pair_removal.rb +13 -31
  84. data/lib/saboteur/mutation_source/range_replacement.rb +1 -1
  85. data/lib/saboteur/mutation_source/regex_replacement.rb +93 -0
  86. data/lib/saboteur/preview.rb +125 -12
  87. data/lib/saboteur/refinement_activations.rb +60 -0
  88. data/lib/saboteur/reporter/agent.rb +9 -1
  89. data/lib/saboteur/reporter/agent_commands.rb +10 -3
  90. data/lib/saboteur/reporter/agent_incomplete.rb +31 -5
  91. data/lib/saboteur/reporter/agent_summary.rb +4 -3
  92. data/lib/saboteur/reporter/findings.rb +5 -2
  93. data/lib/saboteur/reporter/history_policy.rb +11 -0
  94. data/lib/saboteur/reporter/history_reader.rb +47 -0
  95. data/lib/saboteur/reporter/html.rb +26 -4
  96. data/lib/saboteur/reporter/mutation_text.rb +6 -0
  97. data/lib/saboteur/reporter/stryker.rb +27 -3
  98. data/lib/saboteur/reporter/survivor_classification.rb +101 -0
  99. data/lib/saboteur/reporter/survivor_list.rb +15 -9
  100. data/lib/saboteur/reporter/terminal.rb +56 -8
  101. data/lib/saboteur/result.rb +19 -3
  102. data/lib/saboteur/selection/coverage_map.rb +120 -10
  103. data/lib/saboteur/selection/dependency_shield.rb +80 -0
  104. data/lib/saboteur/selection/external_test_map.rb +27 -0
  105. data/lib/saboteur/selection/git_diff.rb +93 -11
  106. data/lib/saboteur/selection/layout_only_lines.rb +80 -0
  107. data/lib/saboteur/selection/recorder.rb +22 -4
  108. data/lib/saboteur/selection/source_scope.rb +22 -0
  109. data/lib/saboteur/subject.rb +42 -7
  110. data/lib/saboteur/subject_finder.rb +173 -29
  111. data/lib/saboteur/version.rb +1 -1
  112. data/lib/saboteur/working_tree.rb +38 -15
  113. data/lib/saboteur.rb +14 -0
  114. metadata +39 -16
@@ -0,0 +1,27 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ module Saboteur
5
+ module Selection
6
+ # The --test-command mode has no coverage map (#140): the command runs
7
+ # the whole suite per mutant, so every subject reads as covered by the
8
+ # one pseudo-test and no recorded timing exists, leaving the per-mutant
9
+ # timeout to the config's baseline_timeout.
10
+ class ExternalTestMap
11
+ def tests = [Execution::ExternalRunner::PSEUDO_TEST]
12
+
13
+ def tests_for(_subject) = tests
14
+
15
+ def tests_for_edit(_subject, _lines) = tests
16
+
17
+ def tests_for_branches(_subject, _edits) = tests
18
+
19
+ def ran_at_load?(_file, _lines) = false
20
+
21
+ # No boot happens in this mode, so nothing can have run only at load.
22
+ def called_only_at_load?(_file, _line) = false
23
+
24
+ def recorded_runtime(_tests) = nil
25
+ end
26
+ end
27
+ end
@@ -3,6 +3,8 @@
3
3
 
4
4
  require "open3"
5
5
  require_relative "../text"
6
+ require_relative "dependency_shield"
7
+ require_relative "layout_only_lines"
6
8
  require_relative "untracked_changes"
7
9
 
8
10
  module Saboteur
@@ -11,7 +13,7 @@ module Saboteur
11
13
  # changes, as { "/abs/path.rb" => Set[line numbers in the current file] }.
12
14
  module GitDiff
13
15
  NO_FILE = "/dev/null" # rubocop:disable Style/FileNull -- literal in git diff output
14
- HUNK = /\A@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/
16
+ HUNK = /\A@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/
15
17
 
16
18
  module_function
17
19
 
@@ -41,7 +43,61 @@ module Saboteur
41
43
  status.success? ? out.b : nil
42
44
  end
43
45
 
46
+ # Paths under ROOT, relative to it, that differ between the merge base
47
+ # of REF and the working tree: { present: [...], deleted: [...] }.
48
+ # present holds added, modified and untracked files; deleted holds
49
+ # files gone from the working tree. A rename counts as a deletion of
50
+ # the old path and an addition of the new one.
51
+ def changed_paths(ref, root:)
52
+ verify_ref!(ref, root: root)
53
+ base = merge_base(ref, root: root)
54
+ diff = %w[diff -z --name-only --relative --no-renames]
55
+ {
56
+ # An untracked file is never in the diff, so the two lists are disjoint.
57
+ present: DependencyShield.reject_trees(names(root, *diff, "--diff-filter=d", base, "--") +
58
+ names(root, "ls-files", "-z", "--others",
59
+ "--exclude-standard"), root).sort,
60
+ deleted: DependencyShield.reject_trees(names(root, *diff, "--diff-filter=D", base, "--"),
61
+ root).sort
62
+ }
63
+ end
64
+
65
+ def names(root, *args)
66
+ git(root, *args).b.split("\0").map { |name| Text.utf8_identity(name) }
67
+ end
68
+
69
+ # The two line maps of a --since diff (#313). changed holds every line
70
+ # the text diff touches: subject admission and the formatting-only
71
+ # skip read it, so a method that was only re-indented is still found
72
+ # and reported as unchanged. behavior drops the layout-only lines:
73
+ # only it narrows a changed method's fine-grained mutants.
74
+ LineMaps = Data.define(:changed, :behavior)
75
+
44
76
  def changed_lines(ref, root:, include: nil, diagnostics: nil)
77
+ line_maps(ref, root: root, include: include, diagnostics: diagnostics).changed
78
+ end
79
+
80
+ # Tracked files whose diff deletes lines without adding any, as
81
+ # canonical absolute paths (realpath'd toplevel joined to each diff
82
+ # path), so callers compare in the same space as
83
+ # CoverageMap.canonical keys whatever the project-root layout: a
84
+ # monorepo root below the git toplevel must not silently disable the
85
+ # exclusion (#484). A pure deletion adds no new code to mutate - the
86
+ # change itself is what the suite covers - so the file yields no
87
+ # subject and no fine-grained mutant, and a spec-less such file must
88
+ # not stop the run. The neighbor lines a pure deletion touches
89
+ # (touched_lines) are context, not change. Untracked files are
90
+ # all-additions and never listed.
91
+ def deletion_only_files(ref, root:) # rubocop:disable Metrics/ModuleLength -- one focused addition to an exactly-full module
92
+ top = File.realpath(Text.utf8_identity(git(root, "rev-parse", "--show-toplevel").b.strip))
93
+ diff = git(root, "-c", "core.quotePath=false", "diff", "--unified=0", "--no-color", "--no-ext-diff",
94
+ merge_base(ref, root: root), "--").b
95
+ added = Hash.new { |hash, file| hash[file] = false }
96
+ each_hunk(diff, top) { |file, (_, _, _, count)| added[file] ||= count.positive? }
97
+ added.reject { |_, yes| yes }.keys
98
+ end
99
+
100
+ def line_maps(ref, root:, include: nil, diagnostics: nil)
45
101
  verify_ref!(ref, root: root)
46
102
  base = merge_base(ref, root: root)
47
103
  # Root and paths are filesystem identity: strip as bytes and retag
@@ -49,14 +105,30 @@ module Saboteur
49
105
  # would rename a raw-byte path). Parse the diff body as bytes and
50
106
  # tag each extracted path UTF-8 at the boundary.
51
107
  top = Text.utf8_identity(git(root, "rev-parse", "--show-toplevel").b.strip)
52
- changes = parse(git(root, "-c", "core.quotePath=false", "diff", "--unified=0", "--no-color",
53
- "--no-ext-diff", base, "--").b, top)
108
+ diff = %w[-c core.quotePath=false diff --unified=0 --no-color --no-ext-diff]
109
+ changes = parse(git(root, *diff, base, "--").b, top)
54
110
  UntrackedChanges.union(changes, top, include: include, diagnostics: diagnostics)
55
- changes
111
+ behavior = drop_layout_only_lines(changes, git(root, *diff, "--ignore-space-change", base, "--").b, base,
112
+ top, include: include)
113
+ LineMaps.new(changed: changes, behavior: behavior)
56
114
  end
57
115
 
58
116
  def parse(diff, top)
59
117
  changes = Hash.new { |hash, file| hash[file] = Set.new }
118
+ each_hunk(diff, top) { |file, (_, _, start, count)| changes[file].merge(touched_lines(start, count)) }
119
+ changes.default_proc = nil
120
+ changes
121
+ end
122
+
123
+ # The new-side lines a hunk touches. A pure deletion touches the lines
124
+ # on either side of it.
125
+ def touched_lines(start, count)
126
+ count.zero? ? [start, start + 1] : (start...(start + count)).to_a
127
+ end
128
+
129
+ # Each hunk header of a zero-context diff as the absolute path of the
130
+ # file and [old_start, old_count, new_start, new_count].
131
+ def each_hunk(diff, top)
60
132
  file = nil
61
133
  diff.each_line do |line|
62
134
  if line.start_with?("+++ ")
@@ -65,15 +137,25 @@ module Saboteur
65
137
  # HUNK is anchored, so a non-hunk line - including a raw-byte
66
138
  # content line - fails the match at its first byte.
67
139
  elsif file && (match = HUNK.match(line))
68
- start = match[1].to_i
69
- count = (match[2] || "1").to_i
70
- # A pure deletion touches the lines on either side of it.
71
- lines = count.zero? ? [start, start + 1] : (start...(start + count)).to_a
72
- changes[file].merge(lines)
140
+ old_start, old_count, new_start, new_count = match.captures
141
+ yield file, [old_start.to_i, (old_count || "1").to_i, new_start.to_i, (new_count || "1").to_i]
73
142
  end
74
143
  end
75
- changes.default_proc = nil
76
- changes
144
+ end
145
+
146
+ # Only files the run could mutate are lexed; a file new at the base
147
+ # has nothing to compare with and keeps every line.
148
+ def drop_layout_only_lines(changes, spaced_diff, base, top, include:)
149
+ hunks = Hash.new { |hash, file| hash[file] = [] }
150
+ each_hunk(spaced_diff, top) { |file, hunk| hunks[file] << hunk }
151
+ changes.to_h do |file, lines|
152
+ mutable = include.nil? || include.include?(file)
153
+ base_source = mutable && file_at(base, file.delete_prefix("#{top}/"), root: top)
154
+ next [file, lines] unless base_source
155
+
156
+ [file, LayoutOnlyLines.new(hunks[file], base_source: base_source, current_source: File.binread(file))
157
+ .restrict(lines)]
158
+ end
77
159
  end
78
160
 
79
161
  ESCAPES = { "n" => "\n", "t" => "\t", '"' => '"', "\\" => "\\", "a" => "\a", "b" => "\b",
@@ -0,0 +1,80 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require "prism"
5
+
6
+ module Saboteur
7
+ module Selection
8
+ # Drops changed lines whose only change is layout (#313). Wrapping
9
+ # existing statements in a block re-indents them; a plain diff marks
10
+ # every one as changed, so their mutants run as if the change touched
11
+ # them.
12
+ #
13
+ # A whitespace-insensitive diff (git diff -b) finds the lines that
14
+ # differ by more than spacing and pairs every other line with its line
15
+ # at the base. A changed line outside that diff is dropped only when
16
+ # Prism lexes it to the same tokens as its base line: whitespace inside
17
+ # a string, heredoc or regexp is part of a token, so it still counts as
18
+ # a change. A `__LINE__` on a line that moved names another number, so
19
+ # that line counts too.
20
+ class LayoutOnlyLines
21
+ LINE_KEYWORD = :KEYWORD___LINE__
22
+
23
+ # hunks: [[old_start, old_count, new_start, new_count], ...] of the
24
+ # whitespace-insensitive diff of one file, in order.
25
+ def initialize(hunks, base_source:, current_source:)
26
+ @hunks = hunks
27
+ @base_tokens = tokens_by_line(base_source)
28
+ @current_tokens = tokens_by_line(current_source)
29
+ end
30
+
31
+ # The -b diff's own lines, with its deletion markers, plus every line
32
+ # of CHANGED whose tokens differ from its base line.
33
+ def restrict(changed)
34
+ spaced_lines | changed.reject { |line| same_tokens?(line) }
35
+ end
36
+
37
+ private
38
+
39
+ def spaced_lines
40
+ @hunks.each_with_object(Set.new) do |(_, _, start, count), lines|
41
+ lines.merge(GitDiff.touched_lines(start, count))
42
+ end
43
+ end
44
+
45
+ def same_tokens?(line)
46
+ base_line = base_line_of(line)
47
+ tokens = @current_tokens[line]
48
+ return false if base_line != line && tokens.any? { |type, _| type == LINE_KEYWORD }
49
+
50
+ tokens == @base_tokens[base_line]
51
+ end
52
+
53
+ # A line outside every hunk sits at its base position shifted by the
54
+ # lines the hunks above it added or removed. A line inside a hunk is
55
+ # one of the -b diff's own lines and is kept whatever this answers.
56
+ def base_line_of(line)
57
+ line - @hunks.sum { |_, old_count, start, count| start + count <= line ? count - old_count : 0 }
58
+ end
59
+
60
+ # Every token on each line it spans, as type and text. Every line has
61
+ # one (a blank line its newline, text after __END__ that token), and a
62
+ # missing line reads as [] through the default. A token that
63
+ # spans lines (a heredoc body, a multi-line string) belongs to each.
64
+ # A token's location ends after its last byte, so one that ends in a
65
+ # newline would reach into the next line without this.
66
+ def tokens_by_line(source)
67
+ Prism.lex(source).value.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |(token, _), lines|
68
+ next if token.type == :EOF
69
+
70
+ token.location.start_line.upto(last_line(token)) { |line| lines[line] << [token.type, token.value] }
71
+ end
72
+ end
73
+
74
+ def last_line(token)
75
+ location = token.location
76
+ token.value.end_with?("\n") ? location.end_line - 1 : location.end_line
77
+ end
78
+ end
79
+ end
80
+ end
@@ -25,7 +25,7 @@ module Saboteur
25
25
 
26
26
  def start
27
27
  Coverage.resume if Coverage.state == :suspended
28
- Coverage.result(stop: false, clear: true)
28
+ CoverageReads.result(stop: false, clear: true)
29
29
  end
30
30
 
31
31
  def test_started(id, test_file)
@@ -44,14 +44,15 @@ module Saboteur
44
44
  end
45
45
  end
46
46
 
47
- def result(build_seconds: nil)
47
+ def result(build_seconds: nil, loaded_files: [])
48
48
  assign_outside(@outside, @lines)
49
49
  assign_outside(@outside_methods, @methods)
50
50
  assign_outside(@outside_branches, @branches)
51
51
  lines = @lines.transform_values { |by_line| by_line.transform_values(&:uniq) }
52
52
  methods = @methods.transform_values { |by_line| by_line.transform_values(&:uniq) }
53
+ branches = @branches.transform_values { |arms| arms.transform_values(&:uniq) }
53
54
  CoverageMap.new(lines: lines, tests: @tests, build_seconds: build_seconds, durations: @durations,
54
- methods: methods, branches: @branches.transform_values { |arms| arms.transform_values(&:uniq) })
55
+ methods: methods, branches: branches, loaded_files: loaded_files)
55
56
  end
56
57
 
57
58
  private
@@ -65,8 +66,15 @@ module Saboteur
65
66
 
66
67
  def record_branches(file, data)
67
68
  data.fetch(:branches, {}).each do |(_kind, _id, *parent_span), arms|
69
+ spans = arms.map { |(_arm_kind, _arm_id, *span), _count| span }
68
70
  arms.each do |(_arm_kind, _arm_id, *span), count|
69
71
  next if span == parent_span
72
+ # Ruby spans a `when X` arm with a bare `nil` body from the
73
+ # `when` to the case's end, containing the sibling arms.
74
+ # Sibling arms are disjoint by definition, so a containing span
75
+ # is bogus; recording it would let it intersect and narrow
76
+ # selection for edits inside the siblings it swallows.
77
+ next if spans.any? { |other| span != other && contains?(span, other) }
70
78
 
71
79
  @branches[file][span] # Keep zero-hit arms as evidence, not as covered code.
72
80
  yield span if count.positive?
@@ -74,6 +82,16 @@ module Saboteur
74
82
  end
75
83
  end
76
84
 
85
+ # [line, column] pairs compare lexically, like CoverageMap#position.
86
+ # Both endpoints come from one slice per side: a separate slice per
87
+ # comparand would make its size irrelevant on the right-hand side of
88
+ # `<=>` (a prefix decides first), hiding an equivalent mutant.
89
+ def contains?(outer, inner)
90
+ outer_start, inner_start = [outer, inner].map { |span| span.first(2) }
91
+ inner_end, outer_end = [inner, outer].map { |span| span.last(2) }
92
+ (outer_start <=> inner_start) <= 0 && (inner_end <=> outer_end) <= 0
93
+ end
94
+
77
95
  def hit_table
78
96
  Hash.new { |hash, file| hash[file] = Hash.new { |by_line, line| by_line[line] = [] } }
79
97
  end
@@ -89,7 +107,7 @@ module Saboteur
89
107
  # Coverage is suspended while the delta is processed, so the recorder's
90
108
  # own work (and anything it calls) is never attributed to a test.
91
109
  def each_hit
92
- snapshot = Coverage.result(stop: false, clear: true)
110
+ snapshot = CoverageReads.result(stop: false, clear: true)
93
111
  suspended = Coverage.running?
94
112
  Coverage.suspend if suspended
95
113
  snapshot.each do |path, data|
@@ -0,0 +1,22 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ module Saboteur
5
+ module Selection
6
+ # Positional source-scope arguments from the command line.
7
+ module SourceScope
8
+ module_function
9
+
10
+ # "One --test, several paths" lands the extra paths in the source
11
+ # scope; specs are never subjects, so the run would mutate nothing
12
+ # and still exit 0. Refuse them as a usage error before any boot.
13
+ def verify_no_specs!(paths)
14
+ spec_files = paths.select { |path| path.end_with?("_spec.rb") }
15
+ return if spec_files.empty?
16
+
17
+ raise ConfigError,
18
+ "spec files are not mutated - pass them with --test, one path per use: #{spec_files.join(', ')}"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -2,12 +2,17 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Saboteur
5
- # One lexical scope enclosing a def: `module X`, `class X` or `class << self`.
5
+ # One lexical scope enclosing a def: `module X`, `class X`, a class-factory
6
+ # block (`X = Data.define(:x) do`) or `class << self`.
6
7
  Scope = Data.define(:kind, :path) do
8
+ # A factory scope wraps the def in `X.class_eval do`: reopening it as
9
+ # `class X` would fight the generated class (a Module.new X is not even
10
+ # a class), while class_eval adds the method to the class already there.
7
11
  def header
8
12
  case kind
9
13
  when :module then "module #{path}"
10
14
  when :class then "class #{path}"
15
+ when :factory then "#{path}.class_eval do"
11
16
  when :sclass then "class << self"
12
17
  end
13
18
  end
@@ -15,6 +20,13 @@ module Saboteur
15
20
  def sclass?
16
21
  kind == :sclass
17
22
  end
23
+
24
+ # A factory scope names a runtime owner (the generated class) but opens
25
+ # no lexical constant scope: `Outer = Class.new do Inner = ... end` binds
26
+ # Inner on the enclosing lexical scope, never on Outer (#339).
27
+ def factory?
28
+ kind == :factory
29
+ end
18
30
  end
19
31
 
20
32
  # A method defined with `def`: the unit saboteur mutates.
@@ -60,6 +72,17 @@ module Saboteur
60
72
  "#{owner}#{singleton ? '.' : '#'}#{name}"
61
73
  end
62
74
 
75
+ # The node whose children are the method's statements: the block body
76
+ # for `define_method(:m) { ... }` (#194), the def body otherwise.
77
+ def body_node
78
+ return node.body unless node.is_a?(Prism::CallNode)
79
+
80
+ # define_method carries its body in the block; scope and the lambda
81
+ # form of define_singleton_method carry it in the lambda argument.
82
+ lambda = node.arguments&.arguments&.find { |arg| arg.is_a?(Prism::LambdaNode) }
83
+ (lambda || node.block)&.body
84
+ end
85
+
63
86
  def source
64
87
  @source ||= file_source.byteslice(start_byte, end_byte - start_byte).force_encoding(Encoding::UTF_8)
65
88
  end
@@ -88,10 +111,10 @@ module Saboteur
88
111
  # An edit outside every statement (signature, around the def, whole
89
112
  # body) maps to the whole method.
90
113
  def coverage_lines_for(edit)
91
- pattern_lines = pattern_coverage_lines(node.body, edit)
114
+ pattern_lines = pattern_coverage_lines(body_node, edit)
92
115
  return pattern_lines if pattern_lines
93
116
 
94
- statement = innermost_statement(node.body, edit.start_byte, edit.end_byte)
117
+ statement = innermost_statement(body_node, edit.start_byte, edit.end_byte)
95
118
  return coverage_lines unless statement
96
119
 
97
120
  first = statement.location.start_line
@@ -102,7 +125,7 @@ module Saboteur
102
125
  # True when anything in the body can turn an error into a value: a
103
126
  # rescue or else clause, a modifier rescue, a nested begin/rescue.
104
127
  def recovers?
105
- rescuing?(node.body)
128
+ rescuing?(body_node)
106
129
  end
107
130
 
108
131
  # The 1-based line of the file that holds this byte offset.
@@ -140,6 +163,12 @@ module Saboteur
140
163
  text.force_encoding(Encoding::UTF_8)
141
164
  end
142
165
 
166
+ # Def subjects prefer surgical re-evaluation; a ConstantSubject always
167
+ # re-evaluates its whole file (there is no method to redefine).
168
+ def whole_file_only?
169
+ false
170
+ end
171
+
143
172
  def to_s
144
173
  id
145
174
  end
@@ -159,10 +188,15 @@ module Saboteur
159
188
  child.compact_child_nodes.any? { |grandchild| rescuing?(grandchild) }
160
189
  end
161
190
 
162
- def innermost_statement(node, start_byte, end_byte, found = nil)
191
+ # An interpolation's own statements (#{"..."}) are eager: they run on
192
+ # the enclosing statement's lines, so they never demote it (#498). A
193
+ # continuation line of a multi-line interpolated statement can lack a
194
+ # per-example line hit, so attribution must span from the statement's
195
+ # first line.
196
+ def innermost_statement(node, start_byte, end_byte, found = nil, embedded: false)
163
197
  return found if node.nil?
164
198
 
165
- if node.is_a?(Prism::StatementsNode)
199
+ if node.is_a?(Prism::StatementsNode) && !embedded
166
200
  statement = node.body.find do |child|
167
201
  child.location.start_offset <= start_byte && end_byte <= child.location.end_offset
168
202
  end
@@ -174,7 +208,8 @@ module Saboteur
174
208
  node.compact_child_nodes.each do |child|
175
209
  next unless child.location.start_offset <= start_byte && end_byte <= child.location.end_offset
176
210
 
177
- return innermost_statement(child, start_byte, end_byte, found)
211
+ return innermost_statement(child, start_byte, end_byte, found,
212
+ embedded: embedded || node.is_a?(Prism::EmbeddedStatementsNode))
178
213
  end
179
214
  found
180
215
  end