saboteur 0.6.0 → 0.8.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 (133) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1024 -0
  3. data/README.md +199 -952
  4. data/exe/saboteur +34 -7
  5. data/lib/saboteur/baseline/workflow.rb +2 -1
  6. data/lib/saboteur/baseline_health.rb +40 -6
  7. data/lib/saboteur/cli/exit_status.rb +80 -4
  8. data/lib/saboteur/cli/help_options.rb +17 -0
  9. data/lib/saboteur/cli/history_command.rb +135 -0
  10. data/lib/saboteur/cli/presets.rb +40 -0
  11. data/lib/saboteur/cli/run_budget_options.rb +37 -0
  12. data/lib/saboteur/cli/run_reporting.rb +74 -5
  13. data/lib/saboteur/cli/selection_options.rb +31 -0
  14. data/lib/saboteur/cli/shard_options.rb +17 -0
  15. data/lib/saboteur/cli/source_set.rb +53 -0
  16. data/lib/saboteur/cli/survivors_command.rb +157 -0
  17. data/lib/saboteur/cli.rb +136 -98
  18. data/lib/saboteur/config.rb +163 -15
  19. data/lib/saboteur/config_file.rb +8 -0
  20. data/lib/saboteur/constant_reads.rb +182 -0
  21. data/lib/saboteur/constant_subject.rb +70 -0
  22. data/lib/saboteur/constant_subject_finder.rb +281 -0
  23. data/lib/saboteur/coverage_reads.rb +29 -0
  24. data/lib/saboteur/crash_report.rb +154 -0
  25. data/lib/saboteur/custom_map.rb +3 -1
  26. data/lib/saboteur/dirty_tree_warning.rb +128 -0
  27. data/lib/saboteur/doctor.rb +49 -1
  28. data/lib/saboteur/engine/baseline_guard.rb +136 -14
  29. data/lib/saboteur/engine/boot.rb +16 -1
  30. data/lib/saboteur/engine/canary.rb +41 -10
  31. data/lib/saboteur/engine/candidate_selection.rb +28 -1
  32. data/lib/saboteur/engine/candidates.rb +43 -3
  33. data/lib/saboteur/engine/coverage_guard.rb +29 -0
  34. data/lib/saboteur/engine/coverage_store.rb +79 -15
  35. data/lib/saboteur/engine/defined_order_prefix.rb +43 -0
  36. data/lib/saboteur/engine/diagnostics.rb +41 -2
  37. data/lib/saboteur/engine/diff_scope.rb +24 -5
  38. data/lib/saboteur/engine/distribution.rb +134 -8
  39. data/lib/saboteur/engine/early_stop_recording.rb +47 -0
  40. data/lib/saboteur/engine/gated_run.rb +35 -0
  41. data/lib/saboteur/engine/load_coverage.rb +45 -0
  42. data/lib/saboteur/engine/out_of_scope_guard.rb +21 -0
  43. data/lib/saboteur/engine/pipelines.rb +107 -0
  44. data/lib/saboteur/engine/result_cache.rb +67 -9
  45. data/lib/saboteur/engine/run_budget.rb +90 -0
  46. data/lib/saboteur/engine/run_fingerprint.rb +4 -0
  47. data/lib/saboteur/engine/sandbox_policy.rb +56 -11
  48. data/lib/saboteur/engine/scheduler.rb +222 -20
  49. data/lib/saboteur/engine/selection_announcement.rb +54 -0
  50. data/lib/saboteur/engine/shard.rb +69 -0
  51. data/lib/saboteur/engine/subject_selection.rb +373 -19
  52. data/lib/saboteur/engine/suite_files.rb +3 -1
  53. data/lib/saboteur/engine/test_guess.rb +438 -25
  54. data/lib/saboteur/engine/thread_warnings.rb +53 -0
  55. data/lib/saboteur/engine/unchanged_syntax.rb +17 -2
  56. data/lib/saboteur/engine/unscoped_suite_warning.rb +41 -0
  57. data/lib/saboteur/engine/verdict_check.rb +54 -18
  58. data/lib/saboteur/engine.rb +202 -158
  59. data/lib/saboteur/execution/external_runner.rb +471 -0
  60. data/lib/saboteur/execution/marker_sweep.rb +79 -0
  61. data/lib/saboteur/execution/peak_memory.rb +28 -0
  62. data/lib/saboteur/execution/process_table.rb +12 -0
  63. data/lib/saboteur/execution/process_tree.rb +65 -5
  64. data/lib/saboteur/execution/protocol.rb +2 -1
  65. data/lib/saboteur/execution/reevaluator.rb +68 -12
  66. data/lib/saboteur/execution/runner.rb +225 -24
  67. data/lib/saboteur/execution/sandbox.rb +21 -3
  68. data/lib/saboteur/execution/seatbelt.rb +226 -0
  69. data/lib/saboteur/execution/supervisor.rb +45 -13
  70. data/lib/saboteur/execution/worker_pool.rb +110 -8
  71. data/lib/saboteur/frameworks/sqlite_memory_database.rb +49 -0
  72. data/lib/saboteur/gates.rb +43 -2
  73. data/lib/saboteur/help.rb +108 -13
  74. data/lib/saboteur/ignore_list.rb +40 -7
  75. data/lib/saboteur/install.rb +4 -3
  76. data/lib/saboteur/integration/base.rb +7 -2
  77. data/lib/saboteur/integration/rspec.rb +175 -9
  78. data/lib/saboteur/mutation_source/argument_removal.rb +1 -1
  79. data/lib/saboteur/mutation_source/builtin.rb +44 -14
  80. data/lib/saboteur/mutation_source/chain_removal.rb +1 -1
  81. data/lib/saboteur/mutation_source/condition_negation.rb +1 -1
  82. data/lib/saboteur/mutation_source/constant_literals.rb +66 -0
  83. data/lib/saboteur/mutation_source/element_removal.rb +73 -0
  84. data/lib/saboteur/mutation_source/endpoint_replacement.rb +1 -1
  85. data/lib/saboteur/mutation_source/entry_edits.rb +44 -0
  86. data/lib/saboteur/mutation_source/filter_replacement.rb +1 -1
  87. data/lib/saboteur/mutation_source/hazard_guard.rb +129 -3
  88. data/lib/saboteur/mutation_source/interpolation_removal.rb +41 -0
  89. data/lib/saboteur/mutation_source/literal_replacement.rb +1 -1
  90. data/lib/saboteur/mutation_source/method_swap.rb +193 -0
  91. data/lib/saboteur/mutation_source/mutation.rb +5 -3
  92. data/lib/saboteur/mutation_source/operand_replacement.rb +54 -0
  93. data/lib/saboteur/mutation_source/operator_assignment.rb +1 -1
  94. data/lib/saboteur/mutation_source/operator_examples.rb +6 -1
  95. data/lib/saboteur/mutation_source/operator_registry.rb +7 -2
  96. data/lib/saboteur/mutation_source/operator_visit.rb +39 -0
  97. data/lib/saboteur/mutation_source/pair_removal.rb +13 -31
  98. data/lib/saboteur/mutation_source/range_replacement.rb +1 -1
  99. data/lib/saboteur/mutation_source/regex_replacement.rb +93 -0
  100. data/lib/saboteur/preview.rb +122 -13
  101. data/lib/saboteur/refinement_activations.rb +60 -0
  102. data/lib/saboteur/reporter/agent.rb +9 -1
  103. data/lib/saboteur/reporter/agent_commands.rb +47 -15
  104. data/lib/saboteur/reporter/agent_incomplete.rb +38 -2
  105. data/lib/saboteur/reporter/agent_summary.rb +4 -3
  106. data/lib/saboteur/reporter/findings.rb +5 -2
  107. data/lib/saboteur/reporter/history_policy.rb +11 -0
  108. data/lib/saboteur/reporter/history_reader.rb +47 -0
  109. data/lib/saboteur/reporter/html.rb +26 -4
  110. data/lib/saboteur/reporter/mutation_text.rb +6 -0
  111. data/lib/saboteur/reporter/next_steps.rb +14 -4
  112. data/lib/saboteur/reporter/score_history.rb +5 -1
  113. data/lib/saboteur/reporter/stryker.rb +40 -7
  114. data/lib/saboteur/reporter/survivor_classification.rb +101 -0
  115. data/lib/saboteur/reporter/survivor_hints.rb +3 -0
  116. data/lib/saboteur/reporter/survivor_list.rb +15 -9
  117. data/lib/saboteur/reporter/terminal.rb +74 -17
  118. data/lib/saboteur/result.rb +38 -7
  119. data/lib/saboteur/selection/coverage_map.rb +107 -11
  120. data/lib/saboteur/selection/dependency_shield.rb +80 -0
  121. data/lib/saboteur/selection/external_test_map.rb +27 -0
  122. data/lib/saboteur/selection/git_diff.rb +105 -13
  123. data/lib/saboteur/selection/layout_only_lines.rb +80 -0
  124. data/lib/saboteur/selection/recorder.rb +22 -4
  125. data/lib/saboteur/selection/source_scope.rb +22 -0
  126. data/lib/saboteur/selection/untracked_changes.rb +126 -0
  127. data/lib/saboteur/subject.rb +53 -7
  128. data/lib/saboteur/subject_finder.rb +173 -29
  129. data/lib/saboteur/text.rb +42 -0
  130. data/lib/saboteur/version.rb +1 -1
  131. data/lib/saboteur/working_tree.rb +38 -15
  132. data/lib/saboteur.rb +29 -1
  133. metadata +50 -2
