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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1024 -0
- data/README.md +199 -952
- data/exe/saboteur +34 -7
- data/lib/saboteur/baseline/workflow.rb +2 -1
- data/lib/saboteur/baseline_health.rb +40 -6
- data/lib/saboteur/cli/exit_status.rb +80 -4
- data/lib/saboteur/cli/help_options.rb +17 -0
- data/lib/saboteur/cli/history_command.rb +135 -0
- data/lib/saboteur/cli/presets.rb +40 -0
- data/lib/saboteur/cli/run_budget_options.rb +37 -0
- data/lib/saboteur/cli/run_reporting.rb +74 -5
- data/lib/saboteur/cli/selection_options.rb +31 -0
- data/lib/saboteur/cli/shard_options.rb +17 -0
- data/lib/saboteur/cli/source_set.rb +53 -0
- data/lib/saboteur/cli/survivors_command.rb +157 -0
- data/lib/saboteur/cli.rb +136 -98
- data/lib/saboteur/config.rb +163 -15
- data/lib/saboteur/config_file.rb +8 -0
- data/lib/saboteur/constant_reads.rb +182 -0
- data/lib/saboteur/constant_subject.rb +70 -0
- data/lib/saboteur/constant_subject_finder.rb +281 -0
- data/lib/saboteur/coverage_reads.rb +29 -0
- data/lib/saboteur/crash_report.rb +154 -0
- data/lib/saboteur/custom_map.rb +3 -1
- data/lib/saboteur/dirty_tree_warning.rb +128 -0
- data/lib/saboteur/doctor.rb +49 -1
- data/lib/saboteur/engine/baseline_guard.rb +136 -14
- data/lib/saboteur/engine/boot.rb +16 -1
- data/lib/saboteur/engine/canary.rb +41 -10
- data/lib/saboteur/engine/candidate_selection.rb +28 -1
- data/lib/saboteur/engine/candidates.rb +43 -3
- data/lib/saboteur/engine/coverage_guard.rb +29 -0
- data/lib/saboteur/engine/coverage_store.rb +79 -15
- data/lib/saboteur/engine/defined_order_prefix.rb +43 -0
- data/lib/saboteur/engine/diagnostics.rb +41 -2
- data/lib/saboteur/engine/diff_scope.rb +24 -5
- data/lib/saboteur/engine/distribution.rb +134 -8
- data/lib/saboteur/engine/early_stop_recording.rb +47 -0
- data/lib/saboteur/engine/gated_run.rb +35 -0
- data/lib/saboteur/engine/load_coverage.rb +45 -0
- data/lib/saboteur/engine/out_of_scope_guard.rb +21 -0
- data/lib/saboteur/engine/pipelines.rb +107 -0
- data/lib/saboteur/engine/result_cache.rb +67 -9
- data/lib/saboteur/engine/run_budget.rb +90 -0
- data/lib/saboteur/engine/run_fingerprint.rb +4 -0
- data/lib/saboteur/engine/sandbox_policy.rb +56 -11
- data/lib/saboteur/engine/scheduler.rb +222 -20
- data/lib/saboteur/engine/selection_announcement.rb +54 -0
- data/lib/saboteur/engine/shard.rb +69 -0
- data/lib/saboteur/engine/subject_selection.rb +373 -19
- data/lib/saboteur/engine/suite_files.rb +3 -1
- data/lib/saboteur/engine/test_guess.rb +438 -25
- data/lib/saboteur/engine/thread_warnings.rb +53 -0
- data/lib/saboteur/engine/unchanged_syntax.rb +17 -2
- data/lib/saboteur/engine/unscoped_suite_warning.rb +41 -0
- data/lib/saboteur/engine/verdict_check.rb +54 -18
- data/lib/saboteur/engine.rb +202 -158
- data/lib/saboteur/execution/external_runner.rb +471 -0
- data/lib/saboteur/execution/marker_sweep.rb +79 -0
- data/lib/saboteur/execution/peak_memory.rb +28 -0
- data/lib/saboteur/execution/process_table.rb +12 -0
- data/lib/saboteur/execution/process_tree.rb +65 -5
- data/lib/saboteur/execution/protocol.rb +2 -1
- data/lib/saboteur/execution/reevaluator.rb +68 -12
- data/lib/saboteur/execution/runner.rb +225 -24
- data/lib/saboteur/execution/sandbox.rb +21 -3
- data/lib/saboteur/execution/seatbelt.rb +226 -0
- data/lib/saboteur/execution/supervisor.rb +45 -13
- data/lib/saboteur/execution/worker_pool.rb +110 -8
- data/lib/saboteur/frameworks/sqlite_memory_database.rb +49 -0
- data/lib/saboteur/gates.rb +43 -2
- data/lib/saboteur/help.rb +108 -13
- data/lib/saboteur/ignore_list.rb +40 -7
- data/lib/saboteur/install.rb +4 -3
- data/lib/saboteur/integration/base.rb +7 -2
- data/lib/saboteur/integration/rspec.rb +175 -9
- data/lib/saboteur/mutation_source/argument_removal.rb +1 -1
- data/lib/saboteur/mutation_source/builtin.rb +44 -14
- data/lib/saboteur/mutation_source/chain_removal.rb +1 -1
- data/lib/saboteur/mutation_source/condition_negation.rb +1 -1
- data/lib/saboteur/mutation_source/constant_literals.rb +66 -0
- data/lib/saboteur/mutation_source/element_removal.rb +73 -0
- data/lib/saboteur/mutation_source/endpoint_replacement.rb +1 -1
- data/lib/saboteur/mutation_source/entry_edits.rb +44 -0
- data/lib/saboteur/mutation_source/filter_replacement.rb +1 -1
- data/lib/saboteur/mutation_source/hazard_guard.rb +129 -3
- data/lib/saboteur/mutation_source/interpolation_removal.rb +41 -0
- data/lib/saboteur/mutation_source/literal_replacement.rb +1 -1
- data/lib/saboteur/mutation_source/method_swap.rb +193 -0
- data/lib/saboteur/mutation_source/mutation.rb +5 -3
- data/lib/saboteur/mutation_source/operand_replacement.rb +54 -0
- data/lib/saboteur/mutation_source/operator_assignment.rb +1 -1
- data/lib/saboteur/mutation_source/operator_examples.rb +6 -1
- data/lib/saboteur/mutation_source/operator_registry.rb +7 -2
- data/lib/saboteur/mutation_source/operator_visit.rb +39 -0
- data/lib/saboteur/mutation_source/pair_removal.rb +13 -31
- data/lib/saboteur/mutation_source/range_replacement.rb +1 -1
- data/lib/saboteur/mutation_source/regex_replacement.rb +93 -0
- data/lib/saboteur/preview.rb +122 -13
- data/lib/saboteur/refinement_activations.rb +60 -0
- data/lib/saboteur/reporter/agent.rb +9 -1
- data/lib/saboteur/reporter/agent_commands.rb +47 -15
- data/lib/saboteur/reporter/agent_incomplete.rb +38 -2
- data/lib/saboteur/reporter/agent_summary.rb +4 -3
- data/lib/saboteur/reporter/findings.rb +5 -2
- data/lib/saboteur/reporter/history_policy.rb +11 -0
- data/lib/saboteur/reporter/history_reader.rb +47 -0
- data/lib/saboteur/reporter/html.rb +26 -4
- data/lib/saboteur/reporter/mutation_text.rb +6 -0
- data/lib/saboteur/reporter/next_steps.rb +14 -4
- data/lib/saboteur/reporter/score_history.rb +5 -1
- data/lib/saboteur/reporter/stryker.rb +40 -7
- data/lib/saboteur/reporter/survivor_classification.rb +101 -0
- data/lib/saboteur/reporter/survivor_hints.rb +3 -0
- data/lib/saboteur/reporter/survivor_list.rb +15 -9
- data/lib/saboteur/reporter/terminal.rb +74 -17
- data/lib/saboteur/result.rb +38 -7
- data/lib/saboteur/selection/coverage_map.rb +107 -11
- data/lib/saboteur/selection/dependency_shield.rb +80 -0
- data/lib/saboteur/selection/external_test_map.rb +27 -0
- data/lib/saboteur/selection/git_diff.rb +105 -13
- data/lib/saboteur/selection/layout_only_lines.rb +80 -0
- data/lib/saboteur/selection/recorder.rb +22 -4
- data/lib/saboteur/selection/source_scope.rb +22 -0
- data/lib/saboteur/selection/untracked_changes.rb +126 -0
- data/lib/saboteur/subject.rb +53 -7
- data/lib/saboteur/subject_finder.rb +173 -29
- data/lib/saboteur/text.rb +42 -0
- data/lib/saboteur/version.rb +1 -1
- data/lib/saboteur/working_tree.rb +38 -15
- data/lib/saboteur.rb +29 -1
- metadata +50 -2
|
@@ -31,6 +31,7 @@ module Saboteur
|
|
|
31
31
|
].freeze
|
|
32
32
|
|
|
33
33
|
OPEN3 = /\A(::)?Open3\z/
|
|
34
|
+
SEND_NAMES = %w[send public_send __send__].freeze
|
|
34
35
|
ALWAYS_NAMES = %w[rmtree].freeze
|
|
35
36
|
KERNEL_RECEIVERS = %w[Kernel Process].freeze
|
|
36
37
|
|
|
@@ -42,6 +43,55 @@ module Saboteur
|
|
|
42
43
|
"hazard_guard"
|
|
43
44
|
end
|
|
44
45
|
|
|
46
|
+
# Slices of hazardous calls that would RUN when the file holding the
|
|
47
|
+
# root node is evaluated: everything outside def bodies (a method body
|
|
48
|
+
# runs when called, not at load). A constant mutant re-evaluates its
|
|
49
|
+
# whole file, so this is what makes that replay safe to consider (#319).
|
|
50
|
+
def load_time_hazards(root)
|
|
51
|
+
slices = []
|
|
52
|
+
stack = [root]
|
|
53
|
+
until stack.empty?
|
|
54
|
+
node = stack.pop
|
|
55
|
+
next if node.is_a?(Prism::DefNode)
|
|
56
|
+
|
|
57
|
+
if hazardous?(node)
|
|
58
|
+
slices << node.slice
|
|
59
|
+
next
|
|
60
|
+
end
|
|
61
|
+
stack.concat(node.compact_child_nodes)
|
|
62
|
+
end
|
|
63
|
+
slices
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Calls that only load more code - and only with a plain literal
|
|
67
|
+
# string argument and no block: require(build_path) or
|
|
68
|
+
# require_relative(begin ... end) runs arbitrary code on every
|
|
69
|
+
# replay, so it is unvetted below like any other helper call.
|
|
70
|
+
SAFE_LOAD_REQUIRES = %w[require require_relative].freeze
|
|
71
|
+
|
|
72
|
+
# Assignments whose value a replay re-runs: vetted when the value is
|
|
73
|
+
# a plain literal construction.
|
|
74
|
+
WRITE_NODES = [Prism::ConstantWriteNode, Prism::ConstantPathWriteNode,
|
|
75
|
+
Prism::ConstantOrWriteNode, Prism::ConstantAndWriteNode,
|
|
76
|
+
Prism::ConstantPathOrWriteNode, Prism::ConstantPathAndWriteNode,
|
|
77
|
+
Prism::LocalVariableWriteNode].freeze
|
|
78
|
+
|
|
79
|
+
# Slices of load-time statements whose effect saboteur does not vet.
|
|
80
|
+
# A constant mutant re-evaluates its whole file, so any code that runs
|
|
81
|
+
# at load runs again on every mutant, outside the method hazard guard.
|
|
82
|
+
# The vetted forms are narrow: require/require_relative, pure defs
|
|
83
|
+
# (a body runs when called, not at load), class/module nesting with a
|
|
84
|
+
# constant superclass, and constant or local writes of plain literal
|
|
85
|
+
# values (a `.freeze` on the literal is looked through). Everything
|
|
86
|
+
# else - helper calls, registrations, writes to disk, class factories,
|
|
87
|
+
# top-level blocks - is named here, NOT proven unsafe: the check only
|
|
88
|
+
# says saboteur cannot account for what re-running it does (#319).
|
|
89
|
+
def unvetted_load_code(root)
|
|
90
|
+
bad = []
|
|
91
|
+
vet_statements(root.statements.body, bad)
|
|
92
|
+
bad
|
|
93
|
+
end
|
|
94
|
+
|
|
45
95
|
# candidates: [[mutation, mutated_text]]. Returns [kept, suppressed].
|
|
46
96
|
def partition(candidates, subject)
|
|
47
97
|
spans = hazard_spans(subject.node)
|
|
@@ -52,6 +102,65 @@ module Saboteur
|
|
|
52
102
|
|
|
53
103
|
private
|
|
54
104
|
|
|
105
|
+
def vet_statements(body, bad)
|
|
106
|
+
body.each { |node| vet_statement(node, bad) }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def vet_statement(node, bad)
|
|
110
|
+
case node
|
|
111
|
+
when Prism::DefNode
|
|
112
|
+
nil
|
|
113
|
+
when Prism::ClassNode, Prism::ModuleNode
|
|
114
|
+
vet_scope(node, bad)
|
|
115
|
+
when *WRITE_NODES
|
|
116
|
+
bad << node.slice unless vetted_value?(node.value)
|
|
117
|
+
when Prism::CallNode
|
|
118
|
+
bad << node.slice unless vetted_call?(node)
|
|
119
|
+
else
|
|
120
|
+
bad << node.slice
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def vet_scope(node, bad)
|
|
125
|
+
superclass = node.respond_to?(:superclass) ? node.superclass : nil
|
|
126
|
+
bad << superclass.slice if superclass && !constant_read?(superclass)
|
|
127
|
+
vet_statements(node.body&.body || [], bad)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def vetted_call?(node)
|
|
131
|
+
return false unless SAFE_LOAD_REQUIRES.include?(node.name.to_s) && node.receiver.nil?
|
|
132
|
+
return false unless node.block.nil?
|
|
133
|
+
|
|
134
|
+
arguments = node.arguments&.arguments || []
|
|
135
|
+
arguments.length == 1 && arguments.first.is_a?(Prism::StringNode)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def constant_read?(node)
|
|
139
|
+
node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# A value whose evaluation constructs data and reads constants only:
|
|
143
|
+
# literal scalars, nested arrays/hashes of the same, constant reads,
|
|
144
|
+
# and `.freeze` on any of those.
|
|
145
|
+
def vetted_value?(node)
|
|
146
|
+
case node
|
|
147
|
+
when Prism::StringNode, Prism::SymbolNode, Prism::IntegerNode, Prism::FloatNode,
|
|
148
|
+
Prism::TrueNode, Prism::FalseNode, Prism::NilNode, Prism::SourceFileNode,
|
|
149
|
+
Prism::ConstantReadNode, Prism::ConstantPathNode
|
|
150
|
+
true
|
|
151
|
+
when Prism::ArrayNode
|
|
152
|
+
node.elements.all? { |element| vetted_value?(element) }
|
|
153
|
+
when Prism::HashNode
|
|
154
|
+
node.elements.all? do |pair|
|
|
155
|
+
pair.is_a?(Prism::AssocNode) && vetted_value?(pair.key) && vetted_value?(pair.value)
|
|
156
|
+
end
|
|
157
|
+
when Prism::CallNode
|
|
158
|
+
node.name == :freeze && node.arguments.nil? && node.block.nil? && vetted_value?(node.receiver)
|
|
159
|
+
else
|
|
160
|
+
false
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
55
164
|
# Byte spans of hazardous calls in the node tree, with the source slice
|
|
56
165
|
# of each. Inner calls are covered by the outer span, so the walk does
|
|
57
166
|
# not descend into a hazardous call.
|
|
@@ -81,6 +190,19 @@ module Saboteur
|
|
|
81
190
|
name = node.name.to_s
|
|
82
191
|
receiver = node.receiver&.slice&.sub(/\A::/, "")
|
|
83
192
|
return true if OPEN3.match?(receiver.to_s)
|
|
193
|
+
|
|
194
|
+
args = node.arguments&.arguments || []
|
|
195
|
+
# obj.send(:rmtree, x) is obj.rmtree(x) with indirection (#240): the
|
|
196
|
+
# resolved selector faces the same direct-call semantics, with the
|
|
197
|
+
# arguments shifted past it (#294 review). A dynamic selector stays
|
|
198
|
+
# out of scope, as documented.
|
|
199
|
+
return true if SEND_NAMES.include?(name) && (selector = literal_selector(args.first)) &&
|
|
200
|
+
direct_hazard?(selector, receiver, args[1])
|
|
201
|
+
|
|
202
|
+
direct_hazard?(name, receiver, args.first)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def direct_hazard?(name, receiver, first_arg)
|
|
84
206
|
return true if ALWAYS_NAMES.include?(name)
|
|
85
207
|
|
|
86
208
|
tag = receiver ? "#{receiver}.#{name}" : name
|
|
@@ -89,14 +211,18 @@ module Saboteur
|
|
|
89
211
|
# spelling of a bare process spawn is the same hazard.
|
|
90
212
|
return true if KERNEL_RECEIVERS.include?(receiver) && @tags.include?(name)
|
|
91
213
|
|
|
92
|
-
piped_open?(
|
|
214
|
+
piped_open?(first_arg, name, receiver)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# A literal symbol or string selector names the real call.
|
|
218
|
+
def literal_selector(first)
|
|
219
|
+
first.unescaped if first.is_a?(Prism::SymbolNode) || first.is_a?(Prism::StringNode)
|
|
93
220
|
end
|
|
94
221
|
|
|
95
222
|
# Kernel#open with a "| command" argument spawns a process.
|
|
96
|
-
def piped_open?(
|
|
223
|
+
def piped_open?(first, name, receiver)
|
|
97
224
|
return false unless name == "open" && (receiver.nil? || receiver == "Kernel")
|
|
98
225
|
|
|
99
|
-
first = node.arguments&.arguments&.first
|
|
100
226
|
case first
|
|
101
227
|
when Prism::StringNode then first.unescaped.start_with?("|")
|
|
102
228
|
when Prism::InterpolatedStringNode
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "prism"
|
|
5
|
+
require_relative "base"
|
|
6
|
+
require_relative "operator_visit"
|
|
7
|
+
|
|
8
|
+
module Saboteur
|
|
9
|
+
module MutationSource
|
|
10
|
+
# Removes one piece of an interpolated string at a time: each #{expr}
|
|
11
|
+
# and each literal segment. Plain strings stay with literal_replacement
|
|
12
|
+
# and backticks stay untouched (the hazard guard owns those).
|
|
13
|
+
class InterpolationRemoval < Base
|
|
14
|
+
include OperatorVisit
|
|
15
|
+
|
|
16
|
+
# Base inherits Object's empty initializer; there is no parent state to set.
|
|
17
|
+
def initialize(arid: []) # rubocop:disable Lint/MissingSuper
|
|
18
|
+
@arid = arid.map { |pattern| Regexp.new(pattern) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def name
|
|
22
|
+
"interpolation_removal"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def mutate_node(node, subject, found)
|
|
28
|
+
remove_parts(node, subject, found) if node.is_a?(Prism::InterpolatedStringNode)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def remove_parts(node, subject, found)
|
|
32
|
+
node.parts.each do |part|
|
|
33
|
+
location = part.location
|
|
34
|
+
edit = Edit.new(file: subject.file, start_byte: location.start_offset,
|
|
35
|
+
end_byte: location.end_offset, replacement: "")
|
|
36
|
+
found << Mutation.new(subject_id: subject.id, operator: name, edits: [edit], source: name)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "prism"
|
|
5
|
+
require_relative "base"
|
|
6
|
+
require_relative "operator_visit"
|
|
7
|
+
|
|
8
|
+
module Saboteur
|
|
9
|
+
module MutationSource
|
|
10
|
+
# Swaps a method whose name has a semantic opposite: min with max,
|
|
11
|
+
# any? with all?, map with each. Only message bytes change; receiver,
|
|
12
|
+
# arguments and block stay intact. A receiver is required, so a bare
|
|
13
|
+
# max(a, b) Kernel call is left alone.
|
|
14
|
+
class MethodSwap < Base
|
|
15
|
+
include OperatorVisit
|
|
16
|
+
|
|
17
|
+
SWAPS = { min: "max", max: "min", any?: "all?", all?: "any?", map: "each", each: "map" }.freeze
|
|
18
|
+
|
|
19
|
+
# Node types that mix used and discarded child positions get a named
|
|
20
|
+
# handler; anything not listed here is conservative: every child's
|
|
21
|
+
# value is treated as used (#440).
|
|
22
|
+
CONTAINER_HANDLERS = {
|
|
23
|
+
Prism::StatementsNode => :visit_statements,
|
|
24
|
+
Prism::IfNode => :visit_if,
|
|
25
|
+
Prism::UnlessNode => :visit_unless,
|
|
26
|
+
Prism::ElseNode => :visit_else,
|
|
27
|
+
Prism::EnsureNode => :visit_ensure,
|
|
28
|
+
Prism::CaseNode => :visit_case,
|
|
29
|
+
Prism::CaseMatchNode => :visit_case,
|
|
30
|
+
Prism::WhenNode => :visit_when,
|
|
31
|
+
Prism::InNode => :visit_in,
|
|
32
|
+
Prism::BeginNode => :visit_begin,
|
|
33
|
+
Prism::RescueNode => :visit_rescue,
|
|
34
|
+
Prism::WhileNode => :visit_loop,
|
|
35
|
+
Prism::UntilNode => :visit_loop
|
|
36
|
+
}.freeze
|
|
37
|
+
|
|
38
|
+
# Base inherits Object's empty initializer; there is no parent state to set.
|
|
39
|
+
def initialize(arid: []) # rubocop:disable Lint/MissingSuper
|
|
40
|
+
@arid = arid.map { |pattern| Regexp.new(pattern) }
|
|
41
|
+
@discard_depth = 0
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def name
|
|
45
|
+
"method_swap"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
# Overrides OperatorVisit#visit to track positions whose value is
|
|
51
|
+
# syntactically discarded: a non-last statement, and - recursing from a
|
|
52
|
+
# discarded statement - the tail expression of each branch of an
|
|
53
|
+
# if/elsif/else/unless/case/when, a begin/rescue body, or a
|
|
54
|
+
# while/until body (a loop's value never observes its body), or an
|
|
55
|
+
# ensure body (its value never contributes to the result). each
|
|
56
|
+
# and map differ only in their return value (both run the block the
|
|
57
|
+
# same way), so swapping them in a discarded-value position yields
|
|
58
|
+
# an equivalent mutant (#399, #440). A call whose value flows into
|
|
59
|
+
# a predicate, receiver, block result, or control-flow argument
|
|
60
|
+
# (return/break/next) is used and stays swappable, as is the whole
|
|
61
|
+
# body of a def or block. A lazy receiver breaks the equivalence:
|
|
62
|
+
# lazy.map defers the block, so discarding it runs nothing (#431).
|
|
63
|
+
# Only a `.lazy` in the receiver chain is detected; a Lazy held in a
|
|
64
|
+
# variable, parameter or ivar is not, and its swaps are suppressed.
|
|
65
|
+
def visit(node, subject, found)
|
|
66
|
+
return if node.nil? || OperatorVisit::SCOPE_NODES.any? { |scope| node.is_a?(scope) }
|
|
67
|
+
|
|
68
|
+
handler = CONTAINER_HANDLERS[node.class]
|
|
69
|
+
handler ? send(handler, node, subject, found) : visit_used_children(node, subject, found)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Non-last statements are discarded-value positions; the last
|
|
73
|
+
# statement inherits the surrounding context (#399).
|
|
74
|
+
def visit_statements(node, subject, found)
|
|
75
|
+
node.body.each_with_index do |statement, index|
|
|
76
|
+
next if arid?(statement)
|
|
77
|
+
|
|
78
|
+
if index < node.body.size - 1
|
|
79
|
+
discarded { visit(statement, subject, found) }
|
|
80
|
+
else
|
|
81
|
+
visit(statement, subject, found)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def visit_else(node, subject, found)
|
|
87
|
+
visit(node.statements, subject, found)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# An ensure body's value never contributes to the result, so it is
|
|
91
|
+
# always a discarded-value position (#440).
|
|
92
|
+
def visit_ensure(node, subject, found)
|
|
93
|
+
discarded { visit(node.statements, subject, found) }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def visit_in(node, subject, found)
|
|
97
|
+
used { visit(node.pattern, subject, found) }
|
|
98
|
+
visit(node.statements, subject, found)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def visit_if(node, subject, found)
|
|
102
|
+
visit_branch(node, subject, found, :subsequent)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def visit_unless(node, subject, found)
|
|
106
|
+
visit_branch(node, subject, found, :else_clause)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Branch bodies inherit the discard context; the predicate's value
|
|
110
|
+
# selects the branch, so it is always used (#440).
|
|
111
|
+
def visit_branch(node, subject, found, else_reader)
|
|
112
|
+
used { visit(node.predicate, subject, found) }
|
|
113
|
+
visit(node.statements, subject, found)
|
|
114
|
+
visit(node.public_send(else_reader), subject, found)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def visit_case(node, subject, found)
|
|
118
|
+
used { visit(node.predicate, subject, found) }
|
|
119
|
+
node.conditions.each { |condition| visit(condition, subject, found) }
|
|
120
|
+
visit(node.else_clause, subject, found)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def visit_when(node, subject, found)
|
|
124
|
+
node.conditions.each { |condition| used { visit(condition, subject, found) } }
|
|
125
|
+
visit(node.statements, subject, found)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def visit_begin(node, subject, found)
|
|
129
|
+
visit(node.statements, subject, found)
|
|
130
|
+
visit(node.rescue_clause, subject, found)
|
|
131
|
+
visit(node.else_clause, subject, found)
|
|
132
|
+
discarded { visit(node.ensure_clause, subject, found) }
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def visit_rescue(node, subject, found)
|
|
136
|
+
node.exceptions.each { |exception| used { visit(exception, subject, found) } }
|
|
137
|
+
visit(node.statements, subject, found)
|
|
138
|
+
visit(node.subsequent, subject, found)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# A loop's value never observes its body, so the body is always a
|
|
142
|
+
# discarded-value position (#440).
|
|
143
|
+
def visit_loop(node, subject, found)
|
|
144
|
+
used { visit(node.predicate, subject, found) }
|
|
145
|
+
discarded { visit(node.statements, subject, found) }
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Default: every child's value is consumed by its parent.
|
|
149
|
+
def visit_used_children(node, subject, found)
|
|
150
|
+
mutate_node(node, subject, found)
|
|
151
|
+
node.compact_child_nodes.each { |child| used { visit(child, subject, found) } }
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Marks value-discarded positions during the walk. `used` resets the
|
|
155
|
+
# depth where a child's value is consumed by its parent.
|
|
156
|
+
def discarded
|
|
157
|
+
@discard_depth += 1
|
|
158
|
+
yield
|
|
159
|
+
ensure
|
|
160
|
+
@discard_depth -= 1
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def used
|
|
164
|
+
depth = @discard_depth
|
|
165
|
+
@discard_depth = 0
|
|
166
|
+
yield
|
|
167
|
+
ensure
|
|
168
|
+
@discard_depth = depth
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Any `.lazy` in the receiver chain, syntactically.
|
|
172
|
+
def lazy_receiver?(node)
|
|
173
|
+
node = node.receiver while node.is_a?(Prism::CallNode) && node.name != :lazy
|
|
174
|
+
node.is_a?(Prism::CallNode)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def mutate_node(node, subject, found)
|
|
178
|
+
swap(node, subject, found) if node.is_a?(Prism::CallNode)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def swap(node, subject, found)
|
|
182
|
+
replacement = SWAPS[node.name]
|
|
183
|
+
return unless replacement && node.receiver
|
|
184
|
+
return if @discard_depth.positive? && %w[each map].include?(node.name.to_s) && !lazy_receiver?(node.receiver)
|
|
185
|
+
|
|
186
|
+
location = node.message_loc
|
|
187
|
+
edit = Edit.new(file: subject.file, start_byte: location.start_offset,
|
|
188
|
+
end_byte: location.end_offset, replacement: replacement)
|
|
189
|
+
found << Mutation.new(subject_id: subject.id, operator: name, edits: [edit], source: name)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
@@ -15,13 +15,15 @@ module Saboteur
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
# Stable id for the ignore list. Offsets are made relative to subject_start
|
|
18
|
-
# so edits elsewhere in the file do not change the id.
|
|
18
|
+
# so edits elsewhere in the file do not change the id. The digest hashes
|
|
19
|
+
# bytes, not encodings: a binary-read source and a UTF-8 replacement hold
|
|
20
|
+
# the same identity, and joining them without .b raises under LANG=C.
|
|
19
21
|
def digest(subject_start:, original_source:)
|
|
20
22
|
parts = edits.map do |edit|
|
|
21
23
|
original = original_source.byteslice(edit.start_byte, edit.end_byte - edit.start_byte)
|
|
22
|
-
[edit.start_byte - subject_start, original, edit.replacement].join("\0")
|
|
24
|
+
[edit.start_byte - subject_start, original.to_s.b, edit.replacement.b].join("\0".b)
|
|
23
25
|
end
|
|
24
|
-
Digest::SHA256.hexdigest([subject_id, operator, *parts].join("\x1F"))[0, 16]
|
|
26
|
+
Digest::SHA256.hexdigest([subject_id.to_s.b, operator.to_s.b, *parts].join("\x1F".b))[0, 16]
|
|
25
27
|
end
|
|
26
28
|
end
|
|
27
29
|
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "prism"
|
|
5
|
+
require_relative "base"
|
|
6
|
+
require_relative "operator_visit"
|
|
7
|
+
|
|
8
|
+
module Saboteur
|
|
9
|
+
module MutationSource
|
|
10
|
+
# Replaces a binary operation with each of its operands: a + b yields a
|
|
11
|
+
# and b. Arithmetic and comparison calls only; boolean connectors stay
|
|
12
|
+
# with boolean_logic, which already replaces a connector with each side.
|
|
13
|
+
# An operand binds at least as tightly as the operation it replaces, so
|
|
14
|
+
# it needs no parentheses (the re-parse check backs this).
|
|
15
|
+
class OperandReplacement < Base
|
|
16
|
+
include OperatorVisit
|
|
17
|
+
|
|
18
|
+
FAMILIES = %i[+ - * / % ** < <= > >= == !=].freeze
|
|
19
|
+
|
|
20
|
+
# Base inherits Object's empty initializer; there is no parent state to set.
|
|
21
|
+
def initialize(arid: []) # rubocop:disable Lint/MissingSuper
|
|
22
|
+
@arid = arid.map { |pattern| Regexp.new(pattern) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def name
|
|
26
|
+
"operand_replacement"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def mutate_node(node, subject, found)
|
|
32
|
+
replace_operands(node, subject, found) if node.is_a?(Prism::CallNode)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def replace_operands(node, subject, found)
|
|
36
|
+
return unless FAMILIES.include?(node.name) && binary?(node)
|
|
37
|
+
|
|
38
|
+
[node.receiver, *node.arguments.arguments].each do |operand|
|
|
39
|
+
edit = Edit.new(file: subject.file, start_byte: node.start_offset,
|
|
40
|
+
end_byte: node.end_offset, replacement: operand.slice)
|
|
41
|
+
found << Mutation.new(subject_id: subject.id, operator: name, edits: [edit], source: name)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Only the infix spelling is binary: an operator call has no call
|
|
46
|
+
# operator, and its receiver, message, single argument and lack of a
|
|
47
|
+
# block all follow from that (a block binds to the last operand's own
|
|
48
|
+
# call, never to the operator).
|
|
49
|
+
def binary?(node)
|
|
50
|
+
node.call_operator_loc.nil?
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -24,11 +24,16 @@ module Saboteur
|
|
|
24
24
|
"literal_boolean" => "true -> false",
|
|
25
25
|
"argument_removal" => "send(a, b) -> send(a)",
|
|
26
26
|
"pair_removal" => "{a: 1, b: 2} -> {b: 2}",
|
|
27
|
+
"element_removal" => "TAGS = [a, b] -> TAGS = [b]",
|
|
27
28
|
"endpoint_replacement" => "xs.first -> xs.last",
|
|
28
29
|
"chain_removal" => "x.first.to_s -> x.to_s",
|
|
29
30
|
"range" => "1..3 -> 1...3",
|
|
30
31
|
"operator_assignment" => "x += 1 -> x -= 1",
|
|
31
|
-
"condition_negation" => "x ? a : b -> !(x) ? a : b"
|
|
32
|
+
"condition_negation" => "x ? a : b -> !(x) ? a : b",
|
|
33
|
+
"method_swap" => "xs.min -> xs.max",
|
|
34
|
+
"operand_replacement" => "a + b -> a",
|
|
35
|
+
"interpolation_removal" => "\"hi \#{name}\" -> \"hi \"",
|
|
36
|
+
"regex_replacement" => "/\\A\\d+\\z/ -> /\\d+\\z/"
|
|
32
37
|
}.freeze
|
|
33
38
|
end
|
|
34
39
|
end
|
|
@@ -31,10 +31,15 @@ module Saboteur
|
|
|
31
31
|
"literal_boolean" => "Swap a boolean literal",
|
|
32
32
|
"argument_removal" => "Remove a call argument",
|
|
33
33
|
"pair_removal" => "Remove a hash pair",
|
|
34
|
+
"element_removal" => "Remove an element of a constant array literal",
|
|
34
35
|
"endpoint_replacement" => "Swap explicit no-argument first and last",
|
|
35
|
-
"chain_removal" => "Remove one call from a method chain"
|
|
36
|
+
"chain_removal" => "Remove one call from a method chain",
|
|
37
|
+
"method_swap" => "Swap a method with its semantic opposite",
|
|
38
|
+
"operand_replacement" => "Replace a binary operation with one operand",
|
|
39
|
+
"interpolation_removal" => "Remove part of an interpolated string",
|
|
40
|
+
"regex_replacement" => "Drop a regex anchor or swap a regex quantifier"
|
|
36
41
|
}.freeze
|
|
37
|
-
POLICY_VERSION =
|
|
42
|
+
POLICY_VERSION = 3
|
|
38
43
|
TIERS = { "all" => DESCRIPTIONS.keys.freeze }.freeze
|
|
39
44
|
|
|
40
45
|
def self.list(out, names: [], excluded: [])
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "prism"
|
|
5
|
+
|
|
6
|
+
module Saboteur
|
|
7
|
+
module MutationSource
|
|
8
|
+
# The tree walk shared by the statement-driven operators: visit every
|
|
9
|
+
# node except nested definitions, skip arid statements, and hand each
|
|
10
|
+
# remaining node to the operator's mutate_node hook.
|
|
11
|
+
module OperatorVisit
|
|
12
|
+
SCOPE_NODES = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode].freeze
|
|
13
|
+
|
|
14
|
+
def mutations_for(subject)
|
|
15
|
+
found = []
|
|
16
|
+
visit(subject.body_node, subject, found)
|
|
17
|
+
found
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def visit(node, subject, found)
|
|
23
|
+
return if node.nil? || SCOPE_NODES.any? { |scope| node.is_a?(scope) }
|
|
24
|
+
|
|
25
|
+
if node.is_a?(Prism::StatementsNode)
|
|
26
|
+
node.body.each { |statement| visit(statement, subject, found) unless arid?(statement) }
|
|
27
|
+
return
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
mutate_node(node, subject, found)
|
|
31
|
+
node.compact_child_nodes.each { |child| visit(child, subject, found) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def arid?(node)
|
|
35
|
+
@arid.any? { |pattern| pattern.match?(node.slice) }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -3,11 +3,14 @@
|
|
|
3
3
|
|
|
4
4
|
require "prism"
|
|
5
5
|
require_relative "base"
|
|
6
|
+
require_relative "entry_edits"
|
|
6
7
|
|
|
7
8
|
module Saboteur
|
|
8
9
|
module MutationSource
|
|
9
10
|
# Deletes one association from a hash literal or keyword argument list.
|
|
10
11
|
class PairRemoval < Base
|
|
12
|
+
include EntryEdits
|
|
13
|
+
|
|
11
14
|
BOUNDARIES = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode,
|
|
12
15
|
Prism::ParametersNode, Prism::BlockParametersNode].freeze
|
|
13
16
|
|
|
@@ -19,10 +22,12 @@ module Saboteur
|
|
|
19
22
|
"pair_removal"
|
|
20
23
|
end
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
# root: the node to walk instead of the subject's method body (a
|
|
26
|
+
# constant's literal, see MutationSource::ConstantLiterals).
|
|
27
|
+
def mutations_for(subject, root: nil)
|
|
23
28
|
found = []
|
|
24
29
|
tokens = Prism.lex(subject.file_source).value.map(&:first)
|
|
25
|
-
visit(subject.
|
|
30
|
+
visit(root || subject.body_node, subject, found, tokens)
|
|
26
31
|
found
|
|
27
32
|
end
|
|
28
33
|
|
|
@@ -56,24 +61,16 @@ module Saboteur
|
|
|
56
61
|
next unless pair.is_a?(Prism::AssocNode)
|
|
57
62
|
next if heredoc?(pair)
|
|
58
63
|
|
|
59
|
-
edits = [
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
edits = [delete_entry(subject, pair.location)]
|
|
65
|
+
# Removing the first keyword argument may need the comma after the
|
|
66
|
+
# call's last positional argument.
|
|
67
|
+
before = call.arguments.arguments[0...-1].last if call
|
|
68
|
+
separator = entry_separator(pairs, index, ending_for(node, call), tokens, before: before)
|
|
69
|
+
edits << delete_entry(subject, separator) if separator
|
|
62
70
|
found << Mutation.new(subject_id: subject.id, operator: name, edits: edits, source: name)
|
|
63
71
|
end
|
|
64
72
|
end
|
|
65
73
|
|
|
66
|
-
def separator_for(node, pairs, index, tokens, call)
|
|
67
|
-
pair = pairs[index]
|
|
68
|
-
ending = ending_for(node, call)
|
|
69
|
-
after = comma_between(pair.location.end_offset, ending, tokens)
|
|
70
|
-
return after if after
|
|
71
|
-
|
|
72
|
-
previous = pairs[0...index].last
|
|
73
|
-
previous ||= call.arguments.arguments[0...-1].last if call
|
|
74
|
-
comma_between(previous.location.end_offset, pair.location.start_offset, tokens) if previous
|
|
75
|
-
end
|
|
76
|
-
|
|
77
74
|
def ending_for(node, call)
|
|
78
75
|
return node.closing_loc.start_offset if node.is_a?(Prism::HashNode)
|
|
79
76
|
return call.closing_loc.start_offset if call.closing_loc
|
|
@@ -81,21 +78,6 @@ module Saboteur
|
|
|
81
78
|
|
|
82
79
|
node.location.end_offset
|
|
83
80
|
end
|
|
84
|
-
|
|
85
|
-
def comma_between(first, last, tokens)
|
|
86
|
-
tokens.find do |token|
|
|
87
|
-
token.type == :COMMA && token.location.start_offset >= first && token.location.end_offset <= last
|
|
88
|
-
end&.location
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def heredoc?(node)
|
|
92
|
-
(node.respond_to?(:opening_loc) && node.opening_loc&.slice&.start_with?("<<")) ||
|
|
93
|
-
node.compact_child_nodes.any? { |child| heredoc?(child) }
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
def delete(subject, location)
|
|
97
|
-
Edit.new(file: subject.file, start_byte: location.start_offset, end_byte: location.end_offset, replacement: "")
|
|
98
|
-
end
|
|
99
81
|
end
|
|
100
82
|
end
|
|
101
83
|
end
|