mutineer-mongoid 2.0.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 +7 -0
- data/CHANGELOG.md +1194 -0
- data/LICENSE +21 -0
- data/README.md +659 -0
- data/bin/mutineer +6 -0
- data/lib/mutineer/baseline.rb +185 -0
- data/lib/mutineer/changed_lines.rb +136 -0
- data/lib/mutineer/child_stdout.rb +35 -0
- data/lib/mutineer/cli.rb +681 -0
- data/lib/mutineer/config.rb +372 -0
- data/lib/mutineer/coverage_map.rb +1366 -0
- data/lib/mutineer/daemon_backend.rb +351 -0
- data/lib/mutineer/daemon_client.rb +407 -0
- data/lib/mutineer/daemon_server.rb +351 -0
- data/lib/mutineer/external_backend.rb +288 -0
- data/lib/mutineer/file_swap.rb +194 -0
- data/lib/mutineer/isolation.rb +321 -0
- data/lib/mutineer/job_plan.rb +352 -0
- data/lib/mutineer/kill_channel.rb +284 -0
- data/lib/mutineer/kill_matrix.rb +88 -0
- data/lib/mutineer/minitest_integration/kill_recorder.rb +301 -0
- data/lib/mutineer/minitest_integration/outer_reporter.rb +126 -0
- data/lib/mutineer/minitest_integration/stop_at_first_failure.rb +145 -0
- data/lib/mutineer/minitest_integration.rb +137 -0
- data/lib/mutineer/mongoid_failures.rb +82 -0
- data/lib/mutineer/mongoid_worker_db.rb +97 -0
- data/lib/mutineer/mutant_id.rb +126 -0
- data/lib/mutineer/mutation.rb +27 -0
- data/lib/mutineer/mutator_registry.rb +109 -0
- data/lib/mutineer/mutators/arithmetic.rb +35 -0
- data/lib/mutineer/mutators/array_literal.rb +44 -0
- data/lib/mutineer/mutators/base.rb +105 -0
- data/lib/mutineer/mutators/boolean_connector.rb +52 -0
- data/lib/mutineer/mutators/boolean_literal.rb +56 -0
- data/lib/mutineer/mutators/chain_link.rb +132 -0
- data/lib/mutineer/mutators/collection_method.rb +44 -0
- data/lib/mutineer/mutators/comparison.rb +35 -0
- data/lib/mutineer/mutators/condition_forcing.rb +134 -0
- data/lib/mutineer/mutators/condition_negation.rb +49 -0
- data/lib/mutineer/mutators/literal_mutation.rb +51 -0
- data/lib/mutineer/mutators/negation_removal.rb +44 -0
- data/lib/mutineer/mutators/operand_removal.rb +58 -0
- data/lib/mutineer/mutators/operator_assignment.rb +113 -0
- data/lib/mutineer/mutators/range_literal.rb +54 -0
- data/lib/mutineer/mutators/regex_literal.rb +72 -0
- data/lib/mutineer/mutators/return_nil.rb +72 -0
- data/lib/mutineer/mutators/safe_navigation.rb +76 -0
- data/lib/mutineer/mutators/statement_removal.rb +33 -0
- data/lib/mutineer/mutators/string_literal.rb +34 -0
- data/lib/mutineer/orphan_guard.rb +28 -0
- data/lib/mutineer/pairing.rb +298 -0
- data/lib/mutineer/parser.rb +45 -0
- data/lib/mutineer/progress.rb +46 -0
- data/lib/mutineer/project.rb +506 -0
- data/lib/mutineer/project_path.rb +57 -0
- data/lib/mutineer/rails_worker_db.rb +212 -0
- data/lib/mutineer/reporter.rb +926 -0
- data/lib/mutineer/result.rb +240 -0
- data/lib/mutineer/runner.rb +418 -0
- data/lib/mutineer/statement_lines.rb +183 -0
- data/lib/mutineer/subject.rb +38 -0
- data/lib/mutineer/test_runners/minitest.rb +22 -0
- data/lib/mutineer/test_runners/rspec.rb +330 -0
- data/lib/mutineer/test_runners.rb +30 -0
- data/lib/mutineer/version.rb +6 -0
- data/lib/mutineer/worker_pool.rb +147 -0
- data/lib/mutineer.rb +48 -0
- metadata +143 -0
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
require "set"
|
|
5
|
+
require_relative "parser"
|
|
6
|
+
require_relative "subject"
|
|
7
|
+
|
|
8
|
+
module Mutineer
|
|
9
|
+
# Subject discovery: parse each path and walk its AST for method definitions,
|
|
10
|
+
# tracking the enclosing class/module namespace.
|
|
11
|
+
class Project
|
|
12
|
+
# Discovers subjects from source paths.
|
|
13
|
+
#
|
|
14
|
+
# @param paths [Array<String>] source file paths.
|
|
15
|
+
# @param only [String, nil] optional qualified-name filter.
|
|
16
|
+
# @return [Array<Mutineer::Subject>] discovered subjects.
|
|
17
|
+
def self.discover(paths, only: nil)
|
|
18
|
+
subjects = Array(paths).flat_map do |path|
|
|
19
|
+
result = Parser.parse_file(path)
|
|
20
|
+
visitor = SubjectVisitor.new(path)
|
|
21
|
+
visitor.visit(result.value)
|
|
22
|
+
visitor.promote_module_functions!
|
|
23
|
+
visitor.subjects
|
|
24
|
+
end
|
|
25
|
+
only ? subjects.select { |s| s.qualified_name == only } : subjects
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Walks an AST, maintaining a namespace stack, emitting Subjects.
|
|
29
|
+
# Nested inside Project to signal its private role.
|
|
30
|
+
class SubjectVisitor < Prism::Visitor
|
|
31
|
+
# Calls whose block is the body of the class they build: receiver name => method.
|
|
32
|
+
CLASS_BUILDERS = { "Data" => :define, "Struct" => :new, "Class" => :new, "Module" => :new }.freeze
|
|
33
|
+
|
|
34
|
+
# A constant name, or a path of them; Ruby allows Unicode in both.
|
|
35
|
+
CONSTANT_PATH = /\A[[:upper:]][[:word:]]*(?:::[[:upper:]][[:word:]]*)*\z/
|
|
36
|
+
|
|
37
|
+
# A name that reads like a constant name but may not be one (#216). It is
|
|
38
|
+
# the singleton class name of a `class << self` opened inside a def or a
|
|
39
|
+
# block that does not build a class, or built on a written path or
|
|
40
|
+
# `#<anonymous>` (`#<Class:Foo::X>` from `class Foo::X` inside `class << self`);
|
|
41
|
+
# or the `X` of `module self::X` or `self::X = ...` in a def or a block. The segment carries that mark (see
|
|
42
|
+
# {#singleton_name}, {#with_namespace}, {#assigned_owner}), and `scope`, the
|
|
43
|
+
# def or block whose `self` the name is under, or a new object when unknown.
|
|
44
|
+
class WrittenName < String
|
|
45
|
+
# @return [Object] the def or block node whose `self` the name is under.
|
|
46
|
+
attr_reader :scope
|
|
47
|
+
|
|
48
|
+
# @param name [String] the name as built.
|
|
49
|
+
# @param scope [Object] the def or block node whose `self` the name is under.
|
|
50
|
+
def initialize(name, scope = Object.new)
|
|
51
|
+
super(name)
|
|
52
|
+
@scope = scope
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
attr_reader :subjects
|
|
57
|
+
|
|
58
|
+
# Builds a subject visitor.
|
|
59
|
+
#
|
|
60
|
+
# @param file [String] source file path being visited.
|
|
61
|
+
def initialize(file)
|
|
62
|
+
@file = file
|
|
63
|
+
@namespace_stack = []
|
|
64
|
+
@lexical_stack = [] # class/module names as written, `::X` kept (#145)
|
|
65
|
+
@subjects = []
|
|
66
|
+
@singleton_depth = 0
|
|
67
|
+
@module_function_active = false # bareword `module_function` seen in this module body
|
|
68
|
+
@module_function_names = [] # [module key, name] from `module_function :a` / `module_function def` (#98)
|
|
69
|
+
@subject_keys = [] # the module key of each subject, by index
|
|
70
|
+
@body = Object.new # identifies the class/module body or builder block being visited
|
|
71
|
+
@block_owner = nil
|
|
72
|
+
@block_namespace = nil
|
|
73
|
+
@owner_unknown = false
|
|
74
|
+
@assigned = nil
|
|
75
|
+
@singleton_cref = nil # the singleton class name, in a builder block inside `class << self`
|
|
76
|
+
@namespace_unknown = false # the namespace is under a singleton class (#208) or a block's `self` (#229)
|
|
77
|
+
@anonymous_block = false # inside a builder block not assigned to a constant
|
|
78
|
+
@self_unknown = false # inside a def or a block that is not a builder, so `self` is not the namespace
|
|
79
|
+
@self_scope = nil # the def or block whose `self` the code runs with
|
|
80
|
+
@singleton_uncertain = false # the open `class << self` was inside a def or a non-builder block
|
|
81
|
+
super()
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Promote `module_function :name` / `module_function def name` subjects to
|
|
85
|
+
# singleton after the full walk — the naming call may appear before or after
|
|
86
|
+
# the def, so it can't be decided at visit_def_node time (#20). Only methods
|
|
87
|
+
# of the module that made the call are promoted (#98), matched by {#module_key}.
|
|
88
|
+
#
|
|
89
|
+
# @return [void]
|
|
90
|
+
def promote_module_functions!
|
|
91
|
+
return if @module_function_names.empty?
|
|
92
|
+
|
|
93
|
+
named = @module_function_names.to_set
|
|
94
|
+
@subjects.each_with_index { |s, i| s.singleton = true if named.include?([@subject_keys[i], s.name]) }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Visits class nodes and tracks namespace nesting.
|
|
98
|
+
#
|
|
99
|
+
# @param node [Prism::ClassNode] class node.
|
|
100
|
+
# @return [void]
|
|
101
|
+
def visit_class_node(node)
|
|
102
|
+
with_namespace(node.constant_path) { super }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Visits module nodes and tracks namespace nesting.
|
|
106
|
+
#
|
|
107
|
+
# @param node [Prism::ModuleNode] module node.
|
|
108
|
+
# @return [void]
|
|
109
|
+
def visit_module_node(node)
|
|
110
|
+
with_namespace(node.constant_path) { super }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Track `module_function` so its methods are recorded as singletons (#20) —
|
|
114
|
+
# the called form is the singleton method on the module object. Bareword
|
|
115
|
+
# `module_function` flips all SUBSEQUENT defs in this body; the argument
|
|
116
|
+
# forms (`:sym`, `def`) name methods promoted after the walk. A builder
|
|
117
|
+
# block's defs belong to the class it builds; one not assigned to a
|
|
118
|
+
# constant has no name, so its subjects are marked `owner_unknown`, and a
|
|
119
|
+
# `module_function :name` in it promotes nothing. Elsewhere it promotes the
|
|
120
|
+
# methods of the same module, matched by {#module_key}.
|
|
121
|
+
#
|
|
122
|
+
# @param node [Prism::CallNode] call node.
|
|
123
|
+
# @return [void]
|
|
124
|
+
def visit_call_node(node)
|
|
125
|
+
if %i[public private protected].include?(node.name) && node.receiver.nil? && node.arguments.nil?
|
|
126
|
+
@module_function_active = false # without arguments, Ruby goes back to instance methods
|
|
127
|
+
end
|
|
128
|
+
if node.name == :module_function && node.receiver.nil?
|
|
129
|
+
args = node.arguments&.arguments || []
|
|
130
|
+
if args.empty?
|
|
131
|
+
@module_function_active = true
|
|
132
|
+
elsif !@anonymous_block
|
|
133
|
+
args.each do |arg|
|
|
134
|
+
@module_function_names << [module_key, arg.value.to_sym] if arg.is_a?(Prism::SymbolNode)
|
|
135
|
+
@module_function_names << [module_key, arg.name] if arg.is_a?(Prism::DefNode)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
return super unless builds_class?(node)
|
|
140
|
+
|
|
141
|
+
anonymous = !@assigned&.first.equal?(node)
|
|
142
|
+
with_block_owner(anonymous ? [nil, @namespace_stack, true] : @assigned.last, anonymous) do
|
|
143
|
+
@self_scope = node.block
|
|
144
|
+
super
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# A block that does not build a class may run with any `self` (`class_eval`,
|
|
149
|
+
# `instance_eval`, a callback), so a `class << self` in it names a singleton
|
|
150
|
+
# class Mutineer cannot be sure of (#216). A lambda is treated the same.
|
|
151
|
+
#
|
|
152
|
+
# @param node [Prism::BlockNode, Prism::LambdaNode] block or lambda node.
|
|
153
|
+
# @return [void]
|
|
154
|
+
def visit_block_node(node)
|
|
155
|
+
return super if node.equal?(@self_scope) # a builder block
|
|
156
|
+
|
|
157
|
+
saved_self = [@self_unknown, @self_scope]
|
|
158
|
+
@self_unknown = true
|
|
159
|
+
@self_scope = node
|
|
160
|
+
super
|
|
161
|
+
@self_unknown, @self_scope = saved_self
|
|
162
|
+
end
|
|
163
|
+
alias visit_lambda_node visit_block_node
|
|
164
|
+
|
|
165
|
+
# Names the class a builder block assigned to this constant builds (see {#builds_class?}).
|
|
166
|
+
#
|
|
167
|
+
# `=`, `||=` and `&&=` store the class, so each names it; `+=` stores what `+`
|
|
168
|
+
# returns, so its builder is left unnamed.
|
|
169
|
+
#
|
|
170
|
+
# @param node [Prism::ConstantWriteNode, Prism::ConstantPathWriteNode, Prism::ConstantOrWriteNode,
|
|
171
|
+
# Prism::ConstantAndWriteNode, Prism::ConstantPathOrWriteNode, Prism::ConstantPathAndWriteNode]
|
|
172
|
+
# constant assignment.
|
|
173
|
+
# @return [void]
|
|
174
|
+
def visit_constant_write_node(node)
|
|
175
|
+
call = assigned_value(node.value)
|
|
176
|
+
return super unless builds_class?(call)
|
|
177
|
+
|
|
178
|
+
saved = @assigned
|
|
179
|
+
@assigned = [call, assigned_owner(node)]
|
|
180
|
+
begin
|
|
181
|
+
super
|
|
182
|
+
ensure
|
|
183
|
+
@assigned = saved
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
alias visit_constant_path_write_node visit_constant_write_node
|
|
187
|
+
alias visit_constant_or_write_node visit_constant_write_node
|
|
188
|
+
alias visit_constant_and_write_node visit_constant_write_node
|
|
189
|
+
alias visit_constant_path_or_write_node visit_constant_write_node
|
|
190
|
+
alias visit_constant_path_and_write_node visit_constant_write_node
|
|
191
|
+
|
|
192
|
+
# Methods inside `class << self` are class methods of the enclosing
|
|
193
|
+
# namespace, but their def nodes have no receiver — track the singleton
|
|
194
|
+
# context so they're recorded as singleton (so redefine targets the
|
|
195
|
+
# singleton_class, not instances). `class << some_other_obj` can't be
|
|
196
|
+
# represented against the namespace, so its defs are skipped (not recursed).
|
|
197
|
+
#
|
|
198
|
+
# @param node [Prism::SingletonClassNode] singleton-class node.
|
|
199
|
+
# @return [void]
|
|
200
|
+
def visit_singleton_class_node(node)
|
|
201
|
+
return unless node.expression.is_a?(Prism::SelfNode)
|
|
202
|
+
|
|
203
|
+
@singleton_depth += 1
|
|
204
|
+
saved_active = @module_function_active
|
|
205
|
+
saved_uncertain = @singleton_uncertain
|
|
206
|
+
@singleton_uncertain ||= @self_unknown # `self` here may be another object (#216)
|
|
207
|
+
super
|
|
208
|
+
@module_function_active = saved_active # a visibility call in here is not the module body's
|
|
209
|
+
@singleton_uncertain = saved_uncertain
|
|
210
|
+
@singleton_depth -= 1
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Records a discovered method definition.
|
|
214
|
+
#
|
|
215
|
+
# @param node [Prism::DefNode] method definition node.
|
|
216
|
+
# @return [void]
|
|
217
|
+
def visit_def_node(node)
|
|
218
|
+
@subjects << Subject.new(
|
|
219
|
+
file: @file,
|
|
220
|
+
namespace: (@block_namespace || @namespace_stack).dup,
|
|
221
|
+
lexical: @lexical_stack.dup,
|
|
222
|
+
block_owner: @block_owner,
|
|
223
|
+
owner_unknown: @owner_unknown,
|
|
224
|
+
name: node.name,
|
|
225
|
+
singleton: !node.receiver.nil? || @singleton_depth.positive? || @module_function_active,
|
|
226
|
+
def_node: node
|
|
227
|
+
)
|
|
228
|
+
@subject_keys << module_key
|
|
229
|
+
saved_active = @module_function_active
|
|
230
|
+
saved_self = [@self_unknown, @self_scope]
|
|
231
|
+
@self_unknown = true # `self` in a method body is the receiver it is called on
|
|
232
|
+
@self_scope = node
|
|
233
|
+
super
|
|
234
|
+
@module_function_active = saved_active # a visibility call in a method body runs only when it is called
|
|
235
|
+
@self_unknown, @self_scope = saved_self
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
private
|
|
239
|
+
|
|
240
|
+
# The module a `module_function` call and a def are matched by: its joined
|
|
241
|
+
# namespace, so a module reopened later in the file matches (#216), and
|
|
242
|
+
# `module A::B` matches nested `module A; module B`. An unknown owner that an
|
|
243
|
+
# anonymous builder block owns, or whose name has no {WrittenName} segment
|
|
244
|
+
# and is not built only from constant names (see {#named_segment?}), may be
|
|
245
|
+
# shared by another module, so it is matched only within this body (#208).
|
|
246
|
+
# Otherwise a name with a {WrittenName}
|
|
247
|
+
# segment is matched by that segment's scope and text, so openings under one
|
|
248
|
+
# `self` match each other, but not those under another, whether the owner is
|
|
249
|
+
# known or not (#229).
|
|
250
|
+
#
|
|
251
|
+
# @return [String, Object]
|
|
252
|
+
def module_key
|
|
253
|
+
namespace = @block_namespace || @namespace_stack
|
|
254
|
+
written = written?(namespace)
|
|
255
|
+
return @body if @owner_unknown && (@anonymous_block || !written && !namespace.all? { |s| named_segment?(s) })
|
|
256
|
+
return namespace.join("::") unless written
|
|
257
|
+
|
|
258
|
+
namespace.map { |s| s.is_a?(WrittenName) ? "#{s.scope.object_id}:#{s}" : s }.join("::")
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# True when any segment of the namespace is a {WrittenName}.
|
|
262
|
+
#
|
|
263
|
+
# @param namespace [Array<String>] namespace segments.
|
|
264
|
+
# @return [Boolean]
|
|
265
|
+
def written?(namespace)
|
|
266
|
+
namespace.any?(WrittenName)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# True when a namespace segment is a constant name, or a singleton class
|
|
270
|
+
# (`#<Class:App::M>`) of constant names. A written path or `#<anonymous>` is
|
|
271
|
+
# not, nor is a singleton class name built on one ({WrittenName}).
|
|
272
|
+
#
|
|
273
|
+
# @param segment [String] namespace segment.
|
|
274
|
+
# @return [Boolean]
|
|
275
|
+
def named_segment?(segment)
|
|
276
|
+
!segment.is_a?(WrittenName) && segment.gsub(/#<Class:|>/, "").match?(CONSTANT_PATH) &&
|
|
277
|
+
(segment.start_with?("#<Class:") || !segment.include?("::"))
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# Runs the block with `path` pushed as the current namespace. A
|
|
281
|
+
# root-anchored path (`module ::X` / `class ::X`) names the top-level X,
|
|
282
|
+
# not X nested in the enclosing scope, so the namespace restarts there.
|
|
283
|
+
# Bareword `module_function` state does not cross a class or module
|
|
284
|
+
# boundary: each body starts without it, and the outer state returns after.
|
|
285
|
+
# A class or module `X` opened inside `class << self`, even within a builder
|
|
286
|
+
# block there, is a constant of the singleton class, which has no constant
|
|
287
|
+
# path. It and everything nested in it is named under `#<Class:...>` with
|
|
288
|
+
# its owner unknown (#208), and its body defines instance methods again.
|
|
289
|
+
# A compact `Foo::X` or a top-level `::X` there is named as written, and
|
|
290
|
+
# its owner is unknown too: redefine reopens the lexical chain without the
|
|
291
|
+
# singleton class, so constants the body looks up through it would not resolve.
|
|
292
|
+
# A `self::X` in any block is under the block's `self`, not the namespace
|
|
293
|
+
# around the block (#229). In a builder block that is the built class, so
|
|
294
|
+
# it is named under the builder's constant; in any other block (`class_eval`,
|
|
295
|
+
# a callback) `self` may be any object, so it is named under the innermost
|
|
296
|
+
# enclosing builder's constant, or else the enclosing namespace. Either way
|
|
297
|
+
# the last segment is a {WrittenName}, and it and everything nested in it
|
|
298
|
+
# has its owner unknown. Redefine reopens the lexical chain (`module Outer;
|
|
299
|
+
# module X`), which never reaches the block's `self`; reopening X through
|
|
300
|
+
# its constant with `class_eval`, as for `self::X = ...` in a builder block,
|
|
301
|
+
# would drop X from `Module.nesting`, so constants its body looks up would not resolve.
|
|
302
|
+
#
|
|
303
|
+
# @param path [Prism::Node] the class/module constant path.
|
|
304
|
+
# @yield the class or module body visit.
|
|
305
|
+
# @return [void]
|
|
306
|
+
def with_namespace(path)
|
|
307
|
+
preserving_scope do
|
|
308
|
+
name = extract_constant_name(path)
|
|
309
|
+
root = root_anchored?(path)
|
|
310
|
+
in_singleton = !@singleton_cref.nil? || @singleton_depth.positive?
|
|
311
|
+
self_in_block = !in_singleton && !root && self_rooted?(path) && (!@block_namespace.nil? || @self_unknown)
|
|
312
|
+
@namespace_stack =
|
|
313
|
+
if root then [name]
|
|
314
|
+
elsif in_singleton then path.is_a?(Prism::ConstantPathNode) ? [path.slice] : [singleton_name, name]
|
|
315
|
+
elsif self_in_block then (@block_namespace || @namespace_stack) + [WrittenName.new(name, @self_scope)]
|
|
316
|
+
else @namespace_stack + [name]
|
|
317
|
+
end
|
|
318
|
+
@lexical_stack += [root ? "::#{name}" : name]
|
|
319
|
+
@module_function_active = false
|
|
320
|
+
@block_owner = @block_namespace = nil
|
|
321
|
+
# redefine reopens the lexical chain, which does not pass through the block's `self`
|
|
322
|
+
@namespace_unknown = @owner_unknown = in_singleton || self_in_block || @namespace_unknown
|
|
323
|
+
@singleton_depth = 0
|
|
324
|
+
@singleton_cref = nil
|
|
325
|
+
@anonymous_block = @self_unknown = @singleton_uncertain = false
|
|
326
|
+
@body = Object.new
|
|
327
|
+
yield
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# Visitor state a class/module body or builder block sets for what it contains.
|
|
332
|
+
SCOPE_STATE = %i[@namespace_stack @lexical_stack @module_function_active @block_owner @block_namespace
|
|
333
|
+
@owner_unknown @singleton_depth @singleton_cref @namespace_unknown @anonymous_block @body
|
|
334
|
+
@self_unknown @self_scope @singleton_uncertain].freeze
|
|
335
|
+
|
|
336
|
+
# Runs the block and then restores every {SCOPE_STATE} variable, so a
|
|
337
|
+
# nested body's state never leaks out of it.
|
|
338
|
+
#
|
|
339
|
+
# @yield the nested visit.
|
|
340
|
+
# @return [void]
|
|
341
|
+
def preserving_scope
|
|
342
|
+
saved = SCOPE_STATE.map { |name| instance_variable_get(name) }
|
|
343
|
+
yield
|
|
344
|
+
ensure
|
|
345
|
+
SCOPE_STATE.zip(saved) { |name, value| instance_variable_set(name, value) }
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# Resolves the constant an assignment writes the way Ruby does. `X` is in the
|
|
349
|
+
# current namespace, `::X` and a path at the top level start from Object, and
|
|
350
|
+
# `self::X` is under the current class (the built class inside a builder
|
|
351
|
+
# block). In a block that does not build a class, `self` may be any object,
|
|
352
|
+
# so `self::X` there has its owner unknown (#229). Any other path is
|
|
353
|
+
# looked up at run time, so its owner is unknown
|
|
354
|
+
# and the subject is named as written. Lexically inside `class << self`,
|
|
355
|
+
# even within a builder block there, the constant belongs to the singleton
|
|
356
|
+
# class, which has no constant path, so its owner is unknown too, as is
|
|
357
|
+
# `X` or `self::X` in a class or module opened there (#208). A `::X` there
|
|
358
|
+
# is named `X`, still unknown (see {#with_namespace}).
|
|
359
|
+
#
|
|
360
|
+
# @param node [Prism::Node] constant assignment.
|
|
361
|
+
# @return [Array(String, Array<String>, Boolean)] owner, namespace, unknown.
|
|
362
|
+
def assigned_owner(node)
|
|
363
|
+
if @singleton_cref || @singleton_depth.positive?
|
|
364
|
+
written = node.respond_to?(:target) ? node.target.slice : node.name.to_s
|
|
365
|
+
return [nil, written.start_with?("::") ? [written.delete_prefix("::")] : [singleton_name, written], true]
|
|
366
|
+
end
|
|
367
|
+
unless node.respond_to?(:target)
|
|
368
|
+
namespace = @namespace_stack + [node.name.to_s]
|
|
369
|
+
return @namespace_unknown ? [nil, namespace, true] : named_owner(namespace)
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
names = []
|
|
373
|
+
path = node.target
|
|
374
|
+
while path.is_a?(Prism::ConstantPathNode)
|
|
375
|
+
names.unshift(path.name.to_s)
|
|
376
|
+
path = path.parent
|
|
377
|
+
end
|
|
378
|
+
# see {#with_namespace}; in a builder block too, so a later `module self::X` there matches it
|
|
379
|
+
if path.is_a?(Prism::SelfNode) && (@self_unknown || @block_namespace)
|
|
380
|
+
names[0] = WrittenName.new(names[0], @self_scope)
|
|
381
|
+
end
|
|
382
|
+
if path.is_a?(Prism::SelfNode) && (@self_unknown || @namespace_unknown && !@anonymous_block)
|
|
383
|
+
return [nil, (@block_namespace || @namespace_stack) + names, true]
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
base =
|
|
387
|
+
case path
|
|
388
|
+
when nil then []
|
|
389
|
+
when Prism::SelfNode then @block_namespace || @namespace_stack unless @owner_unknown
|
|
390
|
+
when Prism::ConstantReadNode then [path.name.to_s] if @namespace_stack.empty?
|
|
391
|
+
end
|
|
392
|
+
return [nil, [node.target.slice], true] unless base
|
|
393
|
+
|
|
394
|
+
@namespace_unknown ? [nil, base + names, true] : named_owner(base + names)
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
# Names the singleton class that owns the constants written here: `class << self`
|
|
398
|
+
# opens that of the current class (the built class inside a builder block),
|
|
399
|
+
# and a builder block inside `class << self` keeps the enclosing one. A block
|
|
400
|
+
# not assigned to a constant builds a class with no name, written `#<anonymous>`.
|
|
401
|
+
# Each nested `class << self` opens the singleton class of the one around it.
|
|
402
|
+
# A name built on a written path or `#<anonymous>`, or opened where `self` may
|
|
403
|
+
# be another object (in a def or a non-builder block), is a {WrittenName} (see {#module_key}).
|
|
404
|
+
#
|
|
405
|
+
# @return [String] e.g. `#<Class:App>`, or `#<Class:#<Class:App>>` two deep.
|
|
406
|
+
def singleton_name
|
|
407
|
+
return @singleton_cref unless @singleton_depth.positive?
|
|
408
|
+
|
|
409
|
+
base = @anonymous_block ? @namespace_stack + ["#<anonymous>"] : @block_namespace || @namespace_stack
|
|
410
|
+
name = @singleton_depth.times.reduce(base.join("::")) { |inner, _| "#<Class:#{inner}>" }
|
|
411
|
+
named = !@singleton_uncertain && (@owner_unknown ? base.all? { |s| named_segment?(s) } : !written?(base))
|
|
412
|
+
named ? name : WrittenName.new(name)
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
# The owner for a resolved namespace, root-anchored so the redefine
|
|
416
|
+
# wrapper loads onto that constant from any nesting.
|
|
417
|
+
#
|
|
418
|
+
# @param namespace [Array<String>] resolved namespace.
|
|
419
|
+
# @return [Array(String, Array<String>, Boolean)]
|
|
420
|
+
def named_owner(namespace)
|
|
421
|
+
["::#{namespace.join("::")}", namespace, false]
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
# The value an assignment stores: the last statement inside parentheses or a
|
|
425
|
+
# `begin` without `rescue`, which Ruby returns from them.
|
|
426
|
+
#
|
|
427
|
+
# @param node [Prism::Node] assigned expression.
|
|
428
|
+
# @return [Prism::Node, nil]
|
|
429
|
+
def assigned_value(node)
|
|
430
|
+
loop do
|
|
431
|
+
body =
|
|
432
|
+
case node
|
|
433
|
+
when Prism::ParenthesesNode then node.body
|
|
434
|
+
when Prism::BeginNode then node.statements unless node.rescue_clause
|
|
435
|
+
end
|
|
436
|
+
return node unless body
|
|
437
|
+
|
|
438
|
+
node = body.is_a?(Prism::StatementsNode) ? body.body.last : body
|
|
439
|
+
end
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
# True when the node is `Data.define`, `Struct.new`, `Class.new` or `Module.new` with a block.
|
|
443
|
+
#
|
|
444
|
+
# @param node [Prism::Node, nil] node.
|
|
445
|
+
# @return [Boolean]
|
|
446
|
+
def builds_class?(node)
|
|
447
|
+
node.is_a?(Prism::CallNode) && node.block.is_a?(Prism::BlockNode) &&
|
|
448
|
+
CLASS_BUILDERS[node.receiver&.slice&.delete_prefix("::")] == node.name
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# Runs the block with the defs it visits owned by the class a builder block builds.
|
|
452
|
+
# The block body defines instance methods of that class, even inside `class << self`,
|
|
453
|
+
# but its constants still land where the enclosing `class << self` puts them.
|
|
454
|
+
#
|
|
455
|
+
# @param owner [Array(String, Array<String>, Boolean)] the owner as written, its namespace,
|
|
456
|
+
# and whether that name is unknown.
|
|
457
|
+
# @param anonymous [Boolean] true when the block is not assigned to a constant.
|
|
458
|
+
# @yield the builder call visit.
|
|
459
|
+
# @return [void]
|
|
460
|
+
def with_block_owner(owner, anonymous)
|
|
461
|
+
preserving_scope do
|
|
462
|
+
@singleton_cref = singleton_name if @singleton_depth.positive?
|
|
463
|
+
@block_owner, @block_namespace, @owner_unknown = owner
|
|
464
|
+
@anonymous_block = anonymous
|
|
465
|
+
@self_unknown = @singleton_uncertain = false
|
|
466
|
+
@module_function_active = false
|
|
467
|
+
@singleton_depth = 0
|
|
468
|
+
@body = Object.new
|
|
469
|
+
yield
|
|
470
|
+
end
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
# True when a constant path starts with `self` (e.g. `self::X`).
|
|
474
|
+
#
|
|
475
|
+
# @param node [Prism::Node] constant path node.
|
|
476
|
+
# @return [Boolean]
|
|
477
|
+
def self_rooted?(node)
|
|
478
|
+
node = node.parent while node.is_a?(Prism::ConstantPathNode)
|
|
479
|
+
node.is_a?(Prism::SelfNode)
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
# True when a constant path starts with `::` (e.g. `::X` or `::A::B`).
|
|
483
|
+
#
|
|
484
|
+
# @param node [Prism::Node] constant path node.
|
|
485
|
+
# @return [Boolean]
|
|
486
|
+
def root_anchored?(node)
|
|
487
|
+
node = node.parent while node.is_a?(Prism::ConstantPathNode) && node.parent
|
|
488
|
+
node.is_a?(Prism::ConstantPathNode)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
# Extracts a constant name from a Prism constant node.
|
|
492
|
+
#
|
|
493
|
+
# @api private
|
|
494
|
+
# @param node [Prism::Node] constant node.
|
|
495
|
+
# @return [String, nil] constant name.
|
|
496
|
+
def extract_constant_name(node)
|
|
497
|
+
case node
|
|
498
|
+
when Prism::ConstantReadNode
|
|
499
|
+
node.name.to_s
|
|
500
|
+
when Prism::ConstantPathNode
|
|
501
|
+
[extract_constant_name(node.parent), node.name.to_s].compact.join("::")
|
|
502
|
+
end
|
|
503
|
+
end
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mutineer
|
|
4
|
+
# Realpath-based path normalization against a project root. Shared by the
|
|
5
|
+
# coverage cache (map keys) and mutant ids (the path hashed into each id), so
|
|
6
|
+
# `lib/x.rb`, `./lib/x.rb`, an absolute path and a path through a symlinked
|
|
7
|
+
# root all resolve to the same key. Realpaths on both sides also absorb the
|
|
8
|
+
# macOS `/var` vs `/private/var` alias.
|
|
9
|
+
module ProjectPath
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
# Path of `path` relative to the real project root. A path outside the root
|
|
13
|
+
# comes back as its absolute real path.
|
|
14
|
+
#
|
|
15
|
+
# @param path [String] relative (to `root`) or absolute path.
|
|
16
|
+
# @param root [String] project root.
|
|
17
|
+
# @return [String] root-relative path, or an absolute path when outside `root`.
|
|
18
|
+
def relative(path, root)
|
|
19
|
+
abs = absolute(path, root)
|
|
20
|
+
real_root = root_real(root)
|
|
21
|
+
prefix = real_root.end_with?("/") ? real_root : "#{real_root}/"
|
|
22
|
+
return abs unless abs.start_with?(prefix)
|
|
23
|
+
|
|
24
|
+
abs.delete_prefix(prefix)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Expands `path` against `root`, resolved to its real path when it exists.
|
|
28
|
+
#
|
|
29
|
+
# @param path [String] relative (to `root`) or absolute path.
|
|
30
|
+
# @param root [String] project root.
|
|
31
|
+
# @return [String] absolute path.
|
|
32
|
+
def absolute(path, root)
|
|
33
|
+
# Join, don't expand: File.expand_path collapses `..` textually, before any
|
|
34
|
+
# symlink is followed, so `link/../x.rb` would name the wrong file. The file
|
|
35
|
+
# system resolves `..` physically in File.realpath.
|
|
36
|
+
# A leading `~` names the home directory (as File.expand_path reads it), so
|
|
37
|
+
# expand it rather than joining it under the root.
|
|
38
|
+
raw = if File.absolute_path?(path) then path
|
|
39
|
+
elsif path.start_with?("~") then File.expand_path(path)
|
|
40
|
+
else File.join(File.expand_path(root), path)
|
|
41
|
+
end
|
|
42
|
+
File.exist?(raw) ? File.realpath(raw) : File.expand_path(raw)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Canonical project root (`/var` vs `/private/var`). Any file system error
|
|
46
|
+
# (missing, unreadable, a symlink loop) falls back to the expanded path, so
|
|
47
|
+
# a caller such as the CLI's run-root warning never crashes on it.
|
|
48
|
+
#
|
|
49
|
+
# @param root [String] project root.
|
|
50
|
+
# @return [String] realpath of the root when it exists, else its expanded path.
|
|
51
|
+
def root_real(root)
|
|
52
|
+
File.realpath(File.expand_path(root))
|
|
53
|
+
rescue SystemCallError
|
|
54
|
+
File.expand_path(root)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|