-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.go
More file actions
226 lines (204 loc) · 6.49 KB
/
selection.go
File metadata and controls
226 lines (204 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"context"
"fmt"
"maps"
"path/filepath"
)
type packageKey struct {
Dir string
Name string
}
type packageInventory struct {
Key packageKey
Tests map[string]struct{}
}
type packageSelection struct {
Key packageKey
Tests map[string]struct{}
Files map[string]struct{}
}
type parsedFileSnapshot struct {
Key packageKey
Snapshot fileSnapshot
}
// selectChange handles the four diff states for a runnable test file: add
// (old absent, new present), delete (old present, new absent), in-place modify
// (both sides present with the same package key), and cross-package move or
// package rename (both sides present with different package keys).
func selectChange(ctx context.Context, cache *inventoryCache, selections map[packageKey]*packageSelection, change testFileChange) error {
cfg := cache.cfg
hunks, err := listDiffHunks(ctx, cfg, cache.git, change)
if err != nil {
return fmt.Errorf("list diff hunks for %s: %w", change.displayPath(), err)
}
if len(hunks) == 0 {
return nil
}
oldParsed, oldExists, err := cache.parseChangeFileAtRevision(ctx, cfg.BaseSHA, change.OldPath)
if err != nil {
return fmt.Errorf("resolve old package for %s: %w", change.displayPath(), err)
}
newParsed, newExists, err := cache.parseChangeFileAtRevision(ctx, cfg.HeadSHA, change.NewPath)
if err != nil {
return fmt.Errorf("resolve new package for %s: %w", change.displayPath(), err)
}
if change.expectsOldFile() && !oldExists {
return fmt.Errorf("base revision %s is missing %s", cfg.BaseSHA, change.OldPath)
}
if change.expectsNewFile() && !newExists {
return fmt.Errorf("head revision %s is missing %s", cfg.HeadSHA, change.NewPath)
}
var oldFile *parsedFileSnapshot
if oldExists {
oldFile = &oldParsed
}
var newFile *parsedFileSnapshot
if newExists {
newFile = &newParsed
}
if newFile != nil {
inventory, err := cache.loadPackageInventory(ctx, cfg.HeadSHA, newFile.Key)
if err != nil {
return fmt.Errorf("load package inventory for %s: %w", newFile.Key.String(), err)
}
var oldSnapshot *fileSnapshot
selectionHunks := hunks
if oldFile != nil && oldFile.Key == newFile.Key {
oldSnapshot = &oldFile.Snapshot
} else {
selectionHunks = newSideOnlyHunks(hunks)
}
selection := selectTestsFromHunks(change, oldSnapshot, newFile.Snapshot, inventory, selectionHunks)
if err := mergeSelection(ctx, cache, selections, selection); err != nil {
return err
}
}
if oldFile != nil && (newFile == nil || oldFile.Key != newFile.Key) {
inventory, err := cache.loadPackageInventory(ctx, cfg.HeadSHA, oldFile.Key)
if err != nil {
return fmt.Errorf("load package inventory for %s: %w", oldFile.Key.String(), err)
}
sourceChange := testFileChange{Kind: changeDeleted, OldPath: change.OldPath}
selection := selectSourceRemoval(sourceChange, oldFile.Snapshot, inventory, hunks)
if err := mergeSelection(ctx, cache, selections, selection); err != nil {
return err
}
}
return nil
}
func (change testFileChange) expectsOldFile() bool {
oldRequired, _ := change.Kind.expectedFileSides()
return oldRequired && isRunnableTestFilePath(change.OldPath)
}
func (change testFileChange) expectsNewFile() bool {
_, newRequired := change.Kind.expectedFileSides()
return newRequired && isRunnableTestFilePath(change.NewPath)
}
func (kind changeKind) expectedFileSides() (oldRequired bool, newRequired bool) {
switch kind {
case changeAdded:
return false, true
case changeDeleted:
return true, false
case changeModified, changeRenamed, changeType:
return true, true
}
// parseChangeKind rejects unknown statuses before selection. This default is a
// safety net for direct callers or future drift, but it only triggers
// missing-file errors when the change carries runnable test paths.
return true, true
}
func parseSnapshotForPath(filePath string, data []byte) (parsedFileSnapshot, error) {
snapshot, err := parseFileSnapshot(data)
if err != nil {
return parsedFileSnapshot{}, fmt.Errorf("parse package clause: %w", err)
}
return parsedFileSnapshot{
Key: packageKey{Dir: filepath.ToSlash(filepath.Dir(filePath)), Name: snapshot.packageName},
Snapshot: snapshot,
}, nil
}
func mergeSelection(_ context.Context, _ *inventoryCache, selections map[packageKey]*packageSelection, selection *packageSelection) error {
if selection == nil || len(selection.Tests) == 0 {
return nil
}
mergePackageSelection(selections, selection)
return nil
}
func mergePackageSelection(selections map[packageKey]*packageSelection, selection *packageSelection) {
merged := selections[selection.Key]
if merged == nil {
merged = &packageSelection{
Key: selection.Key,
Tests: map[string]struct{}{},
Files: map[string]struct{}{},
}
selections[selection.Key] = merged
}
maps.Copy(merged.Files, selection.Files)
maps.Copy(merged.Tests, selection.Tests)
}
func selectTestsFromHunks(change testFileChange, oldSnapshot *fileSnapshot, newSnapshot fileSnapshot, newInventory packageInventory, hunks []diffHunk) *packageSelection {
selected := map[string]struct{}{}
for _, hunk := range hunks {
addMatchingTests(selected, newSnapshot.tests, hunk.New)
if oldSnapshot == nil {
continue
}
for name, declRange := range oldSnapshot.tests {
if !declRange.overlaps(hunk.Old) {
continue
}
if _, ok := newInventory.Tests[name]; ok {
selected[name] = struct{}{}
}
}
}
if len(selected) == 0 {
return nil
}
return &packageSelection{
Key: newInventory.Key,
Tests: selected,
Files: map[string]struct{}{change.displayPath(): {}},
}
}
func selectSourceRemoval(change testFileChange, oldSnapshot fileSnapshot, inventory packageInventory, hunks []diffHunk) *packageSelection {
selected := map[string]struct{}{}
for _, hunk := range hunks {
for name, declRange := range oldSnapshot.tests {
if !declRange.overlaps(hunk.Old) {
continue
}
if _, ok := inventory.Tests[name]; ok {
selected[name] = struct{}{}
}
}
}
if len(selected) == 0 {
return nil
}
return &packageSelection{
Key: inventory.Key,
Tests: selected,
Files: map[string]struct{}{change.displayPath(): {}},
}
}
func addMatchingTests(selected map[string]struct{}, tests map[string]lineRange, candidate lineRange) {
for name, declRange := range tests {
if declRange.overlaps(candidate) {
selected[name] = struct{}{}
}
}
}
func (key packageKey) String() string {
return fmt.Sprintf("%s (%s)", packagePattern(key.Dir), key.Name)
}
func packagePattern(dir string) string {
cleanDir := filepath.ToSlash(filepath.Clean(dir))
if cleanDir == "." {
return "."
}
return "./" + cleanDir
}