This repository has been archived by the owner on Feb 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsolve_bimodal_test.go
644 lines (616 loc) · 15 KB
/
solve_bimodal_test.go
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
package vsolver
import (
"fmt"
"path/filepath"
)
// dsp - "depspec with packages"
//
// Wraps a set of tpkgs onto a depspec, and returns it.
func dsp(ds depspec, pkgs ...tpkg) depspec {
ds.pkgs = pkgs
return ds
}
// pkg makes a tpkg appropriate for use in bimodal testing
func pkg(path string, imports ...string) tpkg {
return tpkg{
path: path,
imports: imports,
}
}
func init() {
for k, fix := range bimodalFixtures {
// Assign the name into the fixture itself
fix.n = k
bimodalFixtures[k] = fix
}
}
// Fixtures that rely on simulated bimodal (project and package-level)
// analysis for correct operation. The name given in the map gets assigned into
// the fixture itself in init().
var bimodalFixtures = map[string]bimodalFixture{
// Simple case, ensures that we do the very basics of picking up and
// including a single, simple import that is not expressed as a constraint
"simple bm-add": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "a")),
dsp(mkDepspec("a 1.0.0"),
pkg("a")),
},
r: mksolution(
"a 1.0.0",
),
},
// Ensure it works when the import jump is not from the package with the
// same path as root, but from a subpkg
"subpkg bm-add": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a"),
),
},
r: mksolution(
"a 1.0.0",
),
},
// The same, but with a jump through two subpkgs
"double-subpkg bm-add": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "root/bar"),
pkg("root/bar", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a"),
),
},
r: mksolution(
"a 1.0.0",
),
},
// Same again, but now nest the subpkgs
"double nested subpkg bm-add": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "root/foo/bar"),
pkg("root/foo/bar", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a"),
),
},
r: mksolution(
"a 1.0.0",
),
},
// Importing package from project with no root package
"bm-add on project with no pkg in root dir": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "a/foo")),
dsp(mkDepspec("a 1.0.0"),
pkg("a/foo")),
},
r: mksolution(
"a 1.0.0",
),
},
// Import jump is in a dep, and points to a transitive dep
"transitive bm-add": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "b"),
),
dsp(mkDepspec("b 1.0.0"),
pkg("b"),
),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0",
),
},
// Constraints apply only if the project that declares them has a
// reachable import
"constraints activated by import": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0", "b 1.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "b"),
),
dsp(mkDepspec("b 1.0.0"),
pkg("b"),
),
dsp(mkDepspec("b 1.1.0"),
pkg("b"),
),
},
r: mksolution(
"a 1.0.0",
"b 1.1.0",
),
},
// Import jump is in a dep, and points to a transitive dep - but only in not
// the first version we try
"transitive bm-add on older version": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0", "a ~1.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "b"),
),
dsp(mkDepspec("a 1.1.0"),
pkg("a"),
),
dsp(mkDepspec("b 1.0.0"),
pkg("b"),
),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0",
),
},
// Import jump is in a dep, and points to a transitive dep - but will only
// get there via backtracking
"backtrack to dep on bm-add": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "a", "b"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "c"),
),
dsp(mkDepspec("a 1.1.0"),
pkg("a"),
),
// Include two versions of b, otherwise it'll be selected first
dsp(mkDepspec("b 0.9.0"),
pkg("b", "c"),
),
dsp(mkDepspec("b 1.0.0"),
pkg("b", "c"),
),
dsp(mkDepspec("c 1.0.0", "a 1.0.0"),
pkg("c", "a"),
),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0",
"c 1.0.0",
),
},
// Import jump is in a dep subpkg, and points to a transitive dep
"transitive subpkg bm-add": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "a/bar"),
pkg("a/bar", "b"),
),
dsp(mkDepspec("b 1.0.0"),
pkg("b"),
),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0",
),
},
// Import jump is in a dep subpkg, pointing to a transitive dep, but only in
// not the first version we try
"transitive subpkg bm-add on older version": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0", "a ~1.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "a/bar"),
pkg("a/bar", "b"),
),
dsp(mkDepspec("a 1.1.0"),
pkg("a", "a/bar"),
pkg("a/bar"),
),
dsp(mkDepspec("b 1.0.0"),
pkg("b"),
),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0",
),
},
// Ensure that if a constraint is expressed, but no actual import exists,
// then the constraint is disregarded - the project named in the constraint
// is not part of the solution.
"ignore constraint without import": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0", "a 1.0.0"),
pkg("root", "root/foo"),
pkg("root/foo"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a"),
),
},
r: mksolution(),
},
// Transitive deps from one project (a) get incrementally included as other
// deps incorporate its various packages.
"multi-stage pkg incorporation": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "a", "d"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "b"),
pkg("a/second", "c"),
),
dsp(mkDepspec("b 2.0.0"),
pkg("b"),
),
dsp(mkDepspec("c 1.2.0"),
pkg("c"),
),
dsp(mkDepspec("d 1.0.0"),
pkg("d", "a/second"),
),
},
r: mksolution(
"a 1.0.0",
"b 2.0.0",
"c 1.2.0",
"d 1.0.0",
),
},
// Regression - make sure that the the constraint/import intersector only
// accepts a project 'match' if exactly equal, or a separating slash is
// present.
"radix path separator post-check": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "foo", "foobar"),
),
dsp(mkDepspec("foo 1.0.0"),
pkg("foo"),
),
dsp(mkDepspec("foobar 1.0.0"),
pkg("foobar"),
),
},
r: mksolution(
"foo 1.0.0",
"foobar 1.0.0",
),
},
// Well-formed failure when there's a dependency on a pkg that doesn't exist
"fail when imports nonexistent package": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0", "a 1.0.0"),
pkg("root", "a/foo"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a"),
),
},
errp: []string{"a", "root", "a"},
},
// Transitive deps from one project (a) get incrementally included as other
// deps incorporate its various packages, and fail with proper error when we
// discover one incrementally that isn't present
"fail multi-stage missing pkg": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "a", "d"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "b"),
pkg("a/second", "c"),
),
dsp(mkDepspec("b 2.0.0"),
pkg("b"),
),
dsp(mkDepspec("c 1.2.0"),
pkg("c"),
),
dsp(mkDepspec("d 1.0.0"),
pkg("d", "a/second"),
pkg("d", "a/nonexistent"),
),
},
errp: []string{"d", "a", "d"},
},
// Check ignores on the root project
"ignore in double-subpkg": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "root/bar", "b"),
pkg("root/bar", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a"),
),
dsp(mkDepspec("b 1.0.0"),
pkg("b"),
),
},
ignore: []string{"root/bar"},
r: mksolution(
"b 1.0.0",
),
},
// Ignores on a dep pkg
"ignore through dep pkg": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "root/foo"),
pkg("root/foo", "a"),
),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "a/bar"),
pkg("a/bar", "b"),
),
dsp(mkDepspec("b 1.0.0"),
pkg("b"),
),
},
ignore: []string{"a/bar"},
r: mksolution(
"a 1.0.0",
),
},
// Preferred version, as derived from a dep's lock, is attempted first
"respect prefv, simple case": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "a")),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "b")),
dsp(mkDepspec("b 1.0.0 foorev"),
pkg("b")),
dsp(mkDepspec("b 2.0.0 barrev"),
pkg("b")),
},
lm: map[string]fixLock{
"a 1.0.0": mklock(
"b 1.0.0 foorev",
),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0 foorev",
),
},
// Preferred version, as derived from a dep's lock, is attempted first, even
// if the root also has a direct dep on it (root doesn't need to use
// preferreds, because it has direct control AND because the root lock
// already supercedes dep lock "preferences")
"respect dep prefv with root import": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "a", "b")),
dsp(mkDepspec("a 1.0.0"),
pkg("a", "b")),
//dsp(newDepspec("a 1.0.1"),
//pkg("a", "b")),
//dsp(newDepspec("a 1.1.0"),
//pkg("a", "b")),
dsp(mkDepspec("b 1.0.0 foorev"),
pkg("b")),
dsp(mkDepspec("b 2.0.0 barrev"),
pkg("b")),
},
lm: map[string]fixLock{
"a 1.0.0": mklock(
"b 1.0.0 foorev",
),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0 foorev",
),
},
// Preferred versions can only work if the thing offering it has been
// selected, or at least marked in the unselected queue
"prefv only works if depper is selected": {
ds: []depspec{
dsp(mkDepspec("root 0.0.0"),
pkg("root", "a", "b")),
// Three atoms for a, which will mean it gets visited after b
dsp(mkDepspec("a 1.0.0"),
pkg("a", "b")),
dsp(mkDepspec("a 1.0.1"),
pkg("a", "b")),
dsp(mkDepspec("a 1.1.0"),
pkg("a", "b")),
dsp(mkDepspec("b 1.0.0 foorev"),
pkg("b")),
dsp(mkDepspec("b 2.0.0 barrev"),
pkg("b")),
},
lm: map[string]fixLock{
"a 1.0.0": mklock(
"b 1.0.0 foorev",
),
},
r: mksolution(
"a 1.1.0",
"b 2.0.0 barrev",
),
},
}
// tpkg is a representation of a single package. It has its own import path, as
// well as a list of paths it itself "imports".
type tpkg struct {
// Full import path of this package
path string
// Slice of full paths to its virtual imports
imports []string
}
type bimodalFixture struct {
// name of this fixture datum
n string
// bimodal project. first is always treated as root project
ds []depspec
// results; map of name/version pairs
r map[string]Version
// max attempts the solver should need to find solution. 0 means no limit
maxAttempts int
// Use downgrade instead of default upgrade sorter
downgrade bool
// lock file simulator, if one's to be used at all
l fixLock
// map of locks for deps, if any. keys should be of the form:
// "<project> <version>"
lm map[string]fixLock
// projects expected to have errors, if any
errp []string
// request up/downgrade to all projects
changeall bool
// pkgs to ignore
ignore []string
}
func (f bimodalFixture) name() string {
return f.n
}
func (f bimodalFixture) specs() []depspec {
return f.ds
}
func (f bimodalFixture) maxTries() int {
return f.maxAttempts
}
func (f bimodalFixture) expectErrs() []string {
return f.errp
}
func (f bimodalFixture) solution() map[string]Version {
return f.r
}
// bmSourceManager is an SM specifically for the bimodal fixtures. It composes
// the general depspec SM, and differs from it in how it answers static analysis
// calls, and its support for package ignores and dep lock data.
type bmSourceManager struct {
depspecSourceManager
lm map[string]fixLock
}
var _ SourceManager = &bmSourceManager{}
func newbmSM(bmf bimodalFixture) *bmSourceManager {
sm := &bmSourceManager{
depspecSourceManager: *newdepspecSM(bmf.ds, bmf.ignore),
}
sm.rm = computeBimodalExternalMap(bmf.ds)
sm.lm = bmf.lm
return sm
}
func (sm *bmSourceManager) ListPackages(n ProjectName, v Version) (PackageTree, error) {
for k, ds := range sm.specs {
// Cheat for root, otherwise we blow up b/c version is empty
if n == ds.n && (k == 0 || ds.v.Matches(v)) {
ptree := PackageTree{
ImportRoot: string(n),
Packages: make(map[string]PackageOrErr),
}
for _, pkg := range ds.pkgs {
ptree.Packages[pkg.path] = PackageOrErr{
P: Package{
ImportPath: pkg.path,
Name: filepath.Base(pkg.path),
Imports: pkg.imports,
},
}
}
return ptree, nil
}
}
return PackageTree{}, fmt.Errorf("Project %s at version %s could not be found", n, v)
}
func (sm *bmSourceManager) GetProjectInfo(n ProjectName, v Version) (Manifest, Lock, error) {
for _, ds := range sm.specs {
if n == ds.n && v.Matches(ds.v) {
if l, exists := sm.lm[string(n)+" "+v.String()]; exists {
return ds, l, nil
} else {
return ds, dummyLock{}, nil
}
}
}
// TODO proper solver-type errors
return nil, nil, fmt.Errorf("Project %s at version %s could not be found", n, v)
}
// computeBimodalExternalMap takes a set of depspecs and computes an
// internally-versioned external reach map that is useful for quickly answering
// ListExternal()-type calls.
//
// Note that it does not do things like stripping out stdlib packages - these
// maps are intended for use in SM fixtures, and that's a higher-level
// responsibility within the system.
func computeBimodalExternalMap(ds []depspec) map[pident]map[string][]string {
// map of project name+version -> map of subpkg name -> external pkg list
rm := make(map[pident]map[string][]string)
// algorithm adapted from externalReach()
for _, d := range ds {
// Keeps a list of all internal and external reaches for packages within
// a given root. We create one on each pass through, rather than doing
// them all at once, because the depspec set may (read: is expected to)
// have multiple versions of the same base project, and each of those
// must be calculated independently.
workmap := make(map[string]wm)
for _, pkg := range d.pkgs {
if !checkPrefixSlash(filepath.Clean(pkg.path), string(d.n)) {
panic(fmt.Sprintf("pkg %s is not a child of %s, cannot be a part of that project", pkg.path, d.n))
}
w := wm{
ex: make(map[string]bool),
in: make(map[string]bool),
}
for _, imp := range pkg.imports {
if !checkPrefixSlash(filepath.Clean(imp), string(d.n)) {
// Easy case - if the import is not a child of the base
// project path, put it in the external map
w.ex[imp] = true
} else {
if w2, seen := workmap[imp]; seen {
// If it is, and we've seen that path, dereference it
// immediately
for i := range w2.ex {
w.ex[i] = true
}
for i := range w2.in {
w.in[i] = true
}
} else {
// Otherwise, put it in the 'in' map for later
// reprocessing
w.in[imp] = true
}
}
}
workmap[pkg.path] = w
}
drm := wmToReach(workmap, "")
rm[pident{n: d.n, v: d.v}] = drm
}
return rm
}