ripr 0.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +146 -0
- data/exe/ripr +6 -0
- data/lib/ripr/accept_ledger.rb +57 -0
- data/lib/ripr/cli.rb +160 -0
- data/lib/ripr/config.rb +102 -0
- data/lib/ripr/core/analyzer.rb +179 -0
- data/lib/ripr/core/classifier.rb +18 -0
- data/lib/ripr/core/instrumenter.rb +110 -0
- data/lib/ripr/core/operators.rb +41 -0
- data/lib/ripr/core/reuse_planner.rb +20 -0
- data/lib/ripr/engine/baseline_cache.rb +95 -0
- data/lib/ripr/engine/child.rb +103 -0
- data/lib/ripr/engine/result_cache.rb +83 -0
- data/lib/ripr/engine/runner.rb +224 -0
- data/lib/ripr/engine/scheduler.rb +81 -0
- data/lib/ripr/git.rb +58 -0
- data/lib/ripr/integration/minitest.rb +72 -0
- data/lib/ripr/integration/rspec.rb +88 -0
- data/lib/ripr/loader.rb +165 -0
- data/lib/ripr/reporters.rb +139 -0
- data/lib/ripr/runtime.rb +205 -0
- data/lib/ripr/snapshot.rb +41 -0
- data/lib/ripr/version.rb +5 -0
- data/lib/ripr.rb +8 -0
- data/schema/agent-v1.json +74 -0
- data/skills/mutation-test-loop/SKILL.md +28 -0
- metadata +94 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "prism"
|
|
5
|
+
require_relative "operators"
|
|
6
|
+
|
|
7
|
+
module Ripr
|
|
8
|
+
module Core
|
|
9
|
+
class Analyzer
|
|
10
|
+
OPERATORS = Operators::NAMES
|
|
11
|
+
|
|
12
|
+
def self.call(source, path, operators: OPERATORS, arid_callees: %w[logger.* puts pp warn])
|
|
13
|
+
new(source, path, operators, arid_callees).call
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(source, path, operators, arid_callees)
|
|
17
|
+
@source = source
|
|
18
|
+
@path = path
|
|
19
|
+
@operators = operators
|
|
20
|
+
@arid_callees = arid_callees
|
|
21
|
+
@sites = []
|
|
22
|
+
@mutants = []
|
|
23
|
+
@warnings = []
|
|
24
|
+
@subjects = Hash.new(0)
|
|
25
|
+
@static = 0
|
|
26
|
+
@disabled = {}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def call
|
|
30
|
+
parsed = Prism.parse(@source)
|
|
31
|
+
if parsed.failure?
|
|
32
|
+
@warnings << "#{@path}: #{parsed.errors.map(&:message).join('; ')}"
|
|
33
|
+
else
|
|
34
|
+
parsed.comments.each { |comment| disable(comment) }
|
|
35
|
+
discover(parsed.value, [], false)
|
|
36
|
+
end
|
|
37
|
+
{ sites: @sites, mutants: @mutants, warnings: @warnings, static: @static }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def children(node)
|
|
43
|
+
node.deconstruct_keys(nil).flat_map do |field, value|
|
|
44
|
+
if value.is_a?(Prism::Node)
|
|
45
|
+
[[field.to_s, value]]
|
|
46
|
+
elsif value.is_a?(Array)
|
|
47
|
+
value.each_with_index.filter_map { |child, index| ["#{field}.#{index}", child] if child.is_a?(Prism::Node) }
|
|
48
|
+
else
|
|
49
|
+
[]
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def discover(node, namespace, singleton)
|
|
55
|
+
return if unsupported?(node)
|
|
56
|
+
|
|
57
|
+
case node
|
|
58
|
+
when Prism::ClassNode, Prism::ModuleNode
|
|
59
|
+
name = node.constant_path.location.slice
|
|
60
|
+
namespace = name.start_with?("::") ? [name.delete_prefix("::")] : [*namespace, name]
|
|
61
|
+
discover(node.body, namespace, false) if node.body
|
|
62
|
+
when Prism::SingletonClassNode
|
|
63
|
+
discover(node.body, namespace, true) if node.body
|
|
64
|
+
when Prism::DefNode
|
|
65
|
+
if node.receiver && !node.receiver.is_a?(Prism::SelfNode)
|
|
66
|
+
@warnings << "#{@path}:#{node.location.start_line}: skipped unsupported singleton receiver"
|
|
67
|
+
return
|
|
68
|
+
end
|
|
69
|
+
owner = namespace.empty? ? "Object" : namespace.join("::")
|
|
70
|
+
separator = node.receiver || singleton ? "." : "#"
|
|
71
|
+
subject = "#{owner}#{separator}#{node.name}"
|
|
72
|
+
@subjects[subject] += 1
|
|
73
|
+
subject += "[#{@subjects[subject]}]" if @subjects[subject] > 1
|
|
74
|
+
context = { subject: subject, method_hash: Digest::SHA256.hexdigest(node.location.slice),
|
|
75
|
+
method_start: node.location.start_line, method_end: node.location.end_line }
|
|
76
|
+
walk(node.body, "body", context, 0) if node.body
|
|
77
|
+
last = node.body.body.last if node.body.is_a?(Prism::StatementsNode)
|
|
78
|
+
add(last, "body.return", "return_nil", "nil", "value", last.location, context) if last && !control?(last)
|
|
79
|
+
else
|
|
80
|
+
@static += Operators.for(node).length
|
|
81
|
+
children(node).each { |entry| discover(entry[1], namespace, singleton) }
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def walk(node, ast_path, context, depth)
|
|
86
|
+
return if unsupported?(node)
|
|
87
|
+
# defined? inspects its operand's syntax without evaluating it.
|
|
88
|
+
return if node.is_a?(Prism::DefinedNode)
|
|
89
|
+
|
|
90
|
+
return discover(node, context[:subject].split(/[.#]/).first.split("::"), false) if node.is_a?(Prism::DefNode)
|
|
91
|
+
|
|
92
|
+
Operators.for(node).each do |operator, replacement, mode, location|
|
|
93
|
+
next if operator == "boolean_connector" && depth >= 3
|
|
94
|
+
|
|
95
|
+
add(node, ast_path, operator, replacement, mode, location, context)
|
|
96
|
+
end
|
|
97
|
+
if [Prism::IfNode, Prism::UnlessNode, Prism::WhileNode, Prism::UntilNode].any? { |klass| node.is_a?(klass) }
|
|
98
|
+
predicate = node.predicate
|
|
99
|
+
add(predicate, "#{ast_path}.predicate", "condition_negation", "!", "always", predicate.location, context)
|
|
100
|
+
end
|
|
101
|
+
if node.is_a?(Prism::StatementsNode)
|
|
102
|
+
node.body[0...-1].each_with_index do |statement, index|
|
|
103
|
+
next if control?(statement) || arid?(statement)
|
|
104
|
+
|
|
105
|
+
add(statement, "#{ast_path}.body.#{index}", "statement_removal", "nil", "unknown", statement.location, context)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
next_depth = depth + (node.is_a?(Prism::AndNode) || node.is_a?(Prism::OrNode) ? 1 : 0)
|
|
109
|
+
children(node).each { |field, child| walk(child, "#{ast_path}.#{field}", context, next_depth) }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def control?(node)
|
|
113
|
+
%i[return_node break_node next_node redo_node retry_node def_node class_node module_node].include?(node.type) ||
|
|
114
|
+
(node.is_a?(Prism::CallNode) && %i[raise fail].include?(node.name))
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def unsupported?(node)
|
|
118
|
+
reason = if node.is_a?(Prism::CallNode) && node.name == :define_method && node.block
|
|
119
|
+
"define_method block"
|
|
120
|
+
elsif node.is_a?(Prism::SingletonClassNode) && !node.expression.is_a?(Prism::SelfNode)
|
|
121
|
+
"singleton receiver"
|
|
122
|
+
end
|
|
123
|
+
return false unless reason
|
|
124
|
+
|
|
125
|
+
@warnings << "#{@path}:#{node.location.start_line}: skipped unsupported #{reason}"
|
|
126
|
+
true
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def arid?(node)
|
|
130
|
+
return true if node.is_a?(Prism::CallNode) && @arid_callees.any? do |pattern|
|
|
131
|
+
callee = node.receiver ? "#{node.receiver.location.slice}.#{node.name}" : node.name.to_s
|
|
132
|
+
File.fnmatch?(pattern, callee)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
node.compact_child_nodes.any? { |child| arid?(child) }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def disable(comment)
|
|
139
|
+
match = comment.location.slice.match(/#\s*ripr:disable-(line|next-line)\s*(.*?)\s*(?:--\s*(.*))?$/)
|
|
140
|
+
return unless match
|
|
141
|
+
|
|
142
|
+
line = comment.location.start_line + (match[1] == "next-line" ? 1 : 0)
|
|
143
|
+
@disabled[line] = [match[2].split(/[\s,]+/), match[3] || "inline disable"]
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def add(node, ast_path, operator, replacement, mode, location, context)
|
|
147
|
+
return unless @operators.include?(operator)
|
|
148
|
+
|
|
149
|
+
key = "#{@path}::#{context.fetch(:subject)}::#{ast_path}::#{operator}"
|
|
150
|
+
full_id = "#{key}::#{replacement}"
|
|
151
|
+
original = node.location.slice
|
|
152
|
+
mutated = case operator
|
|
153
|
+
when "condition_negation" then "!(#{original})"
|
|
154
|
+
when "return_nil" then "(#{original}; nil)"
|
|
155
|
+
when "statement_removal" then "nil"
|
|
156
|
+
else
|
|
157
|
+
offset = location.start_offset - node.location.start_offset
|
|
158
|
+
original.byteslice(0, offset) + replacement + original.byteslice((offset + location.length)..)
|
|
159
|
+
end
|
|
160
|
+
reason = @disabled[node.location.start_line]
|
|
161
|
+
accepted = reason && (reason[0].empty? || reason[0].include?(operator)) ? reason[1] : nil
|
|
162
|
+
mutant = context.merge(id: Digest::SHA256.hexdigest(full_id)[0, 10], full_id: full_id, site: key,
|
|
163
|
+
operator: operator, replacement: replacement, mode: mode, original: original, mutated: mutated,
|
|
164
|
+
accepted: accepted, location: { file: @path, line: node.location.start_line,
|
|
165
|
+
column: node.location.start_column + 1, end_line: node.location.end_line,
|
|
166
|
+
end_column: node.location.end_column + 1 })
|
|
167
|
+
candidate = @source.byteslice(0, node.location.start_offset) + mutated + @source.byteslice(node.location.end_offset..)
|
|
168
|
+
mutant[:invalid] = true if Prism.parse(candidate).failure?
|
|
169
|
+
@mutants << mutant
|
|
170
|
+
site = @sites.find { |entry| entry[:key] == key }
|
|
171
|
+
unless site
|
|
172
|
+
site = { key: key, node: node, operator: operator, variants: [], pure: !@source.match?(/\b(using|refine)\b/) }
|
|
173
|
+
@sites << site
|
|
174
|
+
end
|
|
175
|
+
site[:variants] << mutant
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ripr
|
|
4
|
+
module Core
|
|
5
|
+
module Classifier
|
|
6
|
+
SURVIVORS = %w[not_reached not_infected not_detected].freeze
|
|
7
|
+
STATUSES = %w[killed timeout error not_reached not_infected not_detected accepted static invalid].freeze
|
|
8
|
+
|
|
9
|
+
def self.summary(results)
|
|
10
|
+
counts = STATUSES.to_h { |status| [status, results.count { |result| result["status"] == status }] }
|
|
11
|
+
counts["survived"] = SURVIVORS.sum { |status| counts.fetch(status) }
|
|
12
|
+
detected = counts.fetch("killed") + counts.fetch("timeout")
|
|
13
|
+
total = detected + counts.fetch("survived") + counts.fetch("error")
|
|
14
|
+
counts.merge("score" => total.zero? ? 100.0 : (detected * 100.0 / total).round(1))
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
module Ripr
|
|
6
|
+
module Core
|
|
7
|
+
class Instrumenter
|
|
8
|
+
def self.call(source, path, sites)
|
|
9
|
+
new(source, path, sites).call
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def initialize(source, path, sites)
|
|
13
|
+
@source = source
|
|
14
|
+
@path = path
|
|
15
|
+
@sites = sites.reject { |site| site[:variants].all? { |variant| variant[:invalid] || variant[:accepted] } }
|
|
16
|
+
@prefix = "__ripr"
|
|
17
|
+
@prefix += "_" while @source.include?(@prefix)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call
|
|
21
|
+
selected = isolate(@sites, [])
|
|
22
|
+
code = render(selected)
|
|
23
|
+
selected = selected.select { |site| @emitted.include?(site[:key]) }
|
|
24
|
+
rejected = @sites - selected
|
|
25
|
+
{ code: code, site_table: selected.to_h { |site| [site[:key], site[:variants].map { |variant| variant[:id] }] },
|
|
26
|
+
invalid: rejected.flat_map { |s| s[:variants].map { |v| v[:id] } },
|
|
27
|
+
warnings: rejected.empty? ? [] : ["#{@path}: excluded #{rejected.length} unsupported instrumentation sites"] }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def render(sites)
|
|
33
|
+
@emitted = []
|
|
34
|
+
@by_node = sites.group_by { |site| [site[:node].location.start_offset, site[:node].location.end_offset, site[:node].type] }
|
|
35
|
+
root = Prism.parse(@source).value
|
|
36
|
+
# Prism excludes __END__ data and final whitespace from its root location.
|
|
37
|
+
@source.byteslice(0, root.location.start_offset) + rewrite(root) + @source.byteslice(root.location.end_offset..)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def isolate(candidates, accepted)
|
|
41
|
+
code = render(accepted + candidates)
|
|
42
|
+
return accepted + candidates if code.count("\n") == @source.count("\n") && Prism.parse(code).success?
|
|
43
|
+
return accepted if candidates.length <= 1
|
|
44
|
+
|
|
45
|
+
middle = candidates.length / 2
|
|
46
|
+
isolate(candidates.drop(middle), isolate(candidates.take(middle), accepted))
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def rewrite(node)
|
|
50
|
+
location = node.location
|
|
51
|
+
cursor = location.start_offset
|
|
52
|
+
rendered = {}
|
|
53
|
+
text = +""
|
|
54
|
+
node.compact_child_nodes.sort_by { |child| child.location.start_offset }.each do |child|
|
|
55
|
+
child_location = child.location
|
|
56
|
+
next if child_location.start_offset < cursor || child_location.end_offset > location.end_offset
|
|
57
|
+
|
|
58
|
+
text << @source.byteslice(cursor, child_location.start_offset - cursor)
|
|
59
|
+
rendered[child.node_id] = rewrite(child)
|
|
60
|
+
text << rendered.fetch(child.node_id)
|
|
61
|
+
cursor = child_location.end_offset
|
|
62
|
+
end
|
|
63
|
+
text << @source.byteslice(cursor, location.end_offset - cursor)
|
|
64
|
+
sites = @by_node.fetch([location.start_offset, location.end_offset, node.type], [])
|
|
65
|
+
priority = %w[comparison arithmetic integer_literal boolean_literal boolean_connector condition_negation statement_removal
|
|
66
|
+
return_nil]
|
|
67
|
+
sites.sort_by { |site| priority.index(site[:operator]) }.each do |site|
|
|
68
|
+
text = wrap(site, node, text, rendered)
|
|
69
|
+
end
|
|
70
|
+
text
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def wrap(site, node, text, rendered)
|
|
74
|
+
@emitted << site[:key]
|
|
75
|
+
key = site[:key].dump
|
|
76
|
+
case site[:operator]
|
|
77
|
+
when "comparison", "arithmetic"
|
|
78
|
+
binary(site, node, rendered)
|
|
79
|
+
when "integer_literal", "boolean_literal"
|
|
80
|
+
"::Ripr::R.lit(#{key},(#{text}),#{site[:variants].map { |v| v[:replacement] }.join(',')})"
|
|
81
|
+
when "condition_negation"
|
|
82
|
+
"::Ripr::R.cond(#{key},((#{text}) ? true : false))"
|
|
83
|
+
when "return_nil"
|
|
84
|
+
"::Ripr::R.ret(#{key},(#{text}))"
|
|
85
|
+
when "statement_removal"
|
|
86
|
+
"(::Ripr::R.on?(#{key}) ? nil : (#{text}))"
|
|
87
|
+
when "boolean_connector"
|
|
88
|
+
left = rendered.fetch(node.left.node_id)
|
|
89
|
+
right = rendered.fetch(node.right.node_id)
|
|
90
|
+
replacement = site[:variants].first[:replacement]
|
|
91
|
+
"(::Ripr::R.on?(#{key}) ? ((#{left}) #{replacement} (#{right})) : (#{text}))"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def binary(site, node, rendered)
|
|
96
|
+
left = node.receiver
|
|
97
|
+
right = node.arguments.arguments.first
|
|
98
|
+
gap = @source.byteslice(left.location.end_offset, right.location.start_offset - left.location.end_offset).scan("\n").join
|
|
99
|
+
lvar, rvar, kvar = %w[l r k].map { |part| "#{@prefix}_#{part}" }
|
|
100
|
+
expressions = [node.name.to_s, *site[:variants].map { |variant| variant[:replacement] }]
|
|
101
|
+
expression = "#{lvar} #{expressions.last} #{rvar}"
|
|
102
|
+
(expressions.length - 2).downto(0) do |index|
|
|
103
|
+
expression = "#{kvar} == #{index} ? #{lvar} #{expressions[index]} #{rvar} : (#{expression})"
|
|
104
|
+
end
|
|
105
|
+
"::Ripr::R.bin(#{site[:key].dump},(#{rendered.fetch(left.node_id)}),#{gap}(#{rewrite(right)}),#{site[:pure]}) " \
|
|
106
|
+
"{ |#{lvar},#{rvar},#{kvar}| #{expression} }"
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ripr
|
|
4
|
+
module Core
|
|
5
|
+
module Operators
|
|
6
|
+
COMPARISON = { :>= => ">", :> => ">=", :<= => "<", :< => "<=", :== => "!=", :!= => "==" }.freeze
|
|
7
|
+
ARITHMETIC = { :+ => "-", :- => "+", :* => "/", :/ => "*", :% => "*" }.freeze
|
|
8
|
+
NAMES = %w[comparison arithmetic integer_literal boolean_literal condition_negation boolean_connector return_nil
|
|
9
|
+
statement_removal].freeze
|
|
10
|
+
|
|
11
|
+
def self.for(node)
|
|
12
|
+
case node
|
|
13
|
+
when Prism::CallNode
|
|
14
|
+
return [] unless node.receiver && node.arguments&.arguments&.length == 1 && !node.call_operator_loc && !node.block
|
|
15
|
+
return [] if node.arguments.arguments.first.is_a?(Prism::SplatNode)
|
|
16
|
+
|
|
17
|
+
if COMPARISON.key?(node.name)
|
|
18
|
+
[["comparison", COMPARISON.fetch(node.name), "value", node.message_loc]]
|
|
19
|
+
elsif ARITHMETIC.key?(node.name)
|
|
20
|
+
[["arithmetic", ARITHMETIC.fetch(node.name), "value", node.message_loc]]
|
|
21
|
+
else
|
|
22
|
+
[]
|
|
23
|
+
end
|
|
24
|
+
when Prism::IntegerNode
|
|
25
|
+
[node.value + 1, node.value - 1, 0].uniq.reject { |v| v == node.value }.map do |value|
|
|
26
|
+
["integer_literal", value.to_s, "always", node.location]
|
|
27
|
+
end
|
|
28
|
+
when Prism::TrueNode, Prism::FalseNode
|
|
29
|
+
[["boolean_literal", node.is_a?(Prism::TrueNode) ? "false" : "true", "always", node.location]]
|
|
30
|
+
when Prism::AndNode, Prism::OrNode
|
|
31
|
+
return [] if node.location.slice.include?("\n")
|
|
32
|
+
|
|
33
|
+
replacement = { "&&" => "||", "||" => "&&", "and" => "or", "or" => "and" }.fetch(node.operator_loc.slice)
|
|
34
|
+
[["boolean_connector", replacement, "unknown", node.operator_loc]]
|
|
35
|
+
else
|
|
36
|
+
[]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ripr
|
|
4
|
+
module Core
|
|
5
|
+
module ReusePlanner
|
|
6
|
+
REUSABLE_STATUSES = %w[killed timeout not_reached not_infected not_detected].freeze
|
|
7
|
+
|
|
8
|
+
def self.reusable?(record, mutant, env_hash:, test_hashes:, newly_reached: [])
|
|
9
|
+
return false unless record.is_a?(Hash) && REUSABLE_STATUSES.include?(record["status"])
|
|
10
|
+
return false unless mutant["full_id"] && record["full_id"] == mutant["full_id"]
|
|
11
|
+
return false unless mutant["method_hash"] && record["method_hash"] == mutant["method_hash"]
|
|
12
|
+
return false unless env_hash && record["env_hash"] == env_hash
|
|
13
|
+
return false unless record["tests"].is_a?(Hash)
|
|
14
|
+
return false if [mutant["id"], mutant["full_id"], mutant["site"]].compact.any? { |id| newly_reached.include?(id) }
|
|
15
|
+
|
|
16
|
+
record["tests"].all? { |file, hash| hash && test_hashes[file] == hash }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ripr
|
|
4
|
+
module Engine
|
|
5
|
+
class BaselineCache
|
|
6
|
+
attr_reader :test_ids, :reused_count
|
|
7
|
+
|
|
8
|
+
def initialize(previous, env_hash:, source_hash:, test_hashes:, tests:, full: false)
|
|
9
|
+
@previous = previous
|
|
10
|
+
@env_hash = env_hash
|
|
11
|
+
@source_hash = source_hash
|
|
12
|
+
@test_hashes = test_hashes
|
|
13
|
+
@tests = tests.to_h { |test| [test.fetch("id"), test] }
|
|
14
|
+
@retained = {}
|
|
15
|
+
@test_ids = nil
|
|
16
|
+
prepare unless full
|
|
17
|
+
@reused_count = @retained.length
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def combine(fresh, load_reached: [])
|
|
21
|
+
fresh ||= { "tests" => {}, "probe" => empty_probe }
|
|
22
|
+
old_probe = @retained.empty? ? empty_probe : @previous.fetch("probe")
|
|
23
|
+
retained_ids = @retained.keys
|
|
24
|
+
dirty_ids = @retained.empty? ? [] : @previous.fetch("tests").keys - retained_ids
|
|
25
|
+
samples = old_probe.fetch("samples").reject do |site, _pairs|
|
|
26
|
+
old_probe.fetch("reach").fetch(site, []).intersect?(dirty_ids)
|
|
27
|
+
end
|
|
28
|
+
probe = {
|
|
29
|
+
"reach" => merge_members(old_probe.fetch("reach"), fresh.fetch("probe").fetch("reach"), retained_ids),
|
|
30
|
+
"infect" => merge_members(old_probe.fetch("infect"), fresh.fetch("probe").fetch("infect"), retained_ids),
|
|
31
|
+
"samples" => samples.merge(fresh.fetch("probe").fetch("samples")) { |_site, old, current| (old + current).uniq.take(3) },
|
|
32
|
+
"load_reached" => (old_probe.fetch("load_reached") + fresh.fetch("probe").fetch("load_reached") + load_reached).uniq
|
|
33
|
+
}
|
|
34
|
+
{ "success" => true, "failures" => [], "tests" => @retained.merge(fresh.fetch("tests")), "probe" => probe,
|
|
35
|
+
"env_hash" => @env_hash, "source_hash" => @source_hash, "test_hashes" => @test_hashes }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def prepare
|
|
41
|
+
return unless valid?(@previous)
|
|
42
|
+
|
|
43
|
+
old_hashes = @previous.fetch("test_hashes")
|
|
44
|
+
changed = old_hashes != @test_hashes
|
|
45
|
+
return if changed && (@previous.fetch("probe").fetch("load_reached").any? || @tests.values.any? do |test|
|
|
46
|
+
!@test_hashes.key?(test.fetch("file"))
|
|
47
|
+
end)
|
|
48
|
+
|
|
49
|
+
@retained = @previous.fetch("tests").select do |id, metadata|
|
|
50
|
+
current = @tests[id]
|
|
51
|
+
current && current["file"] == metadata["file"] && current["line"] == metadata["line"] &&
|
|
52
|
+
old_hashes[current["file"]] == @test_hashes[current["file"]]
|
|
53
|
+
end
|
|
54
|
+
@test_ids = @tests.keys - @retained.keys
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def valid?(data)
|
|
58
|
+
return false unless data.is_a?(Hash) && data["success"] == true && data["env_hash"] == @env_hash &&
|
|
59
|
+
data["source_hash"] == @source_hash && data["tests"].is_a?(Hash) && data["test_hashes"].is_a?(Hash)
|
|
60
|
+
return false unless data["tests"].all? do |id, test|
|
|
61
|
+
id.is_a?(String) && !id.empty? && test.is_a?(Hash) && test["file"].is_a?(String) && !test["file"].empty? &&
|
|
62
|
+
test["line"].is_a?(Integer) && test["line"].positive? && test["duration_ms"].is_a?(Numeric) &&
|
|
63
|
+
test["duration_ms"].finite? && test["duration_ms"] >= 0
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
probe = data["probe"]
|
|
67
|
+
return false unless probe.is_a?(Hash) && probe["load_reached"].is_a?(Array) && probe["load_reached"].all?(String)
|
|
68
|
+
return false unless %w[reach infect].all? do |key|
|
|
69
|
+
probe[key].is_a?(Hash) && probe[key].all? do |site, ids|
|
|
70
|
+
site.is_a?(String) && ids.is_a?(Array) && ids.all? { |id| id.is_a?(String) && data["tests"].key?(id) }
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
probe["samples"].is_a?(Hash) && probe["samples"].all? { |site, pairs| site.is_a?(String) && samples?(pairs) }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def samples?(samples)
|
|
78
|
+
samples.is_a?(Array) && samples.length <= 3 && samples.all? do |pair|
|
|
79
|
+
pair.is_a?(Array) && pair.length == 2 && pair.all? do |value|
|
|
80
|
+
value.nil? || [true, false].include?(value) || value.is_a?(String) || (value.is_a?(Numeric) && value.finite?)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def merge_members(previous, current, retained_ids)
|
|
86
|
+
pruned = previous.transform_values { |ids| ids & retained_ids }.reject { |_site, ids| ids.empty? }
|
|
87
|
+
pruned.merge(current) { |_site, old, fresh| (old + fresh).uniq }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def empty_probe
|
|
91
|
+
{ "reach" => {}, "infect" => {}, "samples" => {}, "load_reached" => [] }
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Ripr
|
|
6
|
+
module Engine
|
|
7
|
+
module Child
|
|
8
|
+
def self.call(timeout_ms:, &block)
|
|
9
|
+
batch([nil], jobs: 1, timeout: ->(_) { timeout_ms }) { block.call }.first.last
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.batch(tasks, jobs:, timeout:, on_complete: nil, &)
|
|
13
|
+
pending = tasks.dup
|
|
14
|
+
active = {}
|
|
15
|
+
completed = []
|
|
16
|
+
on_complete&.call(0, tasks.length)
|
|
17
|
+
until pending.empty? && active.empty?
|
|
18
|
+
while active.length < jobs && !pending.empty?
|
|
19
|
+
task = pending.shift
|
|
20
|
+
state = spawn_child(task, timeout.call(task), &)
|
|
21
|
+
active[state.fetch(:reader)] = state
|
|
22
|
+
end
|
|
23
|
+
ready = IO.select(active.keys, nil, nil, 0.01)&.first || []
|
|
24
|
+
ready.each { |reader| drain(active.fetch(reader)) }
|
|
25
|
+
active.each_value.to_a.each do |state|
|
|
26
|
+
state[:wait_status] ||= Process.waitpid2(state.fetch(:pid), Process::WNOHANG)&.last
|
|
27
|
+
expired = monotonic >= state.fetch(:deadline)
|
|
28
|
+
next unless expired || state[:wait_status]
|
|
29
|
+
|
|
30
|
+
drain(state) if state[:wait_status] && !state[:eof]
|
|
31
|
+
payload = state[:wait_status] ? decode(state.fetch(:buffer)) : { "status" => "timeout" }
|
|
32
|
+
stop(state)
|
|
33
|
+
Process.waitpid(state.fetch(:pid)) unless state[:wait_status]
|
|
34
|
+
completed << [state.fetch(:task), payload]
|
|
35
|
+
active.delete(state.fetch(:reader))
|
|
36
|
+
state.fetch(:reader).close
|
|
37
|
+
on_complete&.call(completed.length, tasks.length)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
completed
|
|
41
|
+
ensure
|
|
42
|
+
active&.each_value do |state|
|
|
43
|
+
stop(state)
|
|
44
|
+
begin
|
|
45
|
+
Process.waitpid(state.fetch(:pid)) unless state[:wait_status]
|
|
46
|
+
rescue Errno::ECHILD
|
|
47
|
+
nil
|
|
48
|
+
end
|
|
49
|
+
state.fetch(:reader).close unless state.fetch(:reader).closed?
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.spawn_child(task, timeout_ms)
|
|
54
|
+
reader, writer = IO.pipe
|
|
55
|
+
pid = fork do
|
|
56
|
+
reader.close
|
|
57
|
+
Process.setpgid(0, 0)
|
|
58
|
+
$stdout.reopen(File::NULL, "w")
|
|
59
|
+
$stderr.reopen(File::NULL, "w")
|
|
60
|
+
payload = begin
|
|
61
|
+
yield(task)
|
|
62
|
+
rescue Exception => e # rubocop:disable Lint/RescueException -- Test code may raise SystemExit or a framework Exception subclass.
|
|
63
|
+
{ "status" => "error", "error" => "#{e.class}: #{e.message}" }
|
|
64
|
+
end
|
|
65
|
+
writer.write(JSON.generate(payload))
|
|
66
|
+
writer.close
|
|
67
|
+
exit! 0
|
|
68
|
+
end
|
|
69
|
+
writer.close
|
|
70
|
+
{ task: task, pid: pid, reader: reader, buffer: +"", deadline: monotonic + (timeout_ms / 1000.0) }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.drain(state)
|
|
74
|
+
loop { state.fetch(:buffer) << state.fetch(:reader).read_nonblock(65_536) }
|
|
75
|
+
rescue IO::WaitReadable
|
|
76
|
+
nil
|
|
77
|
+
rescue EOFError
|
|
78
|
+
state[:eof] = true
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.decode(buffer)
|
|
82
|
+
value = JSON.parse(buffer)
|
|
83
|
+
value.is_a?(Hash) ? value : { "status" => "error", "error" => "child returned a non-object result" }
|
|
84
|
+
rescue JSON::ParserError
|
|
85
|
+
{ "status" => "error", "error" => "child exited without a valid result" }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def self.stop(state)
|
|
89
|
+
Process.kill("KILL", -state.fetch(:pid))
|
|
90
|
+
rescue Errno::ESRCH
|
|
91
|
+
begin
|
|
92
|
+
Process.kill("KILL", state.fetch(:pid))
|
|
93
|
+
rescue StandardError
|
|
94
|
+
nil
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def self.monotonic
|
|
99
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require "time"
|
|
6
|
+
require_relative "../snapshot"
|
|
7
|
+
require_relative "../core/reuse_planner"
|
|
8
|
+
|
|
9
|
+
module Ripr
|
|
10
|
+
module Engine
|
|
11
|
+
class ResultCache
|
|
12
|
+
attr_reader :baseline_env_hash, :source_hash, :test_hashes, :previous
|
|
13
|
+
|
|
14
|
+
def initialize(config, sources:, test_files:, framework:, mutants:, test_ids:, probe_sites:)
|
|
15
|
+
@config = config
|
|
16
|
+
@mutants = mutants
|
|
17
|
+
@snapshot = Snapshot.new
|
|
18
|
+
@previous = config[:full] ? {} : @snapshot.read
|
|
19
|
+
@source_hash = digest(sources.sort.to_h.transform_values { |source| Digest::SHA256.hexdigest(source) })
|
|
20
|
+
@test_hashes = test_files.sort.to_h { |path| [path, Digest::SHA256.file(path).hexdigest] }
|
|
21
|
+
lockfiles = %w[Gemfile.lock .rspec]
|
|
22
|
+
lockfiles << Bundler.default_lockfile.to_s if defined?(Bundler) && ENV["BUNDLE_GEMFILE"]
|
|
23
|
+
auxiliary = (Dir.glob("{test,spec}/**/*") + lockfiles).uniq.select do |path|
|
|
24
|
+
File.file?(path) && !test_files.include?(path)
|
|
25
|
+
end
|
|
26
|
+
dependencies = auxiliary.sort.to_h { |path| [path, Digest::SHA256.file(path).hexdigest] }
|
|
27
|
+
settings = config.to_h.slice(:operators, :arid_callees, :tests, :samples, :seed)
|
|
28
|
+
@baseline_env_hash = digest([VERSION, RUBY_ENGINE, RUBY_VERSION, RUBY_PLATFORM, Dir.pwd,
|
|
29
|
+
Prism::VERSION, framework, settings, @source_hash, dependencies, probe_sites.sort])
|
|
30
|
+
@env_hash = digest([@baseline_env_hash, @test_hashes, test_ids.sort, config[:timeout], config[:jobs], config[:verify_not_infected]])
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def result_for(mutant)
|
|
34
|
+
return if @config[:full] || !@config[:only].empty?
|
|
35
|
+
|
|
36
|
+
record = @previous.fetch("mutants", {})[mutant.fetch("full_id")]
|
|
37
|
+
return if @config[:verify_not_infected] && record.is_a?(Hash) && record["status"] == "not_infected"
|
|
38
|
+
return unless Core::ReusePlanner.reusable?(record, mutant, env_hash: @env_hash, test_hashes: @test_hashes)
|
|
39
|
+
return unless valid_record?(record)
|
|
40
|
+
|
|
41
|
+
record.merge(mutant).merge("reused" => true)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def write(results, baseline, metrics)
|
|
45
|
+
current = @mutants.to_h { |mutant| [mutant.fetch("full_id"), mutant] }
|
|
46
|
+
records = @previous.fetch("mutants", {}).select do |id, record|
|
|
47
|
+
mutant = current[id]
|
|
48
|
+
mutant && valid_record?(record) && record["env_hash"] == @env_hash && record["method_hash"] == mutant["method_hash"]
|
|
49
|
+
end
|
|
50
|
+
results.each do |result|
|
|
51
|
+
files = result.fetch("covering_tests", []).map { |test| test.fetch("file") }
|
|
52
|
+
records[result.fetch("full_id")] = result.merge("env_hash" => @env_hash, "tests" => @test_hashes.slice(*files),
|
|
53
|
+
"recorded_at" => Time.now.utc.iso8601)
|
|
54
|
+
end
|
|
55
|
+
@snapshot.write("version" => Snapshot::VERSION, "env_hash" => @env_hash, "source_hash" => @source_hash,
|
|
56
|
+
"test_hashes" => @test_hashes, "mutants" => records, "baseline" => baseline, "metrics" => metrics)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def valid_record?(record)
|
|
62
|
+
return false unless record.is_a?(Hash) && samples?(record["observed"]) && record["covering_tests"].is_a?(Array)
|
|
63
|
+
|
|
64
|
+
record["covering_tests"].all? do |test|
|
|
65
|
+
test.is_a?(Hash) && test["id"].is_a?(String) && !test["id"].empty? &&
|
|
66
|
+
test["file"].is_a?(String) && !test["file"].empty? && test["line"].is_a?(Integer) && test["line"].positive?
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def samples?(samples)
|
|
71
|
+
samples.is_a?(Array) && samples.length <= 3 && samples.all? do |pair|
|
|
72
|
+
pair.is_a?(Array) && pair.length == 2 && pair.all? do |value|
|
|
73
|
+
value.nil? || [true, false].include?(value) || value.is_a?(String) || (value.is_a?(Numeric) && value.finite?)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def digest(value)
|
|
79
|
+
Digest::SHA256.hexdigest(JSON.generate(value))
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|