@@ -0,0 +1,126 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../text"
5
+
6
+ module Saboteur
7
+ module Selection
8
+ # Untracked files as changed-line coverage for --since.
9
+ #
10
+ # A diff cannot see untracked files, so a never-committed source file
11
+ # would silently contribute no changed lines - and, being in no map,
12
+ # not even the changed-files-without-subjects warning (#295). Union
13
+ # every untracked file that could hold subjects (the include set the
14
+ # run will mutate) as fully changed: a new file is the strongest
15
+ # possible "changed since ref" signal. Ignored files stay invisible
16
+ # (--exclude-standard), and binary or empty files carry no lines,
17
+ # matching how a committed binary diffs to no hunks.
18
+ #
19
+ # Each file is opened RDONLY|NOFOLLOW|NONBLOCK and validated on that
20
+ # one descriptor, closing the listing-to-read race: a symlink swapped
21
+ # in after the listing fails the open, a FIFO opens without blocking
22
+ # and fails the regular-file stat, and a vanished or unreadable file
23
+ # is reported. Where a platform lacks either flag the union is skipped
24
+ # with a warning rather than done unsafely (the crash log makes the
25
+ # same trade, #296). The binary sniff reads only the first few
26
+ # kilobytes (git's own heuristic window) and the count streams, so a
27
+ # huge untracked source costs one pass, not its content in memory; the
28
+ # map holds a range, not one Set entry per line.
29
+ module UntrackedChanges
30
+ BINARY_SNIFF_BYTES = 8000
31
+ READ_CHUNK_BYTES = 64 * 1024
32
+
33
+ module_function
34
+
35
+ # Add every include-eligible untracked file to CHANGES as a full
36
+ # range. Eligible files that cannot be read are warned about and
37
+ # recorded as discovery failures, not dropped silently, so the run is
38
+ # reported incomplete instead of clean over mixed source.
39
+ def union(changes, top, include:, diagnostics:)
40
+ unless File.const_defined?(:NOFOLLOW) && File.const_defined?(:NONBLOCK)
41
+ diagnostics&.warn("--since: untracked files are not included: this platform lacks " \
42
+ "O_NOFOLLOW/O_NONBLOCK, so they cannot be opened race-free")
43
+ return
44
+ end
45
+
46
+ flags = File::RDONLY | File::NOFOLLOW | File::NONBLOCK
47
+ unreadable = []
48
+ files(top).each do |name|
49
+ file = File.join(top, name)
50
+ # Membership is lexical first: an included path swapped to a
51
+ # symlink must reach the safe open, which rejects it, so the swap
52
+ # is reported instead of filtered out on the resolved target. The
53
+ # canonical fallback keeps a symlinked ROOT in scope, because the
54
+ # include set is realpath'd.
55
+ next if include && !include.include?(file) && !include.include?(CoverageMap.canonical(file))
56
+
57
+ case (lines = line_count(file, flags))
58
+ when :unreadable then unreadable << name
59
+ when Integer then changes[file] = (1..lines)
60
+ end
61
+ end
62
+ unreadable.each do |name|
63
+ diagnostics&.record_discovery_failure(File.join(top, name), reason: "unreadable")
64
+ end
65
+ warn_unreadable(unreadable, diagnostics)
66
+ end
67
+
68
+ # Absolute paths of untracked, non-ignored files under ROOT: they
69
+ # never appear in the diff but can still sit in excluded territory.
70
+ def paths(root)
71
+ top = Text.utf8_identity(GitDiff.git(root, "rev-parse", "--show-toplevel").b.strip)
72
+ files(top).map { |name| File.join(top, name) }
73
+ end
74
+
75
+ def files(top)
76
+ GitDiff.git(top, "ls-files", "-z", "--others", "--exclude-standard").b.split("\0").map do |name|
77
+ Text.utf8_identity(name)
78
+ end
79
+ end
80
+
81
+ # An included untracked file that cannot be read (vanished,
82
+ # unreadable or replaced after the listing) is missing source
83
+ # coverage: a clean --since must not hide it.
84
+ def warn_unreadable(names, diagnostics)
85
+ return if names.empty? || diagnostics.nil?
86
+
87
+ shown = names.sort.first(5).join(", ")
88
+ shown += ", ..." if names.size > 5
89
+ diagnostics.warn("--since: #{names.size} included untracked file(s) could not be read (vanished, " \
90
+ "unreadable or replaced), so their subjects are not covered: #{shown}")
91
+ end
92
+
93
+ # Line count of an untracked file opened race-free: nil when the
94
+ # file stays out of the map by content (empty, or binary inside the
95
+ # sniff window), or :unreadable when it could not be validated
96
+ # (vanished, unreadable, a symlink, not a regular file).
97
+ def line_count(file, flags)
98
+ File.open(file, flags) do |io|
99
+ next :unreadable unless io.stat.file?
100
+
101
+ count_lines(io)
102
+ end
103
+ rescue SystemCallError, IOError
104
+ :unreadable
105
+ end
106
+
107
+ # Every read is bounded: the binary sniff takes only the first few
108
+ # kilobytes (git's own heuristic window; read of an empty file is
109
+ # nil, never ""), then the count streams in fixed chunks.
110
+ def count_lines(io)
111
+ first = io.read(BINARY_SNIFF_BYTES)
112
+ return if first.nil? || first.include?("\0")
113
+
114
+ lines = first.count("\n")
115
+ trailing_newline = first.end_with?("\n")
116
+ # A no-arg read returns "" at EOF, never nil - guard the loop so a
117
+ # mutant that drops the chunk size spins zero times, not forever.
118
+ while (chunk = io.read(READ_CHUNK_BYTES)) && !chunk.empty?
119
+ lines += chunk.count("\n")
120
+ trailing_newline = chunk.end_with?("\n")
121
+ end
122
+ trailing_newline ? lines : lines + 1
123
+ end
124
+ end
125
+ end
126
+ 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.
@@ -114,6 +137,17 @@ module Saboteur
114
137
  line.between?(start_line, end_line)
