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.
@@ -0,0 +1,386 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Riptide
6
+ # What a test touched besides the code it ran, gathered in windows that
7
+ # nest the way the tracer's do: a test's, a test class's, and the
8
+ # process's, which is always open. The process's keeps file reads,
9
+ # listings and tables, from the moment riptide starts, so boot counts too:
10
+ # an initializer loading a YAML file, or `Dir["test/support/**/*.rb"].each
11
+ # { require it }` picking up a new file.
12
+ #
13
+ # A test's body splits into a window per statement, the way the tracer's
14
+ # does, and each statement also keeps its data flow: what it wrote, what it
15
+ # read, and what its assertions compared.
16
+ module Collectors
17
+ IDENTIFIER = /(?:"?\w+"?\.)?"?(\w+)"?/
18
+ TABLE = /\b(?:JOIN|UPDATE|INTO|TRUNCATE(?:\s+TABLE)?)\s+(?:ONLY\s+)?#{IDENTIFIER}/i
19
+ FROM_LIST = /\bFROM\s+((?:#{IDENTIFIER}(?:\s+(?:AS\s+)?"?\w+"?)?\s*,\s*)*#{IDENTIFIER})/i
20
+
21
+ # What a window gathers into, besides whether it translated anything.
22
+ GATHERED = %i[classes tables reads listings absent].freeze
23
+
24
+ Window = Struct.new(:classes, :tables, :reads, :listings, :absent, :translated, keyword_init: true) do
25
+ def self.blank = new(classes: Set.new.compare_by_identity, tables: Set.new, reads: Set.new, listings: Set.new, absent: Set.new, translated: false)
26
+
27
+ def gathered? = translated || GATHERED.any? { |field| !self[field].empty? }
28
+
29
+ # Takes in what +other+ gathered, and says whether that added anything.
30
+ def absorb(other)
31
+ grew = other.translated && !translated
32
+ self.translated ||= other.translated
33
+ GATHERED.each do |field|
34
+ mine = self[field]
35
+ size = mine.size
36
+ mine.merge(other[field])
37
+ grew ||= mine.size != size
38
+ end
39
+ grew
40
+ end
41
+ end
42
+
43
+ # A statement's data flow. +wrote+ are tables it inserted into, updated
44
+ # or deleted from. +changed+ are tables an update, a delete or an insert
45
+ # outside a record touched, any of which can change what a later query
46
+ # finds. +counted+ are tables it read without loading their rows, like a
47
+ # count, an exists? or a join. +made+ and +loaded+ are [table, id]
48
+ # records, and +compared+ a digest per assertion or mock expectation.
49
+ Flow = Struct.new(:wrote, :changed, :counted, :made, :loaded, :compared, keyword_init: true) do
50
+ def self.blank = new(wrote: Set.new, changed: Set.new, counted: Set.new, made: Set.new, loaded: Set.new, compared: [])
51
+ end
52
+
53
+ WRITE = /\A\s*(INSERT\s+INTO|UPDATE|DELETE\s+FROM)\s+(?:ONLY\s+)?#{IDENTIFIER}/i
54
+ # A record load reads one table's own columns.
55
+ RECORDS = /\A\s*SELECT\s+(?:DISTINCT\s+)?#{IDENTIFIER}\.\*\s+FROM\s+(?:ONLY\s+)?#{IDENTIFIER}/i
56
+ TRANSACTION = /\A\s*(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE)\b/i
57
+
58
+ # What a drain gives back when nothing was gathered, which is most of
59
+ # the time.
60
+ NOTHING = Window.blank.tap { |window| GATHERED.each { |field| window[field].freeze } }.freeze
61
+
62
+ @process = Window.blank
63
+ @enclosing = []
64
+
65
+ class << self
66
+ # The innermost window inside the process's, or nil, which it also is
67
+ # while riptide's own work runs. Inside a split body, the running
68
+ # statement's.
69
+ def current
70
+ return if @aside
71
+ return @current unless @body && (start = Tracer.statement)
72
+
73
+ (@body[start] ||= [Window.blank, Flow.blank]).first
74
+ end
75
+
76
+ # The running statement's data flow, or nil.
77
+ def flow
78
+ return if @aside || !@body
79
+
80
+ start = Tracer.statement
81
+ start && (@body[start] ||= [Window.blank, Flow.blank]).last
82
+ end
83
+
84
+ # Splits the test's window by statement until close_body.
85
+ def open_body
86
+ @body = {}
87
+ @asserting = 0
88
+ end
89
+
90
+ # { statement's first line => [Window, Flow] } since open_body, or nil.
91
+ def close_body
92
+ body = @body
93
+ @body = nil
94
+ body
95
+ end
96
+
97
+ # Riptide's own file reads, listings and lookups aren't the app's, so
98
+ # nothing gathers them.
99
+ def aside
100
+ was = @aside
101
+ @aside = true
102
+ yield
103
+ ensure
104
+ @aside = was
105
+ end
106
+
107
+ def install(root)
108
+ @root_prefix = "#{File.expand_path(root)}/"
109
+ @process = Window.blank
110
+ @enclosing = []
111
+ IO.singleton_class.prepend(Reads) unless IO.singleton_class < Reads
112
+ File.singleton_class.prepend(Opens) unless File.singleton_class < Opens
113
+ File.singleton_class.prepend(Checks) unless File.singleton_class < Checks
114
+ FileTest.singleton_class.prepend(Checks) unless FileTest.singleton_class < Checks
115
+ Kernel.prepend(KernelOpens) unless Kernel < KernelOpens
116
+ Module.prepend(Lookups) unless Module < Lookups
117
+ Dir.singleton_class.prepend(Listings) unless Dir.singleton_class < Listings
118
+ end
119
+
120
+ # ActiveRecord and I18n are only there once the app has loaded.
121
+ def install_frameworks
122
+ if defined?(::ActiveRecord::Base) && !(::ActiveRecord::Base < Instantiations)
123
+ ::ActiveRecord::Base.prepend(Instantiations)
124
+ ::ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
125
+ tables = tables_in(payload[:sql])
126
+ (current || process)&.tables&.merge(tables)
127
+ flowed(payload[:sql], tables) if @body && payload[:name] != "SCHEMA"
128
+ end
129
+ end
130
+ ::I18n.singleton_class.prepend(Translations) if defined?(::I18n) && !(::I18n.singleton_class < Translations)
131
+ end
132
+
133
+ def open
134
+ @enclosing << @current if @current
135
+ @current = Window.blank
136
+ end
137
+
138
+ def close
139
+ closed = @current
140
+ @current = @enclosing.pop
141
+ closed
142
+ end
143
+
144
+ # What the innermost window gathered since it opened or was last
145
+ # drained, leaving it open.
146
+ def drain
147
+ return NOTHING unless (@current || @process).gathered?
148
+
149
+ if @current
150
+ drained = @current
151
+ @current = Window.blank
152
+ else
153
+ drained = @process
154
+ @process = Window.blank
155
+ end
156
+ drained
157
+ end
158
+
159
+ # Every table a statement names, schema-qualified or not, including
160
+ # each table in a comma-separated FROM list.
161
+ def tables_in(sql)
162
+ tables = sql.scan(TABLE).flatten
163
+ sql.scan(FROM_LIST).each do |list,|
164
+ list.split(",").each { |item| tables << item.strip[/\A#{IDENTIFIER}/, 1] }
165
+ end
166
+ tables.compact.map(&:downcase).uniq
167
+ end
168
+
169
+ # Where +sql+, which names +tables+, took the running statement's data.
170
+ def flowed(sql, tables = tables_in(sql))
171
+ return if sql.match?(TRANSACTION) || !(at = flow)
172
+
173
+ if (write = sql.match(WRITE))
174
+ table = write[2].downcase
175
+ at.wrote << table
176
+ at.changed << table unless @creating && write[1].upcase.start_with?("INSERT")
177
+ tables -= [table]
178
+ elsif (own = sql.match(RECORDS)) && own[1].casecmp?(own[2])
179
+ tables -= [own[2].downcase]
180
+ end
181
+ at.counted.merge(tables)
182
+ end
183
+
184
+ # A record's own insert is a row the statement made, not a change to
185
+ # what other queries find.
186
+ def creating
187
+ was = @creating
188
+ @creating = true
189
+ yield
190
+ ensure
191
+ @creating = was
192
+ end
193
+
194
+ def made(record) = flow&.made&.<<([record.class.table_name, record.id])
195
+ def loaded(record) = flow&.loaded&.<<([record.class.table_name, record.id])
196
+
197
+ # Whether an assertion starting now counts for the running statement:
198
+ # one inside another doesn't count again.
199
+ def comparing? = !flow.nil? && @asserting.zero?
200
+
201
+ # Notes what an assertion was handed, as a digest, or nil when it
202
+ # doesn't count. What runs inside it, like assert_difference's block, is
203
+ # still the statement's.
204
+ def compared(digest)
205
+ flow.compared << digest if digest && comparing?
206
+ @asserting += 1 if @body
207
+ yield
208
+ ensure
209
+ @asserting -= 1 if @body
210
+ end
211
+
212
+ def read(path)
213
+ target = (current || process)&.reads
214
+ return unless target && @root_prefix
215
+
216
+ full = File.expand_path(path)
217
+ target << full.delete_prefix(@root_prefix) if full.start_with?(@root_prefix)
218
+ end
219
+
220
+ # A glob or listing under the root, so a file showing up there counts.
221
+ # Outside a test or class only the app's own count: gems like Zeitwerk
222
+ # list every autoload directory at boot, and a new file there changes
223
+ # nothing until something references it.
224
+ def listed(patterns, base, depth: 2)
225
+ target = current&.listings || (process.listings if process && caller_locations(depth, 1).first&.path&.start_with?(@root_prefix.to_s))
226
+ return unless target && @root_prefix
227
+
228
+ Array(patterns).flatten.each do |pattern|
229
+ next unless pattern.is_a?(String) || pattern.respond_to?(:to_path)
230
+
231
+ full = File.expand_path(pattern.to_s, base)
232
+ target << full.delete_prefix(@root_prefix) if full.start_with?(@root_prefix)
233
+ end
234
+ end
235
+
236
+ # A class found by name, like `"#{kind}Policy".constantize` or a
237
+ # gem's `const_get`, is one the test used. A name that wasn't there
238
+ # matters too: a file defining it later changes what the test does.
239
+ def found(value)
240
+ current.classes << value if current && value.is_a?(Module)
241
+ end
242
+
243
+ # Kept as the exact call, since `Card.const_defined?(:Config)` and
244
+ # `Object.const_defined?("Card::Config")` answer differently when a
245
+ # top-level Config exists.
246
+ def missing(scope, name, inherit)
247
+ return unless current && scope.name && (name.is_a?(String) || name.is_a?(Symbol))
248
+
249
+ current.absent << [scope.name, name.to_s, inherit ? true : false]
250
+ end
251
+
252
+ def process = (@process unless @aside)
253
+
254
+ # Whether a file exists is a listing of just that path.
255
+ def checked(path)
256
+ listed(path.to_s.gsub(/[*?\[\]{}\\]/) { "\\#{_1}" }, nil, depth: 3) if path.is_a?(String) || path.respond_to?(:to_path)
257
+ end
258
+ end
259
+
260
+ module Instantiations
261
+ def initialize(...)
262
+ Collectors.current&.classes&.add(self.class)
263
+ super
264
+ end
265
+
266
+ def init_with_attributes(...)
267
+ Collectors.current&.classes&.add(self.class)
268
+ super.tap { Collectors.loaded(self) }
269
+ end
270
+
271
+ def _create_record(...)
272
+ Collectors.creating { super.tap { Collectors.made(self) } }
273
+ end
274
+ end
275
+
276
+ # On IO, so File, Pathname and YAML reach them too.
277
+ module Reads
278
+ # copy_stream reads its first argument, so a path there counts.
279
+ %i[read binread readlines foreach copy_stream].each do |reader|
280
+ define_method(reader) do |path, *rest, **options, &block|
281
+ Collectors.read(path) if path.is_a?(String) || path.respond_to?(:to_path)
282
+ super(path, *rest, **options, &block)
283
+ end
284
+ end
285
+ end
286
+
287
+ # Writes aren't dependencies: a test that writes a tmp file doesn't
288
+ # depend on what was there before.
289
+ module Opens
290
+ %i[open new].each do |opener|
291
+ define_method(opener) do |path, *rest, **options, &block|
292
+ Opens.opened(path, rest, options)
293
+ super(path, *rest, **options, &block)
294
+ end
295
+ end
296
+
297
+ def self.opened(path, rest, options)
298
+ mode = options.fetch(:mode) { rest.first.is_a?(Integer) || rest.first.is_a?(String) ? rest.first : "r" }
299
+ Collectors.read(path) if (path.is_a?(String) || path.respond_to?(:to_path)) && reading?(mode)
300
+ end
301
+
302
+ def self.reading?(mode)
303
+ case mode
304
+ when Integer then (mode & (File::WRONLY | File::RDWR)) != File::WRONLY
305
+ else !mode.to_s.start_with?("w", "a")
306
+ end
307
+ end
308
+ end
309
+
310
+ # Kernel#open opens a file without going through File.open.
311
+ module KernelOpens
312
+ private
313
+
314
+ def open(path, *rest, **options, &block)
315
+ Opens.opened(path, rest, options)
316
+ super
317
+ end
318
+ end
319
+
320
+ # A directory read entry by entry counts as listing everything in it.
321
+ module Listings
322
+ def [](*patterns, base: nil, **options)
323
+ Collectors.listed(patterns, base)
324
+ super
325
+ end
326
+
327
+ def glob(patterns, *rest, base: nil, **options, &block)
328
+ Collectors.listed(patterns, base)
329
+ super
330
+ end
331
+
332
+ %i[children each_child entries foreach empty?].each do |lister|
333
+ define_method(lister) do |dir, *rest, **options, &block|
334
+ Collectors.listed(File.join(dir.to_s, "*"), nil) if dir.is_a?(String) || dir.respond_to?(:to_path)
335
+ super(dir, *rest, **options, &block)
336
+ end
337
+ end
338
+
339
+ def exist?(path)
340
+ Collectors.checked(path)
341
+ super
342
+ end
343
+ end
344
+
345
+ # On File and FileTest, which Pathname's checks go through. Whether a
346
+ # file is there is a listing of it. How big it is depends on what's in
347
+ # it, so that's a read.
348
+ module Checks
349
+ %i[exist? file? directory? symlink? readable? executable?].each do |check|
350
+ define_method(check) do |path|
351
+ Collectors.checked(path)
352
+ super(path)
353
+ end
354
+ end
355
+
356
+ %i[size size? zero? empty?].each do |check|
357
+ define_method(check) do |path|
358
+ Collectors.read(path) if path.is_a?(String) || path.respond_to?(:to_path)
359
+ super(path)
360
+ end
361
+ end
362
+ end
363
+
364
+ module Lookups
365
+ def const_get(name, inherit = true)
366
+ super.tap { |value| Collectors.found(value) }
367
+ rescue NameError
368
+ Collectors.missing(self, name, inherit)
369
+ raise
370
+ end
371
+
372
+ def const_defined?(name, inherit = true)
373
+ super.tap { |defined| Collectors.missing(self, name, inherit) unless defined }
374
+ end
375
+ end
376
+
377
+ module Translations
378
+ %i[translate t translate! t! localize l exists?].each do |lookup|
379
+ define_method(lookup) do |*args, **options, &block|
380
+ Collectors.current&.translated = true
381
+ super(*args, **options, &block)
382
+ end
383
+ end
384
+ end
385
+ end
386
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riptide
4
+ class Configuration
5
+ MODES = %i[record dry_run enforce].freeze
6
+
7
+ # The app root everything is relative to.
8
+ attr_accessor :root
9
+
10
+ # Names the ref runs are kept under, refs/riptide/<suite>. Each CI stage
11
+ # that runs a different slice of the tests is its own suite.
12
+ attr_accessor :suite
13
+
14
+ # Where test processes write their logs until `riptide publish` folds
15
+ # them into the ref. Defaults to tmp/riptide/<suite>. Publishing runs
16
+ # outside the test process, so it needs the same directory as --log-dir.
17
+ attr_writer :log_dir
18
+
19
+ # :record only records. :dry_run also prints what it would run. :enforce
20
+ # runs only the selected tests.
21
+ attr_reader :mode
22
+
23
+ # Files every test depends on as a whole.
24
+ attr_accessor :fallback_patterns
25
+
26
+ # Globs of the files dependencies are recorded on. Empty means every
27
+ # file under the root.
28
+ attr_accessor :source_patterns
29
+
30
+ # Locale files, which a test that translated anything depends on.
31
+ attr_accessor :locale_patterns
32
+
33
+ # The schema file, db/structure.sql or db/schema.rb when unset.
34
+ attr_accessor :schema_path
35
+
36
+ attr_accessor :fixtures_path
37
+
38
+ # When set, the selection is also written here as JSON.
39
+ attr_accessor :plan_path
40
+
41
+ def initialize
42
+ @root = Dir.pwd
43
+ @suite = "default"
44
+ @mode = :record
45
+ @fallback_patterns = %w[Gemfile Gemfile.lock config/application.rb config/environment.rb config/environments/test.rb config/initializers/**/*]
46
+ @source_patterns = []
47
+ @locale_patterns = ["config/locales/**/*"]
48
+ @schema_path = nil
49
+ @fixtures_path = "test/fixtures"
50
+ @plan_path = nil
51
+ end
52
+
53
+ def log_dir
54
+ File.expand_path(@log_dir || "tmp/riptide/#{suite}", root)
55
+ end
56
+
57
+ def mode=(value)
58
+ raise ArgumentError, "mode must be one of #{MODES.inspect}, got #{value.inspect}" unless MODES.include?(value)
59
+
60
+ @mode = value
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "tmpdir"
5
+
6
+ module Riptide
7
+ module Diff
8
+ Hunk = Struct.new(:old_start, :old_count, :new_start, :new_count) do
9
+ # Lines this hunk removes or rewrites in the old file.
10
+ def old_lines = (old_start...(old_start + old_count))
11
+
12
+ # Lines this hunk adds or rewrites in the new file.
13
+ def new_lines = (new_start...(new_start + new_count))
14
+ end
15
+
16
+ HUNK_HEADER = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/
17
+
18
+ # No git config from anywhere reaches the diff, so a setting like
19
+ # diff.interHunkContext can't merge hunks on one machine and not another.
20
+ ISOLATED = { "GIT_CONFIG_NOSYSTEM" => "1", "GIT_CONFIG_GLOBAL" => File::NULL, "GIT_CONFIG_COUNT" => nil,
21
+ "GIT_CONFIG_PARAMETERS" => nil, "GIT_DIR" => nil }.freeze
22
+
23
+ module_function
24
+
25
+ # Git's blob hash of a file's content, the same as `git hash-object`.
26
+ def blob_sha(path)
27
+ content = File.binread(path)
28
+ Digest::SHA1.hexdigest("blob #{content.bytesize}\0#{content}")
29
+ end
30
+
31
+ # Zero-context hunks turning +old_text+ into +new_text+, compared as bytes.
32
+ def hunks(old_text, new_text)
33
+ return [] if old_text.b == new_text.b
34
+
35
+ # A directory riptide just made, with git stopped from looking above it
36
+ # for a repository.
37
+ Dir.mktmpdir("riptide-diff") do |dir|
38
+ File.binwrite(File.join(dir, "old"), old_text)
39
+ File.binwrite(File.join(dir, "new"), new_text)
40
+ env = ISOLATED.merge("GIT_CEILING_DIRECTORIES" => File.dirname(dir))
41
+ # --text keeps a global attributes file from calling it binary.
42
+ # --no-index exits 1 when the files differ.
43
+ out, = Git.new(dir).run("diff", "--no-index", "--no-color", "--no-ext-diff", "--no-textconv", "--text", "--unified=0", "old", "new", env: env, allow: [0, 1])
44
+ parse(out.b)
45
+ end
46
+ end
47
+
48
+ def parse(diff_text)
49
+ diff_text.scan(HUNK_HEADER).map do |old_start, old_count, new_start, new_count|
50
+ Hunk.new(old_start.to_i, (old_count || "1").to_i, new_start.to_i, (new_count || "1").to_i)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "json"
5
+
6
+ module Riptide
7
+ # What every later test in a process depends on: fallback files as a
8
+ # whole, and the code, file reads, tables and listings outside every test
9
+ # and test class, from boot on, like a parallel worker's setup. A new
10
+ # initializer, or a new support file a boot glob requires, can change
11
+ # every test without touching any file a run recorded.
12
+ module Env
13
+ GLOB = "glob:"
14
+ # The fixture files that fill a table, as "fixtures:<table>". A new
15
+ # file, or one that starts filling the table, changes what a test
16
+ # that touched it loads.
17
+ FIXTURES = "fixtures:"
18
+ # A constant a test looked up by name and didn't find, as
19
+ # "const:<scope> <name>", counting the scope's ancestors or not.
20
+ CONSTANT = "const:"
21
+ OWN_CONSTANT = "const-own:"
22
+ ABSENT = "absent"
23
+
24
+ module_function
25
+
26
+ # { path => facets }, stored as one of the process's shared sets.
27
+ # +trace+ and +window+ are what the process's windows gathered.
28
+ def capture(inputs:, layout:, fallback_patterns:, locale_patterns:, trace:, window:)
29
+ files = {}
30
+ (fallback_patterns + locale_patterns + window.listings.to_a).uniq.each do |pattern|
31
+ files["#{GLOB}#{pattern}"] = { "sha" => inputs.listing(pattern) }
32
+ end
33
+ fallback_patterns.flat_map { |pattern| Dir.glob(pattern, base: layout.root) }.uniq.each do |path|
34
+ files[path] = { "sha" => inputs.sha(path), "whole" => true } if inputs.recordable?(path) && File.file?(File.join(layout.root, path))
35
+ end
36
+ inputs.for_process(trace.lines, window.tables).each { |path, facets| files[path] ||= facets }
37
+ (window.reads.to_a + Tracer.preloaded).each do |path|
38
+ files[path] = { "sha" => inputs.sha(path), "whole" => true } if !files.key?(path) && inputs.recordable?(path)
39
+ end
40
+ files.sort.to_h
41
+ end
42
+
43
+ def pseudo?(path) = path.start_with?(GLOB, FIXTURES, CONSTANT, OWN_CONSTANT)
44
+
45
+ def constant?(path) = path.start_with?(CONSTANT, OWN_CONSTANT)
46
+
47
+ def constant_name(path)
48
+ scope, name = path.delete_prefix(OWN_CONSTANT).delete_prefix(CONSTANT).split(" ", 2)
49
+ scope == "Object" ? name : "#{scope}::#{name}"
50
+ end
51
+
52
+ # Whether a constant entry's lookup finds something now, made the same
53
+ # way, counting an autoload as there.
54
+ def presence(path)
55
+ scope, name = path.delete_prefix(OWN_CONSTANT).delete_prefix(CONSTANT).split(" ", 2)
56
+ Object.const_get(scope).const_defined?(name, !path.start_with?(OWN_CONSTANT)) ? "present" : ABSENT
57
+ rescue NameError
58
+ ABSENT
59
+ end
60
+ end
61
+ end