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,273 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require "set"
|
|
6
|
+
|
|
7
|
+
module Riptide
|
|
8
|
+
# Turns what one test touched into the files it depended on. Facets name
|
|
9
|
+
# the parts of a file version it relied on: methods, blocks, constants,
|
|
10
|
+
# tables, or the whole file. What a class's own loading ran is the same
|
|
11
|
+
# for every test that uses the class, so it comes back as one shared set
|
|
12
|
+
# per class.
|
|
13
|
+
class Inputs
|
|
14
|
+
attr_reader :root
|
|
15
|
+
|
|
16
|
+
# A file changed on disk after Ruby loaded it, so there's no knowing
|
|
17
|
+
# which version a test ran.
|
|
18
|
+
class Stale < Error; end
|
|
19
|
+
|
|
20
|
+
TABLE_KINDS = %i[sql schema fixture].freeze
|
|
21
|
+
|
|
22
|
+
def initialize(layout:, files:, source_patterns: [], locale_patterns: ["config/locales/**/*"])
|
|
23
|
+
@layout = layout
|
|
24
|
+
@root = layout.root
|
|
25
|
+
@files = files
|
|
26
|
+
@source_patterns = source_patterns
|
|
27
|
+
@locale_patterns = locale_patterns
|
|
28
|
+
@resolver = Resolver.new(root: @root)
|
|
29
|
+
@sources = {}
|
|
30
|
+
@shas = {}
|
|
31
|
+
@ancestors = {}.compare_by_identity
|
|
32
|
+
@load_constants = Hash.new { |h, k| h[k] = Set.new }
|
|
33
|
+
@owner_sets = {}
|
|
34
|
+
@listings = {}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Remembers which constants each class's loading read. A test that uses
|
|
38
|
+
# the class depends on them too: changing STATES matters to every test
|
|
39
|
+
# that saves a model whose `validates :state, inclusion: STATES` read it.
|
|
40
|
+
def absorb(load_facts)
|
|
41
|
+
load_facts.each do |path, lines|
|
|
42
|
+
source = source_for(path)
|
|
43
|
+
next unless source
|
|
44
|
+
|
|
45
|
+
lines.each do |line, fact|
|
|
46
|
+
constants = source.references_at(line).filter_map { |reference| @resolver.resolve(reference) }
|
|
47
|
+
fact["owners"].each { |owner| @load_constants[owner].merge(constants) }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# [{ path => facets }, { class name => { path => facets } }] for one
|
|
53
|
+
# test. +traced+ is a closed Tracer window, +window+ the collectors'.
|
|
54
|
+
def for_test(traced, window)
|
|
55
|
+
inputs, owners = reached(traced, window)
|
|
56
|
+
[inputs, owner_sets(owners)]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# [{ path => facets }, Set of class names] for what +traced+ and
|
|
60
|
+
# +window+ reached, with the classes it used left as names.
|
|
61
|
+
def reached(traced, window)
|
|
62
|
+
entries = Hash.new { |h, path| h[path] = {} }
|
|
63
|
+
owners = Set.new
|
|
64
|
+
|
|
65
|
+
traced.lines.each do |path, numbers|
|
|
66
|
+
next unless recordable?(path)
|
|
67
|
+
|
|
68
|
+
source = @layout.kind(path) == :ruby && source_for(path)
|
|
69
|
+
source ? ran(entries[path], entries, owners, numbers, source) : entries[path]["whole"] = true
|
|
70
|
+
end
|
|
71
|
+
(window.classes.to_a + traced.receivers).each do |klass|
|
|
72
|
+
name = Tracer.name_of(klass)
|
|
73
|
+
owners << name if name
|
|
74
|
+
end
|
|
75
|
+
tables(entries, window.tables)
|
|
76
|
+
window.reads.each { |path| entries[path]["whole"] = true if recordable?(path) }
|
|
77
|
+
locales.each { |path| entries[path]["whole"] = true } if window.translated
|
|
78
|
+
|
|
79
|
+
inputs = finish(entries).merge(fixture_listings(window.tables))
|
|
80
|
+
window.listings.each { |pattern| inputs["#{Env::GLOB}#{pattern}"] = { "sha" => listing(pattern) } }
|
|
81
|
+
# A name that turned up by the end of the test, like one an autoloader
|
|
82
|
+
# registered once its namespace loaded, was never up to a new file.
|
|
83
|
+
window.absent.each do |scope, name, inherit|
|
|
84
|
+
key = "#{inherit ? Env::CONSTANT : Env::OWN_CONSTANT}#{scope} #{name}"
|
|
85
|
+
inputs[key] = { "sha" => Env::ABSENT } if Env.presence(key) == Env::ABSENT
|
|
86
|
+
end
|
|
87
|
+
[inputs, owners]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# { class name => { path => facets } }, a shared set per class in
|
|
91
|
+
# +owners+ and their ancestors, for what their loading ran.
|
|
92
|
+
def owner_sets(owners)
|
|
93
|
+
expand(owners).each_with_object({}) do |owner, result|
|
|
94
|
+
files = owner_set(owner)
|
|
95
|
+
result[owner] = files unless files.empty?
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The files a glob matches now, as one value to compare. Files outside
|
|
100
|
+
# the source patterns count: a support file boot requires matters
|
|
101
|
+
# whether or not riptide records dependencies on it.
|
|
102
|
+
def listing(pattern)
|
|
103
|
+
@listings[pattern] ||= digest(Dir.glob(pattern, base: @root))
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The fixture files that fill +table+ now, as one value to compare.
|
|
107
|
+
def fixture_listing(table)
|
|
108
|
+
@listings["#{Env::FIXTURES}#{table}"] ||= digest(@layout.fixtures_by_table.fetch(table, []))
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# What a recorded glob, fixture table or name lookup gives now.
|
|
112
|
+
def pseudo(key)
|
|
113
|
+
if Env.constant?(key) then Env.presence(key)
|
|
114
|
+
elsif key.start_with?(Env::FIXTURES) then fixture_listing(key.delete_prefix(Env::FIXTURES))
|
|
115
|
+
else listing(key.delete_prefix(Env::GLOB))
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# { path => facets } for what ran outside every test and test class,
|
|
120
|
+
# boot included: the methods, blocks, constants and tables every later
|
|
121
|
+
# test in the process shares. +lines+ is { path => [line, ...] }.
|
|
122
|
+
def for_process(lines, tables)
|
|
123
|
+
entries = Hash.new { |h, path| h[path] = {} }
|
|
124
|
+
lines.each do |path, numbers|
|
|
125
|
+
next unless recordable?(path)
|
|
126
|
+
|
|
127
|
+
source = @layout.kind(path) == :ruby && source_for(path)
|
|
128
|
+
source ? ran(entries[path], entries, Set.new, numbers, source) : entries[path]["whole"] = true
|
|
129
|
+
end
|
|
130
|
+
tables(entries, tables)
|
|
131
|
+
finish(entries).merge(fixture_listings(tables))
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def sha(path)
|
|
135
|
+
@shas[path] ||= begin
|
|
136
|
+
verify_unchanged(path)
|
|
137
|
+
Diff.blob_sha(File.join(@root, path))
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# In scope and known to git, tracked or at least not ignored. An ignored
|
|
142
|
+
# file, like a key or a log, is never recorded, so it can never be
|
|
143
|
+
# pinned into the store and pushed.
|
|
144
|
+
def recordable?(path)
|
|
145
|
+
known?(path) && in_scope?(path)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def known?(path) = @files.include?(path)
|
|
149
|
+
|
|
150
|
+
def source_for(path)
|
|
151
|
+
return @sources[path] if @sources.key?(path)
|
|
152
|
+
|
|
153
|
+
full = File.join(@root, path)
|
|
154
|
+
@sources[path] = File.file?(full) ? Source.parse(File.read(full)) : nil
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
private
|
|
158
|
+
|
|
159
|
+
# Tables are recorded on the schema and fixtures, so they count wherever
|
|
160
|
+
# the source patterns point.
|
|
161
|
+
def in_scope?(path)
|
|
162
|
+
TABLE_KINDS.include?(@layout.kind(path)) || @source_patterns.empty? || @source_patterns.any? { |pattern| File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def verify_unchanged(path)
|
|
166
|
+
compiled = Tracer.compiled_stat(path)
|
|
167
|
+
return unless compiled
|
|
168
|
+
|
|
169
|
+
stat = File.stat(File.join(@root, path))
|
|
170
|
+
raise Stale, "#{path} changed after it loaded" unless [stat.mtime.to_r, stat.size] == compiled
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def ran(entry, entries, owners, numbers, source)
|
|
174
|
+
entry["lines"] = (entry["lines"] || []) | numbers.to_a
|
|
175
|
+
numbers.each do |line|
|
|
176
|
+
source.blocks_starting_at(line).each { |block| add(entry, "blocks", block) }
|
|
177
|
+
unit = source.unit_at(line)
|
|
178
|
+
next unless unit
|
|
179
|
+
|
|
180
|
+
case unit.kind
|
|
181
|
+
when :method_header, :method_body then add(entry, "methods", unit.name)
|
|
182
|
+
when :block_body then add(entry, "blocks", unit.block)
|
|
183
|
+
end
|
|
184
|
+
owners.merge(source.names_for(unit.owner)) if unit.owner
|
|
185
|
+
source.references_at(line).each do |reference|
|
|
186
|
+
# A dynamic read can't be resolved, so it matches a change to any
|
|
187
|
+
# constant with that name.
|
|
188
|
+
next add(entry, "constants", "*::#{reference.segments.last}") if reference.dynamic
|
|
189
|
+
|
|
190
|
+
constant = @resolver.resolve(reference)
|
|
191
|
+
referenced(entries, owners, constant) if constant
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
entry
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# A constant whose value ran code at load depends on that code too,
|
|
198
|
+
# which is what its class's shared set holds.
|
|
199
|
+
def referenced(entries, owners, constant)
|
|
200
|
+
add(entries[constant.path], "constants", constant.name) if constant.path && recordable?(constant.path)
|
|
201
|
+
if constant.value.is_a?(Module)
|
|
202
|
+
owners << constant.value.name if constant.value.name
|
|
203
|
+
elsif constant.path && (namespace = constant.name.rpartition("::").first) != "" && source_for(constant.path)&.computed?(constant.name)
|
|
204
|
+
owners << namespace
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Every named class or module the test's owners are built from: their
|
|
209
|
+
# ancestors, and the modules they extend.
|
|
210
|
+
def expand(owners)
|
|
211
|
+
owners.each_with_object(Set.new) do |name, all|
|
|
212
|
+
all << name
|
|
213
|
+
mod = @resolver.module_named(name)
|
|
214
|
+
all.merge(ancestry(mod)) if mod
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def ancestry(mod)
|
|
219
|
+
@ancestors[mod] ||= (mod.ancestors + mod.singleton_class.ancestors).filter_map { |ancestor| Tracer.name_of(ancestor) }.uniq
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# The files whose code ran while +owner+ loaded, plus the constants its
|
|
223
|
+
# loading read. Rebuilt only when more of the class loads.
|
|
224
|
+
def owner_set(owner)
|
|
225
|
+
mod = @resolver.module_named(owner)
|
|
226
|
+
files = mod ? Tracer.files_for(mod) : []
|
|
227
|
+
files = [@resolver.relative(Object.const_source_location(owner)&.first)].compact if files.empty? && mod
|
|
228
|
+
constants = @load_constants[owner]
|
|
229
|
+
key = [files.size, constants.size]
|
|
230
|
+
cached = @owner_sets[owner]
|
|
231
|
+
return cached.last if cached&.first == key
|
|
232
|
+
|
|
233
|
+
entries = Hash.new { |h, path| h[path] = {} }
|
|
234
|
+
files.each { |path| add(entries[path], "owners", owner) if recordable?(path) }
|
|
235
|
+
constants.each { |constant| add(entries[constant.path], "constants", constant.name) if constant.path && recordable?(constant.path) }
|
|
236
|
+
@owner_sets[owner] = [key, finish(entries)]
|
|
237
|
+
@owner_sets[owner].last
|
|
238
|
+
rescue NameError
|
|
239
|
+
{}
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def tables(entries, tables)
|
|
243
|
+
return if tables.empty?
|
|
244
|
+
|
|
245
|
+
entries[@layout.schema_path]["tables"] = tables.to_a.sort if @layout.schema_path && recordable?(@layout.schema_path)
|
|
246
|
+
tables.each do |table|
|
|
247
|
+
@layout.fixtures_by_table.fetch(table, []).each { |path| add(entries[path], "tables", table) if recordable?(path) }
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def fixture_listings(tables)
|
|
252
|
+
tables.to_h { |table| ["#{Env::FIXTURES}#{table}", { "sha" => fixture_listing(table) }] }
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def digest(paths) = Digest::SHA1.hexdigest(JSON.generate(paths.select { |path| known?(path) }.sort))
|
|
256
|
+
|
|
257
|
+
def locales
|
|
258
|
+
@locales ||= @locale_patterns.flat_map { |pattern| Dir.glob(pattern, base: @root) }.select { |path| recordable?(path) && File.file?(File.join(@root, path)) }
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def add(entry, facet, value)
|
|
262
|
+
(entry[facet] ||= []) << value unless entry[facet]&.include?(value)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def finish(entries)
|
|
266
|
+
entries.each_with_object({}) do |(path, facets), result|
|
|
267
|
+
next unless File.file?(File.join(@root, path))
|
|
268
|
+
|
|
269
|
+
result[path] = { "sha" => sha(path) }.merge(facets)
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Riptide
|
|
4
|
+
# Where an app keeps the files riptide treats specially, and which
|
|
5
|
+
# analysis each path gets.
|
|
6
|
+
class Layout
|
|
7
|
+
RUBY_EXTENSIONS = %w[.rb .rake .ru].freeze
|
|
8
|
+
FIXTURE_MODEL = /^_fixture:\s*\n\s+model_class:\s*["']?([\w:]+)/
|
|
9
|
+
|
|
10
|
+
attr_reader :root, :schema_path, :fixtures_path
|
|
11
|
+
|
|
12
|
+
def initialize(root:, schema_path: nil, fixtures_path: "test/fixtures")
|
|
13
|
+
@root = root
|
|
14
|
+
@schema_path = schema_path || %w[db/structure.sql db/schema.rb].find { |path| File.exist?(File.join(root, path)) }
|
|
15
|
+
@fixtures_path = fixtures_path
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def kind(path)
|
|
19
|
+
if path == schema_path then path.end_with?(".sql") ? :sql : :schema
|
|
20
|
+
elsif fixture?(path) then :fixture
|
|
21
|
+
elsif RUBY_EXTENSIONS.include?(File.extname(path)) then :ruby
|
|
22
|
+
else :whole
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def fixture?(path)
|
|
27
|
+
path.start_with?("#{fixtures_path}/") && path.end_with?(".yml")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The table a fixture file fills: its model's table when it names one,
|
|
31
|
+
# otherwise its path, the way Rails maps admin/users.yml to admin_users.
|
|
32
|
+
def fixture_table(path, text)
|
|
33
|
+
model = text[FIXTURE_MODEL, 1]
|
|
34
|
+
table = model && Object.const_get(model).table_name rescue nil
|
|
35
|
+
table || path.delete_prefix("#{fixtures_path}/").delete_suffix(".yml").tr("/", "_")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# { table => [fixture path, ...] } for every fixture file on disk.
|
|
39
|
+
def fixtures_by_table
|
|
40
|
+
@fixtures_by_table ||= Dir.glob("#{fixtures_path}/**/*.yml", base: root).group_by do |path|
|
|
41
|
+
fixture_table(path, File.read(File.join(root, path)))
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
module Riptide
|
|
6
|
+
# Records one run per test. Wraps Minitest::Test#run, which every runner
|
|
7
|
+
# ends up calling: Minitest 5, Minitest 6 and Rails' parallel workers, so
|
|
8
|
+
# the window covers setup, the test and teardown alike. The test's body
|
|
9
|
+
# runs in a window per top-level statement, each recorded as a set of its
|
|
10
|
+
# own, so the run is what ran around the body plus its statements. Each
|
|
11
|
+
# test class gets a scope around how Minitest runs it, so what its own
|
|
12
|
+
# setup runs, like a before_all, counts for its tests.
|
|
13
|
+
class Recorder
|
|
14
|
+
class << self
|
|
15
|
+
attr_reader :current
|
|
16
|
+
|
|
17
|
+
# Test classes are all loaded by now. Each one's hook goes ahead of
|
|
18
|
+
# anything the class defines, so the scope covers its overrides too.
|
|
19
|
+
def install(recorder)
|
|
20
|
+
@current = recorder
|
|
21
|
+
Minitest::Test.prepend(Hook) unless Minitest::Test < Hook
|
|
22
|
+
ClassHook.wrap(Minitest::Runnable.respond_to?(:run_suite) ? :run_suite : :run)
|
|
23
|
+
ClassHook.wrap(:with_info_handler)
|
|
24
|
+
Minitest::Runnable.runnables.each do |klass|
|
|
25
|
+
hooks = klass.singleton_class.ancestors.take_while { |ancestor| ancestor != klass.singleton_class }
|
|
26
|
+
klass.singleton_class.prepend(ClassHook) unless hooks.include?(ClassHook)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def scope(klass, &block)
|
|
31
|
+
current ? current.scope(klass, &block) : yield
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Riptide's own work counts for no test: the files it reads, and the
|
|
35
|
+
# app code it loads, like a class it checks for overrides, which runs
|
|
36
|
+
# in a window nobody depends on.
|
|
37
|
+
def aside(&block)
|
|
38
|
+
Tracer.open_window
|
|
39
|
+
Collectors.aside(&block)
|
|
40
|
+
ensure
|
|
41
|
+
Tracer.close_window
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
module Hook
|
|
46
|
+
def run
|
|
47
|
+
recorder = Recorder.current
|
|
48
|
+
recorder ? recorder.record(self) { super } : super
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Teardown runs around the body, not in its last statement.
|
|
52
|
+
def before_teardown
|
|
53
|
+
Recorder.current&.end_body
|
|
54
|
+
super
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Minitest runs a whole test class with run_suite in Minitest 6, which
|
|
59
|
+
# gave run to single tests, and with run before that. Both run the
|
|
60
|
+
# class's tests inside with_info_handler, which is also what a Rails
|
|
61
|
+
# parallel worker calls around each test it's given.
|
|
62
|
+
module ClassHook
|
|
63
|
+
def self.wrap(name)
|
|
64
|
+
return if method_defined?(name, false)
|
|
65
|
+
|
|
66
|
+
define_method(name) { |*args, **options, &block| Recorder.scope(self) { super(*args, **options, &block) } }
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# +process+ turns what ran outside every test and test class into the
|
|
71
|
+
# process's shared set, which every later run gets.
|
|
72
|
+
def initialize(store:, inputs:, process:)
|
|
73
|
+
@store = store
|
|
74
|
+
@inputs = inputs
|
|
75
|
+
@written_sets = Set.new
|
|
76
|
+
@warned = Set.new
|
|
77
|
+
@scopes = [Scope.new { |trace, window| [share(process.call(trace, window), scope: "process")] }]
|
|
78
|
+
@body = nil
|
|
79
|
+
@open = Set.new.compare_by_identity
|
|
80
|
+
@lock = Mutex.new
|
|
81
|
+
@owner = nil
|
|
82
|
+
@depth = 0
|
|
83
|
+
@testing = false
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# A started record goes out before every test, so a test that never
|
|
87
|
+
# finishes, from a hang, a crash or a kill, counts as a failure. A test
|
|
88
|
+
# running alongside another one in a different thread can't have its
|
|
89
|
+
# lines told apart, so only its outcome is recorded.
|
|
90
|
+
def record(test)
|
|
91
|
+
unless enter(test: true)
|
|
92
|
+
started(test)
|
|
93
|
+
result = yield
|
|
94
|
+
outcome(test, result)
|
|
95
|
+
return result
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
begin
|
|
99
|
+
settle
|
|
100
|
+
scopes = Recorder.aside do
|
|
101
|
+
started(test)
|
|
102
|
+
scope_sets
|
|
103
|
+
end
|
|
104
|
+
Tracer.open_window
|
|
105
|
+
Collectors.open
|
|
106
|
+
open_body(test)
|
|
107
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
108
|
+
result = nil
|
|
109
|
+
begin
|
|
110
|
+
result = yield
|
|
111
|
+
ensure
|
|
112
|
+
ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round
|
|
113
|
+
end_body
|
|
114
|
+
body, @body = @body, nil
|
|
115
|
+
traced = Tracer.close_window
|
|
116
|
+
window = Collectors.close
|
|
117
|
+
Recorder.aside { persist(test, result, scopes, traced, window, body, ms) }
|
|
118
|
+
end
|
|
119
|
+
result
|
|
120
|
+
ensure
|
|
121
|
+
leave(test: true)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# A test class's scope, open from before anything Minitest runs for it
|
|
126
|
+
# to after the last of it. A class that's already open, like from an
|
|
127
|
+
# override calling super, goes on in the scope it has.
|
|
128
|
+
def scope(klass)
|
|
129
|
+
return yield if @open.include?(klass) || !enter(test: false)
|
|
130
|
+
|
|
131
|
+
settle
|
|
132
|
+
@open << klass
|
|
133
|
+
# Most classes set nothing up, and a scope that gathered nothing adds
|
|
134
|
+
# no set.
|
|
135
|
+
@scopes << Scope.new do |trace, window|
|
|
136
|
+
next [] if trace.lines.empty? && trace.receivers.empty? && !window.gathered?
|
|
137
|
+
|
|
138
|
+
inputs, owner_sets = @inputs.for_test(trace, window)
|
|
139
|
+
[inputs, *owner_sets.values].reject(&:empty?).map { |files| share(files, scope: "class") }
|
|
140
|
+
end
|
|
141
|
+
Tracer.open_window
|
|
142
|
+
Collectors.open
|
|
143
|
+
begin
|
|
144
|
+
yield
|
|
145
|
+
ensure
|
|
146
|
+
Collectors.close
|
|
147
|
+
Tracer.close_window
|
|
148
|
+
@scopes.pop
|
|
149
|
+
@open.delete(klass)
|
|
150
|
+
leave(test: false)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Takes in what boot ran, +trace+ and +window+, and captures it, so a
|
|
155
|
+
# failure turns riptide off before any test runs instead of during each.
|
|
156
|
+
def start(trace, window)
|
|
157
|
+
@scopes.first.absorb(trace, window)
|
|
158
|
+
@scopes.first.sets
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Ends the split of the running test's body, once, when its teardown
|
|
162
|
+
# starts or the test is over.
|
|
163
|
+
def end_body
|
|
164
|
+
@body = [Tracer.close_body || {}, Collectors.close_body || {}, @span] if @body == :open
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def absorb_load_facts
|
|
168
|
+
facts = Tracer.drain_load_facts
|
|
169
|
+
@inputs.absorb(facts)
|
|
170
|
+
facts.each do |path, lines|
|
|
171
|
+
@store.append_load_facts(@inputs.sha(path), lines) if @inputs.recordable?(path) && File.file?(File.join(@inputs.root, path))
|
|
172
|
+
rescue Inputs::Stale, SystemCallError
|
|
173
|
+
next
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
private
|
|
178
|
+
|
|
179
|
+
# Splits the test's body by its top-level statements, from the source of
|
|
180
|
+
# the file that defines it. A body riptide can't read, like one a library
|
|
181
|
+
# generates, isn't split.
|
|
182
|
+
def open_body(test)
|
|
183
|
+
body = test.method(test.name)
|
|
184
|
+
path, line = body.source_location
|
|
185
|
+
file = path && relative(path)
|
|
186
|
+
ranges = file && @inputs.source_for(file)&.statements_at(line)
|
|
187
|
+
return if ranges.nil? || ranges.empty?
|
|
188
|
+
|
|
189
|
+
Tracer.open_body(body, ranges.map(&:first))
|
|
190
|
+
Collectors.open_body
|
|
191
|
+
@body = :open
|
|
192
|
+
# Where the body's statements are, and the line its method or block
|
|
193
|
+
# starts on, which is how the audit finds it again.
|
|
194
|
+
@span = [file, ranges.first.first..ranges.last.last, line]
|
|
195
|
+
rescue StandardError
|
|
196
|
+
Tracer.close_body
|
|
197
|
+
Collectors.close_body
|
|
198
|
+
@body = nil
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Load facts come first: a class that loaded during this test feeds what
|
|
202
|
+
# the test depends on. A skipped test closes its start with nothing. A
|
|
203
|
+
# failure to trace still records the outcome, so a failure still counts.
|
|
204
|
+
def persist(test, result, scopes, traced, window, body, ms)
|
|
205
|
+
return outcome(test, result) if untraced? || scopes.nil?
|
|
206
|
+
|
|
207
|
+
absorb_load_facts
|
|
208
|
+
return outcome(test, result) if result.nil? || result.skipped? || !result.passed?
|
|
209
|
+
|
|
210
|
+
traces, collected, span = body
|
|
211
|
+
traced = keep_own_lines(traced, traces, span) if body
|
|
212
|
+
inputs, owners = @inputs.reached(traced, window)
|
|
213
|
+
statements = body ? statements_of(traces, collected, owners) : []
|
|
214
|
+
sets = scopes + @inputs.owner_sets(owners).values.map { |files| share(files) } + statements.filter_map { |statement| statement[1] }
|
|
215
|
+
defined_in = file(test)
|
|
216
|
+
body_file = span&.first unless span&.first == defined_in || statements.empty?
|
|
217
|
+
@store.append_run(Store::Run.new(test: key(test), at: Time.now.to_f, passed: true, sets: sets, inputs: inputs, file: defined_in, body: statements,
|
|
218
|
+
ms: ms, body_line: span&.last, body_file: body_file, inherited_from: inherited_from(test)))
|
|
219
|
+
rescue StandardError => e
|
|
220
|
+
warn "[riptide] couldn't record #{test.class.name}##{test.name}: #{e.class}: #{e.message}" if @warned.add?(e.class)
|
|
221
|
+
outcome(test, result)
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# The body's own lines are the test's, not the statements': every test's
|
|
225
|
+
# are its own, so they'd keep identical statements in different tests
|
|
226
|
+
# from sharing a set. They move from +traces+ to the test's +traced+
|
|
227
|
+
# window, so what the run depends on stays the same.
|
|
228
|
+
def keep_own_lines(traced, traces, (file, range))
|
|
229
|
+
own = []
|
|
230
|
+
traces.transform_values! do |statement|
|
|
231
|
+
lines = statement.trace.lines
|
|
232
|
+
next statement unless lines.key?(file)
|
|
233
|
+
|
|
234
|
+
mine, rest = lines[file].partition { |line| range.cover?(line) }
|
|
235
|
+
own.concat(mine)
|
|
236
|
+
kept = rest.empty? ? lines.except(file) : lines.merge(file => rest)
|
|
237
|
+
Tracer::Statement.new(Tracer::Trace.new(kept, statement.trace.receivers), statement.seconds)
|
|
238
|
+
end
|
|
239
|
+
return traced if own.empty?
|
|
240
|
+
|
|
241
|
+
Tracer::Trace.new(traced.lines.merge(file => (traced.lines.fetch(file, []) | own).sort), traced.receivers)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# [line, set id, ms, compared, wrote, reads] per statement that ran, in
|
|
245
|
+
# order. Each statement's set is what it depended on, and the classes it
|
|
246
|
+
# used join +owners+, the run's. A statement reads an earlier one's
|
|
247
|
+
# writes when it loaded a record that one made, or counted or loaded from
|
|
248
|
+
# a table that one changed or made a record in.
|
|
249
|
+
def statements_of(traces, collected, owners)
|
|
250
|
+
starts = (traces.keys | collected.keys).sort
|
|
251
|
+
flows = starts.map { |start| (collected[start] || [nil, Collectors::Flow.blank]).last }
|
|
252
|
+
starts.each_with_index.map do |start, position|
|
|
253
|
+
statement = traces[start]
|
|
254
|
+
window = collected[start]&.first || Collectors::NOTHING
|
|
255
|
+
files, used = @inputs.reached(statement&.trace || Tracer::Trace.new({}, []), window)
|
|
256
|
+
owners.merge(used)
|
|
257
|
+
flow = flows[position]
|
|
258
|
+
seen = flow.counted | flow.loaded.map(&:first)
|
|
259
|
+
reads = (0...position).select do |earlier|
|
|
260
|
+
made = flows[earlier].made
|
|
261
|
+
made.intersect?(flow.loaded) || flows[earlier].changed.intersect?(seen) || made.map(&:first).to_set.intersect?(flow.counted)
|
|
262
|
+
end
|
|
263
|
+
[start, files.empty? ? nil : share(files), ((statement&.seconds || 0) * 1000).round, flow.compared.sort, flow.wrote.any?, reads]
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def relative(path)
|
|
268
|
+
root = "#{File.realpath(@inputs.root)}/"
|
|
269
|
+
real = File.realpath(path)
|
|
270
|
+
real.delete_prefix(root) if real.start_with?(root)
|
|
271
|
+
rescue SystemCallError
|
|
272
|
+
nil
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Once the tracer misses code, like a file it couldn't trace, or what
|
|
276
|
+
# ran around tests got lost, nothing seen after that is complete, so
|
|
277
|
+
# only outcomes get recorded.
|
|
278
|
+
def untraced?
|
|
279
|
+
reason = Tracer.broken || @lost
|
|
280
|
+
return false unless reason
|
|
281
|
+
|
|
282
|
+
warn "[riptide] stopped recording, #{reason}" if @warned.add?(:untraced)
|
|
283
|
+
Tracer.drain_load_facts
|
|
284
|
+
true
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Windows nest in one thread at a time, and a test's never holds another
|
|
288
|
+
# test's. Anything else runs with no window: a test records only its
|
|
289
|
+
# outcome, and a class has no scope.
|
|
290
|
+
def enter(test:)
|
|
291
|
+
@lock.synchronize do
|
|
292
|
+
return false if @testing || (@owner && @owner != Thread.current)
|
|
293
|
+
|
|
294
|
+
@owner = Thread.current
|
|
295
|
+
@depth += 1
|
|
296
|
+
@testing = test
|
|
297
|
+
true
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def leave(test:)
|
|
302
|
+
@lock.synchronize do
|
|
303
|
+
@testing = false if test
|
|
304
|
+
@depth -= 1
|
|
305
|
+
@owner = nil if @depth.zero?
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Takes in what ran in the innermost scope since the last look. Code
|
|
310
|
+
# can't run in an outer one while an inner one is open.
|
|
311
|
+
def settle
|
|
312
|
+
@scopes.last.absorb(Tracer.drain, Collectors.drain)
|
|
313
|
+
rescue StandardError => e
|
|
314
|
+
@lost ||= "lost what ran around tests, #{e.class}: #{e.message}"
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# Nil when they can't be captured, which records only the outcome.
|
|
318
|
+
def scope_sets
|
|
319
|
+
@scopes.flat_map(&:sets)
|
|
320
|
+
rescue StandardError => e
|
|
321
|
+
warn "[riptide] couldn't record what ran around tests: #{e.class}: #{e.message}" if @warned.add?(e.class)
|
|
322
|
+
nil
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
def started(test)
|
|
326
|
+
@store.append_start(key(test))
|
|
327
|
+
rescue StandardError
|
|
328
|
+
nil
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# Records how the test ended without what it ran. A skip is no outcome.
|
|
332
|
+
def outcome(test, result)
|
|
333
|
+
return if result.nil?
|
|
334
|
+
|
|
335
|
+
run = Store::Run.new(test: key(test), at: Time.now.to_f, passed: result.passed? || result.skipped?, file: file(test))
|
|
336
|
+
run.passed = nil if result.skipped?
|
|
337
|
+
@store.append_run(run)
|
|
338
|
+
rescue StandardError
|
|
339
|
+
nil
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def key(test) = [test.class.name, test.name]
|
|
343
|
+
|
|
344
|
+
# The test class this one got the test from, when its superclass has
|
|
345
|
+
# the same method: one it defines, or includes from a module. A class
|
|
346
|
+
# that defines its own doesn't.
|
|
347
|
+
def inherited_from(test)
|
|
348
|
+
klass = test.class
|
|
349
|
+
parent = klass.superclass if klass.is_a?(Class)
|
|
350
|
+
return unless parent && parent < Minitest::Test && parent.public_method_defined?(test.name)
|
|
351
|
+
|
|
352
|
+
parent.name if parent.instance_method(test.name) == klass.instance_method(test.name)
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# The file that defines the test's class, relative to the root. A test
|
|
356
|
+
# a library generates, like shoulda's `should`, lives in its class's
|
|
357
|
+
# file too. Nil when that's outside the app.
|
|
358
|
+
def file(test)
|
|
359
|
+
path = Object.const_source_location(test.class.name)&.first
|
|
360
|
+
root = "#{File.realpath(@inputs.root)}/"
|
|
361
|
+
path = path && File.realpath(path)
|
|
362
|
+
path.delete_prefix(root) if path&.start_with?(root)
|
|
363
|
+
rescue StandardError
|
|
364
|
+
nil
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
# +scope+ says which setup a set came from, nil for a class a test used.
|
|
368
|
+
def share(files, scope: nil)
|
|
369
|
+
id = Store.set_id(files, scope)
|
|
370
|
+
@store.append_set(id, files, scope: scope) if @written_sets.add?(id)
|
|
371
|
+
id
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
end
|