115
138
  end
116
139
 
140
+ # True when a changed-lines set intersects this subject's span. A
141
+ # whole-file range answers by interval overlap in O(1) instead of a
142
+ # per-line scan from line 1 for every subject.
143
+ def overlaps_lines?(lines)
144
+ if lines.is_a?(Range)
145
+ lines.begin <= end_line && start_line <= lines.end
146
+ else
147
+ lines.any? { |line| contains_line?(line) }
148
+ end
149
+ end
150
+
117
151
  # Applies the mutation's edits to this method's text and returns the
118
152
  # mutated method text. Raises ArgumentError for edits outside the method
119
153
  # or overlapping each other.
@@ -129,6 +163,12 @@ module Saboteur
129
163
  text.force_encoding(Encoding::UTF_8)
130
164
  end
131
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
+
132
172
  def to_s
133
173
  id
134
174
  end
@@ -148,10 +188,15 @@ module Saboteur
148
188
  child.compact_child_nodes.any? { |grandchild| rescuing?(grandchild) }
149
189
  end
150
190
 
151
- 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)
152
197
  return found if node.nil?
153
198
 
154
- if node.is_a?(Prism::StatementsNode)
199
+ if node.is_a?(Prism::StatementsNode) && !embedded
155
200
  statement = node.body.find do |child|
