rails_tia 0.1.0 → 0.2.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 +4 -4
- data/README.md +33 -0
- data/docs/configuration.md +4 -0
- data/lib/rails_tia/audit.rb +3 -2
- data/lib/rails_tia/cli.rb +38 -8
- data/lib/rails_tia/context.rb +3 -1
- data/lib/rails_tia/fallback.rb +21 -1
- data/lib/rails_tia/metrics.rb +40 -12
- data/lib/rails_tia/predictor.rb +34 -8
- data/lib/rails_tia/run_report.rb +7 -1
- data/lib/rails_tia/version.rb +1 -1
- data/skills/rails-tia/SKILL.md +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d74940bf8567378cac38f7c9510b37415655c0c57b214f9694188a2c142d6faa
|
|
4
|
+
data.tar.gz: f53fa44603424d2f00959142e7f3c851d0d9697b610c4dfcd9145f252da2d065
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 42e5669446f29cd43c61e432963fbc19637e482c37cd0b97cc88b91ccce94b5d863cce1ac563a303148b56333b1896ad1bebe07ecf12672fc97795e36f534482
|
|
7
|
+
data.tar.gz: 5d804ff68679a503e5114fe187a5d1af9291bc41cee710a8e9c748ecccaba1744b385b97742d8694815e0706d55023ee278186ccae81e294883595fb8ad39877
|
data/README.md
CHANGED
|
@@ -103,8 +103,22 @@ bundle exec rspec "${specs[@]}"
|
|
|
103
103
|
|
|
104
104
|
For Minitest, use `bundle exec ruby bin/rails test "${specs[@]}"` on the last line. If you customize `full_run_token`, use a value your runner accepts as one argument, or handle it in your script. Paths containing newlines are unsupported.
|
|
105
105
|
|
|
106
|
+
Set `RAILS_TIA_FORCE_FULL_RUN=1` (or `true`) to make `select` print `full_run_token` for every change, for example from a CI/CD variable to turn selection off without editing the pipeline. `0`, `false`, or an empty value leaves selection on. Any other value is an error.
|
|
107
|
+
|
|
106
108
|
`select` sends its summary to standard error and writes metrics to `tmp/rails_tia/metrics.json` by default. Use `--metrics PATH` to change the output file or `--junit-dir DIR` to estimate time savings from JUnit timings.
|
|
107
109
|
|
|
110
|
+
The metrics file contains these fields.
|
|
111
|
+
|
|
112
|
+
| Field | Meaning |
|
|
113
|
+
| --- | --- |
|
|
114
|
+
| `full_run` | Whether `select` printed `full_run_token`. |
|
|
115
|
+
| `reason` | Why the full suite runs, or `null` for a selection. |
|
|
116
|
+
| `changed_file_count` | Number of changed files used for selection. |
|
|
117
|
+
| `selected_spec_count` | Number of test files that run. A full run counts every discovered test file. |
|
|
118
|
+
| `total_spec_count` | Number of test files matching `spec_glob`. |
|
|
119
|
+
| `spec_ratio` | `selected_spec_count` divided by `total_spec_count`. |
|
|
120
|
+
| `estimated_seconds`, `total_estimated_seconds`, `time_ratio` | JUnit-based durations of the tests that run and of all tests, and their ratio. Present only with `--junit-dir`. |
|
|
121
|
+
|
|
108
122
|
### Local Git and explicit changes
|
|
109
123
|
|
|
110
124
|
Outside GitLab, automatic adapter selection uses Git. The Git adapter cannot fetch maps, so generate one locally or copy a merged map to `map.path`. It compares `origin/master...HEAD` by default. Set `RAILS_TIA_DIFF_BASE` to use another base, such as `origin/main`. Fetch enough history to include the common ancestor.
|
|
@@ -147,6 +161,8 @@ Set `ci.adapter` to `github_actions` so pull request jobs can fetch the map publ
|
|
|
147
161
|
|
|
148
162
|
Periodically run the full suite on the same commit as a selection. Compare its JUnit results with the saved prediction to find failures the selection missed.
|
|
149
163
|
|
|
164
|
+
A full run is also an audit opportunity. When a merge request runs the full suite because of a full-run rule or `RAILS_TIA_FORCE_FULL_RUN`, and the map is present and fresh, the selection report still records the specs the map would have selected. Audit against them with `--against prediction` (see [Run history](#run-history)). Before enabling selection, you can force full runs in merge requests and audit each one this way.
|
|
165
|
+
|
|
150
166
|
```bash
|
|
151
167
|
bundle exec rails-tia audit --predicted specs.txt --junit tmp/junit
|
|
152
168
|
```
|
|
@@ -161,6 +177,23 @@ Set `RAILS_TIA_REPORT_DIR` (for example, `tmp/rails_tia/reports`) to record map
|
|
|
161
177
|
|
|
162
178
|
To link an audit to its prediction, replace `--predicted specs.txt` with `--selection-report PATH`, using the `select-<report_id>.json` file from the commit under test.
|
|
163
179
|
|
|
180
|
+
```bash
|
|
181
|
+
bundle exec rails-tia audit --selection-report history/select-<report_id>.json --junit tmp/junit --against prediction
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The selection report contains the metrics fields and these fields.
|
|
185
|
+
|
|
186
|
+
| Field | Meaning |
|
|
187
|
+
| --- | --- |
|
|
188
|
+
| `selected_specs` | Test files that run. A full run lists every discovered test file. |
|
|
189
|
+
| `reason_code` | `not_merge_request`, `missing_map`, `stale_map`, `label`, `commit_message`, `changed_file_limit`, `path`, or `forced` for a full run, otherwise `null`. |
|
|
190
|
+
| `forced` | Whether `RAILS_TIA_FORCE_FULL_RUN` forced a full run (`1` or `true`). `reason_code` reports another rule when one also applies. |
|
|
191
|
+
| `prediction` | The map's selection ignoring full-run rules: `specs`, `spec_count`, `spec_ratio`, and `time_ratio` with `--junit-dir`. It matches `selected_specs` for a selection and is `null` when the map is missing or stale, outside merge requests, or when computing it failed. |
|
|
192
|
+
| `prediction_error` | Why computing the prediction failed during a full run, otherwise `null`. The full run proceeds. |
|
|
193
|
+
| `matches` | Per changed file, how many of the prediction's specs came from the map, static mappings, or the file itself. Present when `prediction` is present. |
|
|
194
|
+
|
|
195
|
+
`--against selection` (the default) compares failures with `selected_specs`, so a full run never misses a failure. `--against prediction` compares them with `prediction.specs`. A report without a prediction makes the audit invalid.
|
|
196
|
+
|
|
164
197
|
## Tracking and limitations
|
|
165
198
|
|
|
166
199
|
- **Ruby and ActiveRecord.** Line coverage records code exercised by the suite. Rails tracking adds model definitions observed through ActiveRecord. On CRuby, creating an object of a preloaded application class can also link the test to its definition, for example when a request instantiates a form with declarative validations. See the [allocation tracking settings](docs/configuration.md#dependency-tracking). Reads such as `Config::LIMIT` can link a test that executes the referencing method to the file defining the constant. RSpec also tracks definition files for each named `described_class` and its ancestors, even when an example executes no lines in those files. Reopened classes and unexecuted code can still need static rules.
|
data/docs/configuration.md
CHANGED
|
@@ -40,6 +40,10 @@ Any matching rule makes `select` print `full_run_token`.
|
|
|
40
40
|
| `full_run.labels` | `[]` | Any listed string exactly matches a merge or pull request label. |
|
|
41
41
|
| `full_run.commit_message_tokens` | `[]` | The commit message contains any listed string, for example `'[run all tests]'`. |
|
|
42
42
|
|
|
43
|
+
Set the environment variable `RAILS_TIA_FORCE_FULL_RUN` to `1` or `true` to force a full run without changing the configuration. `0`, `false`, and an empty value disable it, and other values make `select` fail.
|
|
44
|
+
|
|
45
|
+
`select` checks the conditions in this order and reports the first match as `reason_code`: `not_merge_request`, `missing_map`, `stale_map`, `label`, `commit_message`, `changed_file_limit`, `path`, and `forced`. A forced run therefore reports `forced` only when no rule applies. For a full run with any other reason, the selection report still records the map's prediction for auditing, as it does for a selection. The prediction is `null` for the first three reasons and when computing it fails.
|
|
46
|
+
|
|
43
47
|
## Static mappings
|
|
44
48
|
|
|
45
49
|
`static_mapping` is a list of rules (default `[]`) for dependencies that coverage can miss, including new files. Each rule needs a `source` and at least one of `test` and `test_glob`. All three keys accept a string or a list of strings.
|
data/lib/rails_tia/audit.rb
CHANGED
|
@@ -23,10 +23,11 @@ module RailsTia
|
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def initialize(junit_report:, predicted_specs:, full_run: false)
|
|
26
|
+
def initialize(junit_report:, predicted_specs:, full_run: false, input_errors: [])
|
|
27
27
|
@junit_report = junit_report
|
|
28
28
|
@predicted_specs = Set.new(predicted_specs)
|
|
29
29
|
@full_run = full_run
|
|
30
|
+
@input_errors = input_errors
|
|
30
31
|
end
|
|
31
32
|
|
|
32
33
|
def call
|
|
@@ -35,7 +36,7 @@ module RailsTia
|
|
|
35
36
|
failed_specs: failed,
|
|
36
37
|
predicted_specs: @predicted_specs,
|
|
37
38
|
missed_specs: @full_run ? Set.new : failed - @predicted_specs,
|
|
38
|
-
validation_errors: @junit_report.validation_errors,
|
|
39
|
+
validation_errors: @input_errors + @junit_report.validation_errors,
|
|
39
40
|
)
|
|
40
41
|
end
|
|
41
42
|
end
|
data/lib/rails_tia/cli.rb
CHANGED
|
@@ -25,6 +25,7 @@ module RailsTia
|
|
|
25
25
|
--root PATH Project root (default: current directory)
|
|
26
26
|
|
|
27
27
|
Set RAILS_TIA_REPORT_DIR to write JSON reports for CI storage.
|
|
28
|
+
Set RAILS_TIA_FORCE_FULL_RUN=1 to make select run the full suite.
|
|
28
29
|
TEXT
|
|
29
30
|
|
|
30
31
|
def self.start(argv)
|
|
@@ -52,7 +53,7 @@ module RailsTia
|
|
|
52
53
|
end
|
|
53
54
|
rescue Ci::Gitlab::AuthenticationError, Ci::GithubActions::Error, Http::Error, Ci::Git::CommandError,
|
|
54
55
|
Ci::UnknownAdapterError, Ci::Base::UnsupportedError, Map::FormatError, RunReport::Error,
|
|
55
|
-
StaticMapping::InvalidGlob, Errno::ENOENT => e
|
|
56
|
+
StaticMapping::InvalidGlob, Fallback::InvalidForceValue, Errno::ENOENT => e
|
|
56
57
|
warn "[rails_tia] #{e.message}"
|
|
57
58
|
1
|
|
58
59
|
end
|
|
@@ -103,22 +104,28 @@ module RailsTia
|
|
|
103
104
|
end
|
|
104
105
|
end
|
|
105
106
|
|
|
107
|
+
# Validate the override before fetching the diff so a bad value fails without API calls.
|
|
108
|
+
forced = Fallback.forced_by_env?
|
|
109
|
+
context = select_context
|
|
110
|
+
context.force_full_run = forced
|
|
106
111
|
report = RunReport.for('select', config)
|
|
107
112
|
map_id = nil
|
|
108
113
|
map = load_map { |json| map_id = Digest::SHA256.hexdigest(json) if report }
|
|
109
|
-
result = Predictor.new(config: config, map: map).call(
|
|
114
|
+
result = Predictor.new(config: config, map: map).call(context, trace: !!report)
|
|
110
115
|
|
|
111
116
|
metrics = Metrics.new(config: config, result: result, junit_report: junit_report(@options[:junit_dir]))
|
|
112
117
|
data = metrics.to_h
|
|
118
|
+
prediction = metrics.prediction
|
|
113
119
|
metrics.write(@options[:metrics] || config.metrics_path, data: data)
|
|
114
120
|
report&.write do
|
|
115
121
|
data.merge(
|
|
116
122
|
'map_id' => map_id, 'changed_files' => result.sources, 'selected_specs' => result.specs,
|
|
117
|
-
'reason_code' => result.reason_code, 'matches' => result.matches,
|
|
123
|
+
'reason_code' => result.reason_code, 'forced' => result.forced?, 'matches' => result.matches,
|
|
124
|
+
'prediction' => prediction, 'prediction_error' => result.prediction_error,
|
|
118
125
|
)
|
|
119
126
|
end
|
|
120
127
|
puts(result.full_run? ? config.full_run_token : result.specs)
|
|
121
|
-
warn metrics.summary(data)
|
|
128
|
+
warn metrics.summary(data, prediction: result.full_run? ? prediction : nil)
|
|
122
129
|
0
|
|
123
130
|
end
|
|
124
131
|
|
|
@@ -189,6 +196,10 @@ module RailsTia
|
|
|
189
196
|
parser.on('--junit DIR', 'JUnit XML directory from a full-suite run') { |v| @options[:junit] = v }
|
|
190
197
|
parser.on('--predicted PATH', 'Predicted spec list (one path per line)') { |v| @options[:predicted] = v }
|
|
191
198
|
parser.on('--selection-report PATH', 'Use the JSON report from select as the prediction') { |v| @options[:selection_report] = v }
|
|
199
|
+
parser.on('--against TARGET', %w[selection prediction],
|
|
200
|
+
'Compare with the selection report\'s selected specs (selection, default) or prediction') do |v|
|
|
201
|
+
@options[:against] = v
|
|
202
|
+
end
|
|
192
203
|
parser.on('--expect-junit FILE', 'Expected XML path relative to --junit (repeat per shard)') do |v|
|
|
193
204
|
(@options[:expected_junit] ||= []) << v
|
|
194
205
|
end
|
|
@@ -198,14 +209,32 @@ module RailsTia
|
|
|
198
209
|
warn '[rails_tia] Required options: --junit and exactly one of --predicted or --selection-report'
|
|
199
210
|
return 1
|
|
200
211
|
end
|
|
212
|
+
against = @options.fetch(:against, 'selection')
|
|
213
|
+
if against == 'prediction' && @options[:selection_report].nil?
|
|
214
|
+
warn '[rails_tia] --against prediction requires --selection-report'
|
|
215
|
+
return 1
|
|
216
|
+
end
|
|
201
217
|
|
|
202
218
|
if @options[:selection_report]
|
|
203
219
|
selection = RunReport.load_selection(@options[:selection_report])
|
|
204
220
|
if selection['commit_sha'] && RailsTia.commit_sha && selection['commit_sha'] != RailsTia.commit_sha
|
|
205
221
|
raise RunReport::Error, 'Selection report is from a different commit'
|
|
206
222
|
end
|
|
207
|
-
|
|
208
|
-
|
|
223
|
+
input_errors = []
|
|
224
|
+
if against == 'prediction'
|
|
225
|
+
prediction = selection['prediction']
|
|
226
|
+
full_run = false
|
|
227
|
+
if prediction
|
|
228
|
+
predicted = prediction.fetch('specs')
|
|
229
|
+
else
|
|
230
|
+
predicted = []
|
|
231
|
+
cause = selection['prediction_error'] ? ", error: #{selection['prediction_error']}" : ''
|
|
232
|
+
input_errors << "Selection report has no prediction (reason_code: #{selection['reason_code'] || 'unknown'}#{cause})"
|
|
233
|
+
end
|
|
234
|
+
else
|
|
235
|
+
predicted = selection.fetch('selected_specs')
|
|
236
|
+
full_run = selection.fetch('full_run')
|
|
237
|
+
end
|
|
209
238
|
else
|
|
210
239
|
predicted = File.readlines(@options[:predicted], chomp: true).reject(&:empty?)
|
|
211
240
|
full_run = predicted == [config.full_run_token]
|
|
@@ -213,13 +242,14 @@ module RailsTia
|
|
|
213
242
|
|
|
214
243
|
expected = @options.fetch(:expected_junit, [])
|
|
215
244
|
junit = JunitReport.load_dir(@options[:junit], expected_files: expected)
|
|
216
|
-
result = Audit.new(junit_report: junit, predicted_specs: predicted, full_run: full_run
|
|
245
|
+
result = Audit.new(junit_report: junit, predicted_specs: predicted, full_run: full_run,
|
|
246
|
+
input_errors: input_errors || []).call
|
|
217
247
|
report = RunReport.for('audit', config)
|
|
218
248
|
report&.write do
|
|
219
249
|
valid = result.validation_errors.empty?
|
|
220
250
|
{
|
|
221
251
|
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,
|
|
252
|
+
against: against, full_run: full_run, valid: valid, failed_specs: result.failed_specs.sort,
|
|
223
253
|
missed_specs: valid ? result.missed_specs.sort : nil, validation_errors: result.validation_errors,
|
|
224
254
|
junit_file_count: junit.parsed_file_count, expected_junit_files: expected,
|
|
225
255
|
missing_junit_files: junit.missing_files, junit_complete: junit.complete?,
|
data/lib/rails_tia/context.rb
CHANGED
|
@@ -6,11 +6,13 @@ module RailsTia
|
|
|
6
6
|
:labels,
|
|
7
7
|
:commit_message,
|
|
8
8
|
:merge_request,
|
|
9
|
+
:force_full_run,
|
|
9
10
|
keyword_init: true,
|
|
10
11
|
) do
|
|
11
12
|
def merge_request? = !!merge_request
|
|
13
|
+
def force_full_run? = !!force_full_run
|
|
12
14
|
|
|
13
|
-
def initialize(changed_files: [], labels: [], commit_message: nil, merge_request: false)
|
|
15
|
+
def initialize(changed_files: [], labels: [], commit_message: nil, merge_request: false, force_full_run: false)
|
|
14
16
|
super
|
|
15
17
|
end
|
|
16
18
|
end
|
data/lib/rails_tia/fallback.rb
CHANGED
|
@@ -6,6 +6,18 @@ module RailsTia
|
|
|
6
6
|
# Return the reason as text for CI logs and metrics.
|
|
7
7
|
class Fallback
|
|
8
8
|
SAMPLE_SIZE = 5
|
|
9
|
+
FORCE_ENV = 'RAILS_TIA_FORCE_FULL_RUN'.freeze
|
|
10
|
+
|
|
11
|
+
class InvalidForceValue < StandardError; end
|
|
12
|
+
|
|
13
|
+
# Reject unrecognized values so a typo in a CI variable cannot silently leave selection enabled.
|
|
14
|
+
def self.forced_by_env?(env = ENV)
|
|
15
|
+
value = env[FORCE_ENV].to_s.strip.downcase
|
|
16
|
+
return false if ['', '0', 'false'].include?(value)
|
|
17
|
+
return true if %w(1 true).include?(value)
|
|
18
|
+
|
|
19
|
+
raise InvalidForceValue, "#{FORCE_ENV} must be 1, true, 0, or false: #{env[FORCE_ENV].inspect}"
|
|
20
|
+
end
|
|
9
21
|
|
|
10
22
|
def initialize(config:, map:)
|
|
11
23
|
@config = config
|
|
@@ -14,6 +26,7 @@ module RailsTia
|
|
|
14
26
|
|
|
15
27
|
def reason(context) = decision(context)&.last
|
|
16
28
|
|
|
29
|
+
# Check forced runs last so reason_code keeps the rule that would apply without the override.
|
|
17
30
|
def decision(context)
|
|
18
31
|
not_merge_request(context) ||
|
|
19
32
|
missing_map ||
|
|
@@ -21,7 +34,8 @@ module RailsTia
|
|
|
21
34
|
matched_label(context) ||
|
|
22
35
|
matched_commit_message(context) ||
|
|
23
36
|
too_many_changes(context) ||
|
|
24
|
-
matched_full_run_path(context)
|
|
37
|
+
matched_full_run_path(context) ||
|
|
38
|
+
forced(context)
|
|
25
39
|
end
|
|
26
40
|
|
|
27
41
|
private
|
|
@@ -75,5 +89,11 @@ module RailsTia
|
|
|
75
89
|
suffix = hit.size > SAMPLE_SIZE ? " and #{hit.size - SAMPLE_SIZE} more" : ''
|
|
76
90
|
['path', "Full-run path changed: #{sample}#{suffix}"]
|
|
77
91
|
end
|
|
92
|
+
|
|
93
|
+
def forced(context)
|
|
94
|
+
return nil unless context.force_full_run?
|
|
95
|
+
|
|
96
|
+
['forced', "Full run forced by #{FORCE_ENV}"]
|
|
97
|
+
end
|
|
78
98
|
end
|
|
79
99
|
end
|
data/lib/rails_tia/metrics.rb
CHANGED
|
@@ -14,16 +14,25 @@ module RailsTia
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def to_h
|
|
17
|
-
all_specs = @config.all_specs
|
|
18
17
|
base = {
|
|
19
18
|
'full_run' => @result.full_run?,
|
|
20
19
|
'reason' => @result.reason,
|
|
21
20
|
'changed_file_count' => @result.sources.size,
|
|
22
|
-
'
|
|
21
|
+
'selected_spec_count' => @result.specs.size,
|
|
23
22
|
'total_spec_count' => all_specs.size,
|
|
24
23
|
'spec_ratio' => ratio(@result.specs.size, all_specs.size),
|
|
25
24
|
}
|
|
26
|
-
base.merge(time_metrics
|
|
25
|
+
base.merge(time_metrics)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Describe the map's selection without full-run rules, or nil when the map could not be used.
|
|
29
|
+
def prediction
|
|
30
|
+
specs = @result.prediction
|
|
31
|
+
return nil if specs.nil?
|
|
32
|
+
|
|
33
|
+
data = { 'specs' => specs, 'spec_count' => specs.size, 'spec_ratio' => ratio(specs.size, all_specs.size) }
|
|
34
|
+
data['time_ratio'] = time_ratio(specs) if timed?
|
|
35
|
+
data
|
|
27
36
|
end
|
|
28
37
|
|
|
29
38
|
def write(path = @config.metrics_path, data: to_h)
|
|
@@ -32,13 +41,17 @@ module RailsTia
|
|
|
32
41
|
path
|
|
33
42
|
end
|
|
34
43
|
|
|
35
|
-
def summary(data = to_h)
|
|
44
|
+
def summary(data = to_h, prediction: nil)
|
|
36
45
|
if data['full_run']
|
|
37
|
-
"[rails_tia] Full run: #{data['reason']} (#{data['total_spec_count']} specs)"
|
|
46
|
+
line = "[rails_tia] Full run: #{data['reason']} (#{data['total_spec_count']} specs)"
|
|
47
|
+
return line if prediction.nil?
|
|
48
|
+
|
|
49
|
+
line + format("\n[rails_tia] Prediction without full-run rules: %d / %d (%.1f%%)",
|
|
50
|
+
prediction['spec_count'], data['total_spec_count'], (prediction['spec_ratio'] || 0) * 100)
|
|
38
51
|
else
|
|
39
52
|
format(
|
|
40
53
|
'[rails_tia] Selected specs: %d / %d (%.1f%%)%s',
|
|
41
|
-
data['
|
|
54
|
+
data['selected_spec_count'],
|
|
42
55
|
data['total_spec_count'],
|
|
43
56
|
(data['spec_ratio'] || 0) * 100,
|
|
44
57
|
data['time_ratio'] ? format(' / Estimated runtime %.1f%%', data['time_ratio'] * 100) : '',
|
|
@@ -48,17 +61,32 @@ module RailsTia
|
|
|
48
61
|
|
|
49
62
|
private
|
|
50
63
|
|
|
51
|
-
def
|
|
52
|
-
|
|
64
|
+
def all_specs
|
|
65
|
+
@all_specs ||= @config.all_specs
|
|
66
|
+
end
|
|
53
67
|
|
|
68
|
+
def timed?
|
|
69
|
+
!(@junit_report.nil? || @junit_report.empty?)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def seconds(specs)
|
|
54
73
|
durations = @junit_report.durations
|
|
55
|
-
|
|
56
|
-
|
|
74
|
+
specs.sum { |spec| durations[spec] }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def time_ratio(specs)
|
|
78
|
+
ratio(seconds(specs), seconds(all_specs))
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def time_metrics
|
|
82
|
+
return {} unless timed?
|
|
57
83
|
|
|
84
|
+
selected = seconds(@result.specs)
|
|
85
|
+
total = seconds(all_specs)
|
|
58
86
|
{
|
|
59
|
-
'estimated_seconds' =>
|
|
87
|
+
'estimated_seconds' => selected.round(1),
|
|
60
88
|
'total_estimated_seconds' => total.round(1),
|
|
61
|
-
'time_ratio' => ratio(
|
|
89
|
+
'time_ratio' => ratio(selected, total),
|
|
62
90
|
}
|
|
63
91
|
end
|
|
64
92
|
|
data/lib/rails_tia/predictor.rb
CHANGED
|
@@ -7,25 +7,53 @@ module RailsTia
|
|
|
7
7
|
# 1. Reverse lookup in the coverage map (primary).
|
|
8
8
|
# 2. Static mappings for new files, templates, and missed classes without executable bodies.
|
|
9
9
|
# 3. Changed spec files themselves.
|
|
10
|
+
#
|
|
11
|
+
# A full-run rule replaces the selection with every spec. When the map is usable, the prediction is still
|
|
12
|
+
# computed and returned separately so a full run can be audited against what the map would have selected.
|
|
10
13
|
class Predictor
|
|
11
|
-
Result = Struct.new(:full_run, :reason, :reason_code, :matches, :specs, :sources,
|
|
14
|
+
Result = Struct.new(:full_run, :reason, :reason_code, :forced, :matches, :specs, :sources,
|
|
15
|
+
:prediction, :prediction_error, keyword_init: true) do
|
|
12
16
|
def full_run? = !!full_run
|
|
17
|
+
def forced? = !!forced
|
|
13
18
|
end
|
|
14
19
|
|
|
20
|
+
# Without a merge request diff or a usable map, a prediction has no meaning.
|
|
21
|
+
UNPREDICTABLE_REASONS = %w(not_merge_request missing_map stale_map).freeze
|
|
22
|
+
|
|
15
23
|
def initialize(config:, map:)
|
|
16
24
|
@config = config
|
|
17
25
|
@map = map
|
|
18
|
-
@static_mapping = StaticMapping.new(config.static_mapping_rules, root: config.root)
|
|
19
26
|
@fallback = Fallback.new(config: config, map: map)
|
|
20
27
|
end
|
|
21
28
|
|
|
22
29
|
def call(context, trace: false)
|
|
23
30
|
reason_code, reason = @fallback.decision(context)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
common = { reason: reason, reason_code: reason_code, forced: context.force_full_run?, sources: context.changed_files }
|
|
32
|
+
return full_run(context, trace, **common) if reason
|
|
33
|
+
|
|
34
|
+
specs, matches = predict(context, trace)
|
|
35
|
+
Result.new(full_run: false, matches: matches, specs: specs, prediction: specs, **common)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def full_run(context, trace, **common)
|
|
41
|
+
prediction = matches = error = nil
|
|
42
|
+
unless UNPREDICTABLE_REASONS.include?(common[:reason_code])
|
|
43
|
+
begin
|
|
44
|
+
prediction, matches = predict(context, trace)
|
|
45
|
+
rescue StandardError => e
|
|
46
|
+
# Every spec runs regardless, so a failure to record the prediction must not fail the job.
|
|
47
|
+
error = "#{e.class}: #{e.message}"
|
|
48
|
+
warn "[rails_tia] Cannot compute the prediction for a full run: #{error}"
|
|
49
|
+
end
|
|
27
50
|
end
|
|
51
|
+
Result.new(full_run: true, specs: @config.all_specs, matches: matches, prediction: prediction,
|
|
52
|
+
prediction_error: error, **common)
|
|
53
|
+
end
|
|
28
54
|
|
|
55
|
+
def predict(context, trace)
|
|
56
|
+
@static_mapping ||= StaticMapping.new(@config.static_mapping_rules, root: @config.root)
|
|
29
57
|
specs = Set.new
|
|
30
58
|
glob_cache = {}
|
|
31
59
|
static_by_source = {} if trace
|
|
@@ -38,11 +66,9 @@ module RailsTia
|
|
|
38
66
|
|
|
39
67
|
specs.keep_if { |spec| File.exist?(File.join(@config.root, spec)) }
|
|
40
68
|
matches = match_counts(static_by_source, specs) if trace
|
|
41
|
-
|
|
69
|
+
[specs.sort, matches]
|
|
42
70
|
end
|
|
43
71
|
|
|
44
|
-
private
|
|
45
|
-
|
|
46
72
|
def spec_file?(path)
|
|
47
73
|
File.fnmatch?(@config.spec_glob, path, Configuration::FNMATCH_FLAGS)
|
|
48
74
|
end
|
data/lib/rails_tia/run_report.rb
CHANGED
|
@@ -21,7 +21,8 @@ module RailsTia
|
|
|
21
21
|
valid = data.is_a?(Hash) && data['schema_version'] == SCHEMA_VERSION && data['type'] == 'select' &&
|
|
22
22
|
data['report_id'].is_a?(String) && !data['report_id'].empty? &&
|
|
23
23
|
[true, false].include?(data['full_run']) &&
|
|
24
|
-
data['selected_specs'].is_a?(Array) && data['selected_specs'].all?(String)
|
|
24
|
+
data['selected_specs'].is_a?(Array) && data['selected_specs'].all?(String) &&
|
|
25
|
+
valid_prediction?(data['prediction'])
|
|
25
26
|
raise Error, "Invalid selection report: #{path}" unless valid
|
|
26
27
|
|
|
27
28
|
data
|
|
@@ -31,6 +32,11 @@ module RailsTia
|
|
|
31
32
|
raise Error, "Cannot read selection report #{path}: #{e.message}"
|
|
32
33
|
end
|
|
33
34
|
|
|
35
|
+
def self.valid_prediction?(prediction)
|
|
36
|
+
prediction.nil? ||
|
|
37
|
+
(prediction.is_a?(Hash) && prediction['specs'].is_a?(Array) && prediction['specs'].all?(String))
|
|
38
|
+
end
|
|
39
|
+
|
|
34
40
|
def initialize(type, directory)
|
|
35
41
|
@type = type
|
|
36
42
|
@directory = directory
|
data/lib/rails_tia/version.rb
CHANGED
data/skills/rails-tia/SKILL.md
CHANGED
|
@@ -44,6 +44,6 @@ With the GitHub Actions or GitLab adapter, run `bundle exec rails-tia map fetch`
|
|
|
44
44
|
|
|
45
45
|
## Explain a selection
|
|
46
46
|
|
|
47
|
-
Use the selection summary on stderr and `bundle exec rails-tia doctor` to check discovery, configuration, map freshness, and the adapter. A missing or stale map requests a full run. On GitHub Actions, check that a successful map workflow produced an artifact for the exact commit used as the PR diff baseline. For a surprising subset, check the recorded dependencies, matching `static_mapping` rules, and changed test files. Use naming rules for new files with predictable tests and `full_run.paths` for changes with suite-wide effects.
|
|
47
|
+
Use the selection summary on stderr and `bundle exec rails-tia doctor` to check discovery, configuration, map freshness, and the adapter. A missing or stale map requests a full run. `RAILS_TIA_FORCE_FULL_RUN=1` also forces one. For a full run in a merge request with a fresh map, whether forced or triggered by a rule, the summary also shows the prediction the map would have used. On GitHub Actions, check that a successful map workflow produced an artifact for the exact commit used as the PR diff baseline. For a surprising subset, check the recorded dependencies, matching `static_mapping` rules, and changed test files. Use naming rules for new files with predictable tests and `full_run.paths` for changes with suite-wide effects.
|
|
48
48
|
|
|
49
|
-
To investigate missed failures, save the selection and compare it with JUnit XML from a **full suite on the same commit**. Run `bundle exec rails-tia audit --predicted specs.txt --junit DIR`. Gather XML from every shard, including failures, into a fresh directory. Use a repeated `--expect-junit FILE` for known shard outputs. A failing testcase needs a project-relative `file` attribute.
|
|
49
|
+
To investigate missed failures, save the selection and compare it with JUnit XML from a **full suite on the same commit**. Run `bundle exec rails-tia audit --predicted specs.txt --junit DIR`. For a full run with a selection report from `RAILS_TIA_REPORT_DIR`, use `--selection-report PATH --against prediction` to check the map's prediction instead of the full selection. Gather XML from every shard, including failures, into a fresh directory. Use a repeated `--expect-junit FILE` for known shard outputs. A failing testcase needs a project-relative `file` attribute.
|