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,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "open3"
|
|
5
|
+
|
|
6
|
+
# The tracer reaches into the VM, so it builds against the internal headers of
|
|
7
|
+
# the exact Ruby it runs on. Ruby doesn't install those, so they come from
|
|
8
|
+
# Ruby's source at this Ruby's own revision, with the few that Ruby generates
|
|
9
|
+
# while it builds generated the same way. RIPTIDE_RUBY_SOURCE points at a
|
|
10
|
+
# source tree instead, for a build with no way out to GitHub.
|
|
11
|
+
module RubySource
|
|
12
|
+
REPOSITORY = "https://github.com/ruby/ruby.git"
|
|
13
|
+
# What the headers and the generators for the missing ones need.
|
|
14
|
+
PATHS = %w[/*.h /internal/ /ccan/ /prism/ /tool/ /template/ /defs/].freeze
|
|
15
|
+
GENERATED = {
|
|
16
|
+
"id.h" => ->(out) { ["tool/generic_erb.rb", "--output=#{out}", "template/id.h.tmpl"] },
|
|
17
|
+
"prism/ast.h" => ->(out) { ["prism/templates/template.rb", "include/prism/ast.h", out] },
|
|
18
|
+
"prism/diagnostic.h" => ->(out) { ["prism/templates/template.rb", "include/prism/diagnostic.h", out] }
|
|
19
|
+
}.freeze
|
|
20
|
+
# Only what's asked for here decides how git behaves, so a user's config
|
|
21
|
+
# can't change the fetch. A fetch that stalls gives up after a minute.
|
|
22
|
+
GIT_ENV = { "GIT_CONFIG_GLOBAL" => File::NULL, "GIT_CONFIG_NOSYSTEM" => "1", "GIT_TERMINAL_PROMPT" => "0" }.freeze
|
|
23
|
+
GIT_CONFIG = %w[-c http.lowSpeedLimit=1000 -c http.lowSpeedTime=60].freeze
|
|
24
|
+
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
# The directories to build against. A given tree is only read, so headers
|
|
28
|
+
# it hasn't generated yet go in a directory of their own.
|
|
29
|
+
def include_dirs(given = ENV["RIPTIDE_RUBY_SOURCE"])
|
|
30
|
+
return [downloaded] if given.nil? || given.empty?
|
|
31
|
+
|
|
32
|
+
source = File.expand_path(given)
|
|
33
|
+
out = File.expand_path("ruby-generated")
|
|
34
|
+
generate(source, out)
|
|
35
|
+
[source, out]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The tree only gets its name once it's complete, so a build that died
|
|
39
|
+
# partway through starts over instead of building against half of it.
|
|
40
|
+
def downloaded(revision: RUBY_REVISION, repository: REPOSITORY)
|
|
41
|
+
# A full commit sha pins the exact source this Ruby was built from.
|
|
42
|
+
raise "no full revision for this Ruby: #{revision}" unless revision.match?(/\A\h{40}\z/)
|
|
43
|
+
|
|
44
|
+
dir = File.expand_path("ruby-#{revision}")
|
|
45
|
+
return dir if File.directory?(dir)
|
|
46
|
+
|
|
47
|
+
partial = "#{dir}.partial"
|
|
48
|
+
FileUtils.rm_rf(partial)
|
|
49
|
+
fetch(repository, revision, partial)
|
|
50
|
+
generate(partial, partial)
|
|
51
|
+
File.rename(partial, dir)
|
|
52
|
+
dir
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Checks out only PATHS at +revision+, fetching just that commit and the
|
|
56
|
+
# files it needs.
|
|
57
|
+
def fetch(repository, revision, dir)
|
|
58
|
+
git("init", "-q", dir)
|
|
59
|
+
git("-C", dir, "sparse-checkout", "set", "--no-cone", *PATHS)
|
|
60
|
+
git("-C", dir, "fetch", "-q", "--depth", "1", "--filter=blob:none", repository, revision)
|
|
61
|
+
git("-C", dir, "checkout", "-q", "FETCH_HEAD")
|
|
62
|
+
FileUtils.rm_rf(File.join(dir, ".git"))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def git(*args)
|
|
66
|
+
output, status = Open3.capture2e(GIT_ENV, "git", *GIT_CONFIG, *args)
|
|
67
|
+
raise "couldn't get Ruby's source: #{output.lines.last&.strip}" unless status.success?
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Writes the headers +source+ is missing into +out+, with the generators
|
|
71
|
+
# Ruby's own build runs.
|
|
72
|
+
def generate(source, out)
|
|
73
|
+
FileUtils.mkdir_p(out)
|
|
74
|
+
GENERATED.each do |header, command|
|
|
75
|
+
next if File.exist?(File.join(source, header))
|
|
76
|
+
|
|
77
|
+
system(RbConfig.ruby, *command.(File.join(out, header)), chdir: source, out: File::NULL, exception: true)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "minitest"
|
|
4
|
+
require "riptide"
|
|
5
|
+
|
|
6
|
+
# Minitest 5 loads every installed gem's minitest/*_plugin.rb by itself, so
|
|
7
|
+
# riptide does nothing in a process that never called Riptide.configure.
|
|
8
|
+
module Minitest
|
|
9
|
+
def self.plugin_riptide_init(_options)
|
|
10
|
+
return unless Riptide.configured?
|
|
11
|
+
# Rails' own boot calls Minitest.autorun again, which can run plugin
|
|
12
|
+
# init twice in one process.
|
|
13
|
+
return if @riptide_started
|
|
14
|
+
|
|
15
|
+
@riptide_started = true
|
|
16
|
+
Riptide::Session.new(Riptide.configuration).start
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
register_plugin(:riptide)
|
|
20
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Riptide
|
|
4
|
+
module Analysis
|
|
5
|
+
# A fixture file: any change changes the table the recorded version
|
|
6
|
+
# filled. Rows it moves into another table show up in that table's
|
|
7
|
+
# fixture listing.
|
|
8
|
+
class Fixture
|
|
9
|
+
def initialize(old_text, new_text, table:)
|
|
10
|
+
@changed = old_text.b != new_text.b
|
|
11
|
+
@table = table
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def change
|
|
15
|
+
change = Change.new
|
|
16
|
+
change.tables << @table if @changed
|
|
17
|
+
change
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Riptide
|
|
4
|
+
module Analysis
|
|
5
|
+
# Classifies each changed line of a Ruby file by the unit it's in. A
|
|
6
|
+
# change anywhere in a method or block changes that method or block,
|
|
7
|
+
# since Ruby doesn't fire a line event for every line it runs: `else`,
|
|
8
|
+
# `rescue`, or the second line of a multi-line call never show up.
|
|
9
|
+
class Ruby
|
|
10
|
+
# +load_facts+ is { line => { "owners", "called" } } for the old
|
|
11
|
+
# version, or nil if nothing loaded it while riptide was watching.
|
|
12
|
+
# +overrides+ says whether a new method takes over calls that used to
|
|
13
|
+
# go somewhere else. +ancestors+ names a class's ancestors, which a
|
|
14
|
+
# constant added to it can shadow.
|
|
15
|
+
def initialize(old_text, new_text, load_facts:, overrides:, ancestors: ->(_) { [] })
|
|
16
|
+
@old_text = old_text
|
|
17
|
+
@new_text = new_text
|
|
18
|
+
@facts = load_facts
|
|
19
|
+
@overrides = overrides
|
|
20
|
+
@ancestors = ancestors
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def change
|
|
24
|
+
@change = Change.new
|
|
25
|
+
@old = Source.parse(@old_text)
|
|
26
|
+
@new = Source.parse(@new_text)
|
|
27
|
+
hunks = (Diff.hunks(@old_text, @new_text) if @old && @new)
|
|
28
|
+
# Bytes that differ with no hunks to show for it, like a NUL byte
|
|
29
|
+
# that makes git call the file binary, count as everything changing.
|
|
30
|
+
if hunks.nil? || (hunks.empty? && @old_text.b != @new_text.b)
|
|
31
|
+
@change.whole!
|
|
32
|
+
return @change
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
hunks.each do |hunk|
|
|
36
|
+
hunk.old_lines.each { |line| classify_old(hunk, line) }
|
|
37
|
+
hunk.new_lines.each { |line| classify_new(hunk, line) }
|
|
38
|
+
end
|
|
39
|
+
shifted
|
|
40
|
+
@change
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def classify_old(hunk, line)
|
|
46
|
+
unit = @old.unit_at(line)
|
|
47
|
+
return unless unit
|
|
48
|
+
|
|
49
|
+
case unit.kind
|
|
50
|
+
when :method_header, :method_body then method_changed(unit.name)
|
|
51
|
+
when :block_body then old_block(hunk, line, unit)
|
|
52
|
+
else statement(@old, unit)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def classify_new(hunk, line)
|
|
57
|
+
unit = @new.unit_at(line)
|
|
58
|
+
return unless unit
|
|
59
|
+
|
|
60
|
+
case unit.kind
|
|
61
|
+
when :method_header, :method_body
|
|
62
|
+
if @old.methods.key?(unit.name)
|
|
63
|
+
method_changed(unit.name)
|
|
64
|
+
elsif @new.methods[unit.name].includers || @old.declares?(unit.owner, unit.name[/[#.](.+)\z/, 1]) || @overrides.call(unit.name)
|
|
65
|
+
@change.owned(@new.names_for(unit.owner), :override)
|
|
66
|
+
end
|
|
67
|
+
when :block_body then new_block(hunk, line, unit)
|
|
68
|
+
when :constant
|
|
69
|
+
Array(unit.name).each { |name| @old.constants.key?(name) ? constant(@new, name) : added_constant(name) }
|
|
70
|
+
else statement(@new, unit)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# A method that ran as called code while a class loaded, like a macro
|
|
75
|
+
# called from a class body, changes that class too.
|
|
76
|
+
def method_changed(name)
|
|
77
|
+
return if reordered?(name)
|
|
78
|
+
|
|
79
|
+
@change.methods << name
|
|
80
|
+
method = @old.methods[name]
|
|
81
|
+
ran_at_load(method.header.begin..method.body.end) if method
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# A line like `sig` applies to whichever method comes right after it,
|
|
85
|
+
# so a method that gains or loses one changes even when none of its
|
|
86
|
+
# own lines did, like the one a moved method used to sit behind.
|
|
87
|
+
def shifted
|
|
88
|
+
@old.methods.each do |name, old|
|
|
89
|
+
new = @new.methods[name]
|
|
90
|
+
method_changed(name) if new && old.slot && new.slot && behind(old) != behind(new)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# What comes right before a method: another method, a class-level
|
|
95
|
+
# line, or the start of its body.
|
|
96
|
+
def behind(method)
|
|
97
|
+
return :method if method.slot.last == :after
|
|
98
|
+
|
|
99
|
+
method.slot.size == 1 ? :start : :line
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The same definition between the same class-level lines runs the same,
|
|
103
|
+
# wherever it moved to among the methods around it.
|
|
104
|
+
def reordered?(name)
|
|
105
|
+
old = @old.methods[name]
|
|
106
|
+
new = @new.methods[name]
|
|
107
|
+
return false unless old && new && old.slot && @old.defined_once?(name) && @new.defined_once?(name)
|
|
108
|
+
|
|
109
|
+
old.slot == new.slot && old.text == new.text
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Without load facts there's no telling whether a block ran at load. A
|
|
113
|
+
# one-line declaration shares its line with its block, so the rest of
|
|
114
|
+
# the statement has to match for it to count as a block change.
|
|
115
|
+
def old_block(hunk, line, unit)
|
|
116
|
+
return statement(@old, unit.parent) unless @facts
|
|
117
|
+
return statement(@old, unit.parent) if starts_statement?(unit, line) && !same_head_in_new?(hunk, line, unit)
|
|
118
|
+
|
|
119
|
+
block_changed(unit.block)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def new_block(hunk, line, unit)
|
|
123
|
+
if starts_statement?(unit, line)
|
|
124
|
+
return if hunk.old_count.positive? && same_head_in_old?(hunk, line, unit)
|
|
125
|
+
|
|
126
|
+
return statement(@new, unit.parent, added: true)
|
|
127
|
+
end
|
|
128
|
+
return if old_side_decided?(hunk)
|
|
129
|
+
|
|
130
|
+
existing = around(hunk).filter_map { |old_line| @old.unit_at(old_line) }.find { |old| old.kind == :block_body }
|
|
131
|
+
existing && @facts ? block_changed(existing.block) : statement(@new, unit.parent)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Old lines with no unit, like comments, leave the new side to say
|
|
135
|
+
# what changed.
|
|
136
|
+
def old_side_decided?(hunk)
|
|
137
|
+
hunk.old_lines.any? { |old_line| @old.unit_at(old_line) }
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def block_changed(block)
|
|
141
|
+
@change.blocks << block
|
|
142
|
+
ran_at_load(block..(@old_text.count("\n") + 1), block: block)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Adds the classes any of +lines+ ran for as called code at load. For a
|
|
146
|
+
# block, only the lines inside that block count.
|
|
147
|
+
def ran_at_load(lines, block: nil)
|
|
148
|
+
return unless @facts
|
|
149
|
+
|
|
150
|
+
lines.each do |line|
|
|
151
|
+
fact = @facts[line]
|
|
152
|
+
next unless fact && fact["called"]
|
|
153
|
+
next if block && @old.unit_at(line)&.block != block && line != block
|
|
154
|
+
|
|
155
|
+
@change.owned(fact["owners"], :code)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def statement(source, unit, added: false)
|
|
160
|
+
case unit.kind
|
|
161
|
+
when :constant then Array(unit.name).each { |name| added ? added_constant(name) : constant(source, name) }
|
|
162
|
+
when :declaration then @change.owned(source.names_for(unit.owner), :declaration)
|
|
163
|
+
when :top then @change.suite!
|
|
164
|
+
when :data then @change.whole!
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def constant(source, name)
|
|
169
|
+
@change.constants.merge(source.names_for(name))
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# A new Card::LIMIT can shadow a top-level LIMIT, or a LIMIT Card
|
|
173
|
+
# inherited, so every name it could have resolved to counts as changed.
|
|
174
|
+
def added_constant(name)
|
|
175
|
+
@new.names_for(name).each do |full|
|
|
176
|
+
parts = full.split("::")
|
|
177
|
+
parts.each_index { |i| @change.constants << parts.drop(i).join("::") }
|
|
178
|
+
@ancestors.call(parts[0...-1].join("::")).each { |ancestor| @change.constants << "#{ancestor}::#{parts.last}" } if parts.size > 1
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def starts_statement?(unit, line)
|
|
183
|
+
unit.line == line
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def same_head_in_new?(hunk, line, unit)
|
|
187
|
+
same_head?(@new.unit_at(hunk.new_start + (line - hunk.old_start)), unit)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def same_head_in_old?(hunk, line, unit)
|
|
191
|
+
same_head?(@old.unit_at(hunk.old_start + (line - hunk.new_start)), unit)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def same_head?(counterpart, unit)
|
|
195
|
+
return false unless counterpart
|
|
196
|
+
|
|
197
|
+
head = ->(u) { (u.parent || u).head }
|
|
198
|
+
!head.(unit).nil? && head.(counterpart) == head.(unit)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# The old lines a hunk replaces and the ones on either side of it. For
|
|
202
|
+
# an insertion git's old_start is the line it follows.
|
|
203
|
+
def around(hunk)
|
|
204
|
+
(hunk.old_start - 1..hunk.old_start + [hunk.old_count, 1].max).to_a
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
module Riptide
|
|
6
|
+
module Analysis
|
|
7
|
+
# db/schema.rb: each changed line maps to the table its schema call
|
|
8
|
+
# names. The schema version and anything not about one table change the
|
|
9
|
+
# file as a whole.
|
|
10
|
+
class Schema
|
|
11
|
+
TABLE_CALLS = %i[create_table add_index add_foreign_key change_table create_join_table].freeze
|
|
12
|
+
|
|
13
|
+
def initialize(old_text, new_text)
|
|
14
|
+
@old_text = old_text
|
|
15
|
+
@new_text = new_text
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def change
|
|
19
|
+
change = Change.new
|
|
20
|
+
old_tables = tables_by_line(@old_text)
|
|
21
|
+
new_tables = tables_by_line(@new_text)
|
|
22
|
+
Diff.hunks(@old_text, @new_text).each do |hunk|
|
|
23
|
+
hunk.old_lines.each { |line| classify(change, old_tables, line) }
|
|
24
|
+
hunk.new_lines.each { |line| classify(change, new_tables, line) }
|
|
25
|
+
end
|
|
26
|
+
change
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def classify(change, tables, line)
|
|
32
|
+
return unless tables.key?(line)
|
|
33
|
+
|
|
34
|
+
names = tables[line]
|
|
35
|
+
names.empty? ? change.whole! : change.tables.merge(names)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# { line => [table, ...] }, empty for lines in other statements.
|
|
39
|
+
def tables_by_line(text)
|
|
40
|
+
result = {}
|
|
41
|
+
result.default = nil
|
|
42
|
+
program = Prism.parse(text).value
|
|
43
|
+
definition = program.statements.body.find { |node| node.is_a?(Prism::CallNode) && node.block }
|
|
44
|
+
statements = definition&.block&.body&.body || []
|
|
45
|
+
statements.each do |call|
|
|
46
|
+
names = table_names(call)
|
|
47
|
+
(call.location.start_line..call.location.end_line).each { |line| result[line] = names }
|
|
48
|
+
end
|
|
49
|
+
result
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def table_names(call)
|
|
53
|
+
return [] unless call.is_a?(Prism::CallNode) && TABLE_CALLS.include?(call.name)
|
|
54
|
+
|
|
55
|
+
tables = call.arguments&.arguments&.grep(Prism::StringNode)&.map(&:unescaped) || []
|
|
56
|
+
call.name == :add_foreign_key || call.name == :create_join_table ? tables.first(2) : tables.first(1)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Riptide
|
|
4
|
+
module Analysis
|
|
5
|
+
# A pg_dump structure file: each changed line maps to the statement it's
|
|
6
|
+
# in, and the statement to its tables. Migration versions are ignored,
|
|
7
|
+
# and anything that isn't about a table changes the file as a whole.
|
|
8
|
+
class Sql
|
|
9
|
+
TABLE = /(?:CREATE (?:UNLOGGED )?TABLE|ALTER TABLE(?: ONLY)?|CREATE (?:UNIQUE )?INDEX .*? ON(?: ONLY)?|CREATE TRIGGER .*? ON|COMMENT ON (?:TABLE|COLUMN)|CREATE (?:MATERIALIZED )?VIEW)\s+(?:IF NOT EXISTS\s+)?((?:"?\w+"?\.)?"?\w+"?)/m
|
|
10
|
+
OWNED_BY = /OWNED BY ((?:"?\w+"?\.)?"?\w+"?)\.\w+/
|
|
11
|
+
# A foreign key changes what inserts and deletes on the table it points
|
|
12
|
+
# at can do, so that table changes too.
|
|
13
|
+
REFERENCES = /REFERENCES\s+((?:"?\w+"?\.)?"?\w+"?)/
|
|
14
|
+
MIGRATIONS = /\AINSERT INTO "?schema_migrations"?/
|
|
15
|
+
SEQUENCE = /(?:CREATE|ALTER) SEQUENCE\s+(?:IF NOT EXISTS\s+)?((?:"?\w+"?\.)?"?\w+"?)/
|
|
16
|
+
NEXTVAL = /nextval\('((?:"?\w+"?\.)?"?\w+"?)'/
|
|
17
|
+
|
|
18
|
+
def initialize(old_text, new_text)
|
|
19
|
+
@old_text = old_text
|
|
20
|
+
@new_text = new_text
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def change
|
|
24
|
+
change = Change.new
|
|
25
|
+
old_statements = statements(@old_text)
|
|
26
|
+
new_statements = statements(@new_text)
|
|
27
|
+
@sequences = sequence_tables(old_statements.values + new_statements.values)
|
|
28
|
+
Diff.hunks(@old_text, @new_text).each do |hunk|
|
|
29
|
+
hunk.old_lines.each { |line| classify(change, old_statements[line]) }
|
|
30
|
+
hunk.new_lines.each { |line| classify(change, new_statements[line]) }
|
|
31
|
+
end
|
|
32
|
+
change
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def classify(change, statement)
|
|
38
|
+
return if statement.nil? || statement.match?(MIGRATIONS)
|
|
39
|
+
|
|
40
|
+
table = statement[OWNED_BY, 1] || statement[TABLE, 1]
|
|
41
|
+
return [table, *statement.scan(REFERENCES).flatten].each { |name| change.tables << bare(name) } if table
|
|
42
|
+
|
|
43
|
+
# A sequence belongs to the tables that own it or take their ids
|
|
44
|
+
# from it, like the one pg_dump writes for each new table.
|
|
45
|
+
tables = statement[SEQUENCE, 1] && @sequences[bare(statement[SEQUENCE, 1])]
|
|
46
|
+
tables ? change.tables.merge(tables) : change.whole!
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# { sequence => Set of tables }, from OWNED BY and nextval defaults.
|
|
50
|
+
def sequence_tables(statements)
|
|
51
|
+
statements.uniq.each_with_object({}) do |statement, sequences|
|
|
52
|
+
if (owner = statement[OWNED_BY, 1]) && (sequence = statement[SEQUENCE, 1])
|
|
53
|
+
(sequences[bare(sequence)] ||= Set.new) << bare(owner)
|
|
54
|
+
elsif (table = statement[TABLE, 1])
|
|
55
|
+
statement.scan(NEXTVAL).flatten.each { |sequence| (sequences[bare(sequence)] ||= Set.new) << bare(table) }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def bare(name) = name.delete('"').split(".").last
|
|
61
|
+
|
|
62
|
+
# { line => statement text }. Statements end with a semicolon at the
|
|
63
|
+
# end of a line; comments and blank lines belong to none.
|
|
64
|
+
def statements(text)
|
|
65
|
+
result = {}
|
|
66
|
+
current = []
|
|
67
|
+
lines = text.lines
|
|
68
|
+
lines.each.with_index(1) do |line, number|
|
|
69
|
+
stripped = line.strip
|
|
70
|
+
next if current.empty? && (stripped.empty? || stripped.start_with?("--"))
|
|
71
|
+
|
|
72
|
+
current << number
|
|
73
|
+
next unless stripped.end_with?(";")
|
|
74
|
+
|
|
75
|
+
statement = current.map { |n| lines[n - 1] }.join
|
|
76
|
+
current.each { |n| result[n] = statement }
|
|
77
|
+
current = []
|
|
78
|
+
end
|
|
79
|
+
result
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Riptide
|
|
4
|
+
# Works out what changed in a file since a recorded version of it, once
|
|
5
|
+
# per version however many tests depended on it.
|
|
6
|
+
class Analyzer
|
|
7
|
+
# +blobs+ returns a recorded version's content, or nil when this
|
|
8
|
+
# repository doesn't have it. +load_facts+ returns a version's load
|
|
9
|
+
# facts, or nil.
|
|
10
|
+
def initialize(layout:, blobs:, load_facts:)
|
|
11
|
+
@layout = layout
|
|
12
|
+
@blobs = blobs
|
|
13
|
+
@load_facts = load_facts
|
|
14
|
+
@changes = {}
|
|
15
|
+
@sources = {}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The Change from +old_sha+ to what's on disk, or nil when the recorded
|
|
19
|
+
# version isn't available.
|
|
20
|
+
def change(path, old_sha)
|
|
21
|
+
key = [path, old_sha]
|
|
22
|
+
return @changes[key] if @changes.key?(key)
|
|
23
|
+
|
|
24
|
+
old_text = @blobs.call(old_sha)
|
|
25
|
+
@changes[key] = old_text && analyze(path, old_sha, text(old_text), text(File.binread(File.join(@layout.root, path))))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
# App source is UTF-8. Anything that isn't stays bytes, and only whole
|
|
31
|
+
# files get compared that way.
|
|
32
|
+
def text(bytes)
|
|
33
|
+
utf8 = bytes.dup.force_encoding(Encoding::UTF_8)
|
|
34
|
+
utf8.valid_encoding? ? utf8 : bytes.b
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def analyze(path, old_sha, old_text, new_text)
|
|
38
|
+
case @layout.kind(path)
|
|
39
|
+
when :ruby
|
|
40
|
+
Analysis::Ruby.new(old_text, new_text, load_facts: @load_facts.call(old_sha), overrides: ->(name) { overrides?(name, path) }, ancestors: method(:ancestors)).change
|
|
41
|
+
when :sql then Analysis::Sql.new(old_text, new_text).change
|
|
42
|
+
when :schema then Analysis::Schema.new(old_text, new_text).change
|
|
43
|
+
when :fixture then Analysis::Fixture.new(old_text, new_text, table: @layout.fixture_table(path, old_text)).change
|
|
44
|
+
else Change.new.tap { |change| change.whole! unless old_text.b == new_text.b }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Whether a method newly defined in +path+, like "Card#to_s" or
|
|
49
|
+
# "Card.build", takes over calls that used to go somewhere else: an
|
|
50
|
+
# inherited method, an attribute reader, method_missing, or the same
|
|
51
|
+
# method defined in another file that reopens the class. A module's
|
|
52
|
+
# method lands in every class that includes or extends it, ahead of
|
|
53
|
+
# what comes after it there. A method added to a class from outside the
|
|
54
|
+
# app, like String, always might, and so does anything riptide can't
|
|
55
|
+
# check. A new test is only ever called by name.
|
|
56
|
+
def overrides?(name, path)
|
|
57
|
+
owner_name, separator, method_name = name.rpartition(/[#.]/)
|
|
58
|
+
owner = Object.const_get(owner_name)
|
|
59
|
+
method_name = method_name.to_sym
|
|
60
|
+
return false if owner.is_a?(Class) && defined?(Minitest::Test) && owner < Minitest::Test && method_name.start_with?("test_")
|
|
61
|
+
return true unless app_class?(owner_name)
|
|
62
|
+
return true if defined_elsewhere?(owner, name, path)
|
|
63
|
+
|
|
64
|
+
if owner.is_a?(Class) || separator == "."
|
|
65
|
+
target = separator == "." ? owner.singleton_class : owner
|
|
66
|
+
takes_over?(target, owner, method_name, after: target)
|
|
67
|
+
else
|
|
68
|
+
includers = ObjectSpace.each_object(Module).flat_map do |mod|
|
|
69
|
+
next [] if mod.equal?(owner) || mod.singleton_class?
|
|
70
|
+
|
|
71
|
+
[(mod if mod.ancestors.include?(owner)), (mod.singleton_class if mod.is_a?(owner))].compact
|
|
72
|
+
end
|
|
73
|
+
includers.empty? || includers.any? { |target| takes_over?(target, Tracer.attached(target), method_name, after: owner) }
|
|
74
|
+
end
|
|
75
|
+
rescue StandardError
|
|
76
|
+
true
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Whether +method_name+ on +target+ used to resolve past +after+ in its
|
|
80
|
+
# ancestry, or to an attribute or method_missing.
|
|
81
|
+
def takes_over?(target, owner, method_name, after:)
|
|
82
|
+
ancestors = target.ancestors
|
|
83
|
+
ancestors.drop(ancestors.index(after) + 1).any? { |ancestor| defines?(ancestor, method_name) } ||
|
|
84
|
+
attribute?(owner, method_name) || catches_missing?(target, owner, method_name)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def ancestors(name)
|
|
88
|
+
Object.const_get(name).ancestors.drop(1).filter_map(&:name)
|
|
89
|
+
rescue StandardError
|
|
90
|
+
[]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def app_class?(name)
|
|
94
|
+
location = Object.const_source_location(name)&.first
|
|
95
|
+
location&.start_with?("#{File.expand_path(@layout.root)}/")
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def defined_elsewhere?(owner, name, path)
|
|
99
|
+
(Tracer.files_for(owner) | Tracer.files_for(owner.singleton_class)).any? do |file|
|
|
100
|
+
file != path && source(file)&.methods&.key?(name)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def source(path)
|
|
105
|
+
@sources.fetch(path) do
|
|
106
|
+
full = File.join(@layout.root, path)
|
|
107
|
+
@sources[path] = File.file?(full) ? Source.parse(File.read(full)) : nil
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Ruby's own method_missing and respond_to_missing? catch nothing. The
|
|
112
|
+
# app's own might catch anything. A gem's, like ActiveRecord's dynamic
|
|
113
|
+
# finders, catches what its respond_to_missing? says it does.
|
|
114
|
+
def catches_missing?(target, owner, method_name)
|
|
115
|
+
missing = target.instance_method(:method_missing).owner
|
|
116
|
+
responds = target.instance_method(:respond_to_missing?).owner
|
|
117
|
+
return false if missing == BasicObject && responds == Kernel
|
|
118
|
+
return true if app_code?(target.instance_method(:method_missing)) || app_code?(target.instance_method(:respond_to_missing?))
|
|
119
|
+
|
|
120
|
+
receiver = target.singleton_class? ? owner : target.allocate
|
|
121
|
+
responds.instance_method(:respond_to_missing?).bind_call(receiver, method_name, true)
|
|
122
|
+
rescue StandardError
|
|
123
|
+
true
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def app_code?(method)
|
|
127
|
+
method.source_location&.first&.start_with?("#{File.expand_path(@layout.root)}/")
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def defines?(mod, method_name)
|
|
131
|
+
mod.method_defined?(method_name, false) || mod.private_method_defined?(method_name, false)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def attribute?(owner, method_name)
|
|
135
|
+
owner.respond_to?(:attribute_names) && owner.attribute_names.include?(method_name.to_s.delete_suffix("=").delete_suffix("?"))
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|