156
201
  child.location.start_offset <= start_byte && end_byte <= child.location.end_offset
157
202
  end
@@ -163,7 +208,8 @@ module Saboteur
163
208
  node.compact_child_nodes.each do |child|
164
209
  next unless child.location.start_offset <= start_byte && end_byte <= child.location.end_offset
165
210
 
166
- 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))
167
213
  end
168
214
  found
169
215
  end
@@ -10,15 +10,17 @@ module Saboteur
10
10
  # Included: instance methods and `def self.x` inside a class or module,
11
11
  # defs inside `class << self`, and defs passed to a receiverless call such
12
12
  # as `private def x`. Skipped: top-level defs, defs nested in defs, and
13
- # `def obj.x` for obj != self. Defs in blocks (`Class.new do`, `included do`)
13
+ # `def obj.x` for obj != self. Defs in blocks (`included do`, builders)
14
14
  # are found but marked skipped ("defined inside a block"), so the report
15
- # names them instead of leaving them out. `X = Data.define`, `Struct.new`,
16
- # `Class.new` or `Module.new` with a block names X as the owner; any other
17
- # block, and a `A::X = ...` target, keeps the enclosing owner or the
18
- # placeholder.
15
+ # names them instead of leaving them out - except the four core class
16
+ # factories: `X = Data.define`, `Struct.new`, `Class.new` or `Module.new`
17
+ # with a block names X as the owner, and its defs are ordinary subjects
18
+ # that re-evaluate through X.class_eval (#290). Any other block, and a
19
+ # `A::X = ...` target, keeps the enclosing owner or the placeholder.
19
20
  class SubjectFinder
