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,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riptide
4
+ # Resolves constant references the way Ruby's lexical lookup would, to the
5
+ # module that defines each constant. Card::ROLES defined in a concern
6
+ # resolves to Auditable::ROLES, the name a change to that concern
7
+ # produces. Never triggers an autoload.
8
+ class Resolver
9
+ Constant = Struct.new(:name, :path, :value, keyword_init: true)
10
+
11
+ def initialize(root:)
12
+ @root_prefix = "#{File.expand_path(root)}/"
13
+ @cache = {}
14
+ end
15
+
16
+ # The Constant a Source::Reference reads, or nil when it isn't defined
17
+ # or isn't loaded.
18
+ def resolve(reference)
19
+ @cache[[reference.nesting, reference.segments, reference.absolute]] ||= candidates(reference).lazy.filter_map { |candidate| constant(candidate) }.first
20
+ end
21
+
22
+ # The module a name like "Billing::Card" refers to, if it's loaded.
23
+ def module_named(name)
24
+ value = loaded(name.split("::"))
25
+ value.is_a?(Module) ? value : nil
26
+ end
27
+
28
+ def relative(path)
29
+ path&.start_with?(@root_prefix) ? path.delete_prefix(@root_prefix) : nil
30
+ end
31
+
32
+ private
33
+
34
+ def candidates(reference)
35
+ path = reference.segments.join("::")
36
+ return [path] if reference.absolute
37
+
38
+ reference.nesting.reverse.map { |scope| "#{scope}::#{path}" } << path
39
+ end
40
+
41
+ def constant(candidate)
42
+ *scope_names, name = candidate.split("::")
43
+ scope = scope_names.empty? ? Object : loaded(scope_names)
44
+ return nil unless scope.is_a?(Module)
45
+
46
+ definer = scope.ancestors.find { |ancestor| ancestor.const_defined?(name, false) }
47
+ definer = Object if definer.nil? && scope_names.empty? && Object.const_defined?(name, false)
48
+ return nil unless definer && !definer.autoload?(name)
49
+
50
+ location = definer.const_source_location(name, false)
51
+ prefix = definer.equal?(Object) ? nil : definer.name
52
+ Constant.new(name: [prefix, name].compact.join("::"), path: relative(location&.first), value: definer.const_get(name, false))
53
+ rescue NameError
54
+ nil
55
+ end
56
+
57
+ def loaded(segments)
58
+ segments.reduce(Object) do |scope, segment|
59
+ return nil unless scope.is_a?(Module) && scope.const_defined?(segment, false) && !scope.autoload?(segment)
60
+
61
+ scope.const_get(segment, false)
62
+ end
63
+ rescue NameError
64
+ nil
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riptide
4
+ # Narrows Minitest's runnables to a decision's selection.
5
+ class Runner
6
+ def initialize(decision:)
7
+ @decision = decision
8
+ end
9
+
10
+ def apply
11
+ return if @decision.mode == :full
12
+
13
+ allowed = Hash.new { |h, k| h[k] = [] }
14
+ @decision.selected.each { |entry| allowed[entry[:test][0]] << entry[:test][1] }
15
+ Minitest::Runnable.runnables.each do |klass|
16
+ methods = allowed[klass.name]
17
+ klass.define_singleton_method(:runnable_methods) { methods }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Riptide
6
+ # What ran around tests instead of in one: in the process, from boot on,
7
+ # or in a test class, from before its setup. A test depends on what ran in
8
+ # its scopes before it started. What runs after a scope's last test, like
9
+ # an after_all, counts for nobody.
10
+ class Scope
11
+ # +capture+ turns what the scope gathered, as a Tracer::Trace and a
12
+ # Collectors::Window, into the ids of the shared sets its tests get.
13
+ def initialize(&capture)
14
+ @capture = capture
15
+ @lines = Hash.new { |h, path| h[path] = Set.new }
16
+ @receivers = Set.new.compare_by_identity
17
+ @window = Collectors::Window.blank
18
+ @sets = nil
19
+ end
20
+
21
+ # Takes in what the scope's windows gathered since the last drain.
22
+ def absorb(trace, window)
23
+ return if trace.lines.empty? && trace.receivers.empty? && !window.gathered?
24
+
25
+ grew = @window.absorb(window)
26
+ trace.lines.each do |path, numbers|
27
+ lines = @lines[path]
28
+ numbers.each { |number| grew = true if lines.add?(number) }
29
+ end
30
+ trace.receivers.each { |klass| grew = true if @receivers.add?(klass) }
31
+ @sets = nil if grew
32
+ end
33
+
34
+ # Captured again only once the scope ran something new.
35
+ def sets
36
+ @sets ||= @capture.call(Tracer::Trace.new(@lines.transform_values(&:to_a), @receivers.to_a), @window)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Riptide
6
+ # Decides which tests the working tree can affect, from recorded runs
7
+ # alone. Every recorded input names the exact file version it was
8
+ # recorded against, so there's no diff base: the question for each test
9
+ # is whether anything it depended on differs from what's on disk now.
10
+ # Each recorded version that differs gets analyzed once, and the index
11
+ # says which runs used the parts that changed.
12
+ class Selector
13
+ # The rows of the README's table of what a change selects, plus why a
14
+ # test runs with no change behind it.
15
+ KINDS = {
16
+ code: "Anything inside a method or block",
17
+ constant: "A constant's value",
18
+ declaration: "A declaration",
19
+ override: "A new method that could take over a call",
20
+ table: "A table or its fixtures",
21
+ read: "A file a test read, a directory it listed, or a name it found missing that now exists",
22
+ fallback: "A file in fallback_patterns",
23
+ preloaded: "A file that loaded before riptide started, like the test helper",
24
+ boot: "Top-level code that runs at boot",
25
+ deleted: "A deleted file, or a recorded version git no longer has",
26
+ failed: "A test that failed and hasn't passed since",
27
+ shared: "Tests that share a name",
28
+ unrecorded: "A test with no recorded run"
29
+ }.freeze
30
+
31
+ # Where a change reached a test: its own run, which counts the classes
32
+ # it used, its test class's setup, or the process's.
33
+ VIA = { test: "their own runs", class: "their test class's setup", process: "the process's setup" }.freeze
34
+
35
+ # Why a run no longer holds, and how the change reached it.
36
+ Cause = Struct.new(:reason, :kind, :via)
37
+
38
+ Decision = Struct.new(:mode, :selected, :total, keyword_init: true) do
39
+ # How many tests each kind of change selected, then for each way a
40
+ # change reached them, the reasons behind the most.
41
+ def summary(top: 3)
42
+ header = "#{selected.size}/#{total} tests"
43
+ return "full suite, #{header}: #{selected.first&.dig(:reason)}" if mode == :full && selected.map { |entry| entry[:reason] }.uniq.size == 1
44
+
45
+ header = "full suite, #{header}" if mode == :full
46
+ kinds = selected.group_by { |entry| entry[:kind] }.sort_by { |_, entries| -entries.size }
47
+ lines = kinds.map { |kind, entries| " #{KINDS.fetch(kind)}: #{entries.size}" }
48
+ VIA.each do |via, label|
49
+ reached = selected.select { |entry| entry[:via] == via }
50
+ next if reached.empty?
51
+
52
+ causes = reached.group_by { |entry| entry[:reason] }.sort_by { |reason, entries| [-entries.size, reason] }.first(top)
53
+ lines << " through #{label}: #{reached.size}, most from #{causes.map { |reason, entries| "#{reason} (#{entries.size})" }.join(", ")}"
54
+ end
55
+ ([header] + lines).join("\n")
56
+ end
57
+
58
+ def to_h
59
+ { "mode" => mode.to_s, "total" => total,
60
+ "selected" => selected.map do |entry|
61
+ { "test" => entry[:test], "reason" => entry[:reason], "change" => KINDS.fetch(entry[:kind]), "through" => entry[:via] && VIA.fetch(entry[:via]) }.compact
62
+ end }
63
+ end
64
+
65
+ def self.full(tests, cause)
66
+ new(mode: :full, total: tests.size, selected: tests.map { |test| { test: test, reason: cause.reason, kind: cause.kind, via: cause.via } })
67
+ end
68
+ end
69
+
70
+ # +indexes+ hold the recorded runs and open failures, from every unit
71
+ # the build read and its logs. +pseudo+ returns what a recorded glob,
72
+ # fixture table or name lookup gives now.
73
+ # +fallback+ is the fallback patterns.
74
+ def initialize(indexes:, analyzer:, root:, tests:, pseudo:, fallback: [])
75
+ @indexes = indexes
76
+ @fallback = fallback
77
+ @analyzer = analyzer
78
+ @root = root
79
+ @tests = tests
80
+ @pseudo = pseudo
81
+ @current = {}
82
+ @suite_cause = nil
83
+ end
84
+
85
+ # { index => { run id => Cause } } for the runs that don't hold here,
86
+ # once #select has run.
87
+ attr_reader :hits
88
+
89
+ def select
90
+ @hits = @indexes.to_h { |index| [index, hits_in(index)] }
91
+ if @suite_cause
92
+ @hits = @indexes.to_h { |index| [index, index.run_ids.to_h { |id| [id, @suite_cause] }] }
93
+ return Decision.full(@tests, @suite_cause) unless @tests.empty?
94
+ end
95
+
96
+ selected = @tests.filter_map do |test|
97
+ cause = cause_to_run(test)
98
+ { test: test, reason: cause.reason, kind: cause.kind, via: cause.via } if cause
99
+ end
100
+ full = !@tests.empty? && selected.size == @tests.size
101
+ Decision.new(mode: full ? :full : :selected, selected: selected, total: @tests.size)
102
+ end
103
+
104
+ private
105
+
106
+ # A test with an open failure runs until a build passes it, since a
107
+ # failure riptide can't trace, like a flaky one, has nothing else to
108
+ # rerun it. Two tests with one name, like specs with the same describe
109
+ # and it text, record runs nobody can tell apart, so they always run.
110
+ # Otherwise a test is skipped when a passing run still holds.
111
+ def cause_to_run(test)
112
+ return Cause.new("failed and hasn't passed since", :failed) if @indexes.any? { |index| index.marks[test]&.any? }
113
+ return Cause.new("shares its name with another test, so their runs can't be told apart", :shared) if shared_names.include?(test)
114
+
115
+ runs = @indexes.flat_map { |index| index.runs.fetch(test, []).map { |run| [run, @hits[index][run.id]] } }
116
+ return Cause.new("no recorded run", :unrecorded) if runs.empty?
117
+ return nil if runs.any? { |_, hit| hit.nil? }
118
+
119
+ runs.max_by { |run, _| run.at }.last
120
+ end
121
+
122
+ # Looks up the runs each recorded version that differs from the tree
123
+ # reaches, and why.
124
+ def hits_in(index)
125
+ hits = {}
126
+ index.manifest.each do |path, versions|
127
+ current = current_sha(path)
128
+ versions.each do |version|
129
+ next if version == current
130
+
131
+ changed(index, path, version, current) do |reason, kind, reached|
132
+ reached.each { |id, via| hits[id] ||= Cause.new(reason, kind, via) }
133
+ end
134
+ return hits if @suite_cause
135
+ end
136
+ end
137
+ hits
138
+ end
139
+
140
+ # Yields the reason, the kind of change and the runs reached, as { id =>
141
+ # via }, for each part of +version+ that changed. A constant added here
142
+ # can shadow one read elsewhere, so it reaches the runs that recorded
143
+ # this version and read that name in any file. A dynamic read like
144
+ # "*::LIMIT" matches a changed constant of that name.
145
+ def changed(index, path, version, current)
146
+ postings = index.postings(path, version)
147
+ recorded = -> { index.reach(postings["all"]) }
148
+ return yield("#{Env.constant_name(path)} is defined now", :read, recorded.call) if Env.constant?(path)
149
+ return yield("fixture files for #{path.delete_prefix(Env::FIXTURES)} were added or removed", :table, recorded.call) if path.start_with?(Env::FIXTURES)
150
+ return yield("files matching #{path.delete_prefix(Env::GLOB)} were added or removed", fallback?(path.delete_prefix(Env::GLOB)) ? :fallback : :read, recorded.call) if Env.pseudo?(path)
151
+ return yield("#{path} was deleted", :deleted, recorded.call) if current.nil?
152
+
153
+ change = @analyzer.change(path, version)
154
+ return yield("#{path} changed, and the recorded version isn't in this repository", :deleted, recorded.call) if change.nil?
155
+ return if change.empty?
156
+ return suite(Cause.new("#{path} changed top-level code", :boot)) if change.suite?
157
+ return yield("#{path} changed as a whole", ruby?(path) ? :code : whole_kind(path), recorded.call) if change.whole?
158
+
159
+ runs = nil
160
+ index.changed_holders(path, version, change) do |part, name, ids|
161
+ case part
162
+ when :whole then yield("#{path} changed as a whole", whole_kind(path), index.reach(ids))
163
+ when :constant, :named_constant
164
+ runs ||= recorded.call
165
+ reason = part == :constant ? "#{path} changed #{name}" : "#{path} changed #{name}, which the test may have looked up by name"
166
+ yield(reason, :constant, index.reach(ids).slice(*runs.keys))
167
+ else yield("#{path} changed #{name}", facet_kind(part, change, name), index.reach(ids))
168
+ end
169
+ end
170
+ end
171
+
172
+ def facet_kind(facet, change, name)
173
+ case facet
174
+ when "owners" then change.causes.fetch(name)
175
+ when "tables" then :table
176
+ else :code
177
+ end
178
+ end
179
+
180
+ # A Ruby file recorded whole loaded before riptide could trace it.
181
+ def whole_kind(path)
182
+ return :fallback if fallback?(path)
183
+
184
+ ruby?(path) ? :preloaded : :read
185
+ end
186
+
187
+ def ruby?(path) = Layout::RUBY_EXTENSIONS.include?(File.extname(path))
188
+
189
+ def fallback?(path)
190
+ @fallback.any? { |pattern| pattern == path || File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) }
191
+ end
192
+
193
+ def shared_names
194
+ @shared_names ||= @tests.tally.filter_map { |test, count| test if count > 1 }.to_set
195
+ end
196
+
197
+ def suite(cause)
198
+ @suite_cause ||= cause
199
+ end
200
+
201
+ def current_sha(path)
202
+ return @current[path] if @current.key?(path)
203
+ return @current[path] = @pseudo.call(path) if Env.pseudo?(path)
204
+
205
+ full = File.join(@root, path)
206
+ @current[path] = File.file?(full) ? Diff.blob_sha(full) : nil
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "fileutils"
5
+
6
+ module Riptide
7
+ # Everything riptide does in one test process, once every test file has
8
+ # loaded and before any test runs, which is also before Rails forks
9
+ # parallel workers.
10
+ class Session
11
+ def initialize(config)
12
+ @config = config
13
+ @root = config.root
14
+ end
15
+
16
+ # Anything that goes wrong here leaves the suite running in full and
17
+ # unrecorded, the same as without riptide.
18
+ def start
19
+ Tracer.watch_coverage
20
+ raise Error, Tracer.broken if Tracer.broken
21
+
22
+ boot = [Tracer.drain, Collectors.drain]
23
+ Recorder.aside do
24
+ Collectors.install_frameworks
25
+ Assertions.install(Minitest::Runnable.runnables)
26
+ git = Git.new(@root)
27
+ layout = Layout.new(root: @root, schema_path: @config.schema_path, fixtures_path: @config.fixtures_path)
28
+ store = Store.new(root: @root, suite: @config.suite, log_dir: @config.log_dir)
29
+ inputs = Inputs.new(layout: layout, files: git.files, source_patterns: @config.source_patterns, locale_patterns: @config.locale_patterns)
30
+ process = lambda do |trace, window|
31
+ Env.capture(inputs: inputs, layout: layout, fallback_patterns: @config.fallback_patterns, locale_patterns: @config.locale_patterns, trace: trace, window: window)
32
+ end
33
+ tests = Riptide.discovered_tests
34
+
35
+ recorder = Recorder.new(store: store, inputs: inputs, process: process)
36
+ recorder.absorb_load_facts
37
+ recorder.start(*boot)
38
+ Recorder.install(recorder)
39
+ select(store, layout, inputs, tests) unless @config.mode == :record
40
+ end
41
+ rescue StandardError => e
42
+ warn "[riptide] #{@config.suite}: off for this run, #{e.class}: #{e.message}"
43
+ end
44
+
45
+ private
46
+
47
+ # What the selection found about each unit's runs goes in the log, so
48
+ # publishing can tell which held here. A selection that fails leaves
49
+ # every test running, still recorded.
50
+ def select(store, layout, inputs, tests)
51
+ analyzer = Analyzer.new(layout: layout, blobs: store.method(:read_blob), load_facts: store.method(:load_facts))
52
+ selector = Selector.new(indexes: store.indexes, analyzer: analyzer, root: @root, tests: tests,
53
+ pseudo: inputs.method(:pseudo), fallback: @config.fallback_patterns)
54
+ decision = selector.select
55
+ store.append_selected(decision.selected.map { |entry| entry[:test] })
56
+ selector.hits.each { |index, hits| store.append_hits(index, hits.keys) if index.ref }
57
+ report(decision)
58
+ Runner.new(decision: decision).apply if @config.mode == :enforce
59
+ decision
60
+ rescue StandardError => e
61
+ warn "[riptide] #{@config.suite}: running every test, #{e.class}: #{e.message}"
62
+ end
63
+
64
+ def report(decision)
65
+ verb = @config.mode == :enforce ? "running" : "would run"
66
+ puts "[riptide] #{@config.suite}: #{verb} #{decision.summary}"
67
+ return unless @config.plan_path
68
+
69
+ path = File.expand_path(@config.plan_path, @root)
70
+ FileUtils.mkdir_p(File.dirname(path))
71
+ File.write(path, JSON.pretty_generate(decision.to_h))
72
+ end
73
+ end
74
+ end