forked from swiftlang/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPubgrubTests.swift
3984 lines (3437 loc) · 154 KB
/
PubgrubTests.swift
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import OrderedCollections
@testable import PackageGraph
import PackageLoading
@testable import PackageModel
import SourceControl
import _InternalTestSupport
import XCTest
import struct TSCUtility.Version
// There's some useful helper utilities defined below for easier testing:
//
// Terms conform to ExpressibleByStringLiteral in this test module and their
// version requirements can be created with a few options:
// - "[email protected]": equivalent to .exact("1.0.0")
// - "package^1.0.0": equivalent to .upToNextMajor("1.0.0")
// - "package-1.0.0-3.0.0": equivalent to .range("1.0.0"..<"3.0.0")
//
// Mocking a dependency graph is easily achieved by using the builder API. It's
// a global object in this module.
// builder.serve(dependencies: dependencies...)
// or for dependencies
// builder.serve("packageName", at: someVersion, with: dependencies...)
// Calling builder.create() returns a resolver which can then be used to start
// the resolution by calling .solve() on it and passing a reference to the root
// package.
//
// The functions (AssertBindings,) AssertResult, AssertRootCause & AssertError
// can be used for checking the success or error outcomes of the resolver
// without having to manually pull the bindings or errors out of
// the results. They also offer useful failure messages.
let builder = DependencyGraphBuilder()
private let emptyProvider = MockProvider(containers: [])
private let v1: Version = "1.0.0"
private let v1_1: Version = "1.1.0"
private let v1_5: Version = "1.5.0"
private let v2: Version = "2.0.0"
private let v3: Version = "3.0.0"
private let v1Range: VersionSetSpecifier = .range(v1 ..< v2)
private let v1_1Range: VersionSetSpecifier = .range(v1_1 ..< v2)
private let v1_5Range: VersionSetSpecifier = .range(v1_5 ..< v2)
private let v1to3Range: VersionSetSpecifier = .range(v1 ..< v3)
private let v2Range: VersionSetSpecifier = .range(v2 ..< v3)
let aRef = PackageReference.localSourceControl(identity: .plain("a"), path: .root)
let bRef = PackageReference.localSourceControl(identity: .plain("b"), path: .root)
let cRef = PackageReference.localSourceControl(identity: .plain("c"), path: .root)
let rootRef = PackageReference.root(identity: .plain("root"), path: .root)
let rootNode = DependencyResolutionNode.root(package: rootRef)
let rootCause = try! Incompatibility(Term(rootNode, .exact(v1)), root: rootNode)
let _cause = try! Incompatibility("[email protected]", root: rootNode)
final class PubgrubTests: XCTestCase {
func testTermInverse() {
let a = Term("[email protected]")
XCTAssertFalse(a.inverse.isPositive)
XCTAssertTrue(a.inverse.inverse.isPositive)
}
func testTermSatisfies() {
let a100 = Term("[email protected]")
XCTAssertTrue(a100.satisfies(a100))
XCTAssertFalse(a100.satisfies("¬[email protected]"))
XCTAssertTrue(a100.satisfies("a^1.0.0"))
XCTAssertFalse(a100.satisfies("¬a^1.0.0"))
XCTAssertFalse(a100.satisfies("a^2.0.0"))
XCTAssertFalse(Term("¬[email protected]").satisfies("¬a^1.0.0"))
XCTAssertFalse(Term("¬[email protected]").satisfies("a^2.0.0"))
XCTAssertTrue(Term("¬a^1.0.0").satisfies("¬[email protected]"))
XCTAssertTrue(Term("a^2.0.0").satisfies("¬[email protected]"))
XCTAssertTrue(Term("a^1.0.0").satisfies("¬[email protected]"))
XCTAssertTrue(Term("a^1.0.0").satisfies("¬a^2.0.0"))
XCTAssertTrue(Term("a^1.0.0").satisfies(Term("a^1.0.0")))
XCTAssertTrue(Term("a-1.0.0-1.1.0").satisfies(Term("a^1.0.0")))
XCTAssertFalse(Term("a-1.0.0-1.1.0").satisfies(Term("a^2.0.0")))
}
func _testTermIntersection() {
// a^1.0.0 ∩ ¬[email protected] → a >=1.0.0 <1.5.0
XCTAssertEqual(
Term("a^1.0.0").intersect(with: Term("¬[email protected]")),
Term("a-1.0.0-1.5.0")
)
// a^1.0.0 ∩ a >=1.5.0 <3.0.0 → a^1.5.0
XCTAssertEqual(
Term("a^1.0.0").intersect(with: Term("a-1.5.0-3.0.0")),
Term("a^1.5.0")
)
// ¬a^1.0.0 ∩ ¬a >=1.5.0 <3.0.0 → ¬a >=1.0.0 <3.0.0
XCTAssertEqual(
Term("¬a^1.0.0").intersect(with: Term("¬a-1.5.0-3.0.0")),
Term("¬a-1.0.0-3.0.0")
)
XCTAssertEqual(
Term("a^1.0.0").intersect(with: Term("a^1.0.0")),
Term("a^1.0.0")
)
XCTAssertEqual(
Term("¬a^1.0.0").intersect(with: Term("¬a^1.0.0")),
Term("¬a^1.0.0")
)
XCTAssertNil(Term("a^1.0.0").intersect(with: Term("¬a^1.0.0")))
XCTAssertNil(Term("[email protected]").difference(with: Term("[email protected]")))
XCTAssertEqual(
Term("¬a^1.0.0").intersect(with: Term("a^2.0.0")),
Term("a^2.0.0")
)
XCTAssertEqual(
Term("a^2.0.0").intersect(with: Term("¬a^1.0.0")),
Term("a^2.0.0")
)
XCTAssertEqual(
Term("¬a^1.0.0").intersect(with: Term("¬a^1.0.0")),
Term("¬a^1.0.0")
)
XCTAssertEqual(
Term("¬[email protected]").intersect(with: Term("¬[email protected]")),
Term("¬[email protected]")
)
// Check difference.
let anyA = Term(.empty(package: "a"), .any)
XCTAssertNil(Term("a^1.0.0").difference(with: anyA))
let notEmptyA = Term(not: .empty(package: "a"), .empty)
XCTAssertNil(Term("a^1.0.0").difference(with: notEmptyA))
}
func testTermRelation() {
// Both positive.
XCTAssertEqual(Term("a^1.1.0").relation(with: "a^1.0.0"), .subset)
XCTAssertEqual(Term("a^1.9.0").relation(with: "a^1.8.9"), .subset)
XCTAssertEqual(Term("a^1.5.0").relation(with: "a^1.0.0"), .subset)
XCTAssertEqual(Term("a^1.9.0").relation(with: "[email protected]"), .overlap)
XCTAssertEqual(Term("a^1.9.0").relation(with: "[email protected]"), .overlap)
XCTAssertEqual(Term("a^1.9.0").relation(with: "[email protected]"), .overlap)
XCTAssertEqual(Term("a^2.0.0").relation(with: "a^2.9.0"), .overlap)
XCTAssertEqual(Term("a^2.0.0").relation(with: "a^2.9.0"), .overlap)
XCTAssertEqual(Term("a-1.5.0-3.0.0").relation(with: "a^1.0.0"), .overlap)
XCTAssertEqual(Term("a^1.9.0").relation(with: "[email protected]"), .disjoint)
XCTAssertEqual(Term("a^1.9.0").relation(with: "[email protected]"), .disjoint)
XCTAssertEqual(Term("a^2.0.0").relation(with: "[email protected]"), .disjoint)
// First term is negative, second term is positive.
XCTAssertEqual(Term("¬a^1.0.0").relation(with: "[email protected]"), .disjoint)
XCTAssertEqual(Term("¬a^1.5.0").relation(with: "a^1.0.0"), .overlap)
XCTAssertEqual(Term("¬a^2.0.0").relation(with: "a^1.5.0"), .overlap)
// First term is positive, second term is negative.
XCTAssertEqual(Term("a^2.0.0").relation(with: "¬a^1.0.0"), .subset)
XCTAssertEqual(Term("a^1.5.0").relation(with: "¬a^1.0.0"), .disjoint)
XCTAssertEqual(Term("a^1.0.0").relation(with: "¬a^1.5.0"), .overlap)
XCTAssertEqual(Term("a-1.0.0-2.0.0").relation(with: "¬a-1.0.0-1.2.0"), .overlap)
// Both terms are negative.
XCTAssertEqual(Term("¬a^1.0.0").relation(with: "¬a^1.5.0"), .subset)
XCTAssertEqual(Term("¬a^2.0.0").relation(with: "¬a^1.0.0"), .overlap)
XCTAssertEqual(Term("¬a^1.5.0").relation(with: "¬a^1.0.0"), .overlap)
}
func testTermIsValidDecision() {
let solution100_150 = PartialSolution(assignments: [
.derivation("a^1.0.0", cause: _cause, decisionLevel: 1),
.derivation("a^1.5.0", cause: _cause, decisionLevel: 2),
])
let allSatisfied = Term("[email protected]")
XCTAssertTrue(allSatisfied.isValidDecision(for: solution100_150))
let partiallySatisfied = Term("[email protected]")
XCTAssertFalse(partiallySatisfied.isValidDecision(for: solution100_150))
}
func testIncompatibilityNormalizeTermsOnInit() throws {
let i1 = try Incompatibility(
Term("a^1.0.0"),
Term("a^1.5.0"),
Term("¬[email protected]"),
root: rootNode
)
XCTAssertEqual(i1.terms.count, 2)
let a1 = i1.terms.first { $0.node.package == "a" }
let b1 = i1.terms.first { $0.node.package == "b" }
XCTAssertEqual(a1?.requirement, v1_5Range)
XCTAssertEqual(b1?.requirement, .exact(v1))
let i2 = try Incompatibility(
Term("¬a^1.0.0"),
Term("a^2.0.0"),
root: rootNode
)
XCTAssertEqual(i2.terms.count, 1)
let a2 = i2.terms.first
XCTAssertEqual(a2?.requirement, v2Range)
}
func testSolutionPositive() {
let s1 = PartialSolution(assignments: [
.derivation("a^1.5.0", cause: _cause, decisionLevel: 0),
.derivation("[email protected]", cause: _cause, decisionLevel: 0),
.derivation("a^1.0.0", cause: _cause, decisionLevel: 0),
])
let a1 = s1._positive.first { $0.key.package.identity == PackageIdentity("a") }?.value
XCTAssertEqual(a1?.requirement, v1_5Range)
let b1 = s1._positive.first { $0.key.package.identity == PackageIdentity("b") }?.value
XCTAssertEqual(b1?.requirement, .exact(v2))
let s2 = PartialSolution(assignments: [
.derivation("¬a^1.5.0", cause: _cause, decisionLevel: 0),
.derivation("a^1.0.0", cause: _cause, decisionLevel: 0),
])
let a2 = s2._positive.first { $0.key.package.identity == PackageIdentity("a") }?.value
XCTAssertEqual(a2?.requirement, .range(v1 ..< v1_5))
}
func testSolutionUndecided() throws {
var solution = PartialSolution()
solution.derive("a^1.0.0", cause: rootCause)
solution.decide(.empty(package: "b"), at: v2)
solution.derive("a^1.5.0", cause: rootCause)
solution.derive("¬c^1.5.0", cause: rootCause)
solution.derive("d^1.9.0", cause: rootCause)
solution.derive("d^1.9.9", cause: rootCause)
let undecided = solution.undecided.sorted { $0.node.package.identity < $1.node.package.identity }
XCTAssertEqual(undecided, [Term("a^1.5.0"), Term("d^1.9.9")])
}
func testSolutionAddAssignments() throws {
let root = Term(rootNode, .exact("1.0.0"))
let a = Term("[email protected]")
let b = Term("[email protected]")
var solution = PartialSolution(assignments: [])
solution.decide(rootNode, at: v1)
solution.decide(.product("a", package: aRef), at: v1)
solution.derive(b, cause: _cause)
XCTAssertEqual(solution.decisionLevel, 1)
XCTAssertEqual(solution.assignments, [
.decision(root, decisionLevel: 0),
.decision(a, decisionLevel: 1),
.derivation(b, cause: _cause, decisionLevel: 1),
])
XCTAssertEqual(solution.decisions, [
rootNode: v1,
.product("a", package: aRef): v1,
])
}
func testSolutionBacktrack() {
// TODO: This should probably add derivations to cover that logic as well.
var solution = PartialSolution()
solution.decide(.empty(package: aRef), at: v1)
solution.decide(.empty(package: bRef), at: v1)
solution.decide(.empty(package: cRef), at: v1)
XCTAssertEqual(solution.decisionLevel, 2)
solution.backtrack(toDecisionLevel: 1)
XCTAssertEqual(solution.assignments.count, 2)
XCTAssertEqual(solution.decisionLevel, 1)
}
func testPositiveTerms() {
let s1 = PartialSolution(assignments: [
.derivation("a^1.0.0", cause: _cause, decisionLevel: 0),
])
XCTAssertEqual(
s1._positive[.product("a", package: "a")]?.requirement,
v1Range
)
let s2 = PartialSolution(assignments: [
.derivation("a^1.0.0", cause: _cause, decisionLevel: 0),
.derivation("a^1.5.0", cause: _cause, decisionLevel: 0),
])
XCTAssertEqual(
s2._positive[.product("a", package: "a")]?.requirement,
v1_5Range
)
}
func testResolverAddIncompatibility() throws {
let state = PubGrubDependencyResolver.State(root: rootNode)
let a = try Incompatibility(Term("[email protected]"), root: rootNode)
state.addIncompatibility(a, at: .topLevel)
let ab = try Incompatibility(Term("[email protected]"), Term("[email protected]"), root: rootNode)
state.addIncompatibility(ab, at: .topLevel)
XCTAssertEqual(state.incompatibilities, [
.product("a", package: "a"): [a, ab],
.product("b", package: "b"): [ab],
])
}
func testUpdatePackageIdentifierAfterResolution() throws {
let fooURL = SourceControlURL("https://example.com/foo")
let fooRef = PackageReference.remoteSourceControl(identity: PackageIdentity(url: fooURL), url: fooURL)
let foo = MockContainer(package: fooRef, dependenciesByVersion: [v1: [:]])
foo.manifestName = "bar"
let provider = MockProvider(containers: [foo])
let resolver = PubGrubDependencyResolver(provider: provider, observabilityScope: ObservabilitySystem.NOOP)
let deps = try builder.create(dependencies: [
"foo": (.versionSet(v1Range), .specific(["foo"])),
])
let result = resolver.solve(constraints: deps)
switch result {
case .failure(let error):
XCTFail("Unexpected error: \(error)")
case .success(let bindings):
XCTAssertEqual(bindings.count, 1)
let foo = bindings.first { $0.package.identity == .plain("foo") }
XCTAssertEqual(foo?.package.deprecatedName, "bar")
}
}
func testResolverConflictResolution() throws {
let solver1 = PubGrubDependencyResolver(provider: emptyProvider, observabilityScope: ObservabilitySystem.NOOP)
let state1 = PubGrubDependencyResolver.State(root: rootNode)
let notRoot = try Incompatibility(
Term(not: rootNode, .any),
root: rootNode,
cause: .root
)
state1.addIncompatibility(notRoot, at: .topLevel)
XCTAssertThrowsError(try solver1.resolve(state: state1, conflict: notRoot))
}
func testResolverDecisionMaking() throws {
let solver1 = PubGrubDependencyResolver(provider: emptyProvider, observabilityScope: ObservabilitySystem.NOOP)
let state1 = PubGrubDependencyResolver.State(root: rootNode)
// No decision can be made if no unsatisfied terms are available.
XCTAssertNil(try temp_await { solver1.makeDecision(state: state1, completion: $0) })
let a = MockContainer(package: aRef, dependenciesByVersion: [
"0.0.0": [:],
v1: ["a": [(package: bRef, requirement: v1Range, productFilter: .specific(["b"]))]],
])
let provider = MockProvider(containers: [a])
let solver2 = PubGrubDependencyResolver(provider: provider, observabilityScope: ObservabilitySystem.NOOP)
let solution = PartialSolution(assignments: [
.derivation("a^1.0.0", cause: rootCause, decisionLevel: 0),
])
let state2 = PubGrubDependencyResolver.State(root: rootNode, solution: solution)
XCTAssertEqual(state2.incompatibilities.count, 0)
let decision = try temp_await { solver2.makeDecision(state: state2, completion: $0) }
XCTAssertEqual(decision, .product("a", package: "a"))
XCTAssertEqual(state2.incompatibilities.count, 3)
XCTAssertEqual(state2.incompatibilities[.product("a", package: "a")], try [
Incompatibility(
Term(not: .product("b", package: "b"), v1Range),
root: rootNode,
cause: .dependency(node: .product("a", package: "a"))
),
Incompatibility(
Term(not: .empty(package: "a"), .exact("1.0.0")),
root: rootNode,
cause: .dependency(node: .product("a", package: "a"))
),
])
}
func testResolverUnitPropagation() throws {
let solver1 = PubGrubDependencyResolver(provider: emptyProvider, observabilityScope: ObservabilitySystem.NOOP)
let state1 = PubGrubDependencyResolver.State(root: rootNode)
// no known incompatibilities should result in no satisfaction checks
try solver1.propagate(state: state1, node: .root(package: "root"))
// even if incompatibilities are present
try state1.addIncompatibility(Incompatibility(Term("[email protected]"), root: rootNode), at: .topLevel)
try solver1.propagate(state: state1, node: .empty(package: "a"))
try solver1.propagate(state: state1, node: .empty(package: "a"))
try solver1.propagate(state: state1, node: .empty(package: "a"))
// adding a satisfying term should result in a conflict
state1.decide(.empty(package: aRef), at: v1)
// FIXME: This leads to fatal error.
// try solver1.propagate(aRef)
// Unit propagation should derive a new assignment from almost satisfied incompatibilities.
let solver2 = PubGrubDependencyResolver(provider: emptyProvider, observabilityScope: ObservabilitySystem.NOOP)
let state2 = PubGrubDependencyResolver.State(root: rootNode)
try state2.addIncompatibility(Incompatibility(
Term(.root(package: "root"), .any),
Term("¬[email protected]"),
root: rootNode
), at: .topLevel)
state2.decide(rootNode, at: v1)
XCTAssertEqual(state2.solution.assignments.count, 1)
try solver2.propagate(
state: state2,
node: .root(package: .root(identity: PackageIdentity("root"), path: .root))
)
XCTAssertEqual(state2.solution.assignments.count, 2)
}
func testSolutionFindSatisfiers() throws {
var solution = PartialSolution()
solution.decide(rootNode, at: v1) // ← previous, but actually nil because this is the root decision
solution.derive(Term(.product("a", package: aRef), .any), cause: _cause) // ← satisfier
solution.decide(.product("a", package: aRef), at: v2)
solution.derive("b^1.0.0", cause: _cause)
XCTAssertEqual(try solution.satisfier(for: Term("b^1.0.0")).term, "b^1.0.0")
XCTAssertEqual(try solution.satisfier(for: Term("¬a^1.0.0")).term, "[email protected]")
XCTAssertEqual(try solution.satisfier(for: Term("a^2.0.0")).term, "[email protected]")
}
// this test reconstruct the conditions described in radar/93335995
func testRadar93335995() throws {
let observability = ObservabilitySystem.makeForTesting()
let delegate = ObservabilityDependencyResolverDelegate(observabilityScope: observability.topScope)
let solver = PubGrubDependencyResolver(
provider: emptyProvider,
observabilityScope: observability.topScope,
delegate: delegate
)
let state = PubGrubDependencyResolver.State(root: rootNode)
state.decide(.root(package: aRef), at: "1.1.0")
do {
let cause = Incompatibility(
terms: .init([
Term(
node: .root(package: aRef),
requirement: .exact("1.1.0"),
isPositive: true
),
Term(
node: .root(package: bRef),
requirement: .range("1.1.0" ..< "2.0.0"),
isPositive: true
),
]),
cause: .noAvailableVersion
)
state.derive(
Term(node: .root(package: bRef), requirement: .range("1.1.0" ..< "2.0.0"), isPositive: true),
cause: cause
)
}
do {
let cause = Incompatibility(
terms: .init([
Term(
node: .root(package: aRef),
requirement: .exact("1.1.0"),
isPositive: true
),
Term(
node: .root(package: bRef),
requirement: .range("1.3.1" ..< "2.0.0"),
isPositive: true
),
]),
cause: .noAvailableVersion
)
state.derive(
Term(node: .root(package: bRef), requirement: .range("1.3.1" ..< "2.0.0"), isPositive: false),
cause: cause
)
// order here matters to reproduce the issue
state.derive(
Term(node: .root(package: bRef), requirement: .exact("1.3.0"), isPositive: false),
cause: cause
)
state.derive(
Term(node: .root(package: bRef), requirement: .exact("1.3.1"), isPositive: false),
cause: cause
)
state.derive(
Term(node: .root(package: bRef), requirement: .exact("1.2.0"), isPositive: false),
cause: cause
)
state.derive(
Term(node: .root(package: bRef), requirement: .exact("1.1.0"), isPositive: false),
cause: cause
)
}
let conflict = Incompatibility(
terms: .init([
Term(
node: .root(package: aRef),
requirement: .exact("1.1.0"),
isPositive: true
),
Term(
node: .root(package: bRef),
requirement: .ranges(["1.1.1" ..< "1.2.0", "1.2.1" ..< "1.3.0"]),
isPositive: true
),
]),
cause: .noAvailableVersion
)
_ = try solver.resolve(state: state, conflict: conflict)
XCTAssertNoDiagnostics(observability.diagnostics)
}
func testNoInfiniteLoop() throws {
let observability = ObservabilitySystem.makeForTesting()
let delegate = ObservabilityDependencyResolverDelegate(observabilityScope: observability.topScope)
let solver = PubGrubDependencyResolver(
provider: emptyProvider,
observabilityScope: observability.topScope,
delegate: delegate
)
let state = PubGrubDependencyResolver.State(root: rootNode)
do {
let cause = Incompatibility(
terms: .init([
Term(
node: .root(package: aRef),
requirement: .exact("1.1.0"),
isPositive: true
),
Term(
node: .root(package: bRef),
requirement: .range("1.1.0" ..< "2.0.0"),
isPositive: true
),
]),
cause: .noAvailableVersion
)
state.derive(
Term(node: .root(package: aRef), requirement: .exact("1.1.0"), isPositive: true),
cause: cause
) // no decision available on this which will throw it into an infinite loop
state.derive(
Term(node: .root(package: bRef), requirement: .range("1.1.0" ..< "2.0.0"), isPositive: true),
cause: cause
)
}
do {
let cause = Incompatibility(
terms: .init([
Term(
node: .root(package: aRef),
requirement: .exact("1.1.0"),
isPositive: true
),
Term(
node: .root(package: bRef),
requirement: .range("1.2.0" ..< "2.0.0"),
isPositive: true
),
]),
cause: .noAvailableVersion
)
state.derive(
Term(node: .root(package: bRef), requirement: .range("1.2.0" ..< "2.0.0"), isPositive: false),
cause: cause
)
state.derive(
Term(node: .root(package: bRef), requirement: .exact("1.2.0"), isPositive: false),
cause: cause
)
state.derive(
Term(node: .root(package: bRef), requirement: .exact("1.1.0"), isPositive: false),
cause: cause
)
}
let conflict = Incompatibility(
terms: .init([
Term(
node: .root(package: aRef),
requirement: .exact("1.1.0"),
isPositive: true
),
Term(
node: .root(package: bRef),
requirement: .ranges(["1.1.1" ..< "1.2.0"]),
isPositive: true
),
]),
cause: .noAvailableVersion
)
XCTAssertThrowsError(try solver.resolve(state: state, conflict: conflict)) { error in
XCTAssertTrue(error is PubGrubDependencyResolver.PubgrubError)
}
}
func testResolutionNoConflicts() throws {
try builder.serve("a", at: v1, with: ["a": ["b": (.versionSet(v1Range), .specific(["b"]))]])
try builder.serve("b", at: v1)
try builder.serve("b", at: v2)
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"a": (.versionSet(v1Range), .specific(["a"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("a", .version(v1)),
("b", .version(v1)),
])
}
func testResolutionAvoidingConflictResolutionDuringDecisionMaking() throws {
try builder.serve("a", at: v1)
try builder.serve("a", at: v1_1, with: ["a": ["b": (.versionSet(v2Range), .specific(["b"]))]])
try builder.serve("b", at: v1)
try builder.serve("b", at: v1_1)
try builder.serve("b", at: v2)
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"a": (.versionSet(v1Range), .specific(["a"])),
"b": (.versionSet(v1Range), .specific(["b"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("a", .version(v1)),
("b", .version("1.1.0")),
])
}
func testResolutionPerformingConflictResolution() throws {
// Pubgrub has a listed as >=1.0.0, which we can't really represent here.
// It's either .any or 1.0.0..<n.0.0 with n>2. Both should have the same
// effect though.
try builder.serve("a", at: v1)
try builder.serve("a", at: v2, with: ["a": ["b": (.versionSet(v1Range), .specific(["b"]))]])
try builder.serve("b", at: v1, with: ["b": ["a": (.versionSet(v1Range), .specific(["a"]))]])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"a": (.versionSet(v1to3Range), .specific(["a"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("a", .version(v1)),
])
}
func testResolutionConflictResolutionWithAPartialSatisfier() throws {
try builder.serve("foo", at: v1)
try builder.serve("foo", at: v1_1, with: [
"foo": ["left": (.versionSet(v1Range), .specific(["left"]))],
"foo": ["right": (.versionSet(v1Range), .specific(["right"]))],
])
try builder.serve("left", at: v1, with: ["left": ["shared": (.versionSet(v1Range), .specific(["shared"]))]])
try builder.serve("right", at: v1, with: ["right": ["shared": (.versionSet(v1Range), .specific(["shared"]))]])
try builder.serve("shared", at: v1, with: ["shared": ["target": (.versionSet(v1Range), .specific(["target"]))]])
try builder.serve("shared", at: v2)
try builder.serve("target", at: v1)
try builder.serve("target", at: v2)
// foo 1.1.0 transitively depends on a version of target that's not compatible
// with root's constraint. This dependency only exists because of left
// *and* right, choosing only one of these would be fine.
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.versionSet(v1Range), .specific(["foo"])),
"target": (.versionSet(v2Range), .specific(["target"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .version(v1)),
("target", .version(v2)),
])
}
func testUsecase1() throws {
try builder.serve(
"a",
at: "1.0.0",
with: [
"a": [
"b": (.versionSet(.range("1.1.0" ..< "2.0.0")), .everything),
"c": (.versionSet(.range("1.2.1" ..< "2.0.0")), .everything),
],
]
)
try builder.serve(
"b",
at: "1.0.0",
with: [
"b": [
"c": (.versionSet(.range("1.3.1" ..< "2.0.0")), .everything),
],
]
)
try builder.serve(
"b",
at: "1.1.0",
with: [
"b": [
"c": (.versionSet(.range("1.4.1" ..< "2.0.0")), .everything),
],
]
)
try builder.serve(
"c",
at: ["1.0.0", "1.1.0", "1.2.0", "1.2.1", "1.3.0", "1.3.1", "1.4.0", "1.4.1", "2.0.0", "2.1.0"],
with: [
"c": ["d": (.versionSet(v1Range), .everything)],
]
)
try builder.serve("d", at: ["1.0.0", "1.1.0", "1.1.1", "1.1.2", "1.2.0", "1.2.1"])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"a": (.versionSet(v1Range), .everything),
"c": (.versionSet(v1Range), .everything),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("a", .version(v1)),
("b", .version("1.1.0")),
("c", .version("1.4.1")),
("d", .version("1.2.1")),
])
}
func testCycle1() throws {
try builder.serve("foo", at: v1_1, with: ["foo": ["foo": (.versionSet(v1Range), .specific(["foo"]))]])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.versionSet(v1Range), .specific(["foo"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .version(v1_1)),
])
}
func testCycle2() throws {
try builder.serve("foo", at: v1_1, with: ["foo": ["bar": (.versionSet(v1Range), .specific(["bar"]))]])
try builder.serve("bar", at: v1, with: ["bar": ["baz": (.versionSet(v1Range), .specific(["baz"]))]])
try builder.serve("baz", at: v1, with: ["baz": ["bam": (.versionSet(v1Range), .specific(["bam"]))]])
try builder.serve("bam", at: v1, with: ["bam": ["baz": (.versionSet(v1Range), .specific(["baz"]))]])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.versionSet(v1Range), .specific(["foo"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .version(v1_1)),
("bar", .version(v1)),
("baz", .version(v1)),
("bam", .version(v1)),
])
}
func testLocalPackageCycle() throws {
try builder.serve("foo", at: .unversioned, with: [
"foo": ["bar": (.unversioned, .specific(["bar"]))],
])
try builder.serve("bar", at: .unversioned, with: [
"bar": ["baz": (.unversioned, .specific(["baz"]))],
])
try builder.serve("baz", at: .unversioned, with: [
"baz": ["foo": (.unversioned, .specific(["foo"]))],
])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.unversioned, .specific(["foo"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .unversioned),
("bar", .unversioned),
("baz", .unversioned),
])
}
func testBranchBasedPackageCycle() throws {
try builder.serve("foo", at: .revision("develop"), with: [
"foo": ["bar": (.revision("develop"), .specific(["bar"]))],
])
try builder.serve("bar", at: .revision("develop"), with: [
"bar": ["baz": (.revision("develop"), .specific(["baz"]))],
])
try builder.serve("baz", at: .revision("develop"), with: [
"baz": ["foo": (.revision("develop"), .specific(["foo"]))],
])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.revision("develop"), .specific(["foo"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .revision("develop")),
("bar", .revision("develop")),
("baz", .revision("develop")),
])
}
func testNonExistentPackage() throws {
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"package": (.versionSet(.exact(v1)), .specific(["package"])),
])
let result = resolver.solve(constraints: dependencies)
AssertError(result, _MockLoadingError.unknownModule)
}
func testUnversioned1() throws {
try builder.serve("foo", at: .unversioned)
try builder.serve("bar", at: v1_5)
try builder.serve("bar", at: v2)
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.unversioned, .specific(["foo"])),
"bar": (.versionSet(v1Range), .specific(["bar"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .unversioned),
("bar", .version(v1_5)),
])
}
func testUnversioned2() throws {
try builder.serve("foo", at: .unversioned, with: [
"foo": ["bar": (.versionSet(.range(v1 ..< "1.2.0")), .specific(["bar"]))],
])
try builder.serve("bar", at: v1)
try builder.serve("bar", at: v1_1)
try builder.serve("bar", at: v1_5)
try builder.serve("bar", at: v2)
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.unversioned, .specific(["foo"])),
"bar": (.versionSet(v1Range), .specific(["bar"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .unversioned),
("bar", .version(v1_1)),
])
}
func testUnversioned3() throws {
try builder.serve("foo", at: .unversioned)
try builder.serve("bar", at: v1, with: [
"bar": ["foo": (.versionSet(.exact(v1)), .specific(["foo"]))],
])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.unversioned, .specific(["foo"])),
"bar": (.versionSet(v1Range), .specific(["bar"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .unversioned),
("bar", .version(v1)),
])
}
func testUnversioned4() throws {
try builder.serve("foo", at: .unversioned)
try builder.serve("bar", at: .revision("master"), with: [
"bar": ["foo": (.versionSet(v1Range), .specific(["foo"]))],
])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.unversioned, .specific(["foo"])),
"bar": (.revision("master"), .specific(["bar"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .unversioned),
("bar", .revision("master")),
])
}
func testUnversioned5() throws {
try builder.serve("foo", at: .unversioned)
try builder.serve("foo", at: .revision("master"))
try builder.serve("bar", at: .revision("master"), with: [
"bar": ["foo": (.revision("master"), .specific(["foo"]))],
])
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"foo": (.unversioned, .specific(["foo"])),
"bar": (.revision("master"), .specific(["bar"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("foo", .unversioned),
("bar", .revision("master")),
])
}
func testUnversioned7() throws {
try builder.serve("local", at: .unversioned, with: [
"local": ["remote": (.unversioned, .specific(["remote"]))],
])
try builder.serve("remote", at: .unversioned)
try builder.serve("remote", at: v1)
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"local": (.unversioned, .specific(["local"])),
"remote": (.versionSet(v1Range), .specific(["remote"])),
])
let result = resolver.solve(constraints: dependencies)
AssertResult(result, [
("remote", .unversioned),
("local", .unversioned),
])
}
func testUnversioned8() throws {
try builder.serve("entry", at: .unversioned, with: [
"entry": [
"remote": (.versionSet(v1Range), .specific(["remote"])),
"local": (.unversioned, .specific(["local"])),
],
])
try builder.serve("local", at: .unversioned, with: [
"local": ["remote": (.unversioned, .specific(["remote"]))],
])
try builder.serve("remote", at: .unversioned)
try builder.serve("remote", at: v1)
let resolver = builder.create()
let dependencies = try builder.create(dependencies: [
"entry": (.unversioned, .specific(["entry"])),