20
21
  Parsed = Data.define(:subjects, :errors)
21
22
  ANONYMOUS_OWNER = "(anonymous block)"
23
+ CALL_DEFINED_SINGLETONS = %i[scope define_singleton_method].freeze
22
24
  CLASS_FACTORIES = { Data: :define, Struct: :new, Class: :new, Module: :new }.freeze
23
25
 
24
26
  # source: the text to parse when it is not the file on disk (the file as
@@ -51,24 +53,142 @@ module Saboteur
51
53
  when Prism::ConstantWriteNode
52
54
  walk_constant_assignment(node, nesting, out, **ctx)
53
55
  when Prism::ModuleNode
54
- walk(node.body, nesting + [Scope.new(kind: :module, path: node.constant_path.slice)], out, **ctx)
56
+ scope = Scope.new(kind: :module, path: node.constant_path.slice)
57
+ walk(node.body, lexical_nesting(nesting) + [scope], out, **ctx)
55
58
  when Prism::ClassNode
56
- walk(node.body, nesting + [Scope.new(kind: :class, path: node.constant_path.slice)], out, **ctx)
59
+ scope = Scope.new(kind: :class, path: node.constant_path.slice)
60
+ walk(node.body, lexical_nesting(nesting) + [scope], out, **ctx)
57
61
  when Prism::SingletonClassNode
58
62
  walk_singleton_class(node, nesting, out, **ctx)
59
63
  when Prism::StatementsNode
60
- previous_child = nil
61
- node.body.each do |child|
62
- walk(child, nesting, out, previous: previous_child, **ctx)
63
- previous_child = child
64
- end
64
+ walk_statements(node, nesting, out, **ctx)
65
65
  when Prism::DefNode
66
66
  add_subject(node, nesting, out, previous: previous, **ctx)
67
+ when Prism::CallNode
68
+ walk_call(node, nesting, out, **ctx)
67
69
  else
68
70
  node.compact_child_nodes.each { |child| walk(child, nesting, out, **ctx) }
69
71
  end
70
72
  end
71
73
 
