rails_tia 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/LICENSE.txt +21 -0
- data/README.md +180 -0
- data/docs/configuration.md +109 -0
- data/docs/github-actions.md +147 -0
- data/exe/rails-tia +5 -0
- data/ext/rails_tia/allocated_classes.c +111 -0
- data/ext/rails_tia/extconf.rb +7 -0
- data/lib/rails_tia/action_view_support.rb +67 -0
- data/lib/rails_tia/audit.rb +42 -0
- data/lib/rails_tia/boot.rb +12 -0
- data/lib/rails_tia/ci/base.rb +38 -0
- data/lib/rails_tia/ci/git.rb +49 -0
- data/lib/rails_tia/ci/github_actions.rb +237 -0
- data/lib/rails_tia/ci/gitlab.rb +199 -0
- data/lib/rails_tia/ci.rb +36 -0
- data/lib/rails_tia/cli.rb +328 -0
- data/lib/rails_tia/configuration.rb +169 -0
- data/lib/rails_tia/constant_reads.rb +84 -0
- data/lib/rails_tia/context.rb +17 -0
- data/lib/rails_tia/coverage_collector.rb +198 -0
- data/lib/rails_tia/described_class_support.rb +75 -0
- data/lib/rails_tia/fallback.rb +79 -0
- data/lib/rails_tia/formatter.rb +88 -0
- data/lib/rails_tia/http.rb +89 -0
- data/lib/rails_tia/junit_report.rb +116 -0
- data/lib/rails_tia/map.rb +178 -0
- data/lib/rails_tia/metrics.rb +71 -0
- data/lib/rails_tia/minitest.rb +243 -0
- data/lib/rails_tia/native_extension.rb +6 -0
- data/lib/rails_tia/object_allocations.rb +23 -0
- data/lib/rails_tia/predictor.rb +61 -0
- data/lib/rails_tia/rails_support.rb +86 -0
- data/lib/rails_tia/run_report.rb +116 -0
- data/lib/rails_tia/static_mapping.rb +85 -0
- data/lib/rails_tia/templates/minitest.yml +39 -0
- data/lib/rails_tia/templates/rspec.yml +45 -0
- data/lib/rails_tia/version.rb +3 -0
- data/lib/rails_tia.rb +27 -0
- data/skills/rails-tia/SKILL.md +49 -0
- metadata +145 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'optparse'
|
|
3
|
+
require 'rails_tia'
|
|
4
|
+
require 'rails_tia/audit'
|
|
5
|
+
require 'rails_tia/ci'
|
|
6
|
+
require 'rails_tia/junit_report'
|
|
7
|
+
require 'rails_tia/metrics'
|
|
8
|
+
require 'rails_tia/run_report'
|
|
9
|
+
|
|
10
|
+
module RailsTia
|
|
11
|
+
class Cli
|
|
12
|
+
USAGE = <<~TEXT.freeze
|
|
13
|
+
Usage: rails-tia <command> [options]
|
|
14
|
+
|
|
15
|
+
init Create a Rails configuration (--runner rspec|minitest)
|
|
16
|
+
select Print selected specs (or full_run_token for a full run)
|
|
17
|
+
map merge Merge map fragments into a single map
|
|
18
|
+
map fetch Fetch and merge map fragments from CI
|
|
19
|
+
audit Compare full-suite results with predictions to detect missed failures
|
|
20
|
+
doctor Check configuration and environment
|
|
21
|
+
version Print the version
|
|
22
|
+
|
|
23
|
+
Common options:
|
|
24
|
+
--config PATH Configuration file (default: .rails_tia.yml)
|
|
25
|
+
--root PATH Project root (default: current directory)
|
|
26
|
+
|
|
27
|
+
Set RAILS_TIA_REPORT_DIR to write JSON reports for CI storage.
|
|
28
|
+
TEXT
|
|
29
|
+
|
|
30
|
+
def self.start(argv)
|
|
31
|
+
new(argv).run
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def initialize(argv)
|
|
35
|
+
@argv = argv.dup
|
|
36
|
+
@options = {}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def run
|
|
40
|
+
command = @argv.shift
|
|
41
|
+
case command
|
|
42
|
+
when 'init' then init
|
|
43
|
+
when 'select' then select
|
|
44
|
+
when 'map' then map_command
|
|
45
|
+
when 'audit' then audit
|
|
46
|
+
when 'doctor' then doctor
|
|
47
|
+
when 'version', '--version', '-v' then puts(VERSION) || 0
|
|
48
|
+
when nil, 'help', '--help', '-h' then puts(USAGE) || 0
|
|
49
|
+
else
|
|
50
|
+
warn "Unknown command: #{command}\n\n#{USAGE}"
|
|
51
|
+
1
|
|
52
|
+
end
|
|
53
|
+
rescue Ci::Gitlab::AuthenticationError, Ci::GithubActions::Error, Http::Error, Ci::Git::CommandError,
|
|
54
|
+
Ci::UnknownAdapterError, Ci::Base::UnsupportedError, Map::FormatError, RunReport::Error,
|
|
55
|
+
StaticMapping::InvalidGlob, Errno::ENOENT => e
|
|
56
|
+
warn "[rails_tia] #{e.message}"
|
|
57
|
+
1
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def init
|
|
63
|
+
runner = nil
|
|
64
|
+
parse!('init') do |parser|
|
|
65
|
+
parser.on('--runner RUNNER', %w[rspec minitest], 'Test runner (rspec or minitest)') { |value| runner = value }
|
|
66
|
+
end
|
|
67
|
+
path = config_path
|
|
68
|
+
template = File.read(File.join(__dir__, 'templates', "#{runner || default_runner}.yml"))
|
|
69
|
+
contents = template.sub(" # Generated configuration path\n") { config_path_rule(path) }
|
|
70
|
+
File.open(path, 'wx') { |file| file.write(contents) }
|
|
71
|
+
puts "Created #{path}"
|
|
72
|
+
0
|
|
73
|
+
rescue Errno::EEXIST
|
|
74
|
+
warn "[rails_tia] #{path} already exists"
|
|
75
|
+
1
|
|
76
|
+
rescue SystemCallError, OptionParser::ParseError => e
|
|
77
|
+
warn "[rails_tia] #{e.message}"
|
|
78
|
+
1
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def default_runner
|
|
82
|
+
return 'rspec' if File.directory?(File.join(root, 'spec'))
|
|
83
|
+
|
|
84
|
+
File.directory?(File.join(root, 'test')) ? 'minitest' : 'rspec'
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def config_path_rule(path)
|
|
88
|
+
root_prefix = File.join(File.realpath(File.expand_path(root)), '')
|
|
89
|
+
absolute = File.join(File.realpath(File.dirname(path)), File.basename(path))
|
|
90
|
+
return '' unless absolute.start_with?(root_prefix)
|
|
91
|
+
|
|
92
|
+
relative = absolute.delete_prefix(root_prefix)
|
|
93
|
+
pattern = relative.gsub(/[\\*?\[\]{}]/) { |char| "\\#{char}" }
|
|
94
|
+
" - #{JSON.generate(pattern)}\n"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def select
|
|
98
|
+
parse!('select') do |parser|
|
|
99
|
+
parser.on('--junit-dir DIR', 'JUnit XML directory for timing-based savings estimates') { |v| @options[:junit_dir] = v }
|
|
100
|
+
parser.on('--metrics PATH', 'Metrics output path') { |v| @options[:metrics] = v }
|
|
101
|
+
parser.on('--changed-files-from PATH', 'Read changed files from a file instead of CI (- for stdin)') do |v|
|
|
102
|
+
@options[:changed_files_from] = v
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
report = RunReport.for('select', config)
|
|
107
|
+
map_id = nil
|
|
108
|
+
map = load_map { |json| map_id = Digest::SHA256.hexdigest(json) if report }
|
|
109
|
+
result = Predictor.new(config: config, map: map).call(select_context, trace: !!report)
|
|
110
|
+
|
|
111
|
+
metrics = Metrics.new(config: config, result: result, junit_report: junit_report(@options[:junit_dir]))
|
|
112
|
+
data = metrics.to_h
|
|
113
|
+
metrics.write(@options[:metrics] || config.metrics_path, data: data)
|
|
114
|
+
report&.write do
|
|
115
|
+
data.merge(
|
|
116
|
+
'map_id' => map_id, 'changed_files' => result.sources, 'selected_specs' => result.specs,
|
|
117
|
+
'reason_code' => result.reason_code, 'matches' => result.matches,
|
|
118
|
+
)
|
|
119
|
+
end
|
|
120
|
+
puts(result.full_run? ? config.full_run_token : result.specs)
|
|
121
|
+
warn metrics.summary(data)
|
|
122
|
+
0
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def map_command
|
|
126
|
+
case @argv.shift
|
|
127
|
+
when 'merge' then map_merge
|
|
128
|
+
when 'fetch' then map_fetch
|
|
129
|
+
else
|
|
130
|
+
warn "Usage: rails-tia map [merge|fetch]"
|
|
131
|
+
1
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def map_merge
|
|
136
|
+
parse!('map merge') do |parser|
|
|
137
|
+
parser.on('--fragments DIR', 'Map fragment directory') { |v| @options[:fragments] = v }
|
|
138
|
+
parser.on('-o', '--output PATH', 'Merged map output path') { |v| @options[:output] = v }
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
dir = @options[:fragments] || config.map_fragment_dir
|
|
142
|
+
fragments = Dir.glob(File.join(dir, '*.json*')).sort
|
|
143
|
+
if fragments.empty?
|
|
144
|
+
warn "[rails_tia] No map fragments found: #{dir}"
|
|
145
|
+
return 1
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
merge_fragments(fragments)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def map_fetch
|
|
152
|
+
parse!('map fetch') do |parser|
|
|
153
|
+
parser.on('--dest DIR', 'Fragment download directory') { |v| @options[:dest] = v }
|
|
154
|
+
parser.on('--[no-]merge', 'Merge after downloading (default: enabled)') { |v| @options[:merge] = v }
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
adapter = Ci.build(config)
|
|
158
|
+
dest = @options[:dest] || adapter.map_download_dir
|
|
159
|
+
fragments = adapter.fetch_map_fragments(dest)
|
|
160
|
+
if fragments.empty?
|
|
161
|
+
# Ensure even a reused workspace falls back to a full run when no map is available.
|
|
162
|
+
File.delete(config.map_path) if File.exist?(config.map_path)
|
|
163
|
+
warn '[rails_tia] No map fragments available (selection will fall back to a full run)'
|
|
164
|
+
return 0
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
warn "[rails_tia] Fetched #{fragments.size} fragments -> #{dest}"
|
|
168
|
+
return 0 if @options[:merge] == false
|
|
169
|
+
|
|
170
|
+
merge_fragments(fragments)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def merge_fragments(fragments)
|
|
174
|
+
path = @options[:output] || config.map_path
|
|
175
|
+
report = RunReport.for('map_merge', config)
|
|
176
|
+
if report
|
|
177
|
+
map = report.merge_map(fragments, path)
|
|
178
|
+
else
|
|
179
|
+
map = Map.merge_files(fragments)
|
|
180
|
+
map.dump(path)
|
|
181
|
+
end
|
|
182
|
+
warn format('[rails_tia] Merged %d fragments -> %s (%d specs / %d sources)',
|
|
183
|
+
fragments.size, path, map.specs.size, map.sources.size)
|
|
184
|
+
0
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def audit
|
|
188
|
+
parse!('audit') do |parser|
|
|
189
|
+
parser.on('--junit DIR', 'JUnit XML directory from a full-suite run') { |v| @options[:junit] = v }
|
|
190
|
+
parser.on('--predicted PATH', 'Predicted spec list (one path per line)') { |v| @options[:predicted] = v }
|
|
191
|
+
parser.on('--selection-report PATH', 'Use the JSON report from select as the prediction') { |v| @options[:selection_report] = v }
|
|
192
|
+
parser.on('--expect-junit FILE', 'Expected XML path relative to --junit (repeat per shard)') do |v|
|
|
193
|
+
(@options[:expected_junit] ||= []) << v
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
unless @options[:junit] && [@options[:predicted], @options[:selection_report]].compact.one?
|
|
198
|
+
warn '[rails_tia] Required options: --junit and exactly one of --predicted or --selection-report'
|
|
199
|
+
return 1
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
if @options[:selection_report]
|
|
203
|
+
selection = RunReport.load_selection(@options[:selection_report])
|
|
204
|
+
if selection['commit_sha'] && RailsTia.commit_sha && selection['commit_sha'] != RailsTia.commit_sha
|
|
205
|
+
raise RunReport::Error, 'Selection report is from a different commit'
|
|
206
|
+
end
|
|
207
|
+
predicted = selection.fetch('selected_specs')
|
|
208
|
+
full_run = selection.fetch('full_run')
|
|
209
|
+
else
|
|
210
|
+
predicted = File.readlines(@options[:predicted], chomp: true).reject(&:empty?)
|
|
211
|
+
full_run = predicted == [config.full_run_token]
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
expected = @options.fetch(:expected_junit, [])
|
|
215
|
+
junit = JunitReport.load_dir(@options[:junit], expected_files: expected)
|
|
216
|
+
result = Audit.new(junit_report: junit, predicted_specs: predicted, full_run: full_run).call
|
|
217
|
+
report = RunReport.for('audit', config)
|
|
218
|
+
report&.write do
|
|
219
|
+
valid = result.validation_errors.empty?
|
|
220
|
+
{
|
|
221
|
+
prediction_id: selection&.fetch('report_id'), map_id: selection&.fetch('map_id', nil),
|
|
222
|
+
full_run: full_run, valid: valid, failed_specs: result.failed_specs.sort,
|
|
223
|
+
missed_specs: valid ? result.missed_specs.sort : nil, validation_errors: result.validation_errors,
|
|
224
|
+
junit_file_count: junit.parsed_file_count, expected_junit_files: expected,
|
|
225
|
+
missing_junit_files: junit.missing_files, junit_complete: junit.complete?,
|
|
226
|
+
}
|
|
227
|
+
end
|
|
228
|
+
warn result.summary
|
|
229
|
+
result.ok? ? 0 : 1
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def doctor
|
|
233
|
+
parse!('doctor')
|
|
234
|
+
checks = [
|
|
235
|
+
['Configuration', config_status],
|
|
236
|
+
['Spec discovery', spec_status],
|
|
237
|
+
['Coverage map', map_status],
|
|
238
|
+
['CI adapter', adapter_status],
|
|
239
|
+
['Static mappings', static_mapping_status],
|
|
240
|
+
]
|
|
241
|
+
ok = checks.all? { |_, (result, _)| result }
|
|
242
|
+
checks.each { |name, (result, detail)| puts format('%s %-16s %s', result ? 'ok ' : 'NG ', name, detail) }
|
|
243
|
+
ok ? 0 : 1
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def config_status
|
|
247
|
+
path = config_path
|
|
248
|
+
File.exist?(path) ? [true, path] : [false, "#{path} not found (using defaults)"]
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def spec_status
|
|
252
|
+
count = config.all_specs.size
|
|
253
|
+
[count.positive?, "#{config.spec_glob} -> #{count} specs"]
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def map_status
|
|
257
|
+
return [false, "#{config.map_path} does not exist"] unless File.exist?(config.map_path)
|
|
258
|
+
|
|
259
|
+
map = Map.load(config.map_path)
|
|
260
|
+
stale = map.stale?(max_age_days: config.map_max_age_days)
|
|
261
|
+
detail = "#{map.specs.size} specs / #{map.sources.size} sources / generated #{map.generated_at&.utc&.iso8601 || 'unknown'}"
|
|
262
|
+
[!stale, stale ? "#{detail} (stale)" : detail]
|
|
263
|
+
rescue Map::FormatError => e
|
|
264
|
+
[false, e.message]
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def adapter_status
|
|
268
|
+
adapter = Ci.build(config)
|
|
269
|
+
[true, "#{adapter.class.name} (MR: #{adapter.merge_request?})"]
|
|
270
|
+
rescue Ci::UnknownAdapterError => e
|
|
271
|
+
[false, e.message]
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def static_mapping_status
|
|
275
|
+
StaticMapping.new(config.static_mapping_rules, root: root)
|
|
276
|
+
[true, "#{config.static_mapping_rules.size} rules"]
|
|
277
|
+
rescue RegexpError, KeyError, StaticMapping::InvalidGlob => e
|
|
278
|
+
[false, e.message]
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def parse!(command)
|
|
282
|
+
OptionParser.new do |parser|
|
|
283
|
+
parser.banner = "Usage: rails-tia #{command} [options]"
|
|
284
|
+
parser.on('--config PATH') { |v| @options[:config] = v }
|
|
285
|
+
parser.on('--root PATH') { |v| @options[:root] = v }
|
|
286
|
+
yield parser if block_given?
|
|
287
|
+
end.parse!(@argv)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def root
|
|
291
|
+
@options[:root] || Dir.pwd
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def config
|
|
295
|
+
@config ||= Configuration.load(config_path, root: root)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def config_path
|
|
299
|
+
@options[:config] || File.join(File.expand_path(root), Configuration::DEFAULT_PATH)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Allow changed files to be supplied externally.
|
|
303
|
+
# This supports offline evaluation of savings and missed failures using historical merge commits.
|
|
304
|
+
def select_context
|
|
305
|
+
source = @options[:changed_files_from]
|
|
306
|
+
return Ci.build(config).context if source.nil?
|
|
307
|
+
|
|
308
|
+
lines = source == '-' ? $stdin.read : File.read(source)
|
|
309
|
+
Context.new(
|
|
310
|
+
changed_files: lines.split("\n").map(&:strip).reject(&:empty?),
|
|
311
|
+
merge_request: true,
|
|
312
|
+
)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def load_map(&on_json)
|
|
316
|
+
path = config.map_path
|
|
317
|
+
return Map.new unless File.exist?(path)
|
|
318
|
+
|
|
319
|
+
Map.load(path, &on_json)
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def junit_report(dir)
|
|
323
|
+
return nil if dir.nil? || !Dir.exist?(dir)
|
|
324
|
+
|
|
325
|
+
JunitReport.load_dir(dir)
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
end
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
|
|
3
|
+
module RailsTia
|
|
4
|
+
# Load .rails_tia.yml and apply defaults.
|
|
5
|
+
#
|
|
6
|
+
# Match path patterns with File.fnmatch?(FNM_PATHNAME | FNM_EXTGLOB).
|
|
7
|
+
# Use patterns such as config/**/*, as in GitLab CI rules:changes.
|
|
8
|
+
# The pattern config/** does not match nested paths.
|
|
9
|
+
class Configuration
|
|
10
|
+
DEFAULT_PATH = '.rails_tia.yml'.freeze
|
|
11
|
+
FNMATCH_FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB
|
|
12
|
+
|
|
13
|
+
DEFAULTS = {
|
|
14
|
+
'spec_glob' => 'spec/**/*_spec.rb',
|
|
15
|
+
# The single line emitted by select for a full run; use a value accepted by RSpec or a splitting script.
|
|
16
|
+
'full_run_token' => 'spec',
|
|
17
|
+
'map' => {
|
|
18
|
+
'fragment_dir' => 'tmp/rails_tia/fragments',
|
|
19
|
+
'path' => 'tmp/rails_tia/map.json.gz',
|
|
20
|
+
'max_age_days' => 3,
|
|
21
|
+
},
|
|
22
|
+
'full_run' => {
|
|
23
|
+
'paths' => [],
|
|
24
|
+
'max_changed_files' => 300,
|
|
25
|
+
'labels' => [],
|
|
26
|
+
'commit_message_tokens' => [],
|
|
27
|
+
},
|
|
28
|
+
'static_mapping' => [],
|
|
29
|
+
'described_class' => { 'enabled' => true },
|
|
30
|
+
'rails' => {
|
|
31
|
+
'enabled' => 'auto',
|
|
32
|
+
'active_record' => true,
|
|
33
|
+
'action_view' => true,
|
|
34
|
+
'object_allocations' => 'auto',
|
|
35
|
+
'constant_reads' => true,
|
|
36
|
+
'app_dirs' => [],
|
|
37
|
+
},
|
|
38
|
+
'ci' => {
|
|
39
|
+
'adapter' => 'auto',
|
|
40
|
+
'gitlab' => {
|
|
41
|
+
'map_ref' => 'master',
|
|
42
|
+
'map_pipeline_source' => 'schedule',
|
|
43
|
+
'map_job_name' => nil,
|
|
44
|
+
'map_artifact_glob' => 'tmp/rails_tia/fragments',
|
|
45
|
+
},
|
|
46
|
+
'github_actions' => {
|
|
47
|
+
'map_workflow' => nil,
|
|
48
|
+
'map_event' => 'push',
|
|
49
|
+
'map_artifact_name' => 'rails-tia-map',
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
'metrics_path' => 'tmp/rails_tia/metrics.json',
|
|
53
|
+
}.freeze
|
|
54
|
+
|
|
55
|
+
class << self
|
|
56
|
+
def load(path = nil, root: Dir.pwd)
|
|
57
|
+
root = File.expand_path(root)
|
|
58
|
+
path ||= File.join(root, DEFAULT_PATH)
|
|
59
|
+
raw = File.exist?(path) ? (YAML.safe_load_file(path) || {}) : {}
|
|
60
|
+
new(raw, root: root)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
attr_reader :root, :app_dirs
|
|
65
|
+
|
|
66
|
+
def initialize(raw, root: Dir.pwd)
|
|
67
|
+
@root = File.expand_path(root)
|
|
68
|
+
raw ||= {}
|
|
69
|
+
@data = deep_merge(DEFAULTS, raw)
|
|
70
|
+
@app_dirs = build_app_dirs
|
|
71
|
+
rails = raw['rails']
|
|
72
|
+
if rails.is_a?(Hash) && rails.key?('form_allocations') && !rails.key?('object_allocations')
|
|
73
|
+
warn '[rails_tia] rails.form_allocations is deprecated; use rails.object_allocations to configure tracking for all application classes'
|
|
74
|
+
@data.fetch('rails')['object_allocations'] = rails.fetch('form_allocations')
|
|
75
|
+
end
|
|
76
|
+
@constant_reads = case dig('rails', 'constant_reads')
|
|
77
|
+
when true, 'true' then true
|
|
78
|
+
when false, 'false' then false
|
|
79
|
+
else raise Error, 'rails.constant_reads must be true or false'
|
|
80
|
+
end
|
|
81
|
+
@allocation_mode = case dig('rails', 'object_allocations')
|
|
82
|
+
when true, 'true' then true
|
|
83
|
+
when false, 'false' then false
|
|
84
|
+
when 'auto' then 'auto'
|
|
85
|
+
else raise Error, 'rails.object_allocations must be true, false or auto'
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def spec_glob = @data.fetch('spec_glob')
|
|
90
|
+
def full_run_token = @data.fetch('full_run_token')
|
|
91
|
+
def metrics_path = absolute(@data.fetch('metrics_path'))
|
|
92
|
+
|
|
93
|
+
def map_fragment_dir = absolute(dig('map', 'fragment_dir'))
|
|
94
|
+
def map_path = absolute(dig('map', 'path'))
|
|
95
|
+
def map_max_age_days = dig('map', 'max_age_days')
|
|
96
|
+
|
|
97
|
+
def full_run_paths = dig('full_run', 'paths')
|
|
98
|
+
def max_changed_files = dig('full_run', 'max_changed_files')
|
|
99
|
+
def full_run_labels = dig('full_run', 'labels')
|
|
100
|
+
def full_run_commit_message_tokens = dig('full_run', 'commit_message_tokens')
|
|
101
|
+
|
|
102
|
+
def static_mapping_rules = @data.fetch('static_mapping')
|
|
103
|
+
def described_class_enabled? = dig('described_class', 'enabled')
|
|
104
|
+
|
|
105
|
+
def ci_adapter = dig('ci', 'adapter')
|
|
106
|
+
def gitlab_options = dig('ci', 'gitlab')
|
|
107
|
+
def github_actions_options = dig('ci', 'github_actions')
|
|
108
|
+
|
|
109
|
+
def constant_reads_enabled? = @constant_reads
|
|
110
|
+
|
|
111
|
+
def object_allocations_required? = @allocation_mode == true
|
|
112
|
+
|
|
113
|
+
def object_allocations_enabled?
|
|
114
|
+
@allocation_mode == 'auto' ? RUBY_ENGINE == 'ruby' : @allocation_mode
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def active_record_enabled? = dig('rails', 'active_record')
|
|
118
|
+
def action_view_enabled? = dig('rails', 'action_view')
|
|
119
|
+
|
|
120
|
+
# In auto mode, enable support when Rails is loaded.
|
|
121
|
+
# Enable eager-load handling and model dependency supplementation by default to avoid silently losing accuracy.
|
|
122
|
+
def rails_enabled?
|
|
123
|
+
value = dig('rails', 'enabled')
|
|
124
|
+
return !!value unless value == 'auto'
|
|
125
|
+
|
|
126
|
+
defined?(::Rails) ? true : false
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def all_specs
|
|
130
|
+
Dir.chdir(@root) { Dir.glob(spec_glob).sort }
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def full_run_path?(path)
|
|
134
|
+
full_run_paths.any? { |pattern| File.fnmatch?(pattern, path, FNMATCH_FLAGS) }
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def absolute(path)
|
|
138
|
+
return path if path.nil?
|
|
139
|
+
|
|
140
|
+
File.absolute_path?(path) ? path : File.join(@root, path)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
private
|
|
144
|
+
|
|
145
|
+
def build_app_dirs
|
|
146
|
+
extra = dig('rails', 'app_dirs')
|
|
147
|
+
unless extra.is_a?(Array) && extra.all? { |dir| valid_application_directory?(dir) }
|
|
148
|
+
raise Error, 'rails.app_dirs must be a list of project-relative directories without .. components'
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
(%w[app lib] + extra).freeze
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def valid_application_directory?(dir)
|
|
155
|
+
return false unless dir.is_a?(String) && !dir.strip.empty? && !dir.include?("\0")
|
|
156
|
+
|
|
157
|
+
path = File::ALT_SEPARATOR ? dir.tr(File::ALT_SEPARATOR, '/') : dir
|
|
158
|
+
!File.absolute_path?(path) && !path.split('/').include?('..')
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def dig(*keys) = @data.dig(*keys)
|
|
162
|
+
|
|
163
|
+
def deep_merge(base, override)
|
|
164
|
+
base.merge(override) do |_key, old, new|
|
|
165
|
+
old.is_a?(Hash) && new.is_a?(Hash) ? deep_merge(old, new) : new
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require 'rubydex'
|
|
3
|
+
require 'set'
|
|
4
|
+
|
|
5
|
+
module RailsTia
|
|
6
|
+
class ConstantReads
|
|
7
|
+
def initialize(config)
|
|
8
|
+
root = File.join(config.root, '')
|
|
9
|
+
real_root = File.join(File.realpath(config.root), '')
|
|
10
|
+
included = lambda do |path|
|
|
11
|
+
path.start_with?(root) && path.end_with?('.rb') && File.file?(path) && File.realpath(path).start_with?(real_root)
|
|
12
|
+
end
|
|
13
|
+
app_files = config.app_dirs.flat_map do |dir|
|
|
14
|
+
absolute = File.absolute_path(dir, root)
|
|
15
|
+
Dir.glob('**/*.rb', base: absolute).map { |file| File.join(absolute, file) }
|
|
16
|
+
end.select(&included).to_set
|
|
17
|
+
reader_globs = [config.spec_glob, '{spec,test}/support/**/*.rb', 'spec/{rails,spec}_helper.rb', 'test/test_helper.rb']
|
|
18
|
+
tests = Dir.glob(reader_globs, base: root).map { |file| File.absolute_path(file, root) }.select(&included)
|
|
19
|
+
|
|
20
|
+
graph = Rubydex::Graph.new
|
|
21
|
+
graph.index_all((app_files | tests).to_a)
|
|
22
|
+
graph.resolve
|
|
23
|
+
@dependencies = {}
|
|
24
|
+
methods = {}
|
|
25
|
+
graph.declarations.each do |declaration|
|
|
26
|
+
next unless declaration.is_a?(Rubydex::Constant) || declaration.is_a?(Rubydex::ConstantAlias)
|
|
27
|
+
|
|
28
|
+
index_references(declaration, app_files, root, methods)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def sources(file, lines, previous)
|
|
33
|
+
groups = @dependencies[file]
|
|
34
|
+
return [] unless groups
|
|
35
|
+
|
|
36
|
+
groups.flat_map do |range, files|
|
|
37
|
+
if range.nil? || range.any? { |line| lines[line - 1].to_i > (previous && previous[line - 1]).to_i }
|
|
38
|
+
files.to_a
|
|
39
|
+
else
|
|
40
|
+
[]
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def index_references(declaration, app_files, root, methods)
|
|
48
|
+
definitions = declaration.definitions.filter_map do |definition|
|
|
49
|
+
location = definition.location
|
|
50
|
+
next unless location.uri.start_with?('file:')
|
|
51
|
+
|
|
52
|
+
path = location.to_file_path
|
|
53
|
+
path.delete_prefix(root) if app_files.include?(path)
|
|
54
|
+
end.uniq
|
|
55
|
+
return if definitions.empty?
|
|
56
|
+
|
|
57
|
+
declaration.references.each do |reference|
|
|
58
|
+
location = reference.location
|
|
59
|
+
next unless location.uri.start_with?('file:')
|
|
60
|
+
|
|
61
|
+
path = location.to_file_path
|
|
62
|
+
file = path.delete_prefix(root)
|
|
63
|
+
sources = definitions - [file]
|
|
64
|
+
next if sources.empty?
|
|
65
|
+
|
|
66
|
+
ranges = methods[path] ||= method_ranges(Prism.parse_file(path).value)
|
|
67
|
+
line = location.start_line + 1
|
|
68
|
+
body = ranges.find { |definition_range, _| definition_range.cover?(line) }&.last
|
|
69
|
+
groups = (@dependencies[file] ||= {})
|
|
70
|
+
(groups[body] ||= Set.new).merge(sources)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def method_ranges(node, ranges = [])
|
|
75
|
+
# Prefer the innermost method when Ruby definitions are nested.
|
|
76
|
+
node.compact_child_nodes.each { |child| method_ranges(child, ranges) }
|
|
77
|
+
if node.is_a?(Prism::DefNode)
|
|
78
|
+
body = node.body || node
|
|
79
|
+
ranges << [node.location.start_line..node.location.end_line, body.location.start_line..body.location.end_line]
|
|
80
|
+
end
|
|
81
|
+
ranges
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module RailsTia
|
|
2
|
+
# Prediction inputs collected by a CI adapter.
|
|
3
|
+
# Normalize adapter differences here so Predictor and downstream logic remain CI-independent.
|
|
4
|
+
Context = Struct.new(
|
|
5
|
+
:changed_files,
|
|
6
|
+
:labels,
|
|
7
|
+
:commit_message,
|
|
8
|
+
:merge_request,
|
|
9
|
+
keyword_init: true,
|
|
10
|
+
) do
|
|
11
|
+
def merge_request? = !!merge_request
|
|
12
|
+
|
|
13
|
+
def initialize(changed_files: [], labels: [], commit_message: nil, merge_request: false)
|
|
14
|
+
super
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|