riptide 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 +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +157 -0
- data/exe/riptide +6 -0
- data/ext/riptide/extconf.rb +49 -0
- data/ext/riptide/riptide_native.c +892 -0
- data/ext/riptide/ruby_source.rb +80 -0
- data/lib/minitest/riptide_plugin.rb +20 -0
- data/lib/riptide/analysis/fixture.rb +21 -0
- data/lib/riptide/analysis/ruby.rb +208 -0
- data/lib/riptide/analysis/schema.rb +60 -0
- data/lib/riptide/analysis/sql.rb +83 -0
- data/lib/riptide/analyzer.rb +138 -0
- data/lib/riptide/assertions.rb +82 -0
- data/lib/riptide/audit.rb +433 -0
- data/lib/riptide/change.rb +41 -0
- data/lib/riptide/cli.rb +109 -0
- data/lib/riptide/collectors.rb +386 -0
- data/lib/riptide/configuration.rb +63 -0
- data/lib/riptide/diff.rb +54 -0
- data/lib/riptide/env.rb +61 -0
- data/lib/riptide/git.rb +193 -0
- data/lib/riptide/harvest.rb +87 -0
- data/lib/riptide/index.rb +458 -0
- data/lib/riptide/inputs.rb +273 -0
- data/lib/riptide/layout.rb +45 -0
- data/lib/riptide/recorder.rb +374 -0
- data/lib/riptide/resolver.rb +67 -0
- data/lib/riptide/runner.rb +21 -0
- data/lib/riptide/scope.rb +39 -0
- data/lib/riptide/selector.rb +209 -0
- data/lib/riptide/session.rb +74 -0
- data/lib/riptide/source.rb +449 -0
- data/lib/riptide/store.rb +446 -0
- data/lib/riptide/tracer.rb +176 -0
- data/lib/riptide/version.rb +9 -0
- data/lib/riptide.rb +69 -0
- data/riptide.gemspec +34 -0
- metadata +116 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "set"
|
|
5
|
+
|
|
6
|
+
module Riptide
|
|
7
|
+
# Notes what each assertion and mock expectation in a test body compared,
|
|
8
|
+
# for the statement that ran it.
|
|
9
|
+
module Assertions
|
|
10
|
+
NAME = /\A(assert|refute)/
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# Each assertion is wrapped in the module that defines it, so the
|
|
14
|
+
# wrapper reaches every class that includes it, like ActiveSupport's
|
|
15
|
+
# assert_difference, which one on Minitest::Test would sit behind.
|
|
16
|
+
# +runnables+ are the test classes, loaded by now.
|
|
17
|
+
def install(runnables)
|
|
18
|
+
owners = Hash.new { |h, owner| h[owner] = Set.new }
|
|
19
|
+
runnables.each do |klass|
|
|
20
|
+
next unless klass <= ::Minitest::Test
|
|
21
|
+
|
|
22
|
+
klass.public_instance_methods.grep(NAME).each { |name| owners[klass.instance_method(name).owner] << name }
|
|
23
|
+
end
|
|
24
|
+
owners.each { |owner, names| owner.prepend(wrapper(names)) unless wrapped?(owner) }
|
|
25
|
+
::Mocha::Expectation.prepend(Expectations) if defined?(::Mocha::Expectation) && !(::Mocha::Expectation < Expectations)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# 32 bits of what a comparison saw, with what changes from run to run
|
|
29
|
+
# left out: a record's id and timestamps, and times themselves.
|
|
30
|
+
# Nothing here queries, so a relation that hasn't loaded counts by its
|
|
31
|
+
# SQL. Only worked out when it counts, and a value riptide can't
|
|
32
|
+
# describe never fails the assertion: its digest is 0.
|
|
33
|
+
def digest(values)
|
|
34
|
+
return unless Collectors.comparing?
|
|
35
|
+
|
|
36
|
+
Digest::SHA1.hexdigest(shape(values).inspect)[0, 8].to_i(16)
|
|
37
|
+
rescue StandardError
|
|
38
|
+
0
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def wrapped?(owner) = owner.ancestors.take_while { |mod| !mod.equal?(owner) }.any? { |mod| mod.is_a?(Wrapper) }
|
|
44
|
+
|
|
45
|
+
def wrapper(names)
|
|
46
|
+
Wrapper.new do
|
|
47
|
+
names.each do |name|
|
|
48
|
+
define_method(name) { |*args, **options, &block| Collectors.compared(Assertions.digest(args)) { super(*args, **options, &block) } }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def shape(value)
|
|
54
|
+
case value
|
|
55
|
+
when Array then value.map { shape(_1) }
|
|
56
|
+
when Hash then value.to_h { |key, inner| [shape(key), shape(inner)] }
|
|
57
|
+
when Time, Proc, Method then value.class.name
|
|
58
|
+
else
|
|
59
|
+
if defined?(::ActiveRecord::Base) && value.is_a?(::ActiveRecord::Base)
|
|
60
|
+
[value.class.name, value.attributes.except(value.class.primary_key).reject { |name, _| name.end_with?("_at") }.transform_values { shape(_1) }]
|
|
61
|
+
elsif defined?(::ActiveRecord::Relation) && value.is_a?(::ActiveRecord::Relation)
|
|
62
|
+
value.loaded? ? value.to_a.map { shape(_1) } : value.to_sql
|
|
63
|
+
elsif defined?(::Date) && value.is_a?(::Date)
|
|
64
|
+
value.class.name
|
|
65
|
+
else
|
|
66
|
+
value.inspect[0, 500]
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The modules install prepends, so installing twice wraps nothing twice.
|
|
73
|
+
class Wrapper < Module; end
|
|
74
|
+
|
|
75
|
+
# What a mock expects to be called with is what it checks.
|
|
76
|
+
module Expectations
|
|
77
|
+
def with(*args, **options, &block)
|
|
78
|
+
Collectors.compared(Assertions.digest([args, options])) { super }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require "prism"
|
|
6
|
+
require "set"
|
|
7
|
+
|
|
8
|
+
module Riptide
|
|
9
|
+
# Finds the tests to remove and the tests worth taking apart, from what
|
|
10
|
+
# riptide records for every run. It only reports what the runs prove.
|
|
11
|
+
#
|
|
12
|
+
# Only a test's newest passing run of the file as it is now counts. One
|
|
13
|
+
# that's failing, or last passed on an older version of its file, isn't
|
|
14
|
+
# what's there anymore.
|
|
15
|
+
#
|
|
16
|
+
# An inherited test runs again in every test class that subclasses the
|
|
17
|
+
# one it's from, which nesting a class to group tests does without saying
|
|
18
|
+
# so. One the class it's from never ran, like a base class's that only its
|
|
19
|
+
# subclasses run, is the subclass's own.
|
|
20
|
+
#
|
|
21
|
+
# A duplicate is the same program as a test in its class that stays: its
|
|
22
|
+
# method or block as written, everything it ran outside that, down to the
|
|
23
|
+
# lines, and the values each of its checks compared. Within one class,
|
|
24
|
+
# both share everything the class sets up, run or not. A test can't be
|
|
25
|
+
# compared that way when something it runs reads what isn't in the
|
|
26
|
+
# source: a local from around its body, like a test defined in a loop, or
|
|
27
|
+
# the test's own name. A test a subclass inherits is never the one to
|
|
28
|
+
# delete, since deleting it deletes theirs.
|
|
29
|
+
#
|
|
30
|
+
# A megalith is a big test, and it's one of two shapes: steps that build
|
|
31
|
+
# on each other, which split into a test per step, or one scenario, often
|
|
32
|
+
# with setup copied from its neighbors. Everything is measured by how
|
|
33
|
+
# long it takes a build.
|
|
34
|
+
class Audit
|
|
35
|
+
CHECK = /\A(assert|refute|must_|wont_)/
|
|
36
|
+
# Tokens that don't change what a statement says.
|
|
37
|
+
SKIPPED = %i[COMMENT NEWLINE IGNORED_NEWLINE EMBDOC_BEGIN EMBDOC_LINE EMBDOC_END EOF __END__].freeze
|
|
38
|
+
# What a test calls to ask for its own name.
|
|
39
|
+
NAMES = %i[name method_name __method__].freeze
|
|
40
|
+
# Tests this big count as megaliths.
|
|
41
|
+
BIG = 15
|
|
42
|
+
# Setup lines a big test has to share with its neighbors to be worth a
|
|
43
|
+
# helper.
|
|
44
|
+
SHARED = 5
|
|
45
|
+
# Tests the report names of a megalith's neighbors.
|
|
46
|
+
SOME = 3
|
|
47
|
+
|
|
48
|
+
# +check+ is a statement written as an assertion or one that ran one,
|
|
49
|
+
# like a helper's, and +mock+ one that set up a mock expectation.
|
|
50
|
+
Statement = Struct.new(:line, :source, :check, :mock, :compared, :ms, :wrote, keyword_init: true) do
|
|
51
|
+
def checks? = check || mock
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# +statements+ are the ones that ran and +written+ the whole method or
|
|
55
|
+
# block. +run+ is the run's id, +span+ the body's lines, and +unseen+
|
|
56
|
+
# whether it reads something the source doesn't hold, like a local from
|
|
57
|
+
# around it or its own name.
|
|
58
|
+
Test = Struct.new(:name, :file, :ms, :statements, :written, :index, :run, :span, :unseen, :inherited_from, keyword_init: true)
|
|
59
|
+
|
|
60
|
+
Copy = Struct.new(:test, :from, :ms, keyword_init: true)
|
|
61
|
+
# +of+ is the test that stays.
|
|
62
|
+
Duplicate = Struct.new(:test, :of, :ms, keyword_init: true)
|
|
63
|
+
# A step's test runs the steps before it too, since it builds on them.
|
|
64
|
+
Part = Struct.new(:lines, :statements, :ms, keyword_init: true)
|
|
65
|
+
# +split_ms+ is what splitting adds to a build.
|
|
66
|
+
Megalith = Struct.new(:test, :shape, :statements, :checks, :ms, :parts, :split_ms, :shared_with, :shared_lines, keyword_init: true)
|
|
67
|
+
|
|
68
|
+
# +outdated+ are the tests that are failing or last passed on an older
|
|
69
|
+
# version of their file, and +places+ where each test read starts, as
|
|
70
|
+
# { name => "file:line" }.
|
|
71
|
+
Report = Struct.new(:tests, :unread, :outdated, :inherited, :duplicates, :unchecked, :megaliths, :places, keyword_init: true) do
|
|
72
|
+
def to_s(top: 25)
|
|
73
|
+
skipped = [(", #{unread.size} couldn't be" if unread.any?), (", #{outdated.size} failing or changed since they passed" if outdated.any?)].join
|
|
74
|
+
out = ["#{tests} tests read#{skipped}", ""]
|
|
75
|
+
heading(out, "Inherited", "tests a class runs again because it subclasses the test class they're from", inherited)
|
|
76
|
+
inherited.group_by { |copy| [copy.test.split("#").first, copy.from] }.sort_by { |(klass, _), copies| [-copies.sum(&:ms), klass] }.each do |(klass, from), copies|
|
|
77
|
+
out << format(" %s inherits %d from %s: %.1fs", klass, copies.size, from, copies.sum(&:ms) / 1000.0)
|
|
78
|
+
copies.sort_by(&:test).each { |copy| out << " #{places[copy.test]} #{copy.test.split("#", 2).last}" }
|
|
79
|
+
end
|
|
80
|
+
heading(out << "", "Duplicates", "the same body, run the same way, checking the same values, as a test in its class that stays", duplicates)
|
|
81
|
+
duplicates.each do |duplicate|
|
|
82
|
+
out << format(" %s %s: %.2fs", places[duplicate.test], duplicate.test, duplicate.ms / 1000.0)
|
|
83
|
+
out << " same as #{places[duplicate.of]} #{duplicate.of}"
|
|
84
|
+
end
|
|
85
|
+
out << "" << "Checks nothing (#{unchecked.size})"
|
|
86
|
+
unchecked.each { |name| out << " #{places[name]} #{name}" }
|
|
87
|
+
shown = megaliths.first(top)
|
|
88
|
+
out << "" << "Megaliths (#{shown.size} of #{megaliths.size}, slowest first)"
|
|
89
|
+
shown.each do |m|
|
|
90
|
+
out << format(" %s %s: %d statements, %d checks, %.2fs", places[m.test], m.test, m.statements, m.checks, m.ms / 1000.0)
|
|
91
|
+
if m.shape == :steps
|
|
92
|
+
out << format(" %d steps that build on each other; a split adds %.1fs a build and names the failing step", m.parts.size, m.split_ms / 1000.0)
|
|
93
|
+
else
|
|
94
|
+
out << " one scenario"
|
|
95
|
+
end
|
|
96
|
+
out << " shares setup lines #{ranges(m.shared_lines)} with #{some(m.test, m.shared_with)}" if m.shared_with.any?
|
|
97
|
+
end
|
|
98
|
+
out.join("\n")
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
def heading(out, title, rule, found)
|
|
104
|
+
out << format("%s (%d, %.1fs a build): %s", title, found.size, found.sum(&:ms) / 1000.0, rule)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# 3, 4, 5, 9 as "3-5, 9".
|
|
108
|
+
def ranges(lines)
|
|
109
|
+
lines.sort.uniq.chunk_while { |a, b| b == a + 1 }.map { |run| run.size > 1 ? "#{run.first}-#{run.last}" : run.first.to_s }.join(", ")
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# A few of +names+, by method alone when they're in +test+'s class.
|
|
113
|
+
def some(test, names)
|
|
114
|
+
klass = test.split("#").first
|
|
115
|
+
short = names.map { |name| name.split("#").first == klass ? name.split("#", 2).last : name }
|
|
116
|
+
return short.join(", ") if short.size <= SOME
|
|
117
|
+
|
|
118
|
+
"#{short.first(SOME).join(", ")} and #{short.size - SOME} more"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# +indexes+ are the units fetch brought.
|
|
123
|
+
def initialize(indexes:, git:, root:)
|
|
124
|
+
@indexes = indexes.reject { |index| index.runs.empty? }
|
|
125
|
+
@git = git
|
|
126
|
+
@root = root
|
|
127
|
+
@models = {}
|
|
128
|
+
@facts = {}
|
|
129
|
+
@current = {}
|
|
130
|
+
@named = {}
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def run
|
|
134
|
+
tests, unread, outdated = read
|
|
135
|
+
by_name = tests.to_h { |test| [test.name, test] }
|
|
136
|
+
originals = tests.to_h { |test| [test, test.inherited_from && by_name["#{test.inherited_from}##{test.name.split("#", 2).last}"]] }
|
|
137
|
+
copies, own = tests.partition { |test| originals[test] }
|
|
138
|
+
Report.new(tests: tests.size, unread: unread, outdated: outdated, inherited: inherited(copies), duplicates: duplicates(own, originals.values.compact.to_set),
|
|
139
|
+
unchecked: tests.reject { |test| test.statements.any?(&:checks?) }.map(&:name).sort,
|
|
140
|
+
megaliths: megaliths(tests), places: tests.to_h { |test| [test.name, "#{test.file}:#{test.span.first}"] })
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
private
|
|
144
|
+
|
|
145
|
+
# Each test's newest run, wherever it came from, read a file version at
|
|
146
|
+
# a time, since a version's syntax tree is big and only its tests need
|
|
147
|
+
# it. A model decodes its whole index, so each is read once.
|
|
148
|
+
def read
|
|
149
|
+
newest = {}
|
|
150
|
+
failing = Set.new
|
|
151
|
+
@indexes.each do |index|
|
|
152
|
+
model = @models[index] = index.model
|
|
153
|
+
failing.merge(index.marks.keys)
|
|
154
|
+
model.runs.each do |test, runs|
|
|
155
|
+
id, run = runs.max_by { |_, holder| holder.at }
|
|
156
|
+
newest[test] = [index, model, id, run] if !newest[test] || newest[test].last.at < run.at
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
@test_files = newest.values.flat_map { |_, model, _, run| [model.test_files[run.test], run.body_file] }.compact.to_set
|
|
160
|
+
unread = []
|
|
161
|
+
outdated = []
|
|
162
|
+
versions = Hash.new { |h, k| h[k] = [] }
|
|
163
|
+
newest.each do |test, (index, model, id, run)|
|
|
164
|
+
file = run.body_file || model.test_files[run.test]
|
|
165
|
+
next unread << test.join("#") unless file && run.body_line && run.body.any?
|
|
166
|
+
|
|
167
|
+
sha = pinned(model, run, file)
|
|
168
|
+
next outdated << test.join("#") if failing.include?(test) || (sha && sha != current(file))
|
|
169
|
+
|
|
170
|
+
versions[[file, sha]] << [index, model, id, run]
|
|
171
|
+
end
|
|
172
|
+
tests = versions.flat_map do |(file, sha), runs|
|
|
173
|
+
text = text_of(file, sha)
|
|
174
|
+
program, tokens = text && parsed(text)
|
|
175
|
+
@named[[file, sha]] ||= program ? named(program) : Set.new
|
|
176
|
+
runs.filter_map do |index, model, id, run|
|
|
177
|
+
found = program && test_of(index, model, id, run, file, program, tokens)
|
|
178
|
+
unread << run.test.join("#") unless found
|
|
179
|
+
found || nil
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
[tests, unread.sort, outdated.sort]
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def test_of(index, model, id, run, file, program, tokens)
|
|
186
|
+
node = body_at(program, run.body_line)
|
|
187
|
+
return unless node
|
|
188
|
+
|
|
189
|
+
nodes = body_statements(node).to_h { |statement| [statement.location.start_line, statement] }
|
|
190
|
+
statements = run.body.map do |recorded|
|
|
191
|
+
statement = nodes.fetch(recorded.line)
|
|
192
|
+
calls = calls_in(statement)
|
|
193
|
+
mock = calls.include?("expects")
|
|
194
|
+
Statement.new(line: recorded.line, source: written(statement, tokens), mock: mock,
|
|
195
|
+
check: !mock && (calls.any? { |name| name.match?(CHECK) } || recorded.compared.any?),
|
|
196
|
+
compared: recorded.compared, ms: recorded.ms, wrote: recorded.wrote)
|
|
197
|
+
end
|
|
198
|
+
span = node.location.start_line..last_line(node)
|
|
199
|
+
Test.new(name: run.test.join("#"), file: file, ms: run.ms.to_i, statements: statements, written: written(node, tokens), index: index, run: id, span: span,
|
|
200
|
+
unseen: captures?(node) || reads_name?(model, run, file, span), inherited_from: run.inherited_from)
|
|
201
|
+
rescue KeyError
|
|
202
|
+
nil
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# The version of +file+ the run recorded, or nil when the file isn't
|
|
206
|
+
# one riptide records.
|
|
207
|
+
def pinned(model, run, file)
|
|
208
|
+
([run] + run.sets.map { |id| model.set(id) }).filter_map { |holder| holder.files.dig(file, "sha") }.first
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# +file+ at +sha+, or as it is on disk, which is the same version when
|
|
212
|
+
# the run's is current.
|
|
213
|
+
def text_of(file, sha)
|
|
214
|
+
(sha && @git.read(sha)) || (File.read(File.join(@root, file)) if File.file?(File.join(@root, file)))
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def current(file)
|
|
218
|
+
@current.fetch(file) { @current[file] = File.file?(full = File.join(@root, file)) ? Diff.blob_sha(full) : nil }
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# The file's syntax tree, and its tokens in order without comments and
|
|
222
|
+
# line breaks, as [type, value, offset].
|
|
223
|
+
def parsed(text)
|
|
224
|
+
program, tokens = Prism.parse_lex(text).value
|
|
225
|
+
[program, tokens.filter_map { |token, _| [token.type, token.value, token.location.start_offset] unless SKIPPED.include?(token.type) }.sort_by(&:last)]
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# The method or block whose body starts at +line+, the way the recorder
|
|
229
|
+
# found it: the outermost one starting there, or whose call does.
|
|
230
|
+
def body_at(node, line)
|
|
231
|
+
return node if (node.is_a?(Prism::DefNode) || node.is_a?(Prism::BlockNode)) && node.location.start_line == line
|
|
232
|
+
return node.block if node.is_a?(Prism::CallNode) && node.block.is_a?(Prism::BlockNode) && node.location.start_line == line
|
|
233
|
+
|
|
234
|
+
node.compact_child_nodes.each do |child|
|
|
235
|
+
found = body_at(child, line)
|
|
236
|
+
return found if found
|
|
237
|
+
end
|
|
238
|
+
nil
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def body_statements(node)
|
|
242
|
+
body = node.body
|
|
243
|
+
body = body.statements if body.is_a?(Prism::BeginNode)
|
|
244
|
+
body.is_a?(Prism::StatementsNode) ? body.body : [body].compact
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def calls_in(node)
|
|
248
|
+
calls = Set.new
|
|
249
|
+
walk = lambda do |inner|
|
|
250
|
+
calls << inner.name.to_s if inner.is_a?(Prism::CallNode)
|
|
251
|
+
inner.compact_child_nodes.each(&walk)
|
|
252
|
+
end
|
|
253
|
+
walk.(node)
|
|
254
|
+
calls
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Whether a block body reads or sets a local from around it. A local's
|
|
258
|
+
# depth counts the blocks out to where it lives, so one deeper than the
|
|
259
|
+
# blocks inside the body lives outside it. A method's body can't.
|
|
260
|
+
def captures?(node)
|
|
261
|
+
return false if node.is_a?(Prism::DefNode)
|
|
262
|
+
|
|
263
|
+
walk = lambda do |inner, depth|
|
|
264
|
+
return true if inner.class.name.start_with?("Prism::LocalVariable") && inner.respond_to?(:depth) && inner.depth > depth
|
|
265
|
+
|
|
266
|
+
inside = inner.is_a?(Prism::BlockNode) || inner.is_a?(Prism::LambdaNode) ? depth + 1 : depth
|
|
267
|
+
inner.compact_child_nodes.any? { |child| walk.(child, inside) }
|
|
268
|
+
end
|
|
269
|
+
node.compact_child_nodes.any? { |child| walk.(child, 0) }
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# Whether the test's body, or test code it ran, asks for the test's
|
|
273
|
+
# name, which tells two tests apart without a line of difference. Code
|
|
274
|
+
# that ran outside every test, in a class's or the process's setup,
|
|
275
|
+
# had no test to ask about.
|
|
276
|
+
def reads_name?(model, run, file, span)
|
|
277
|
+
return true if @named.fetch([file, pinned(model, run, file)], Set.new).any? { |line| span.cover?(line) }
|
|
278
|
+
|
|
279
|
+
([run] + run.sets.map { |id| model.set(id) }.reject(&:scope)).any? do |holder|
|
|
280
|
+
holder.files.any? do |path, facets|
|
|
281
|
+
@test_files.include?(path) && facets["lines"]&.any? { |line| named_at(path, facets["sha"]).include?(line) }
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def named_at(path, sha)
|
|
287
|
+
@named.fetch([path, sha]) do
|
|
288
|
+
text = text_of(path, sha)
|
|
289
|
+
@named[[path, sha]] = text ? named(Prism.parse(text).value) : Set.new
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
# The lines that ask for the running test's name.
|
|
294
|
+
def named(program)
|
|
295
|
+
lines = Set.new
|
|
296
|
+
walk = lambda do |node|
|
|
297
|
+
if node.is_a?(Prism::CallNode) && NAMES.include?(node.name) && (node.receiver.nil? || node.receiver.is_a?(Prism::SelfNode)) && node.arguments.nil?
|
|
298
|
+
lines << node.location.start_line
|
|
299
|
+
end
|
|
300
|
+
node.compact_child_nodes.each(&walk)
|
|
301
|
+
end
|
|
302
|
+
walk.(program)
|
|
303
|
+
lines
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# A method or block as its tokens, from after a method's name, so
|
|
307
|
+
# spacing and comments don't count but what's inside a string does, a
|
|
308
|
+
# heredoc's body too.
|
|
309
|
+
def written(node, tokens)
|
|
310
|
+
from = node.is_a?(Prism::DefNode) ? node.name_loc.end_offset : node.location.start_offset
|
|
311
|
+
to = ends(node)
|
|
312
|
+
first = tokens.bsearch_index { |token| token.last >= from } || tokens.size
|
|
313
|
+
tokens[first..].take_while { |token| token.last < to }.map { |type, value, _| [type, value] }
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# The last line of +node+, past any heredoc it opens.
|
|
317
|
+
def last_line(node)
|
|
318
|
+
own = node.respond_to?(:heredoc?) && node.heredoc? && node.closing_loc ? node.closing_loc.start_line : node.location.end_line
|
|
319
|
+
node.compact_child_nodes.map { |child| last_line(child) }.push(own).max
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
# Where +node+ ends, past any heredoc it opens.
|
|
323
|
+
def ends(node)
|
|
324
|
+
own = node.respond_to?(:heredoc?) && node.heredoc? && node.closing_loc ? node.closing_loc.end_offset : node.location.end_offset
|
|
325
|
+
node.compact_child_nodes.map { |child| ends(child) }.push(own).max
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
# Inherited and duplicate tests
|
|
329
|
+
|
|
330
|
+
def inherited(copies)
|
|
331
|
+
copies.map { |copy| Copy.new(test: copy.name, from: copy.inherited_from, ms: copy.ms) }
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Of each set of tests in a class that are the same program, the first
|
|
335
|
+
# by name stays. One that +lent+ to a subclass stays too, since deleting
|
|
336
|
+
# it deletes the subclass's, which runs another way.
|
|
337
|
+
def duplicates(tests, lent)
|
|
338
|
+
tests.reject(&:unseen).group_by { |test| [test.name.split("#").first, program(test)] }.values.select { |same| same.size > 1 }.flat_map do |same|
|
|
339
|
+
stays, *rest = same.sort_by(&:name)
|
|
340
|
+
rest.reject { |test| lent.include?(test) }.map do |test|
|
|
341
|
+
Duplicate.new(test: test.name, of: stays.name, ms: test.ms)
|
|
342
|
+
end
|
|
343
|
+
end.sort_by { |duplicate| [-duplicate.ms, duplicate.test] }
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# The test as written, which of its statements ran and what their checks
|
|
347
|
+
# compared, and what it ran outside its own body. A test file's code
|
|
348
|
+
# counts by its lines alone, since the classes it names are the test's,
|
|
349
|
+
# which every copy names differently.
|
|
350
|
+
def program(test)
|
|
351
|
+
model = @models.fetch(test.index)
|
|
352
|
+
run = model.set(test.run)
|
|
353
|
+
ran = [facts(run, test.file, test.span)] + run.sets.map { |id| @facts[[test.index, id]] ||= facts(model.set(id), nil, nil) }
|
|
354
|
+
[test.written, test.statements.map { |statement| [statement.line - test.span.first, statement.compared] }, ran.compact.sort]
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# A digest of what +holder+ ran, leaving out the lines in +span+ of
|
|
358
|
+
# +body_file+, or nil when that's nothing.
|
|
359
|
+
def facts(holder, body_file, span)
|
|
360
|
+
files = holder.files.filter_map do |path, facets|
|
|
361
|
+
next [path, facets] unless @test_files.include?(path)
|
|
362
|
+
|
|
363
|
+
lines = Array(facets["lines"])
|
|
364
|
+
lines = lines.reject { |line| span.cover?(line) } if path == body_file
|
|
365
|
+
[path, { "sha" => facets["sha"], "lines" => lines }] if lines.any?
|
|
366
|
+
end
|
|
367
|
+
return if files.empty? && holder.constants.empty?
|
|
368
|
+
|
|
369
|
+
Digest::SHA1.hexdigest(JSON.generate([holder.scope, files, holder.constants.sort]))
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
# Megaliths
|
|
373
|
+
|
|
374
|
+
def megaliths(tests)
|
|
375
|
+
shared = shared_setup(tests)
|
|
376
|
+
tests.filter_map do |test|
|
|
377
|
+
next if test.statements.size < BIG
|
|
378
|
+
|
|
379
|
+
megalith = Megalith.new(test: test.name, statements: test.statements.size, checks: test.statements.count(&:checks?), ms: test.ms,
|
|
380
|
+
shared_with: shared.dig(test.name, :with) || [],
|
|
381
|
+
shared_lines: shared.dig(test.name, :lines) || [])
|
|
382
|
+
if (stages = steps(test)).size > 1
|
|
383
|
+
split(megalith, test, stages.each_index.map { |k| stages[0..k].flatten })
|
|
384
|
+
else
|
|
385
|
+
megalith.shape = :scenario
|
|
386
|
+
end
|
|
387
|
+
megalith
|
|
388
|
+
end.sort_by { |megalith| [-megalith.ms, megalith.test] }
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
# Assertions that come after a write the later ones build on. A mock is
|
|
392
|
+
# set up ahead of what it checks and verified at the end, so it never
|
|
393
|
+
# ends a step.
|
|
394
|
+
def steps(test)
|
|
395
|
+
stages = [[]]
|
|
396
|
+
checked = false
|
|
397
|
+
test.statements.each_with_index do |statement, i|
|
|
398
|
+
if statement.check
|
|
399
|
+
checked = true
|
|
400
|
+
elsif checked && statement.wrote
|
|
401
|
+
stages << []
|
|
402
|
+
checked = false
|
|
403
|
+
end
|
|
404
|
+
stages.last << i
|
|
405
|
+
end
|
|
406
|
+
stages.select { |stage| stage.any? { test.statements[_1].check } }
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
def split(megalith, test, parts)
|
|
410
|
+
megalith.shape = :steps
|
|
411
|
+
around = [test.ms - test.statements.sum(&:ms), 0].max
|
|
412
|
+
megalith.parts = parts.map do |indexes|
|
|
413
|
+
Part.new(lines: indexes.map { test.statements[_1] }.select(&:checks?).map(&:line), statements: indexes.size,
|
|
414
|
+
ms: around + indexes.sum { test.statements[_1].ms })
|
|
415
|
+
end
|
|
416
|
+
megalith.split_ms = megalith.parts.sum(&:ms) - megalith.ms
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
# Setup lines a big test has word for word with others in its file.
|
|
420
|
+
def shared_setup(tests)
|
|
421
|
+
by_file = tests.group_by(&:file)
|
|
422
|
+
tests.each_with_object({}) do |test, result|
|
|
423
|
+
next if test.statements.size < BIG
|
|
424
|
+
|
|
425
|
+
mine = test.statements.reject(&:checks?)
|
|
426
|
+
others = by_file[test.file] - [test]
|
|
427
|
+
with = others.select { |other| (mine.map(&:source) & other.statements.map(&:source)).size >= SHARED }
|
|
428
|
+
lines = mine.select { |s| with.any? { |other| other.statements.any? { _1.source == s.source } } }.map(&:line)
|
|
429
|
+
result[test.name] = { with: with.map(&:name).sort, lines: lines } if with.any? && lines.size >= SHARED
|
|
430
|
+
end
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
module Riptide
|
|
6
|
+
# What changed between two versions of one file, in the terms a recorded
|
|
7
|
+
# run depends on: methods and blocks by their key in the old version,
|
|
8
|
+
# constants, classes and tables by name. +suite+ means every test, like a
|
|
9
|
+
# change to top-level code.
|
|
10
|
+
class Change
|
|
11
|
+
attr_reader :methods, :blocks, :constants, :tables
|
|
12
|
+
|
|
13
|
+
# { class name => why it changed }: :declaration, :override for a new
|
|
14
|
+
# method that could take over a call, or :code for a method that ran
|
|
15
|
+
# while the class loaded.
|
|
16
|
+
attr_reader :causes
|
|
17
|
+
|
|
18
|
+
def initialize
|
|
19
|
+
@methods = Set.new
|
|
20
|
+
@blocks = Set.new
|
|
21
|
+
@constants = Set.new
|
|
22
|
+
@causes = {}
|
|
23
|
+
@tables = Set.new
|
|
24
|
+
@whole = false
|
|
25
|
+
@suite = false
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def owners = @causes.keys
|
|
29
|
+
|
|
30
|
+
def owned(names, cause) = names.each { |name| @causes[name] ||= cause }
|
|
31
|
+
|
|
32
|
+
def whole! = @whole = true
|
|
33
|
+
def suite! = @suite = true
|
|
34
|
+
def whole? = @whole || @suite
|
|
35
|
+
def suite? = @suite
|
|
36
|
+
|
|
37
|
+
def empty?
|
|
38
|
+
!whole? && [@methods, @blocks, @constants, owners, @tables].all?(&:empty?)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/riptide/cli.rb
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "optparse"
|
|
4
|
+
require "riptide"
|
|
5
|
+
|
|
6
|
+
module Riptide
|
|
7
|
+
# `riptide fetch` before tests and `riptide publish` after them, outside
|
|
8
|
+
# the test process. Publishing writes to .git, so it runs wherever .git is
|
|
9
|
+
# writable and the remote's credential is available.
|
|
10
|
+
#
|
|
11
|
+
# A PR build passes its number and the branch it merges into. A build of
|
|
12
|
+
# a long-lived branch like main passes --harvest: it reads the runs of the
|
|
13
|
+
# PRs that landed in it, keeps the ones that held, and deletes their
|
|
14
|
+
# units. A release branch also reads main's with --upstream main.
|
|
15
|
+
#
|
|
16
|
+
# `riptide audit` reads the units fetch brought and prints the tests to
|
|
17
|
+
# remove and the ones worth taking apart. It only reads.
|
|
18
|
+
#
|
|
19
|
+
# Riptide never fails a build: anything that goes wrong is a warning, and
|
|
20
|
+
# the command still exits 0. Only a malformed command line exits 1.
|
|
21
|
+
class CLI
|
|
22
|
+
USAGE = "usage: riptide fetch [identity] | riptide publish --suite NAME [identity] [--log-dir DIR] [--retained-runs N]\n" \
|
|
23
|
+
" riptide audit --suite NAME\n" \
|
|
24
|
+
"identity: --pr NUMBER --target BRANCH, or --branch NAME [--upstream BRANCH] [--harvest]\n" \
|
|
25
|
+
"all take [--root DIR], fetch and publish [--remote NAME]"
|
|
26
|
+
|
|
27
|
+
def initialize(argv, out: $stdout, err: $stderr)
|
|
28
|
+
@argv = argv.dup
|
|
29
|
+
@out = out
|
|
30
|
+
@err = err
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def call
|
|
34
|
+
command = @argv.shift
|
|
35
|
+
unless %w[fetch publish audit].include?(command)
|
|
36
|
+
@err.puts USAGE
|
|
37
|
+
return 1
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
options = parse(command)
|
|
41
|
+
run(command, options)
|
|
42
|
+
0
|
|
43
|
+
rescue OptionParser::ParseError => e
|
|
44
|
+
@err.puts "riptide: #{e.message}"
|
|
45
|
+
@err.puts USAGE
|
|
46
|
+
1
|
|
47
|
+
rescue StandardError => e
|
|
48
|
+
@err.puts "[riptide] #{command} didn't finish, so this build reads or saves less: #{e.class}: #{e.message}"
|
|
49
|
+
0
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def run(command, options)
|
|
55
|
+
return audit(options) if command == "audit"
|
|
56
|
+
|
|
57
|
+
build = build_of(options)
|
|
58
|
+
return Store.fetch(root: options[:root], build: build, remote: options[:remote]) if command == "fetch"
|
|
59
|
+
|
|
60
|
+
suite = options[:suite]
|
|
61
|
+
log_dir = File.expand_path(options[:log_dir] || "tmp/riptide/#{suite}", options[:root])
|
|
62
|
+
store = Store.new(root: options[:root], suite: suite, log_dir: log_dir, remote: options[:remote], **options.slice(:retained_runs))
|
|
63
|
+
published = store.publish(build: build)
|
|
64
|
+
@out.puts "[riptide] published #{published.ref}: #{published.runs} new runs, #{published.harvested} harvested"
|
|
65
|
+
published.deleted.each { |ref| @out.puts "[riptide] deleted #{ref}" }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def audit(options)
|
|
69
|
+
root = options[:root]
|
|
70
|
+
store = Store.new(root: root, suite: options[:suite], log_dir: File.expand_path(options[:log_dir] || "tmp/riptide/#{options[:suite]}", root))
|
|
71
|
+
@out.puts Audit.new(indexes: store.indexes, git: Git.new(root), root: root).run
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def build_of(options)
|
|
75
|
+
return Store::Build.pr(options[:pr], target: options[:target]) if options[:pr]
|
|
76
|
+
|
|
77
|
+
Store::Build.branch(options[:branch], upstream: options[:upstream], harvest: options[:harvest])
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def parse(command)
|
|
81
|
+
options = { root: Dir.pwd, remote: "origin", harvest: false }
|
|
82
|
+
OptionParser.new do |parser|
|
|
83
|
+
parser.banner = USAGE
|
|
84
|
+
parser.on("--suite NAME") { |value| options[:suite] = value }
|
|
85
|
+
parser.on("--pr NUMBER") { |value| options[:pr] = value }
|
|
86
|
+
parser.on("--target BRANCH") { |value| options[:target] = value }
|
|
87
|
+
parser.on("--branch NAME") { |value| options[:branch] = value }
|
|
88
|
+
parser.on("--upstream BRANCH") { |value| options[:upstream] = value }
|
|
89
|
+
parser.on("--harvest") { options[:harvest] = true }
|
|
90
|
+
parser.on("--remote NAME") { |value| options[:remote] = value }
|
|
91
|
+
parser.on("--log-dir DIR") { |value| options[:log_dir] = value }
|
|
92
|
+
parser.on("--retained-runs N", Integer) do |value|
|
|
93
|
+
raise OptionParser::InvalidArgument, "--retained-runs #{value}" unless value.positive?
|
|
94
|
+
|
|
95
|
+
options[:retained_runs] = value
|
|
96
|
+
end
|
|
97
|
+
parser.on("--root DIR") { |value| options[:root] = File.expand_path(value) }
|
|
98
|
+
end.parse!(@argv)
|
|
99
|
+
raise OptionParser::MissingArgument, "--suite" if %w[publish audit].include?(command) && !options[:suite]
|
|
100
|
+
return options if command == "audit"
|
|
101
|
+
|
|
102
|
+
raise OptionParser::MissingArgument, "--pr or --branch" unless options[:pr] || options[:branch]
|
|
103
|
+
raise OptionParser::InvalidOption, "--pr and --branch" if options[:pr] && options[:branch]
|
|
104
|
+
raise OptionParser::MissingArgument, "--target" if options[:pr] && !options[:target]
|
|
105
|
+
|
|
106
|
+
options
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|