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
@@ -2,28 +2,92 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative "../text"
5
+ require_relative "../constant_subject"
6
+ require_relative "../constant_subject_finder"
7
+ require_relative "../attribute_finder"
8
+ require_relative "../constant_reads"
5
9
 
6
10
  module Saboteur
7
11
  class Engine
8
12
  # The subjects to mutate: every def in the included files, narrowed by
9
13
  # --subject patterns and by --since (methods with a changed line).
14
+ # A heredoc body is code the static subject walk cannot see: methods
15
+ # defined by class_eval'ing the string, or behavior living in a
16
+ # template constant, exist only when the string is evaluated (#467).
17
+ module HeredocSpans
18
+ module_function
19
+
20
+ def changed_lines_inside?(file, changed_lines)
21
+ result = Prism.parse(File.binread(file))
22
+ return false if result.failure?
23
+
24
+ spans = collect(result.value)
25
+ changed_lines.any? { |line| spans.any? { |span| span.cover?(line) } }
26
+ end
27
+
28
+ def collect(node, spans = [])
29
+ if (node.is_a?(Prism::StringNode) || node.is_a?(Prism::InterpolatedStringNode)) &&
30
+ node.opening_loc&.slice&.start_with?("<<")
31
+ # The node location of an interpolated string stops at the opening
32
+ # marker; opening through closing covers the whole heredoc body.
33
+ spans << (node.opening_loc.start_line..node.closing_loc.end_line)
34
+ end
35
+ node.compact_child_nodes.each { |child| collect(child, spans) }
36
+ spans
37
+ end
38
+ end
39
+
10
40
  class SubjectSelection
11
41
  # Lines changed since the --since ref, by file; nil without --since.
12
42
  attr_reader :changed_lines
43
+ # The changed lines minus layout-only ones (#313): the lines that
44
+ # narrow a changed method's fine-grained mutants.
45
+ attr_reader :behavior_lines
13
46
 
14
- def initialize(config, diagnostics)
47
+ def initialize(config, diagnostics, test_files: nil, custom_map: nil)
15
48
  @config = config
16
49
  @diagnostics = diagnostics
17
50
  @root = File.realpath(config.root)
51
+ @test_files = test_files
52
+ @custom_map = custom_map || CustomMap.load(config.path("custom_map"), root: config.root)
18
53
  end
19
54
 
20
55
  def select(files)
21
56
  @files = files
22
57
  subjects = find(files)
58
+ if @config["since"]
59
+ @changed_lines, @behavior_lines = changed_line_maps
60
+ subjects += constant_subjects
61
+ end
23
62
  @discovered = subjects
63
+ warn_unmatched_whole_file(subjects) if @config["since"]
24
64
  subjects = by_pattern(subjects, @config["subjects"]) if @config["subjects"].any?
25
65
  subjects = by_change(subjects, @config["since"]) if @config["since"]
26
- subjects
66
+ subjects = drop_intentionally_untested(subjects)
67
+ inform_nothing_to_test(subjects)
68
+ attach_consumers(subjects)
69
+ end
70
+
71
+ # A --since run can legitimately select nothing: the range is empty
72
+ # (a checkout AT the base, #643) or its changes carry no mutable Ruby
73
+ # (a docs-only diff, #659). Exit 0 with a notice, not a silent green:
74
+ # name the base, the changed-file count and why nothing was selected.
75
+ def inform_nothing_to_test(subjects)
76
+ ref = @config["since"]
77
+ return unless ref && subjects.empty?
78
+
79
+ changed = Selection::GitDiff.changed_paths(ref, root: @root)
80
+ count = changed[:present].size + changed[:deleted].size
81
+ reason = if count.zero?
82
+ "the resolved range has no changes"
83
+ elsif !@raw_changed_files.intersect?(@files)
84
+ "#{count} changed file#{'s' unless count == 1}, none in mutation scope " \
85
+ "(docs-only, excluded, or non-Ruby)"
86
+ else
87
+ "no subject covers the changed lines (layout-only edits, deletions, " \
88
+ "or intentionally untested code)"
89
+ end
90
+ @diagnostics.inform("--since #{ref}: nothing to test - #{reason}")
27
91
  end
28
92
 
29
93
  # "Klass#meth", "Klass.meth", or a namespace prefix ("Klass", "A::B").
@@ -34,15 +98,24 @@ module Saboteur
34
98
  # The files (absolute paths) holding a subject matching any pattern,