74
+ # `define_method(:m) { ... }` is a method definition: the block body is
75
+ # the method body, and re-evaluating the whole call redefines the method
76
+ # on the enclosing owner (#194). Only a bare call with a literal name and
77
+ # a block counts; a computed name, an explicit receiver, or a lambda
78
+ # argument says nothing the finder can name.
79
+ # A define_method subject's body is its block; any other call is walked
80
+ # through its children.
81
+ def walk_call(node, nesting, out, **ctx)
82
+ return walk_heredoc_eval(node, nesting, out, **ctx) if heredoc_eval?(node)
83
+ return walk_children(node, nesting, out, **ctx) unless call_defined_method?(node)
84
+
85
+ in_sclass = nesting.last&.sclass? || false
86
+ owner = owner_path(nesting)
87
+ owner ||= ANONYMOUS_OWNER if ctx[:in_block]
88
+ return walk_children(node, nesting, out, **ctx) unless owner
89
+
90
+ name = node.arguments.arguments.first.unescaped.to_sym
91
+ # scope and define_singleton_method define on the class itself;
92
+ # define_method follows the surrounding nesting like a def (#194).
93
+ singleton = in_sclass || CALL_DEFINED_SINGLETONS.include?(node.name)
94
+ subject = Subject.new(name: name, owner: owner, singleton: singleton, sclass: in_sclass,
95
+ file: ctx[:file], node: node, file_source: ctx[:source], nesting: nesting,
96
+ frozen_literals: ctx[:frozen], sig_span: nil)
97
+ subject.skip_reason = "defined inside a block" if ctx[:in_block]
98
+ out << subject
99
+ end
100
+
101
+ # Statements carry `previous` so a def can see the sig comment above it.
102
+ def walk_statements(node, nesting, out, **ctx)
103
+ previous_child = nil
104
+ node.body.each do |child|
105
+ walk(child, nesting, out, previous: previous_child, **ctx)
106
+ previous_child = child
107
+ end
108
+ end
109
+
110
+ def walk_children(node, nesting, out, **ctx)
111
+ node.compact_child_nodes.each { |child| walk(child, nesting, out, **ctx) }
112
+ end
113
+
114
+ # `class_eval <<-RUBY ... RUBY` with a non-interpolated heredoc defines
115
+ # methods on the enclosing owner (#544). Only a bare call with one
116
+ # heredoc argument counts: an explicit receiver names another owner, and
117
+ # an interpolated heredoc does not map to file bytes 1:1, so it stays on
118
+ # the code-in-strings warning (#467).
119
+ def heredoc_eval?(node)
120
+ return false unless %i[class_eval module_eval].include?(node.name) && node.receiver.nil? && node.block.nil?
121
+
122
+ arguments = node.arguments&.arguments
123
+ return false unless arguments&.size == 1
124
+
125
+ heredoc = arguments.first
126
+ heredoc.is_a?(Prism::StringNode) && heredoc.opening_loc&.slice&.start_with?("<<")
127
+ end
128
+
129
+ # The heredoc body's bytes sit in the real file 1:1, so blanking every
130
+ # byte outside the body (keeping newlines) parses the defs at their
131
+ # real offsets and lines: mutants apply through the ordinary file-bytes
132
+ # path. The enclosing nesting supplies the owner. A body that does not
133
+ # parse defines nothing.
134
+ def walk_heredoc_eval(node, nesting, out, **ctx)
135
+ location = node.arguments.arguments.first.content_loc
136
+ virtual = blank_outside(ctx[:source], location.start_offset...location.end_offset)
137
+ result = Prism.parse(virtual, filepath: ctx[:file])
138
+ return if result.failure?
139
+
140
+ walk(result.value, nesting, out, **ctx)
141
+ end
142
+
143
+ # source with every byte outside range spaced over, except newlines:
144
+ # offsets and lines inside range stay exactly as in the real file.
145
+ def blank_outside(source, range)
146
+ source.bytes.each_with_index.map { |byte, index| range.cover?(index) || byte == 10 ? byte : 32 }.pack("C*")
147
+ .force_encoding(source.encoding)
148
+ end
149
+
150
+ def define_method_subject?(node)
151
+ return false unless node.name == :define_method && node.receiver.nil?
152
+ return false unless node.block.is_a?(Prism::BlockNode)
153
+
154
+ # With two arguments the second is the body and the block is dead code.
155
+ return false unless node.arguments&.arguments&.size == 1
156
+
157
+ name_arg = node.arguments.arguments.first
158
+ name_arg.is_a?(Prism::SymbolNode) || name_arg.is_a?(Prism::StringNode)
159
+ end
160
+
161
+ # Calls that define a method, all with a literal first argument for the
162
+ # name: define_method (instance, or singleton inside class << self) and
163
+ # the two class-level shapes the human-essentials dogfood exposed,
164
+ # `scope :name, -> { ... }` and define_singleton_method (#194). The
165
+ # whole call is the subject's source and re-evaluates on the owner.
166
+ def call_defined_method?(node)
167
+ return false unless node.receiver.nil?
168
+ return false unless literal_name?(node)
169
+
170
+ case node.name
171
+ when :define_method then define_method_subject?(node)
172
+ when :define_singleton_method then lambda_body?(node) || block_only?(node)
173
+ # Rails scope: exactly a literal name and a lambda body.
174
+ when :scope then lambda_body?(node) && node.arguments.arguments.size == 2
175
+ else false
176
+ end
177
+ end
178
+
179
+ def literal_name?(node)
180
+ name_arg = node.arguments&.arguments&.first
181
+ name_arg.is_a?(Prism::SymbolNode) || name_arg.is_a?(Prism::StringNode)
182
+ end
183
+
184
+ def lambda_body?(node)
185
+ node.block.nil? && node.arguments&.arguments&.last.is_a?(Prism::LambdaNode)
186
+ end
187
+
188
+ def block_only?(node)
189
+ node.block.is_a?(Prism::BlockNode) && node.arguments&.arguments&.size == 1
190
+ end
191
+
72
192
  # `Point = Data.define(:x) do ... end`: the defs belong to Point. Only the
73
193
  # four core factories count, called on the bare constant; any other call
74
194
  # that takes a block says nothing about the class it returns.
@@ -77,9 +197,24 @@ module Saboteur
77
197
  return node.compact_child_nodes.each { |child| walk(child, nesting, out, **ctx) } unless class_factory?(call)
78
198
 
79
199
  # The receiver and arguments stay in the enclosing scope; only the
80
- # factory's own block belongs to the new class.
200
+ # factory's own block belongs to the new class. The block's body is
201
+ # walked directly, not as a block: the factory names its class, so the
202
+ # defs re-evaluate through X.class_eval and are ordinary subjects
203
+ # (#290), while a block nested deeper still marks its defs skipped.
81
204
  (call.compact_child_nodes - [call.block]).each { |child| walk(child, nesting, out, **ctx) }
