polyrun 3.0.1 → 3.1.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 +12 -0
- data/README.md +4 -2
- data/docs/QUALITY.md +80 -0
- data/docs/QUALITY_CONFIGURATION.md +95 -0
- data/lib/polyrun/cli/ci_shard_run_command.rb +1 -1
- data/lib/polyrun/cli/graph_command.rb +68 -0
- data/lib/polyrun/cli/help.rb +5 -1
- data/lib/polyrun/cli/helpers.rb +3 -2
- data/lib/polyrun/cli/mutation_command.rb +67 -0
- data/lib/polyrun/cli/quality_commands.rb +65 -0
- data/lib/polyrun/cli/run_shards_plan_options.rb +1 -1
- data/lib/polyrun/cli/tool_options.rb +48 -0
- data/lib/polyrun/cli.rb +17 -1
- data/lib/polyrun/config/effective.rb +9 -0
- data/lib/polyrun/config/resolver.rb +9 -0
- data/lib/polyrun/config/schema.rb +8 -3
- data/lib/polyrun/config/suite.rb +6 -1
- data/lib/polyrun/config/tool_defaults.rb +42 -0
- data/lib/polyrun/config/tool_validation.rb +79 -0
- data/lib/polyrun/config/tools.rb +120 -0
- data/lib/polyrun/config.rb +6 -4
- data/lib/polyrun/graph/client.rb +64 -0
- data/lib/polyrun/graph/error.rb +13 -0
- data/lib/polyrun/graph/index.rb +75 -0
- data/lib/polyrun/graph/inspect.rb +58 -0
- data/lib/polyrun/graph/plan.rb +134 -0
- data/lib/polyrun/graph/planner.rb +117 -0
- data/lib/polyrun/graph/syntax.rb +114 -0
- data/lib/polyrun/graph/transaction.rb +81 -0
- data/lib/polyrun/graph/worker.rb +55 -0
- data/lib/polyrun/graph/workspace.rb +117 -0
- data/lib/polyrun/minitest_support.rb +1 -1
- data/lib/polyrun/mutation/candidates.rb +91 -0
- data/lib/polyrun/mutation/operators.rb +51 -0
- data/lib/polyrun/mutation/runner.rb +131 -0
- data/lib/polyrun/mutation/subjects.rb +88 -0
- data/lib/polyrun/quality/command.rb +84 -0
- data/lib/polyrun/quality/measure.rb +38 -0
- data/lib/polyrun/quality/observe.rb +53 -0
- data/lib/polyrun/quality/observe_hook.rb +103 -0
- data/lib/polyrun/quality/sandbox.rb +70 -0
- data/lib/polyrun/rspec_support.rb +1 -1
- data/lib/polyrun/spec_quality/config.rb +8 -3
- data/lib/polyrun/spec_quality.rb +3 -4
- data/lib/polyrun/version.rb +1 -1
- data/polyrun.gemspec +1 -1
- data/sig/polyrun/config.rbs +2 -2
- metadata +29 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
require "securerandom"
|
|
2
|
+
require "yaml"
|
|
3
|
+
require_relative "index"
|
|
4
|
+
require_relative "plan"
|
|
5
|
+
require_relative "../quality/sandbox"
|
|
6
|
+
|
|
7
|
+
module Polyrun
|
|
8
|
+
module Graph
|
|
9
|
+
class Planner
|
|
10
|
+
def initialize(root, settings: {})
|
|
11
|
+
@workspace = Workspace.new(root, settings: settings)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call(change_path)
|
|
15
|
+
change = load_change(change_path)
|
|
16
|
+
index = fresh_index(change)
|
|
17
|
+
operations = change["operations"]
|
|
18
|
+
raise Error.new("INVALID_PLAN", "operations must be a nonempty array") unless operations.is_a?(Array) && (1..@workspace.settings["max_operations"]).cover?(operations.length)
|
|
19
|
+
|
|
20
|
+
replacements = prepare(operations, index)
|
|
21
|
+
id = "plan-#{SecureRandom.hex(12)}"
|
|
22
|
+
plan = Plan.new(@workspace.root, id, settings: @workspace.settings)
|
|
23
|
+
stage_plan(plan, replacements)
|
|
24
|
+
save_plan(plan, id, index, operations)
|
|
25
|
+
hashes = index.fetch("files")
|
|
26
|
+
{"ok" => true, "plan_id" => id, "diff" => diff(plan, hashes)}
|
|
27
|
+
rescue JSON::ParserError, Psych::Exception, KeyError, TypeError => error
|
|
28
|
+
raise Error.new("INVALID_PLAN", error.message)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def stage_plan(plan, replacements)
|
|
34
|
+
Quality::Sandbox.new(@workspace.root, settings: @workspace.settings["staging"].merge("exclude_paths" => @workspace.settings["staging"]["exclude_paths"] + [@workspace.state])).copy_to(plan.tree)
|
|
35
|
+
write_edits(plan, replacements)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def save_plan(plan, id, index, operations)
|
|
39
|
+
hashes = index.fetch("files")
|
|
40
|
+
staged = hashes.keys.to_h { |relative| [relative, Digest::SHA256.file(@workspace.path(relative, base: plan.tree)).hexdigest] }
|
|
41
|
+
@workspace.write_json("#{@workspace.state}/plans/#{id}/meta.json", {"id" => id, "snapshot" => index["snapshot"],
|
|
42
|
+
"pre_hashes" => hashes, "staged_hashes" => staged, "operations" => operations}, limit: @workspace.settings["max_plan_bytes"])
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def load_change(change_path)
|
|
46
|
+
file = File.expand_path(change_path, @workspace.root)
|
|
47
|
+
raise Error.new("RESOURCE_LIMIT", "change description is too large") if File.size(file) > @workspace.settings["max_input_bytes"]
|
|
48
|
+
|
|
49
|
+
raw = File.read(file)
|
|
50
|
+
raw.lstrip.start_with?("{") ? JSON.parse(raw) : YAML.safe_load(raw)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def fresh_index(change)
|
|
54
|
+
index = Index.new(@workspace.root, settings: @workspace.settings).load
|
|
55
|
+
hashes = @workspace.hashes
|
|
56
|
+
unless change.is_a?(Hash) && change["snapshot"] == index["snapshot"] && hashes == index["files"]
|
|
57
|
+
raise Error.new("STALE_SNAPSHOT", "index the current source before planning changes")
|
|
58
|
+
end
|
|
59
|
+
index
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def write_edits(plan, replacements)
|
|
63
|
+
replacements.group_by { |operation| operation.fetch("path") }.each do |relative, edits|
|
|
64
|
+
path = @workspace.path(relative, base: plan.tree)
|
|
65
|
+
source = File.binread(path)
|
|
66
|
+
edits.sort_by { |edit| -edit.fetch("byte_start") }.each do |edit|
|
|
67
|
+
source[edit["byte_start"]...edit["byte_end"]] = edit["replacement"]
|
|
68
|
+
end
|
|
69
|
+
raise Error.new("PARSE_FAILED", "replacement is not valid Ruby") if Prism.parse(source).failure?
|
|
70
|
+
|
|
71
|
+
File.binwrite(path, source)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def prepare(operations, index)
|
|
76
|
+
nodes = index["calls"].to_h { |node| [node["id"], node] }
|
|
77
|
+
seen = {}
|
|
78
|
+
operations.map do |operation|
|
|
79
|
+
raise Error.new("INVALID_PLAN", "unsupported graph operation") unless operation.is_a?(Hash) && operation["op"] == "replace_call_name"
|
|
80
|
+
|
|
81
|
+
node = nodes[operation["node"]]
|
|
82
|
+
raise Error.new("UNKNOWN_TARGET", "call site missing") unless node
|
|
83
|
+
raise Error.new("INVALID_PLAN", "duplicate call-site operation") if seen[node["id"]]
|
|
84
|
+
|
|
85
|
+
seen[node["id"]] = true
|
|
86
|
+
validate_edit!(operation, node)
|
|
87
|
+
node.merge("replacement" => operation["replacement"].b)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def validate_edit!(operation, node)
|
|
92
|
+
raise Error.new("EXPECT_MISMATCH", "call-site fingerprint changed") unless operation["expect"] == node.slice("name", "fingerprint")
|
|
93
|
+
|
|
94
|
+
replacement = operation["replacement"]
|
|
95
|
+
unless replacement.is_a?(String) && replacement.match?(/\A[[:alpha:]_][[:alnum:]_]*[!?=]?\z/)
|
|
96
|
+
raise Error.new("INVALID_PLAN", "replacement must be a method name")
|
|
97
|
+
end
|
|
98
|
+
original = File.binread(@workspace.path(node["path"])).byteslice(node["byte_start"]...node["byte_end"])
|
|
99
|
+
raise Error.new("EXPECT_MISMATCH", "call-site bytes changed or unsupported target") unless original == node["name"].b
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def diff(plan, hashes)
|
|
103
|
+
text = hashes.keys.filter_map do |relative|
|
|
104
|
+
before = File.binread(@workspace.path(relative))
|
|
105
|
+
after = File.binread(@workspace.path(relative, base: plan.tree))
|
|
106
|
+
next if before == after
|
|
107
|
+
|
|
108
|
+
"--- a/#{relative}\n+++ b/#{relative}\n@@ -1,#{before.lines.size} +1,#{after.lines.size} @@\n" +
|
|
109
|
+
before.lines.map { |line| "-#{line.chomp}\n" }.join + after.lines.map { |line| "+#{line.chomp}\n" }.join
|
|
110
|
+
end.join
|
|
111
|
+
raise Error.new("RESOURCE_LIMIT", "plan diff is too large") if text.bytesize > @workspace.settings["max_diff_bytes"]
|
|
112
|
+
|
|
113
|
+
text
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
require "prism"
|
|
2
|
+
require "digest"
|
|
3
|
+
require_relative "error"
|
|
4
|
+
require_relative "../config/tool_defaults"
|
|
5
|
+
|
|
6
|
+
module Polyrun
|
|
7
|
+
module Graph
|
|
8
|
+
class Syntax < Prism::Visitor
|
|
9
|
+
attr_reader :methods, :calls
|
|
10
|
+
|
|
11
|
+
def initialize(path, source, max_nodes: Config::ToolDefaults::COMMON.fetch("max_nodes"))
|
|
12
|
+
@max_nodes = max_nodes
|
|
13
|
+
@path = path
|
|
14
|
+
@source = source
|
|
15
|
+
@methods = []
|
|
16
|
+
@calls = []
|
|
17
|
+
@owner = "Object"
|
|
18
|
+
@singleton = false
|
|
19
|
+
@method = nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def parse
|
|
23
|
+
parsed = Prism.parse(@source)
|
|
24
|
+
raise Error.new("PARSE_FAILED", "invalid Ruby: #{@path}") if parsed.failure?
|
|
25
|
+
|
|
26
|
+
parsed.value.accept(self)
|
|
27
|
+
self
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def visit_class_node(node)
|
|
31
|
+
scope(node.constant_path) { node.body&.accept(self) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def visit_module_node(node)
|
|
35
|
+
scope(node.constant_path) { node.body&.accept(self) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def visit_singleton_class_node(node)
|
|
39
|
+
previous = [@owner, @singleton]
|
|
40
|
+
@owner = constant_name(node.expression) unless node.expression.is_a?(Prism::SelfNode)
|
|
41
|
+
@singleton = true
|
|
42
|
+
node.body&.accept(self)
|
|
43
|
+
ensure
|
|
44
|
+
@owner, @singleton = previous
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def visit_def_node(node)
|
|
48
|
+
previous = @method
|
|
49
|
+
owner = (node.receiver && !node.receiver.is_a?(Prism::SelfNode)) ? constant_name(node.receiver) : @owner
|
|
50
|
+
singleton = @singleton || !node.receiver.nil?
|
|
51
|
+
@method = entry("method", node.name.to_s, node.location).merge("owner" => owner,
|
|
52
|
+
"scope" => singleton ? "singleton" : "instance", "subject" => owner && "#{owner}#{singleton ? "." : "#"}#{node.name}",
|
|
53
|
+
"body_start" => node.body&.location&.start_offset, "body_end" => node.body&.location&.end_offset)
|
|
54
|
+
@methods << @method
|
|
55
|
+
super
|
|
56
|
+
ensure
|
|
57
|
+
@method = previous
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def visit_call_node(node)
|
|
61
|
+
if node.message_loc
|
|
62
|
+
receiver = if node.receiver.nil?
|
|
63
|
+
nil
|
|
64
|
+
elsif node.receiver.is_a?(Prism::SelfNode)
|
|
65
|
+
"self"
|
|
66
|
+
else
|
|
67
|
+
constant_name(node.receiver) || "dynamic"
|
|
68
|
+
end
|
|
69
|
+
@calls << entry("call", node.name.to_s, node.message_loc).merge("method_id" => @method&.fetch("id"),
|
|
70
|
+
"owner" => @method ? @method["owner"] : @owner, "scope" => @method ? @method["scope"] : "singleton", "receiver" => receiver)
|
|
71
|
+
end
|
|
72
|
+
super
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def scope(node)
|
|
78
|
+
previous = [@owner, @singleton, @method]
|
|
79
|
+
name = constant_name(node)
|
|
80
|
+
@owner = if name.nil? || @owner.nil?
|
|
81
|
+
nil
|
|
82
|
+
elsif name.start_with?("::") || @owner == "Object"
|
|
83
|
+
name.delete_prefix("::")
|
|
84
|
+
elsif name.include?("::")
|
|
85
|
+
nil
|
|
86
|
+
else
|
|
87
|
+
[@owner, name].compact.join("::")
|
|
88
|
+
end
|
|
89
|
+
@singleton = false
|
|
90
|
+
@method = nil
|
|
91
|
+
yield
|
|
92
|
+
ensure
|
|
93
|
+
@owner, @singleton, @method = previous
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def constant_name(node)
|
|
97
|
+
node.full_name if node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
|
|
98
|
+
rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def entry(kind, name, location)
|
|
103
|
+
raise Error.new("RESOURCE_LIMIT", "too many graph nodes") if @methods.size + @calls.size >= @max_nodes
|
|
104
|
+
|
|
105
|
+
bytes = @source.byteslice(location.start_offset, location.length)
|
|
106
|
+
fingerprint = Digest::SHA256.hexdigest(bytes)
|
|
107
|
+
identity = Digest::SHA256.hexdigest([kind, @path, location.start_offset, fingerprint].join("\0"))
|
|
108
|
+
{"id" => "#{kind}-#{identity[0, 24]}", "kind" => kind, "name" => name, "path" => @path,
|
|
109
|
+
"line" => location.start_line, "byte_start" => location.start_offset, "byte_end" => location.end_offset,
|
|
110
|
+
"fingerprint" => fingerprint, "provenance" => "syntax"}
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
require_relative "workspace"
|
|
2
|
+
|
|
3
|
+
module Polyrun
|
|
4
|
+
module Graph
|
|
5
|
+
class Transaction
|
|
6
|
+
def initialize(root, directory, settings: {})
|
|
7
|
+
@workspace = Workspace.new(root, settings: settings)
|
|
8
|
+
relative = File.realpath(directory).delete_prefix("#{@workspace.root}/")
|
|
9
|
+
@directory = @workspace.path(relative)
|
|
10
|
+
@journal = "#{relative}/transaction.json"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def recover!
|
|
14
|
+
return false unless File.file?(@workspace.path(@journal))
|
|
15
|
+
|
|
16
|
+
journal = @workspace.read_json(@journal)
|
|
17
|
+
return false unless %w[prepared writing restoring].include?(journal["state"])
|
|
18
|
+
|
|
19
|
+
save(journal.merge("state" => "restoring"))
|
|
20
|
+
journal.fetch("files").reverse_each do |relative, hashes|
|
|
21
|
+
source = @workspace.path(relative)
|
|
22
|
+
backup = @workspace.path("backups/#{relative}", base: @directory)
|
|
23
|
+
unless File.file?(source) && File.file?(backup) && Digest::SHA256.file(backup).hexdigest == hashes["before"]
|
|
24
|
+
raise Error.new("RECOVERY_REQUIRED", "source recovery needs a valid original: #{relative}")
|
|
25
|
+
end
|
|
26
|
+
restore_source(source, backup, hashes, relative)
|
|
27
|
+
end
|
|
28
|
+
save(journal.merge("state" => "restored"))
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def commit(replacements)
|
|
33
|
+
files = replacements.to_h do |relative, before, after|
|
|
34
|
+
backup = @workspace.path("backups/#{relative}", base: @directory)
|
|
35
|
+
FileUtils.mkdir_p(File.dirname(backup))
|
|
36
|
+
File.binwrite(backup, before)
|
|
37
|
+
[relative, {"before" => Digest::SHA256.hexdigest(before), "after" => Digest::SHA256.hexdigest(after)}]
|
|
38
|
+
end
|
|
39
|
+
journal = {"state" => "prepared", "files" => files}
|
|
40
|
+
save(journal)
|
|
41
|
+
save(journal.merge("state" => "writing"))
|
|
42
|
+
replacements.each do |relative, before, after|
|
|
43
|
+
source = @workspace.path(relative)
|
|
44
|
+
raise Error.new("STALE_SNAPSHOT", "source changed during apply") unless File.binread(source) == before
|
|
45
|
+
|
|
46
|
+
write_source(source, after)
|
|
47
|
+
end
|
|
48
|
+
save(journal.merge("state" => "applied"))
|
|
49
|
+
true
|
|
50
|
+
rescue Exception # rubocop:disable Lint/RescueException -- rollback also covers cooperative cancellation
|
|
51
|
+
recover!
|
|
52
|
+
raise
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def restore_source(source, backup, hashes, relative)
|
|
58
|
+
current = Digest::SHA256.file(source).hexdigest
|
|
59
|
+
raise Error.new("RECOVERY_REQUIRED", "source changed outside graph apply: #{relative}") unless [hashes["before"], hashes["after"]].include?(current)
|
|
60
|
+
|
|
61
|
+
write_source(source, File.binread(backup)) unless current == hashes["before"]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def save(journal)
|
|
65
|
+
@workspace.write_json(@journal, journal)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def write_source(path, bytes)
|
|
69
|
+
mode = File.stat(path).mode & 0o777
|
|
70
|
+
Tempfile.create("polyrun-apply", File.dirname(path)) do |file|
|
|
71
|
+
file.chmod(mode)
|
|
72
|
+
file.binmode
|
|
73
|
+
file.write(bytes)
|
|
74
|
+
file.flush
|
|
75
|
+
file.fsync
|
|
76
|
+
File.rename(file.path, path)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require_relative "planner"
|
|
3
|
+
require_relative "inspect"
|
|
4
|
+
|
|
5
|
+
module Polyrun
|
|
6
|
+
module Graph
|
|
7
|
+
module Worker
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def run(arguments)
|
|
11
|
+
root = ENV.fetch("POLYRUN_GRAPH_ROOT", Dir.pwd)
|
|
12
|
+
json = arguments.include?("--json")
|
|
13
|
+
arguments = arguments.reject { |argument| argument == "--json" }
|
|
14
|
+
settings = JSON.parse(ENV.delete("POLYRUN_GRAPH_SETTINGS") || "{}")
|
|
15
|
+
result = dispatch(root, arguments, settings)
|
|
16
|
+
puts(json ? JSON.generate(result) : text_result(arguments.first, result))
|
|
17
|
+
0
|
|
18
|
+
rescue Error => error
|
|
19
|
+
warn(json ? JSON.generate("ok" => false, "code" => error.code, "message" => error.message) : "#{error.code}: #{error.message}")
|
|
20
|
+
error.exitstatus
|
|
21
|
+
rescue SystemCallError, ArgumentError, KeyError, JSON::ParserError => error
|
|
22
|
+
warn(json ? JSON.generate("ok" => false, "code" => "INVALID_ARGUMENT", "message" => error.message) : error.message)
|
|
23
|
+
2
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def dispatch(root, arguments, settings)
|
|
27
|
+
verb, argument, *flags = arguments
|
|
28
|
+
index = Index.new(root, settings: settings)
|
|
29
|
+
case verb
|
|
30
|
+
when "index"
|
|
31
|
+
graph = index.call
|
|
32
|
+
{"ok" => true, "snapshot" => graph["snapshot"], "nodes" => graph["methods"].size + graph["calls"].size, "reused" => graph["reused"]}
|
|
33
|
+
when "inspect" then Inspect.new(index.load).call(argument, flags + (argument&.start_with?("--") ? [argument] : []))
|
|
34
|
+
when "plan" then Planner.new(root, settings: settings).call(argument)
|
|
35
|
+
when "check" then Plan.new(root, argument, settings: settings).check!
|
|
36
|
+
when "apply"
|
|
37
|
+
Plan.new(root, argument, settings: settings).apply!
|
|
38
|
+
{"ok" => true, "plan_id" => argument, "applied" => true, "snapshot" => index.call["snapshot"]}
|
|
39
|
+
else raise Error.new("INVALID_ARGUMENT", "unknown graph command")
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def text_result(verb, result)
|
|
44
|
+
case verb
|
|
45
|
+
when "index" then "indexed #{result["snapshot"]} nodes=#{result["nodes"]} reused=#{result["reused"]}"
|
|
46
|
+
when "plan" then "plan #{result["plan_id"]}\n#{result["diff"]}"
|
|
47
|
+
when "check" then "checked plan: #{result["status"]}"
|
|
48
|
+
when "apply" then "applied #{result["plan_id"]}"
|
|
49
|
+
else JSON.pretty_generate(result)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
private_class_method :dispatch, :text_result
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
require "digest"
|
|
2
|
+
require "fileutils"
|
|
3
|
+
require "find"
|
|
4
|
+
require "json"
|
|
5
|
+
require "tempfile"
|
|
6
|
+
require_relative "error"
|
|
7
|
+
require_relative "../config/tools"
|
|
8
|
+
|
|
9
|
+
module Polyrun
|
|
10
|
+
module Graph
|
|
11
|
+
class Workspace
|
|
12
|
+
STATE = Config::ToolDefaults::GRAPH.fetch("state_dir")
|
|
13
|
+
attr_reader :root, :settings
|
|
14
|
+
|
|
15
|
+
def state
|
|
16
|
+
@settings.fetch("state_dir", STATE)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initialize(root, settings: {})
|
|
20
|
+
@settings = Config::Tools.runtime("graph", settings)
|
|
21
|
+
@root = File.realpath(root)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def path(relative, base: @root)
|
|
25
|
+
parts = relative.is_a?(String) ? relative.split("/", -1) : []
|
|
26
|
+
if parts.empty? || parts.any? { |part| part.empty? || part == "." || part == ".." || part.include?("\0") }
|
|
27
|
+
raise Error.new("INVALID_PLAN", "invalid project path")
|
|
28
|
+
end
|
|
29
|
+
parts.reduce(base) do |parent, part|
|
|
30
|
+
result = File.join(parent, part)
|
|
31
|
+
raise Error.new("INVALID_PLAN", "symbolic link in project path") if File.symlink?(result)
|
|
32
|
+
|
|
33
|
+
result
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def files
|
|
38
|
+
found = source_directories.flat_map { |directory| source_files(directory) }.uniq.sort
|
|
39
|
+
raise Error.new("RESOURCE_LIMIT", "too many source files") if found.size > @settings["max_source_files"]
|
|
40
|
+
|
|
41
|
+
found
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def hashes
|
|
45
|
+
total = 0
|
|
46
|
+
files.to_h do |relative|
|
|
47
|
+
absolute = path(relative)
|
|
48
|
+
total += File.size(absolute)
|
|
49
|
+
raise Error.new("RESOURCE_LIMIT", "source file is too large") if File.size(absolute) > @settings["max_source_file_bytes"]
|
|
50
|
+
raise Error.new("RESOURCE_LIMIT", "source tree exceeds configured byte limit") if total > @settings["max_source_bytes"]
|
|
51
|
+
|
|
52
|
+
[relative, Digest::SHA256.file(absolute).hexdigest]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def snapshot(hashes)
|
|
57
|
+
"snapshot-#{Digest::SHA256.hexdigest(JSON.generate(hashes))}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def read_json(relative, limit: @settings["max_artifact_bytes"])
|
|
61
|
+
file = path(relative)
|
|
62
|
+
raise Error.new("MISSING_STATE", "graph state missing; run polyrun graph index") unless File.file?(file)
|
|
63
|
+
raise Error.new("RESOURCE_LIMIT", "graph state is too large") if File.size(file) > limit
|
|
64
|
+
|
|
65
|
+
JSON.parse(File.binread(file))
|
|
66
|
+
rescue JSON::ParserError
|
|
67
|
+
raise Error.new("INVALID_PLAN", "invalid graph data")
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def write_json(relative, value, limit: @settings["max_artifact_bytes"])
|
|
71
|
+
destination = path(relative)
|
|
72
|
+
payload = JSON.generate(value)
|
|
73
|
+
raise Error.new("RESOURCE_LIMIT", "graph state is too large") if payload.bytesize > limit
|
|
74
|
+
|
|
75
|
+
FileUtils.mkdir_p(File.dirname(destination))
|
|
76
|
+
Tempfile.create("polyrun-graph", File.dirname(destination)) do |file|
|
|
77
|
+
file.write(payload)
|
|
78
|
+
file.flush
|
|
79
|
+
file.fsync
|
|
80
|
+
File.rename(file.path, destination)
|
|
81
|
+
end
|
|
82
|
+
value
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def source_directories
|
|
88
|
+
roots = @settings["source_roots"].map { |name| (name == ".") ? @root : path(name) }.select { |directory| File.directory?(directory) }
|
|
89
|
+
roots = [@root] if roots.empty? && @settings["source_fallback"]
|
|
90
|
+
roots
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def source_files(directory)
|
|
94
|
+
found = []
|
|
95
|
+
Find.find(directory) do |entry|
|
|
96
|
+
relative = entry.delete_prefix("#{@root}/")
|
|
97
|
+
if entry != directory && (relative == state || excluded?(relative))
|
|
98
|
+
Find.prune if File.directory?(entry)
|
|
99
|
+
next
|
|
100
|
+
end
|
|
101
|
+
if File.symlink?(entry)
|
|
102
|
+
raise Error.new("INVALID_PLAN", "symbolic link in source tree")
|
|
103
|
+
elsif File.file?(entry) && entry.end_with?(".rb")
|
|
104
|
+
found << relative
|
|
105
|
+
raise Error.new("RESOURCE_LIMIT", "too many source files") if found.length > @settings["max_source_files"]
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
found
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def excluded?(relative)
|
|
112
|
+
name = File.basename(relative)
|
|
113
|
+
name.start_with?(".") || @settings["source_exclude"].any? { |pattern| File.fnmatch?(pattern, name) || File.fnmatch?(pattern, relative, File::FNM_PATHNAME) }
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -50,7 +50,7 @@ module Polyrun
|
|
|
50
50
|
|
|
51
51
|
# Per-test spec quality when +POLYRUN_SPEC_QUALITY=1+ (requires stdlib +Coverage+ for line deltas).
|
|
52
52
|
def install_spec_quality!(only_if: nil, root: nil, output_path: nil)
|
|
53
|
-
pred = only_if || -> { Polyrun::SpecQuality.enabled? }
|
|
53
|
+
pred = only_if || -> { Polyrun::SpecQuality.enabled?(root: root || Dir.pwd) }
|
|
54
54
|
return unless pred.call
|
|
55
55
|
|
|
56
56
|
require_relative "spec_quality/minitest_hook"
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require "prism"
|
|
2
|
+
require "digest"
|
|
3
|
+
require_relative "../graph/syntax"
|
|
4
|
+
require_relative "../graph/workspace"
|
|
5
|
+
require_relative "operators"
|
|
6
|
+
|
|
7
|
+
module Polyrun
|
|
8
|
+
module Mutation
|
|
9
|
+
class Candidates
|
|
10
|
+
def initialize(root, operators: nil, limit: nil, settings: {})
|
|
11
|
+
settings = Config::Tools.runtime("mutation", settings)
|
|
12
|
+
graph = settings.slice(*Config::ToolDefaults.for("graph").keys)
|
|
13
|
+
@workspace = Graph::Workspace.new(root, settings: graph)
|
|
14
|
+
@operators = operators || Operators.select(settings["operators"])
|
|
15
|
+
@limit = limit || settings["max_mutations"]
|
|
16
|
+
@count = 0
|
|
17
|
+
@seen = {}
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call(subjects)
|
|
21
|
+
inventory = source_inventory
|
|
22
|
+
subjects.flat_map do |subject|
|
|
23
|
+
matches = select_methods(inventory, subject)
|
|
24
|
+
matches.flat_map { |path, source, method| generate(path, source, method) }
|
|
25
|
+
end.uniq { |mutation| [mutation["path"], mutation["source_hash"]] }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def source_inventory
|
|
31
|
+
nodes = 0
|
|
32
|
+
@workspace.hashes.keys.map do |relative|
|
|
33
|
+
source = File.binread(@workspace.path(relative))
|
|
34
|
+
syntax = Graph::Syntax.new(relative, source, max_nodes: @workspace.settings["max_nodes"]).parse
|
|
35
|
+
nodes += syntax.methods.size + syntax.calls.size
|
|
36
|
+
raise ArgumentError, "mutation source exceeds configured node limit" if nodes > @workspace.settings["max_nodes"]
|
|
37
|
+
|
|
38
|
+
[relative, source, syntax.methods]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def select_methods(inventory, subject)
|
|
43
|
+
matches = inventory.flat_map { |path, source, methods| methods.filter_map { |method| [path, source, method] if match?(subject, method["subject"]) } }
|
|
44
|
+
raise ArgumentError, "unknown mutation subject: #{subject}" if matches.empty?
|
|
45
|
+
names = matches.map { |_, _, method| method["subject"] }
|
|
46
|
+
raise ArgumentError, "ambiguous mutation subject: #{subject}" unless names.uniq.length == names.length
|
|
47
|
+
|
|
48
|
+
matches
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def match?(selector, subject)
|
|
52
|
+
subject && (selector == subject || (selector.include?("*") && File.fnmatch?(selector, subject)))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def generate(path, source, method)
|
|
56
|
+
parsed = Prism.parse(source)
|
|
57
|
+
nodes = [parsed.value]
|
|
58
|
+
edits = []
|
|
59
|
+
until nodes.empty?
|
|
60
|
+
node = nodes.pop
|
|
61
|
+
location = node.location
|
|
62
|
+
if location.start_offset >= method["byte_start"] && location.end_offset <= method["byte_end"]
|
|
63
|
+
@operators.each { |operator| edits.concat(operator.call(node)) }
|
|
64
|
+
end
|
|
65
|
+
nodes.concat(node.compact_child_nodes)
|
|
66
|
+
end
|
|
67
|
+
edits.filter_map do |location, replacement, operator|
|
|
68
|
+
next if source.byteslice(location.start_offset, location.length) == replacement
|
|
69
|
+
|
|
70
|
+
changed = source.byteslice(0, location.start_offset) + replacement.b + source.byteslice(location.end_offset..)
|
|
71
|
+
next if Prism.parse(changed).failure?
|
|
72
|
+
|
|
73
|
+
build_candidate(path, source, changed, method, location, replacement, operator)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def build_candidate(path, source, changed, method, location, replacement, operator)
|
|
78
|
+
digest = Digest::SHA256.hexdigest(changed)
|
|
79
|
+
return if @seen[[path, digest]]
|
|
80
|
+
|
|
81
|
+
@seen[[path, digest]] = true
|
|
82
|
+
@count += 1
|
|
83
|
+
raise ArgumentError, "mutation limit exceeded; narrow subjects or raise --max-mutations" if @count > @limit
|
|
84
|
+
|
|
85
|
+
{"id" => "mutation-#{Digest::SHA256.hexdigest(path + digest)[0, 24]}", "subject" => method["subject"],
|
|
86
|
+
"path" => path, "line" => location.start_line, "operator" => operator, "start" => location.start_offset,
|
|
87
|
+
"end" => location.end_offset, "replacement" => replacement, "original_hash" => Digest::SHA256.hexdigest(source), "source_hash" => digest}
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module Polyrun
|
|
2
|
+
module Mutation
|
|
3
|
+
module Operators
|
|
4
|
+
class Literal
|
|
5
|
+
def call(node)
|
|
6
|
+
replacements = case node
|
|
7
|
+
when Prism::TrueNode then ["false"]
|
|
8
|
+
when Prism::FalseNode then ["true"]
|
|
9
|
+
when Prism::NilNode then ["true"]
|
|
10
|
+
when Prism::IntegerNode then ["0", "1", (node.value + 1).to_s]
|
|
11
|
+
when Prism::FloatNode then ["0.0", "1.0"]
|
|
12
|
+
when Prism::StringNode, Prism::InterpolatedStringNode then ['""']
|
|
13
|
+
when Prism::ArrayNode then ["[]"]
|
|
14
|
+
when Prism::HashNode then ["{}"]
|
|
15
|
+
else []
|
|
16
|
+
end
|
|
17
|
+
replacements.map { |value| [node.location, value, "literal"] }
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class Operator
|
|
22
|
+
REPLACEMENTS = {"==" => "!=", "!=" => "==", ">" => "<=", ">=" => "<", "<" => ">=", "<=" => ">",
|
|
23
|
+
"+" => "-", "-" => "+", "*" => "/", "/" => "*", "&" => "|", "|" => "&"}.freeze
|
|
24
|
+
|
|
25
|
+
def call(node)
|
|
26
|
+
return [] unless node.is_a?(Prism::CallNode) && node.message_loc
|
|
27
|
+
|
|
28
|
+
replacement = REPLACEMENTS[node.name.to_s]
|
|
29
|
+
replacement ? [[node.message_loc, replacement, "operator"]] : []
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Body
|
|
34
|
+
def call(node)
|
|
35
|
+
return [] unless node.is_a?(Prism::DefNode)
|
|
36
|
+
|
|
37
|
+
return %w[false true 0].map { |value| [node.end_keyword_loc.copy(length: 0), ";#{value};", "body"] } unless node.body
|
|
38
|
+
|
|
39
|
+
%w[nil false true 0].map { |value| [node.body.location, value, "body"] }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
REGISTRY = {"literal" => Literal.new, "operator" => Operator.new, "body" => Body.new}.freeze
|
|
44
|
+
STANDARD = REGISTRY.values.freeze
|
|
45
|
+
|
|
46
|
+
def self.select(names)
|
|
47
|
+
names.map { |name| REGISTRY.fetch(name) }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|