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_basic_test.go
1394 lines (1281 loc) · 34.2 KB
/
solve_basic_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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package vsolver
import (
"fmt"
"regexp"
"strings"
"github.com/Masterminds/semver"
)
var regfrom = regexp.MustCompile(`^(\w*) from (\w*) ([0-9\.]*)`)
// nvSplit splits an "info" string on " " into the pair of name and
// version/constraint, and returns each individually.
//
// This is for narrow use - panics if there are less than two resulting items in
// the slice.
func nvSplit(info string) (id ProjectIdentifier, version string) {
if strings.Contains(info, " from ") {
parts := regfrom.FindStringSubmatch(info)
info = parts[1] + " " + parts[3]
id.NetworkName = parts[2]
}
s := strings.SplitN(info, " ", 2)
if len(s) < 2 {
panic(fmt.Sprintf("Malformed name/version info string '%s'", info))
}
id.LocalName, version = ProjectName(s[0]), s[1]
if id.NetworkName == "" {
id.NetworkName = string(id.LocalName)
}
return
}
// nvrSplit splits an "info" string on " " into the triplet of name,
// version/constraint, and revision, and returns each individually.
//
// It will work fine if only name and version/constraint are provided.
//
// This is for narrow use - panics if there are less than two resulting items in
// the slice.
func nvrSplit(info string) (id ProjectIdentifier, version string, revision Revision) {
if strings.Contains(info, " from ") {
parts := regfrom.FindStringSubmatch(info)
info = parts[1] + " " + parts[3]
id.NetworkName = parts[2]
}
s := strings.SplitN(info, " ", 3)
if len(s) < 2 {
panic(fmt.Sprintf("Malformed name/version info string '%s'", info))
}
id.LocalName, version = ProjectName(s[0]), s[1]
if id.NetworkName == "" {
id.NetworkName = string(id.LocalName)
}
if len(s) == 3 {
revision = Revision(s[2])
}
return
}
// mkAtom splits the input string on a space, and uses the first two elements as
// the project identifier and version, respectively.
//
// The version segment may have a leading character indicating the type of
// version to create:
//
// p: create a "plain" (non-semver) version.
// b: create a branch version.
// r: create a revision.
//
// No prefix is assumed to indicate a semver version.
//
// If a third space-delimited element is provided, it will be interepreted as a
// revision, and used as the underlying version in a PairedVersion. No prefix
// should be provided in this case. It is an error (and will panic) to try to
// pass a revision with an underlying revision.
func mkAtom(info string) atom {
id, ver, rev := nvrSplit(info)
var v Version
switch ver[0] {
case 'r':
if rev != "" {
panic("Cannot pair a revision with a revision")
}
v = Revision(ver[1:])
case 'p':
v = NewVersion(ver[1:])
case 'b':
v = NewBranch(ver[1:])
default:
_, err := semver.NewVersion(ver)
if err != nil {
// don't want to allow bad test data at this level, so just panic
panic(fmt.Sprintf("Error when converting '%s' into semver: %s", ver, err))
}
v = NewVersion(ver)
}
if rev != "" {
v = v.(UnpairedVersion).Is(rev)
}
return atom{
id: id,
v: v,
}
}
// mkPDep splits the input string on a space, and uses the first two elements
// as the project identifier and constraint body, respectively.
//
// The constraint body may have a leading character indicating the type of
// version to create:
//
// p: create a "plain" (non-semver) version.
// b: create a branch version.
// r: create a revision.
//
// If no leading character is used, a semver constraint is assumed.
func mkPDep(info string) ProjectDep {
id, ver, rev := nvrSplit(info)
var c Constraint
switch ver[0] {
case 'r':
c = Revision(ver[1:])
case 'p':
c = NewVersion(ver[1:])
case 'b':
c = NewBranch(ver[1:])
default:
// Without one of those leading characters, we know it's a proper semver
// expression, so use the other parser that doesn't look for a rev
rev = ""
id, ver = nvSplit(info)
var err error
c, err = NewSemverConstraint(ver)
if err != nil {
// don't want bad test data at this level, so just panic
panic(fmt.Sprintf("Error when converting '%s' into semver constraint: %s (full info: %s)", ver, err, info))
}
}
// There's no practical reason that a real tool would need to produce a
// constraint that's a PairedVersion, but it is a possibility admitted by the
// system, so we at least allow for it in our testing harness.
if rev != "" {
// Of course, this *will* panic if the predicate is a revision or a
// semver constraint, neither of which implement UnpairedVersion. This
// is as intended, to prevent bad data from entering the system.
c = c.(UnpairedVersion).Is(rev)
}
return ProjectDep{
Ident: id,
Constraint: c,
}
}
// A depspec is a fixture representing all the information a SourceManager would
// ordinarily glean directly from interrogating a repository.
type depspec struct {
n ProjectName
v Version
deps []ProjectDep
devdeps []ProjectDep
pkgs []tpkg
}
// mkDepspec creates a depspec by processing a series of strings, each of which
// contains an identiifer and version information.
//
// The first string is broken out into the name and version of the package being
// described - see the docs on mkAtom for details. subsequent strings are
// interpreted as dep constraints of that dep at that version. See the docs on
// mkPDep for details.
//
// If a string other than the first includes a "(dev) " prefix, it will be
// treated as a test-only dependency.
func mkDepspec(pi string, deps ...string) depspec {
pa := mkAtom(pi)
if string(pa.id.LocalName) != pa.id.NetworkName {
panic("alternate source on self makes no sense")
}
ds := depspec{
n: pa.id.LocalName,
v: pa.v,
}
for _, dep := range deps {
var sl *[]ProjectDep
if strings.HasPrefix(dep, "(dev) ") {
dep = strings.TrimPrefix(dep, "(dev) ")
sl = &ds.devdeps
} else {
sl = &ds.deps
}
*sl = append(*sl, mkPDep(dep))
}
return ds
}
// mklock makes a fixLock, suitable to act as a lock file
func mklock(pairs ...string) fixLock {
l := make(fixLock, 0)
for _, s := range pairs {
pa := mkAtom(s)
l = append(l, NewLockedProject(pa.id.LocalName, pa.v, pa.id.netName(), "", nil))
}
return l
}
// mkrevlock makes a fixLock, suitable to act as a lock file, with only a name
// and a rev
func mkrevlock(pairs ...string) fixLock {
l := make(fixLock, 0)
for _, s := range pairs {
pa := mkAtom(s)
l = append(l, NewLockedProject(pa.id.LocalName, pa.v.(PairedVersion).Underlying(), pa.id.netName(), "", nil))
}
return l
}
// mksolution makes a result set
func mksolution(pairs ...string) map[string]Version {
m := make(map[string]Version)
for _, pair := range pairs {
a := mkAtom(pair)
// TODO identifierify
m[string(a.id.LocalName)] = a.v
}
return m
}
// computeBasicReachMap takes a depspec and computes a reach map which is
// identical to the explicit depgraph.
//
// Using a reachMap here is overkill for what the basic fixtures actually need,
// but we use it anyway for congruence with the more general cases.
func computeBasicReachMap(ds []depspec) reachMap {
rm := make(reachMap)
for k, d := range ds {
n := string(d.n)
lm := map[string][]string{
n: nil,
}
v := d.v
if k == 0 {
// Put the root in with a nil rev, to accommodate the solver
v = nil
}
rm[pident{n: d.n, v: v}] = lm
for _, dep := range d.deps {
lm[n] = append(lm[n], string(dep.Ident.LocalName))
}
// first is root
if k == 0 {
for _, dep := range d.devdeps {
lm[n] = append(lm[n], string(dep.Ident.LocalName))
}
}
}
return rm
}
type pident struct {
n ProjectName
v Version
}
type specfix interface {
name() string
specs() []depspec
maxTries() int
expectErrs() []string
solution() map[string]Version
}
// A basicFixture is a declarative test fixture that can cover a wide variety of
// solver cases. All cases, however, maintain one invariant: package == project.
// There are no subpackages, and so it is impossible for them to trigger or
// require bimodal solving.
//
// This type is separate from bimodalFixture in part for legacy reasons - many
// of these were adapted from similar tests in dart's pub lib, where there is no
// such thing as "bimodal solving".
//
// But it's also useful to keep them separate because bimodal solving involves
// considerably more complexity than simple solving, both in terms of fixture
// declaration and actual solving mechanics. Thus, we gain a lot of value for
// contributors and maintainers by keeping comprehension costs relatively low
// while still covering important cases.
type basicFixture struct {
// name of this fixture datum
n string
// depspecs. always treat first as root
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
// projects expected to have errors, if any
errp []string
// request up/downgrade to all projects
changeall bool
}
func (f basicFixture) name() string {
return f.n
}
func (f basicFixture) specs() []depspec {
return f.ds
}
func (f basicFixture) maxTries() int {
return f.maxAttempts
}
func (f basicFixture) expectErrs() []string {
return f.errp
}
func (f basicFixture) solution() map[string]Version {
return f.r
}
// A table of basicFixtures, used in the basic solving test set.
var basicFixtures = []basicFixture{
// basic fixtures
{
n: "no dependencies",
ds: []depspec{
mkDepspec("root 0.0.0"),
},
r: mksolution(),
},
{
n: "simple dependency tree",
ds: []depspec{
mkDepspec("root 0.0.0", "a 1.0.0", "b 1.0.0"),
mkDepspec("a 1.0.0", "aa 1.0.0", "ab 1.0.0"),
mkDepspec("aa 1.0.0"),
mkDepspec("ab 1.0.0"),
mkDepspec("b 1.0.0", "ba 1.0.0", "bb 1.0.0"),
mkDepspec("ba 1.0.0"),
mkDepspec("bb 1.0.0"),
},
r: mksolution(
"a 1.0.0",
"aa 1.0.0",
"ab 1.0.0",
"b 1.0.0",
"ba 1.0.0",
"bb 1.0.0",
),
},
{
n: "shared dependency with overlapping constraints",
ds: []depspec{
mkDepspec("root 0.0.0", "a 1.0.0", "b 1.0.0"),
mkDepspec("a 1.0.0", "shared >=2.0.0, <4.0.0"),
mkDepspec("b 1.0.0", "shared >=3.0.0, <5.0.0"),
mkDepspec("shared 2.0.0"),
mkDepspec("shared 3.0.0"),
mkDepspec("shared 3.6.9"),
mkDepspec("shared 4.0.0"),
mkDepspec("shared 5.0.0"),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0",
"shared 3.6.9",
),
},
{
n: "downgrade on overlapping constraints",
ds: []depspec{
mkDepspec("root 0.0.0", "a 1.0.0", "b 1.0.0"),
mkDepspec("a 1.0.0", "shared >=2.0.0, <=4.0.0"),
mkDepspec("b 1.0.0", "shared >=3.0.0, <5.0.0"),
mkDepspec("shared 2.0.0"),
mkDepspec("shared 3.0.0"),
mkDepspec("shared 3.6.9"),
mkDepspec("shared 4.0.0"),
mkDepspec("shared 5.0.0"),
},
r: mksolution(
"a 1.0.0",
"b 1.0.0",
"shared 3.0.0",
),
downgrade: true,
},
{
n: "shared dependency where dependent version in turn affects other dependencies",
ds: []depspec{
mkDepspec("root 0.0.0", "foo <=1.0.2", "bar 1.0.0"),
mkDepspec("foo 1.0.0"),
mkDepspec("foo 1.0.1", "bang 1.0.0"),
mkDepspec("foo 1.0.2", "whoop 1.0.0"),
mkDepspec("foo 1.0.3", "zoop 1.0.0"),
mkDepspec("bar 1.0.0", "foo <=1.0.1"),
mkDepspec("bang 1.0.0"),
mkDepspec("whoop 1.0.0"),
mkDepspec("zoop 1.0.0"),
},
r: mksolution(
"foo 1.0.1",
"bar 1.0.0",
"bang 1.0.0",
),
},
{
n: "removed dependency",
ds: []depspec{
mkDepspec("root 1.0.0", "foo 1.0.0", "bar *"),
mkDepspec("foo 1.0.0"),
mkDepspec("foo 2.0.0"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 2.0.0", "baz 1.0.0"),
mkDepspec("baz 1.0.0", "foo 2.0.0"),
},
r: mksolution(
"foo 1.0.0",
"bar 1.0.0",
),
maxAttempts: 2,
},
{
n: "with mismatched net addrs",
ds: []depspec{
mkDepspec("root 1.0.0", "foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.0", "bar from baz 1.0.0"),
mkDepspec("bar 1.0.0"),
},
// TODO ugh; do real error comparison instead of shitty abstraction
errp: []string{"foo", "foo", "root"},
},
// fixtures with locks
{
n: "with compatible locked dependency",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1", "bar 1.0.1"),
mkDepspec("foo 1.0.2", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
},
l: mklock(
"foo 1.0.1",
),
r: mksolution(
"foo 1.0.1",
"bar 1.0.1",
),
},
{
n: "upgrade through lock",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1", "bar 1.0.1"),
mkDepspec("foo 1.0.2", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
},
l: mklock(
"foo 1.0.1",
),
r: mksolution(
"foo 1.0.2",
"bar 1.0.2",
),
changeall: true,
},
{
n: "downgrade through lock",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1", "bar 1.0.1"),
mkDepspec("foo 1.0.2", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
},
l: mklock(
"foo 1.0.1",
),
r: mksolution(
"foo 1.0.0",
"bar 1.0.0",
),
changeall: true,
downgrade: true,
},
{
n: "with incompatible locked dependency",
ds: []depspec{
mkDepspec("root 0.0.0", "foo >1.0.1"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1", "bar 1.0.1"),
mkDepspec("foo 1.0.2", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
},
l: mklock(
"foo 1.0.1",
),
r: mksolution(
"foo 1.0.2",
"bar 1.0.2",
),
},
{
n: "with unrelated locked dependency",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1", "bar 1.0.1"),
mkDepspec("foo 1.0.2", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
mkDepspec("baz 1.0.0 bazrev"),
},
l: mklock(
"baz 1.0.0 bazrev",
),
r: mksolution(
"foo 1.0.2",
"bar 1.0.2",
),
},
{
n: "unlocks dependencies if necessary to ensure that a new dependency is satisfied",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *", "newdep *"),
mkDepspec("foo 1.0.0 foorev", "bar <2.0.0"),
mkDepspec("bar 1.0.0 barrev", "baz <2.0.0"),
mkDepspec("baz 1.0.0 bazrev", "qux <2.0.0"),
mkDepspec("qux 1.0.0 quxrev"),
mkDepspec("foo 2.0.0", "bar <3.0.0"),
mkDepspec("bar 2.0.0", "baz <3.0.0"),
mkDepspec("baz 2.0.0", "qux <3.0.0"),
mkDepspec("qux 2.0.0"),
mkDepspec("newdep 2.0.0", "baz >=1.5.0"),
},
l: mklock(
"foo 1.0.0 foorev",
"bar 1.0.0 barrev",
"baz 1.0.0 bazrev",
"qux 1.0.0 quxrev",
),
r: mksolution(
"foo 2.0.0",
"bar 2.0.0",
"baz 2.0.0",
"qux 1.0.0 quxrev",
"newdep 2.0.0",
),
maxAttempts: 4,
},
{
n: "locked atoms are matched on both local and net name",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *"),
mkDepspec("foo 1.0.0 foorev"),
mkDepspec("foo 2.0.0 foorev2"),
},
l: mklock(
"foo from baz 1.0.0 foorev",
),
r: mksolution(
"foo 2.0.0 foorev2",
),
},
{
n: "pairs bare revs in lock with versions",
ds: []depspec{
mkDepspec("root 0.0.0", "foo ~1.0.1"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1 foorev", "bar 1.0.1"),
mkDepspec("foo 1.0.2", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
},
l: mkrevlock(
"foo 1.0.1 foorev", // mkrevlock drops the 1.0.1
),
r: mksolution(
"foo 1.0.1 foorev",
"bar 1.0.1",
),
},
{
n: "pairs bare revs in lock with all versions",
ds: []depspec{
mkDepspec("root 0.0.0", "foo ~1.0.1"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1 foorev", "bar 1.0.1"),
mkDepspec("foo 1.0.2 foorev", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
},
l: mkrevlock(
"foo 1.0.1 foorev", // mkrevlock drops the 1.0.1
),
r: mksolution(
"foo 1.0.2 foorev",
"bar 1.0.1",
),
},
{
n: "does not pair bare revs in manifest with unpaired lock version",
ds: []depspec{
mkDepspec("root 0.0.0", "foo ~1.0.1"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1 foorev", "bar 1.0.1"),
mkDepspec("foo 1.0.2", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
},
l: mkrevlock(
"foo 1.0.1 foorev", // mkrevlock drops the 1.0.1
),
r: mksolution(
"foo 1.0.1 foorev",
"bar 1.0.1",
),
},
{
n: "includes root package's dev dependencies",
ds: []depspec{
mkDepspec("root 1.0.0", "(dev) foo 1.0.0", "(dev) bar 1.0.0"),
mkDepspec("foo 1.0.0"),
mkDepspec("bar 1.0.0"),
},
r: mksolution(
"foo 1.0.0",
"bar 1.0.0",
),
},
{
n: "includes dev dependency's transitive dependencies",
ds: []depspec{
mkDepspec("root 1.0.0", "(dev) foo 1.0.0"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("bar 1.0.0"),
},
r: mksolution(
"foo 1.0.0",
"bar 1.0.0",
),
},
{
n: "ignores transitive dependency's dev dependencies",
ds: []depspec{
mkDepspec("root 1.0.0", "(dev) foo 1.0.0"),
mkDepspec("foo 1.0.0", "(dev) bar 1.0.0"),
mkDepspec("bar 1.0.0"),
},
r: mksolution(
"foo 1.0.0",
),
},
{
n: "no version that matches requirement",
ds: []depspec{
mkDepspec("root 0.0.0", "foo >=1.0.0, <2.0.0"),
mkDepspec("foo 2.0.0"),
mkDepspec("foo 2.1.3"),
},
errp: []string{"foo", "root"},
},
{
n: "no version that matches combined constraint",
ds: []depspec{
mkDepspec("root 0.0.0", "foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.0", "shared >=2.0.0, <3.0.0"),
mkDepspec("bar 1.0.0", "shared >=2.9.0, <4.0.0"),
mkDepspec("shared 2.5.0"),
mkDepspec("shared 3.5.0"),
},
errp: []string{"shared", "foo", "bar"},
},
{
n: "disjoint constraints",
ds: []depspec{
mkDepspec("root 0.0.0", "foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.0", "shared <=2.0.0"),
mkDepspec("bar 1.0.0", "shared >3.0.0"),
mkDepspec("shared 2.0.0"),
mkDepspec("shared 4.0.0"),
},
//errp: []string{"shared", "foo", "bar"}, // dart's has this...
errp: []string{"foo", "bar"},
},
{
n: "no valid solution",
ds: []depspec{
mkDepspec("root 0.0.0", "a *", "b *"),
mkDepspec("a 1.0.0", "b 1.0.0"),
mkDepspec("a 2.0.0", "b 2.0.0"),
mkDepspec("b 1.0.0", "a 2.0.0"),
mkDepspec("b 2.0.0", "a 1.0.0"),
},
errp: []string{"b", "a"},
maxAttempts: 2,
},
{
n: "no version that matches while backtracking",
ds: []depspec{
mkDepspec("root 0.0.0", "a *", "b >1.0.0"),
mkDepspec("a 1.0.0"),
mkDepspec("b 1.0.0"),
},
errp: []string{"b", "root"},
},
{
// The latest versions of a and b disagree on c. An older version of either
// will resolve the problem. This test validates that b, which is farther
// in the dependency graph from myapp is downgraded first.
n: "rolls back leaf versions first",
ds: []depspec{
mkDepspec("root 0.0.0", "a *"),
mkDepspec("a 1.0.0", "b *"),
mkDepspec("a 2.0.0", "b *", "c 2.0.0"),
mkDepspec("b 1.0.0"),
mkDepspec("b 2.0.0", "c 1.0.0"),
mkDepspec("c 1.0.0"),
mkDepspec("c 2.0.0"),
},
r: mksolution(
"a 2.0.0",
"b 1.0.0",
"c 2.0.0",
),
maxAttempts: 2,
},
{
// Only one version of baz, so foo and bar will have to downgrade until they
// reach it.
n: "simple transitive",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *"),
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 2.0.0", "bar 2.0.0"),
mkDepspec("foo 3.0.0", "bar 3.0.0"),
mkDepspec("bar 1.0.0", "baz *"),
mkDepspec("bar 2.0.0", "baz 2.0.0"),
mkDepspec("bar 3.0.0", "baz 3.0.0"),
mkDepspec("baz 1.0.0"),
},
r: mksolution(
"foo 1.0.0",
"bar 1.0.0",
"baz 1.0.0",
),
maxAttempts: 3,
},
{
// Ensures the solver doesn"t exhaustively search all versions of b when
// it's a-2.0.0 whose dependency on c-2.0.0-nonexistent led to the
// problem. We make sure b has more versions than a so that the solver
// tries a first since it sorts sibling dependencies by number of
// versions.
n: "simple transitive",
ds: []depspec{
mkDepspec("root 0.0.0", "a *", "b *"),
mkDepspec("a 1.0.0", "c 1.0.0"),
mkDepspec("a 2.0.0", "c 2.0.0"),
mkDepspec("b 1.0.0"),
mkDepspec("b 2.0.0"),
mkDepspec("b 3.0.0"),
mkDepspec("c 1.0.0"),
},
r: mksolution(
"a 1.0.0",
"b 3.0.0",
"c 1.0.0",
),
maxAttempts: 2,
},
{
// Dependencies are ordered so that packages with fewer versions are
// tried first. Here, there are two valid solutions (either a or b must
// be downgraded once). The chosen one depends on which dep is traversed
// first. Since b has fewer versions, it will be traversed first, which
// means a will come later. Since later selections are revised first, a
// gets downgraded.
n: "traverse into package with fewer versions first",
ds: []depspec{
mkDepspec("root 0.0.0", "a *", "b *"),
mkDepspec("a 1.0.0", "c *"),
mkDepspec("a 2.0.0", "c *"),
mkDepspec("a 3.0.0", "c *"),
mkDepspec("a 4.0.0", "c *"),
mkDepspec("a 5.0.0", "c 1.0.0"),
mkDepspec("b 1.0.0", "c *"),
mkDepspec("b 2.0.0", "c *"),
mkDepspec("b 3.0.0", "c *"),
mkDepspec("b 4.0.0", "c 2.0.0"),
mkDepspec("c 1.0.0"),
mkDepspec("c 2.0.0"),
},
r: mksolution(
"a 4.0.0",
"b 4.0.0",
"c 2.0.0",
),
maxAttempts: 2,
},
{
// This is similar to the preceding fixture. When getting the number of
// versions of a package to determine which to traverse first, versions
// that are disallowed by the root package"s constraints should not be
// considered. Here, foo has more versions of bar in total (4), but
// fewer that meet myapp"s constraints (only 2). There is no solution,
// but we will do less backtracking if foo is tested first.
n: "traverse into package with fewer versions first",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *", "bar *"),
mkDepspec("foo 1.0.0", "none 2.0.0"),
mkDepspec("foo 2.0.0", "none 2.0.0"),
mkDepspec("foo 3.0.0", "none 2.0.0"),
mkDepspec("foo 4.0.0", "none 2.0.0"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 2.0.0"),
mkDepspec("bar 3.0.0"),
mkDepspec("none 1.0.0"),
},
errp: []string{"none", "foo"},
maxAttempts: 2,
},
{
// If there"s a disjoint constraint on a package, then selecting other
// versions of it is a waste of time: no possible versions can match. We
// need to jump past it to the most recent package that affected the
// constraint.
n: "backjump past failed package on disjoint constraint",
ds: []depspec{
mkDepspec("root 0.0.0", "a *", "foo *"),
mkDepspec("a 1.0.0", "foo *"),
mkDepspec("a 2.0.0", "foo <1.0.0"),
mkDepspec("foo 2.0.0"),
mkDepspec("foo 2.0.1"),
mkDepspec("foo 2.0.2"),
mkDepspec("foo 2.0.3"),
mkDepspec("foo 2.0.4"),
mkDepspec("none 1.0.0"),
},
r: mksolution(
"a 1.0.0",
"foo 2.0.4",
),
maxAttempts: 2,
},
{
// Revision enters vqueue if a dep has a constraint on that revision
n: "revision injected into vqueue",
ds: []depspec{
mkDepspec("root 0.0.0", "foo r123abc"),
mkDepspec("foo r123abc"),
mkDepspec("foo 1.0.0 foorev"),
mkDepspec("foo 2.0.0 foorev2"),
},
r: mksolution(
"foo r123abc",
),
},
// TODO decide how to refactor the solver in order to re-enable these.
// Checking for revision existence is important...but kinda obnoxious.
//{
//// Solve fails if revision constraint calls for a nonexistent revision
//n: "fail on missing revision",
//ds: []depspec{
//mkDepspec("root 0.0.0", "bar *"),
//mkDepspec("bar 1.0.0", "foo r123abc"),
//mkDepspec("foo r123nomatch"),
//mkDepspec("foo 1.0.0"),
//mkDepspec("foo 2.0.0"),
//},
//errp: []string{"bar", "foo", "bar"},
//},
//{
//// Solve fails if revision constraint calls for a nonexistent revision,
//// even if rev constraint is specified by root
//n: "fail on missing revision from root",
//ds: []depspec{
//mkDepspec("root 0.0.0", "foo r123nomatch"),
//mkDepspec("foo r123abc"),
//mkDepspec("foo 1.0.0"),
//mkDepspec("foo 2.0.0"),
//},
//errp: []string{"foo", "root", "foo"},
//},
// TODO add fixture that tests proper handling of loops via aliases (where
// a project that wouldn't be a loop is aliased to a project that is a loop)
}
func init() {
// This sets up a hundred versions of foo and bar, 0.0.0 through 9.9.0. Each
// version of foo depends on a baz with the same major version. Each version
// of bar depends on a baz with the same minor version. There is only one
// version of baz, 0.0.0, so only older versions of foo and bar will
// satisfy it.
fix := basicFixture{
n: "complex backtrack",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *", "bar *"),
mkDepspec("baz 0.0.0"),
},
r: mksolution(
"foo 0.9.0",
"bar 9.0.0",
"baz 0.0.0",
),
maxAttempts: 10,
}
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
fix.ds = append(fix.ds, mkDepspec(fmt.Sprintf("foo %v.%v.0", i, j), fmt.Sprintf("baz %v.0.0", i)))
fix.ds = append(fix.ds, mkDepspec(fmt.Sprintf("bar %v.%v.0", i, j), fmt.Sprintf("baz 0.%v.0", j)))
}
}
basicFixtures = append(basicFixtures, fix)
}
// reachMaps contain externalReach()-type data for a given depspec fixture's
// universe of proejcts, packages, and versions.
type reachMap map[pident]map[string][]string
type depspecSourceManager struct {
specs []depspec
rm reachMap
ig map[string]bool
}
type fixSM interface {
SourceManager
rootSpec() depspec
allSpecs() []depspec
ignore() map[string]bool
}
var _ fixSM = &depspecSourceManager{}
func newdepspecSM(ds []depspec, ignore []string) *depspecSourceManager {
ig := make(map[string]bool)
if len(ignore) > 0 {
for _, pkg := range ignore {
ig[pkg] = true
}
}
return &depspecSourceManager{
specs: ds,
rm: computeBasicReachMap(ds),
ig: ig,
}
}
func (sm *depspecSourceManager) GetProjectInfo(n ProjectName, v Version) (Manifest, Lock, error) {
for _, ds := range sm.specs {
if n == ds.n && v.Matches(ds.v) {
return ds, dummyLock{}, nil
}