82
- walk(call.block, nesting + [Scope.new(kind: :class, path: node.name.to_s)], out, **ctx)
205
+ walk(call.block.body, lexical_nesting(nesting) + [Scope.new(kind: :factory, path: node.name.to_s)], out, **ctx)
206
+ end
207
+
208
+ # Constant-introducing declarations (class/module keywords and factory
209
+ # assignments) bind on the enclosing lexical scope: factory blocks open
210
+ # none, so their scopes never join a new constant's path (#339) - unless
211
+ # an sclass follows the factory: `class << self` inside a factory block
212
+ # attaches to the factory's class at runtime (class_eval sets self), and
213
+ # the sclass needs the factory's path to name that attachment (#438).
214
+ def lexical_nesting(nesting)
215
+ nesting.each_with_index.reject do |scope, index|
216
+ scope.factory? && nesting[(index + 1)..].none?(&:sclass?)
217
+ end.map(&:first)
83
218
  end
84
219
 
85
220
  def class_factory?(call)
@@ -123,26 +258,35 @@ module Saboteur
123
258
  end
124
259
 
125
260
  # Lexical path of the enclosing class/module. `class ::Foo` restarts at the top.
261
+ # A bare `class << self` scope leaves the owner alone (its defs are the
262
+ # attached class's singleton methods), but a factory constant assigned
263
+ # through it binds on the singleton class (#352), so the sclass renders
264
+ # as a `singleton_class` path segment when a factory scope sits after it
265
+ # (#406): `Host.singleton_class::Widget`, mirroring the constant side
266
+ # (#380).
126
267
  def owner_path(nesting)
127
- parts = nesting.reject(&:sclass?).map(&:path)
128
- return nil if parts.empty?
268
+ segments = nesting.each_with_index.filter_map do |scope, index|
269
+ if scope.sclass?
270
+ :singleton_class if nesting[(index + 1)..].any?(&:factory?)
271
+ else
272
+ scope.path
273
+ end
274
+ end
275
+ return nil if segments.empty?
129
276
 
130
- parts.each_with_object([]) do |path, acc|
131
- acc.clear if path.start_with?("::")
132
- acc << path.delete_prefix("::")
133
- end.join("::")
277
+ segments.each_with_object(+"") do |segment, out|
278
+ if segment == :singleton_class
279
+ out << ".singleton_class"
280
+ else
281
+ out.clear if segment.start_with?("::")
282
+ out << "::" unless out.empty?
283
+ out << segment.delete_prefix("::")
284
+ end
285
+ end
134
286
  end
135
287
 
136
288
  def mark_refinements(subjects, program)
137
- return unless uses_refinements?(program)
138
-
139
- subjects.each { |subject| subject.skip_reason = "refinements" }
140
- end
141
-
142
- def uses_refinements?(node)
143
- return true if node.is_a?(Prism::CallNode) && node.name == :using && node.receiver.nil?
144
-
145
- node.compact_child_nodes.any? { |child| uses_refinements?(child) }
289
+ RefinementActivations.mark(subjects, program)
146
290
  end
147
291
  end
148
292
  end
@@ -0,0 +1,42 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+
6
+ module Saboteur
7
+ # Bytes arriving from outside the process (git output, cache and log files,
8
+ # worker pipes) are tagged with the process locale. Under a non-UTF-8
9
+ # locale such as LANG=C that tag is US-ASCII, where any non-ASCII byte
10
+ # raises on use. Re-tag everything crossing the boundary as UTF-8.
11
+ module Text
12
+ module_function
13
+
14
+ # Lossy: bytes that are not valid UTF-8 become the replacement
15
+ # character. For log and message display only - never feed the result
16
+ # back into filesystem, cache or protocol identity: scrubbing rewrites
17
+ # raw bytes, which can alias two different names or make a path vanish
18
+ # from a File.exist? probe.
19
+ def utf8(string)
20
+ string.dup.force_encoding(Encoding::UTF_8).scrub
21
+ end
22
+
23
+ # Lossless: every byte survives; only the encoding tag changes. Use for
24
+ # paths and identity-bearing fields. File.* calls and String == then
25
+ # work on the exact bytes git reported; only rendering (regexes over
26
+ # arbitrary content, JSON, terminal output) needs #utf8.
27
+ def utf8_identity(string)
28
+ string.dup.force_encoding(Encoding::UTF_8)
29
+ end
30
+
31
+ # Lossless + strict: retag without scrubbing, then reject bytes
32
+ # that are not valid UTF-8 with JSON::ParserError, so a JSON
33
+ # boundary's existing parse-error handling rejects corrupt input
34
+ # instead of silently accepting a rewritten version of it.
35
+ def utf8_strict(string)
36
+ tagged = utf8_identity(string)
37
+ raise JSON::ParserError, "invalid UTF-8" unless tagged.valid_encoding?
38
+
39
+ tagged
40
+ end
41
+ end
42
+ end
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Saboteur
5
- VERSION = "0.6.0"
5
+ VERSION = "0.8.0"
6
6
  end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "open3"