35
99
  # returned relative to root. --subject inference guesses specs for
36
100
  # exactly these, so one unrelated spec-less file cannot block a
37
- # one-method run.
38
- def self.files_matching(root, files, patterns)
101
+ # one-method run. With --since, constant literal subjects count too:
102
+ # a file holding only the named constant otherwise falls out of
103
+ # inference and runs the whole suite (#319).
104
+ def self.files_matching(root, files, patterns, config = { "since" => nil })
105
+ include_constants = !config["since"].nil?
39
106
  finder = SubjectFinder.new
107
+ constant_finder = ConstantSubjectFinder.new if include_constants
40
108
  prefix = "#{root}/"
41
109
  files.filter_map do |file|
42
110
  parsed = finder.call(file)
43
111
  matching = parsed.subjects.any? do |subject|
44
112
  patterns.any? { |pattern| match?(subject.id, pattern) }
45
113
  end
114
+ if !matching && constant_finder
115
+ matching = constant_finder.call(file).subjects.any? do |subject|
116
+ patterns.any? { |pattern| match?(subject.id, pattern) }
117
+ end
118
+ end
46
119
  file.delete_prefix(prefix) if matching
47
120
  end.sort
48
121
  end
@@ -52,36 +125,242 @@ module Saboteur
52
125
  def find(files)
53
126
  finder = SubjectFinder.new
54
127
  files.flat_map do |file|
55
- InlineSuppression.new(file, File.read(file))
128
+ source = File.read(file)
129
+ InlineSuppression.new(file, source)
56
130
  parsed = finder.call(file)
