-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathextractor_test.go
More file actions
343 lines (325 loc) · 9.55 KB
/
Copy pathextractor_test.go
File metadata and controls
343 lines (325 loc) · 9.55 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package extractor
import (
"go/ast"
"testing"
"golang.org/x/tools/go/packages"
)
func TestIsExactTestPackage(t *testing.T) {
tests := []struct {
name string
pkgID string
pkgPath string
expected bool
}{
{
name: "exact test package",
pkgID: "github.com/foo/bar [github.com/foo/bar.test]",
pkgPath: "github.com/foo/bar",
expected: true,
},
{
name: "nested test package",
pkgID: "github.com/foo/bar [github.com/foo/bar/nested.test]",
pkgPath: "github.com/foo/bar",
expected: false,
},
{
name: "deeply nested test package",
pkgID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]",
pkgPath: "github.com/go-git/go-git/v6",
expected: false,
},
{
name: "exact test package with version",
pkgID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]",
pkgPath: "github.com/go-git/go-git/v6",
expected: true,
},
{
name: "non-test package",
pkgID: "github.com/foo/bar",
pkgPath: "github.com/foo/bar",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pkg := &packages.Package{
ID: tt.pkgID,
PkgPath: tt.pkgPath,
}
result := isExactTestPackage(pkg)
if result != tt.expected {
t.Errorf("isExactTestPackage(%q) = %v, want %v", tt.pkgID, result, tt.expected)
}
})
}
}
func TestIsBetterPackage(t *testing.T) {
// Helper to create a package with specified properties
makePkg := func(id, path string, syntaxCount int) *packages.Package {
syntax := make([]*ast.File, syntaxCount)
return &packages.Package{
ID: id,
PkgPath: path,
Syntax: syntax,
}
}
tests := []struct {
name string
pkg *packages.Package
current *packages.Package
expected bool // true if pkg is better than current
}{
{
name: "exact test package beats nested test package",
pkg: makePkg(
"github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]",
"github.com/go-git/go-git/v6",
39, // 19 production + 20 test files
),
current: makePkg(
"github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]",
"github.com/go-git/go-git/v6",
19, // production files only
),
expected: true,
},
{
name: "nested test package loses to exact test package",
pkg: makePkg(
"github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]",
"github.com/go-git/go-git/v6",
19,
),
current: makePkg(
"github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]",
"github.com/go-git/go-git/v6",
39,
),
expected: false,
},
{
name: "more syntax nodes wins when both are exact tests",
pkg: makePkg(
"github.com/foo/bar [github.com/foo/bar.test]",
"github.com/foo/bar",
50,
),
current: makePkg(
"github.com/foo/bar [github.com/foo/bar.test]",
"github.com/foo/bar",
30,
),
expected: true,
},
{
name: "fewer syntax nodes loses when both are exact tests",
pkg: makePkg(
"github.com/foo/bar [github.com/foo/bar.test]",
"github.com/foo/bar",
30,
),
current: makePkg(
"github.com/foo/bar [github.com/foo/bar.test]",
"github.com/foo/bar",
50,
),
expected: false,
},
{
name: "more syntax nodes wins when both are nested tests",
pkg: makePkg(
"github.com/foo/bar [github.com/foo/bar/pkg1.test]",
"github.com/foo/bar",
25,
),
current: makePkg(
"github.com/foo/bar [github.com/foo/bar/pkg2.test]",
"github.com/foo/bar",
20,
),
expected: true,
},
{
name: "longer ID wins when same syntax count",
pkg: makePkg(
"github.com/foo/bar [github.com/foo/bar/verylongpackagename.test]",
"github.com/foo/bar",
20,
),
current: makePkg(
"github.com/foo/bar [github.com/foo/bar/short.test]",
"github.com/foo/bar",
20,
),
expected: true,
},
{
name: "test package beats non-test with same syntax count",
pkg: makePkg(
"github.com/foo/bar [github.com/foo/bar.test]",
"github.com/foo/bar",
20,
),
current: makePkg(
"github.com/foo/bar",
"github.com/foo/bar",
20,
),
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isBetterPackage(tt.pkg, tt.current)
if result != tt.expected {
t.Errorf("isBetterPackage() = %v, want %v\n pkg: %q (%d syntax nodes)\n current: %q (%d syntax nodes)",
result, tt.expected,
tt.pkg.ID, len(tt.pkg.Syntax),
tt.current.ID, len(tt.current.Syntax))
}
})
}
}
// TestSelectBestPackages tests the selectBestPackages function
func TestSelectBestPackages(t *testing.T) {
// Helper to create a package with specified properties
makePkg := func(id, path string, syntaxCount int) *packages.Package {
syntax := make([]*ast.File, syntaxCount)
return &packages.Package{
ID: id,
PkgPath: path,
Syntax: syntax,
}
}
tests := []struct {
name string
pkgs []*packages.Package
expectedPkgIDs map[string]string // pkgPath -> expected selected ID
}{
{
name: "single package",
pkgs: []*packages.Package{
makePkg("example.com/pkg", "example.com/pkg", 10),
},
expectedPkgIDs: map[string]string{
"example.com/pkg": "example.com/pkg",
},
},
{
name: "test package preferred over production",
pkgs: []*packages.Package{
makePkg("example.com/pkg", "example.com/pkg", 10),
makePkg("example.com/pkg [example.com/pkg.test]", "example.com/pkg", 15),
},
expectedPkgIDs: map[string]string{
"example.com/pkg": "example.com/pkg [example.com/pkg.test]",
},
},
{
name: "exact test preferred over nested test",
pkgs: []*packages.Package{
makePkg("example.com/pkg [example.com/pkg.test]", "example.com/pkg", 20),
makePkg("example.com/pkg [example.com/pkg/nested.test]", "example.com/pkg", 15),
},
expectedPkgIDs: map[string]string{
"example.com/pkg": "example.com/pkg [example.com/pkg.test]",
},
},
{
name: "multiple packages with different paths",
pkgs: []*packages.Package{
makePkg("example.com/pkg1", "example.com/pkg1", 10),
makePkg("example.com/pkg1 [example.com/pkg1.test]", "example.com/pkg1", 15),
makePkg("example.com/pkg2", "example.com/pkg2", 8),
makePkg("example.com/pkg2 [example.com/pkg2.test]", "example.com/pkg2", 12),
},
expectedPkgIDs: map[string]string{
"example.com/pkg1": "example.com/pkg1 [example.com/pkg1.test]",
"example.com/pkg2": "example.com/pkg2 [example.com/pkg2.test]",
},
},
{
name: "more syntax nodes wins among nested tests",
pkgs: []*packages.Package{
makePkg("example.com/pkg [example.com/pkg/a.test]", "example.com/pkg", 10),
makePkg("example.com/pkg [example.com/pkg/b.test]", "example.com/pkg", 20),
},
expectedPkgIDs: map[string]string{
"example.com/pkg": "example.com/pkg [example.com/pkg/b.test]",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := selectBestPackages(tt.pkgs)
// Check that all expected packages are present
for pkgPath, expectedID := range tt.expectedPkgIDs {
selected, found := result[pkgPath]
if !found {
t.Errorf("Expected package path %q not found in result", pkgPath)
continue
}
if selected.ID != expectedID {
t.Errorf("For package path %q: got ID %q, want %q",
pkgPath, selected.ID, expectedID)
}
}
// Check that no unexpected packages are present
if len(result) != len(tt.expectedPkgIDs) {
t.Errorf("Expected %d packages in result, got %d",
len(tt.expectedPkgIDs), len(result))
}
})
}
}
// TestPackageSelectionRealWorld simulates the real-world go-git scenario
func TestPackageSelectionRealWorld(t *testing.T) {
// Simulate the actual packages.Load result for go-git repository
// when EXTRACT_TESTS=true
pkgs := []*packages.Package{
// Production package only
{
ID: "github.com/go-git/go-git/v6",
PkgPath: "github.com/go-git/go-git/v6",
Syntax: make([]*ast.File, 19), // 19 production files
},
// Root test package - this is what we want!
{
ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]",
PkgPath: "github.com/go-git/go-git/v6",
Syntax: make([]*ast.File, 39), // 19 production + 20 test files
},
// Nested test dependency 1
{
ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/format/packfile.test]",
PkgPath: "github.com/go-git/go-git/v6",
Syntax: make([]*ast.File, 19), // production files only (dependency)
},
// Nested test dependency 2
{
ID: "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6/plumbing/object.test]",
PkgPath: "github.com/go-git/go-git/v6",
Syntax: make([]*ast.File, 19), // production files only (dependency)
},
}
// Use the actual selection logic from the extractor
bestPackageIds := selectBestPackages(pkgs)
// Verify the correct package was selected
selected := bestPackageIds["github.com/go-git/go-git/v6"]
expectedID := "github.com/go-git/go-git/v6 [github.com/go-git/go-git/v6.test]"
expectedSyntaxCount := 39
if selected.ID != expectedID {
t.Errorf("Wrong package selected!\n got: %q (%d syntax nodes)\n want: %q (%d syntax nodes)",
selected.ID, len(selected.Syntax),
expectedID, expectedSyntaxCount)
}
if len(selected.Syntax) != expectedSyntaxCount {
t.Errorf("Wrong syntax count: got %d, want %d", len(selected.Syntax), expectedSyntaxCount)
}
// Verify it's recognized as an exact test package
if !isExactTestPackage(selected) {
t.Errorf("Selected package %q should be recognized as exact test package", selected.ID)
}
}