5
+ require_relative "text"
5
6
 
6
7
  module Saboteur
7
8
  # The state of the working source a run looked at: the commit it was on and
@@ -11,23 +12,25 @@ module Saboteur
11
12
  # report from an earlier run does not make the tree look dirty. Without git, or
12
13
  # outside a repository, the state is unknown.
13
14
  class WorkingTree
14
- State = Data.define(:head, :dirty, :changed_files) do
15
- def label
16
- return "unknown" if dirty.nil?
15
+ # One uncommitted change: its path relative to the root (the new path
16
+ # of a rename, the old path of a deletion) and whether git tracks it.
17
+ Change = Data.define(:path, :tracked)
17
18
 
18
- dirty ? "dirty" : "clean"
19
+ State = Data.define(:head, :dirty, :changed_files, :changes) do
20
+ def initialize(head:, dirty:, changed_files:, changes: [])
21
+ super
19
22
  end
20
23
 
21
- # Shown before a run: mutants run your code and can change the tree.
22
- def warning
23
- return unless dirty
24
+ def label
25
+ return "unknown" if dirty.nil?
24
26
 
25
- "saboteur: warning: #{changed_files} uncommitted change(s) in the working tree. " \
26
- "Mutants run your code and could alter or delete files; run on a clean, committed tree or a CI checkout."
27
+ dirty ? "dirty" : "clean"
27
28
  end
28
29
  end
29
30
 
30
31
  UNKNOWN = State.new(head: nil, dirty: nil, changed_files: nil)
32
+ UNTRACKED = "??"
33
+ RENAMED_OR_COPIED = /[RC]/
31
34
 
32
35
  # ignored: paths relative to root that are not source (saboteur's cache and reports).
33
36
  def initialize(root, ignored: [])
@@ -35,22 +38,42 @@ module Saboteur
35
38
  @ignored = ignored
36
39
  end
37
40
 
41
+ # git status names paths from the top of the repository; the prefix
42
+ # turns them into paths relative to the root.
38
43
  def state
39
- head, head_status = Open3.capture2e("git", "rev-parse", "HEAD", chdir: @root)
40
- changes, changes_status = Open3.capture2e(*status_command, chdir: @root)
41
- return UNKNOWN unless head_status.success? && changes_status.success?
44
+ head, _, head_status = Open3.capture3("git", "rev-parse", "HEAD", "--show-prefix", chdir: @root)
45
+ status, _, status_status = Open3.capture3(*status_command, chdir: @root)
46
+ return UNKNOWN unless head_status.success? && status_status.success?
42
47
 
43
- count = changes.lines.size
44
- State.new(head: head.strip, dirty: count.positive?, changed_files: count)
48
+ sha, prefix = head.lines.map(&:chomp)
49
+ changes = changes_in(status, Text.utf8_identity(prefix.to_s))
50
+ State.new(head: sha, dirty: changes.any?, changed_files: changes.size, changes: changes)
45
51
  rescue SystemCallError
46
52
  UNKNOWN
47
53
  end
48
54
 
49
55
  private
50
56
 
57
+ # Porcelain v1 with -z: "XY path" entries separated by NUL, a rename or
58
+ # copy followed by one more entry holding its old path.
59
+ def changes_in(status, prefix)
60
+ entries = status.b.split("\0")
61
+ changes = []
62
+ until entries.empty?
63
+ entry = entries.shift
64
+ code = entry[0, 2]
65
+ entries.shift if code.match?(RENAMED_OR_COPIED)
66
+ path = Text.utf8_identity(entry[3..]).delete_prefix(prefix)
67
+ changes << Change.new(path: path, tracked: code != UNTRACKED)
68
+ end
69
+ changes
70
+ end
71
+
72
+ # -z implies --porcelain=v1, and status takes no revisions, so the
73
+ # pathspecs need no "--" in front.
51
74
  def status_command
52
75
  excluded = @ignored.map { |path| ":(exclude)#{path}" }
53
- ["git", "status", "--porcelain", "--untracked-files=all", "--", ".", *excluded]
76
+ ["git", "status", "-z", "--untracked-files=all", ".", *excluded]
54
77
  end
55
78
  end
56
79
  end