57
- @diagnostics.record_discovery_failure(file, reason: "invalid_file_syntax") unless parsed.errors.empty?
58
- parsed.errors.first(1).each do |message|
59
- @diagnostics.warn("#{file.delete_prefix("#{@root}/")} does not parse: #{message}")
131
+ unless parsed.errors.empty?
132
+ if source.include?("<%")
133
+ @diagnostics.record_erb_template_skip(file, root: @root)
134
+ else
135
+ @diagnostics.record_discovery_failure(file, reason: "invalid_file_syntax")
136
+ parsed.errors.first(1).each do |message|
137
+ @diagnostics.warn("#{file.delete_prefix("#{@root}/")} does not parse: #{message}")
138
+ end
139
+ end
60
140
  end
61
141
  parsed.subjects
62
142
  end
63
143
  end
64
144
 
65
145
  def by_pattern(subjects, patterns)
66
- patterns.each do |pattern|
67
- next if subjects.any? { |subject| self.class.match?(subject.id, pattern) }
146
+ missed = patterns.reject do |pattern|
147
+ subjects.any? { |subject| self.class.match?(subject.id, pattern) }
148
+ end
149
+ missed.each do |pattern|
150
+ @diagnostics.warn(missed_pattern_message(pattern))
151
+ end
152
+ # A total miss is a typo: refuse instead of a green zero-subject
153
+ # summary (#379). A partial miss runs, but the matched count is
154
+ # on the record.
155
+ raise ConfigError, total_miss_message(missed) if missed.size == patterns.size
68
156
 
69
- @diagnostics.warn("--subject #{pattern} matches no subject (patterns are names like " \
70
- "Klass, Klass#meth or Klass.meth, not globs)")
157
+ if missed.any?
158
+ @diagnostics.warn("#{patterns.size - missed.size} of #{patterns.size} --subject patterns matched")
71
159
  end
72
160
  subjects.select { |subject| patterns.any? { |pattern| self.class.match?(subject.id, pattern) } }
73
161
  end
74
162
 
163
+ # The refusal when every pattern missed (#379). A pattern naming an
164
+ # attribute-macro method gets the real reason - the method exists but
165
+ # has no body to mutate (#373) - instead of the pattern-format hint.
166
+ def total_miss_message(missed)
167
+ if missed.all? { |pattern| attribute_macro_for(pattern) }
168
+ return missed.map { |pattern| missed_pattern_message(pattern) }.join("; ")
169
+ end
170
+
171
+ "--subject #{missed.join(', ')} matches no subject (patterns are names like " \
172
+ "Klass, Klass#meth or Klass.meth, not globs)"
173
+ end
174
+
175
+ # Why one missed pattern matched nothing: when it names a method an
176
+ # attribute macro generated, say so (#373); otherwise the pattern
177
+ # itself is suspect.
178
+ def missed_pattern_message(pattern)
179
+ macro = attribute_macro_for(pattern)
180
+ return "--subject #{pattern} is defined by #{macro} and has no body to mutate" if macro
181
+
182
+ "--subject #{pattern} matches no subject (patterns are names like " \
183
+ "Klass, Klass#meth or Klass.meth, not globs)"
184
+ end
185
+
186
+ # The attribute macro that generated the method a pattern names, if
187
+ # any. The files are parsed for macros once, only when a pattern
188
+ # missed.
189
+ def attribute_macro_for(pattern)
190
+ attributes = @attributes ||= @files.flat_map { |file| AttributeFinder.new.call(file) }
191
+ attributes.find { |found| found.id == pattern }&.macro
192
+ end
193
+
75
194
  def by_change(subjects, ref)
76
- @changed_lines = Selection::GitDiff.changed_lines(ref, root: @root, include: @files.to_set,
77
- diagnostics: @diagnostics)
78
- .transform_keys { |file| Selection::CoverageMap.canonical(file) }
79
195
  changed = changed_subjects(subjects)
80
196
  warn_changed_files_without_subjects
81
197
  warn_excluded_changed_files
82
198
  drop_unchanged_syntax(changed, ref)
83
199
  end
84
200
 
201
+ # Lines changed since the --since ref, and their behavior-only
202
+ # subset, canonicalized by file.
203
+ def changed_line_maps
204
+ maps = Selection::GitDiff.line_maps(@config["since"], root: @root, include: @files.to_set,
205
+ diagnostics: @diagnostics)
206
+ # A deletion-only file yields no subject and no fine-grained mutant
207
+ # (#484); TestGuess reads the same list, so inference never demands
208
+ # a spec for it either.
209
+ canonical = ->(map) { map.transform_keys { |file| Selection::CoverageMap.canonical(file) } }
210
+ changed = canonical.call(maps.changed)
211
+ # Deleted-subject discovery (#466) probes every changed file,
212
+ # including the deletion-only ones stripped below for scope (#484).
213
+ @raw_changed_files = changed.keys
214
+ return [changed, canonical.call(maps.behavior)] if changed.empty?
215
+
216
+ # deletion_only_files returns canonical-absolute paths, the same
217
+ # space as the canonical map keys in any project-root layout.
218
+ deletion_only = Selection::GitDiff.deletion_only_files(@config["since"], root: @root)
219
+ [changed.except(*deletion_only), canonical.call(maps.behavior).except(*deletion_only)]
220
+ end
221
+
222
+ # Changed entries of a constant hash or array literal are behavior
223
+ # changes no method owns (#306): the constant joins the diff scope as
224
+ # a subject, and the diff scope narrows its mutants to the changed
225
+ # entries. A changed constant assignment of an unsupported form is
226
+ # named, so a clean method score never implies it was tested.
227
+ def constant_subjects
228
+ finder = ConstantSubjectFinder.new
229
+ files_by_canonical = @files.to_h do |file|
230
+ [Selection::CoverageMap.canonical(Text.utf8_identity(file)), file]
231
+ end
232
+ subjects = []
233
+ @changed_lines.each do |canonical, lines|
234
+ file = files_by_canonical[canonical]
235
+ next unless file && canonical.end_with?(".rb")
236
+
237
+ parsed = finder.call(file)
238
+ parsed.unsupported.each do |found|
239
+ next unless found.overlaps_lines?(lines)
240
+
241
+ warn_unsupported_constant(found)
242
+ constant_warned_files << canonical
243
+ end
244
+ accepted = parsed.subjects.select { |subject| subject.overlaps_lines?(lines) }
245
+ subjects.concat(admit_replayable_file(file, accepted, canonical))
246
+ end
247
+ subjects
248
+ end
249
+
250
+ # A constant mutant re-evaluates the whole file with the literal
251
+ # changed, so whatever the file runs at load runs again on every
252
+ # mutant, outside the method hazard guard. saboteur only replays
253
+ # files whose load-time code it can vet (requires, pure defs,
254
+ # class/module nesting, literal assignments) and that trip no hazard
255
+ # denylist entry; anything else - a helper call, a registration, a
256
+ # write to disk - needs the explicit whole_file opt-in, which says
257
+ # the user accepts the replay (#319). Passing the vet is not a safety
258
+ # proof; it only means no unrecognized code runs at load.
259
+ def admit_replayable_file(file, subjects, canonical)
260
+ opted_in, rest = subjects.partition { |subject| whole_file_opted?(subject) }
261
+ return subjects if rest.empty?
262
+
263
+ guard = MutationSource::HazardGuard.new(@config["hazard"])
264
+ root = Prism.parse(File.binread(file)).value
265
+ reasons = (guard.load_time_hazards(root) + guard.unvetted_load_code(root)).uniq
266
+ return subjects if reasons.empty?
267
+
268
+ constant_warned_files << canonical
269
+ path = file.delete_prefix("#{@root}/")
270
+ listed = reasons.first(2).join(", ")
271
+ @diagnostics.warn("--since: constant subject(s) in #{path} are not mutated: the file runs code at " \
272
+ "load (#{listed}) that every constant mutant would run again, and saboteur " \
273
+ "cannot vet what re-running it does. List the constant in c.whole_file to " \
274
+ "replay the file anyway, or move the literal into its own file. " \
275
+ "#{whole_file_id_hint(rest)}")
276
+ opted_in
277
+ end
278
+
279
+ # The explicit opt-in for whole-file replay: subject ids or globs,
280
+ # matched like Execution::Reevaluator#whole_file?.
281
+ def whole_file_opted?(subject)
282
+ Array(@config["whole_file"]).any? { |pattern| File.fnmatch?(pattern.to_s, subject.id) }
283
+ end
284
+
285
+ # A bare constant name only ever matches a top-level constant's id -
286
+ # never a nested one - so name the exact ids to copy into c.whole_file.
287
+ # The glob example carries its blast radius: it opts in every matching
288
+ # subject project-wide, so the exact id(s) are the safer pick (#635).
289
+ def whole_file_id_hint(subjects)
290
+ ids = subjects.map(&:id)
291
+ named = ids.first(3).map { |id| "\"#{id}\"" }.join(", ")
292
+ named += ", +#{ids.size - 3} more" if ids.size > 3
293
+ "Exact id(s): #{named} (preferred); a glob like \"*#{ids.first.split('::').last}*\" also " \
294
+ "works but opts in every matching subject project-wide."
295
+ end
296
+
297
+ # A whole_file pattern that matches nothing silently does nothing (#635):
298
+ # name the pattern and what subject ids look like, so a bare constant
299
+ # name for a nested constant (which never matches its qualified id)
300
+ # stands out. Scoped to this run: a pattern for a file outside the
301
+ # current include set also matches nothing.
302
+ def warn_unmatched_whole_file(subjects)
303
+ @config["whole_file"].each do |pattern|
304
+ next if subjects.any? { |subject| File.fnmatch?(pattern.to_s, subject.id) }
305
+
306
+ @diagnostics.warn("--since: whole_file pattern \"#{pattern}\" matches no discovered " \
307
+ "subject in this run; subject ids look like RULES, A::B::C or A::B#m")
308
+ end
309
+ end
310
+
311
+ def warn_unsupported_constant(found)
312
+ path = found.file.delete_prefix("#{@root}/")
313
+ @diagnostics.warn("--since: changed constant #{found.id} in #{path} is not mutated: #{found.reason}. " \
314
+ "Only hash and array literal entries are mutated.")
315
+ end
316
+
317
+ # The tests that can observe a constant mutant are the ones covering a
318
+ # read of the constant; find those reads across the include set AND the
319
+ # specs, which can read a constant directly (#319). A read another file
320
+ # captured at load time (`COPY = A::RULES`) never observes the mutant:
321
+ # it holds the original object. A constant with no observable read at
322
+ # all is dropped with a diagnostic rather than reported Survived.
323
+ def attach_consumers(subjects)
324
+ files = @files + spec_files
325
+ subjects.filter_map do |subject|
326
+ next subject unless subject.is_a?(ConstantSubject)
327
+
328
+ reads = ConstantReads.across(files, subject)
329
+ subject.consumer_lines = ConstantReads.observable_lines(reads, subject)
330
+ subject.consumer_defs = reads.transform_values(&:defs)
331
+ if subject.consumer_lines.empty? && reads.any? { |_file, r| r.load.any? }
332
+ path = subject.file.delete_prefix("#{@root}/")
333
+ @diagnostics.warn("--since: changed constant #{subject.id} in #{path} is not mutated: its only " \
334
+ "reads capture the value at load time in another file, so no test can " \
335
+ "observe a change to it.")
336
+ next
337
+ end
338
+ subject
339
+ end
340
+ end
341
+
342
+ # The specs selected for this run (inferred or explicit), as absolute
343
+ # paths that exist. Directory entries expand to their specs. With no
344
+ # tests named at all (the --all case) the configured integration's
345
+ # loaded spec files answer; the conventional spec tree only stands in
346
+ # when nothing else is known (preview, or an empty suite): a spec can
347
+ # read a constant directly (#319).
348
+ def spec_files
349
+ configured = @config["tests"]
350
+ return configured.flat_map { |entry| expand_spec_entry(entry) }.uniq unless configured.empty?
351
+ return Array(@test_files).uniq unless Array(@test_files).empty?
352
+
353
+ expand_spec_entry("spec")
354
+ end
355
+
356
+ def expand_spec_entry(entry)
357
+ path = File.absolute_path(entry, @root)
358
+ return Dir.glob(File.join(path, "**", "*_spec.rb")) if File.directory?(path)
359
+ return [] unless path.end_with?(".rb") && File.file?(path)
360
+
361
+ [path]
362
+ end
363
+
85
364
  # subject.file carries the locale tag under a non-UTF-8 locale
86
365
  # while the diff keys are UTF-8; retag losslessly before lookup so
87
366
  # a non-ASCII path matches by its exact bytes (#231).
@@ -93,12 +372,22 @@ module Saboteur
93
372
  end
94
373
  end
95
374
 
375
+ # Changed files a constant literal warning already covers (#537):
376
+ # the umbrella counts them, but its parse/method guesses do not apply,
377
+ # so it says how many sit in a constant literal instead.
378
+ def constant_warned_files
379
+ @constant_warned_files ||= Set.new
380
+ end
381
+
96
382
  # A changed Ruby file in scope whose changed lines cover no subject
97
383
  # silently contributes nothing to the run (#232): warn, or a typo, an
98
384
  # unparseable file or a selection bug passes as a clean selection.
99
385
  # Comment-only changes stay quiet: their file matched subjects before
100
386
  # drop_unchanged_syntax pruned them, and that prune is reported. A
101
387
  # diff touching no included Ruby file is a legitimate empty scope.
388
+ # A file whose only finding is an unsupported or unsafe constant still
389
+ # counts here: the specific diagnostic says why, this says nothing ran
390
+ # (#295 keeps its contract).
102
391
  def warn_changed_files_without_subjects
103
392
  canonical = ->(file) { Selection::CoverageMap.canonical(Text.utf8_identity(file)) }
104
393
  included = @files.map(&canonical)
@@ -108,11 +397,43 @@ module Saboteur
108
397
 
109
398
  names = unmatched.first(5).map { |file| file.delete_prefix("#{@root}/") }.join(", ")
110
399
  more = unmatched.size > 5 ? ", ..." : ""
400
+ note = constant_note(unmatched)
111
401
  @diagnostics.warn("--since: #{unmatched.size} changed Ruby file(s) have no subjects covering " \
112
- "the changed lines, so nothing is mutated from them: #{names}#{more}. The " \
113
- "file may not parse, or every change may sit outside a method.")
402
+ "the changed lines, so nothing is mutated from them: #{names}#{more}. " \
403
+ "#{subjectless_hint(unmatched)}#{note}")
404
+ end
405
+
406
+ # One sentence naming how many umbrella files a constant warning
407
+ # already covers (#537): without it a constant edit the safety bar
408
+ # refuses to mutate reads like an unexplained miss in the umbrella.
409
+ def constant_note(unmatched)
410
+ constants = unmatched.count { |file| constant_warned_files.include?(file) }
411
+ return "" if constants.zero?
412
+
413
+ " #{constants} of these #{constants == 1 ? 'file has' : 'files have'} changes inside " \
414
+ "a constant literal that is not mutated (the constant warnings above name them)."
114
415
  end
115
416
 
417
+ # Specs land here when one --test carries several paths; the
418
+ # parse/method guesses do not apply to them, so say what happened.
419
+ def subjectless_hint(unmatched)
420
+ return "These are spec files; specs are not mutated - pass them with --test." if
421
+ unmatched.all? { |file| file.end_with?("_spec.rb") }
422
+
423
+ embedded = unmatched.select { |file| changed_lines_inside_heredoc?(file) }
424
+ return "The file may not parse, or every change may sit outside a method." if embedded.empty?
425
+
426
+ names = embedded.first(5).map { |file| file.delete_prefix("#{@root}/") }.join(", ")
427
+ more = embedded.size > 5 ? ", ..." : ""
428
+ "In #{names}#{more} the changed lines are code embedded in a string (a heredoc, often the " \
429
+ "argument to class_eval/module_eval or a template constant); methods defined by evaluating " \
430
+ "strings are not mutation subjects, so this run cannot see that behavior."
431
+ end
432
+
433
+ # Changed lines inside a heredoc deserve the specific warning, not the
434
+ # generic parse/method guess (#467); the scan lives in HeredocSpans.
435
+ def changed_lines_inside_heredoc?(file) = HeredocSpans.changed_lines_inside?(file, @changed_lines[file])
436
+
116
437
  # A changed Ruby file that an include pattern covers but c.exclude
117
438
  # removes yields zero candidates with no other sign - this repo's own
118
439
  # config excludes lib/saboteur/execution/**, so harness changes once
@@ -141,25 +462,63 @@ module Saboteur
141
462
  (territory & excluded).select { |file| file.end_with?(".rb") }.map(&canonical)
142
463
  end
143
464
 
465
+ # A file the custom map marks intentionally untested ("path" => []) is
466
+ # never mutated: with no test there is no signal, and a guessed
467
+ # selection would read its empty spec list as "run the whole suite"
468
+ # (#369). The skip is named, never silent.
469
+ def drop_intentionally_untested(subjects)
470
+ kept, dropped = subjects.partition do |subject|
471
+ relative = subject.file.delete_prefix("#{@root}/")
472
+ @custom_map.specs_for(relative) != :excluded
473
+ end
474
+ return subjects if dropped.empty?
475
+
476
+ names = dropped.first(5).map(&:id).join(", ")
477
+ more = dropped.size > 5 ? ", ..." : ""
478
+ @diagnostics.inform("#{dropped.size} subject(s) skipped: intentionally untested in " \
479
+ "config/saboteur_custom_map.rb: #{names}#{more}")
480
+ kept
481
+ end
482
+
144
483
  # A method whose only changes are comments, blank lines or spacing has
145
- # nothing new to test.
484
+ # nothing new to test. A method the diff deleted is neither: it is
485
+ # reported as deleted, and the neighbours its lines drifted onto are
486
+ # skipped quietly rather than blamed for a comment-only change (#466).
146
487
  def drop_unchanged_syntax(subjects, ref)
147
- return subjects if subjects.empty?
488
+ # No diff at all: no merge-base lookup, no report.
489
+ return subjects if @raw_changed_files.empty?
148
490
 
149
491
  comparison = UnchangedSyntax.new(Selection::GitDiff.merge_base(ref, root: @root), root: @root)
492
+ deleted = comparison.deleted_subjects(@raw_changed_files)
493
+ report_deleted(deleted)
494
+ deleted_files = deleted.to_set { |subject| canonical_path(subject.file) }
150
495
  same, kept = subjects.partition { |subject| comparison.unchanged?(subject) }
151
- report_unchanged_syntax(same)
496
+ report_unchanged_syntax(same.reject { |subject| deleted_files.include?(canonical_path(subject.file)) })
152
497
  kept
153
498
  end
154
499
 
500
+ # Names at most five, like the other selection notes.
501
+ def report_deleted(deleted)
502
+ return if deleted.empty?
503
+
504
+ names = deleted.first(5).map(&:id).join(", ")
505
+ more = deleted.size > 5 ? ", ..." : ""
506
+ verb = deleted.one? ? "subject was" : "subjects were"
507
+ @diagnostics.inform("#{deleted.size} changed #{verb} deleted, nothing to mutate: #{names}#{more}")
508
+ end
509
+
155
510
  def report_unchanged_syntax(subjects)
156
511
  return if subjects.empty?
157
512
 
158
513
  names = subjects.first(5).map(&:id).join(", ")
159
514
  more = subjects.size > 5 ? ", ..." : ""
160
- @diagnostics.inform("#{subjects.size} changed method(s) differ only in comments or formatting, " \
515
+ @diagnostics.inform("#{subjects.size} changed subject(s) differ only in comments or formatting, " \
161
516
  "so they are skipped: #{names}#{more}")
162
517
  end
518
+
519
+ def canonical_path(file)
520
+ Selection::CoverageMap.canonical(Text.utf8_identity(file))
521
+ end
163
522
  end
164
523
  end
165
524
  end