11module Buckaroo.Tests.Solver
22
33open Xunit
4- open Buckaroo
54open FSharp.Control
6-
5+ open Buckaroo
76open Buckaroo.Console
87open Buckaroo.Tasks
98open Buckaroo.Tests
109
1110type CookBook = List< PackageIdentifier * Set< Version> * Manifest>
12- type LockBookEntries = List<( string* int) * List< string* int* Set< Version>>>
11+ type LockBookEntries = List<( string * int) * List< string * int * Set< Version>>>
1312type LockBook = Map< PackageLock, Lock>
1413
1514let package name = PackageIdentifier.Adhoc {
@@ -18,38 +17,41 @@ let package name = PackageIdentifier.Adhoc {
1817}
1918
2019let br b = Version.Git ( GitVersion.Branch b)
21- let rev ( x : int ) = Version.Git( GitVersion.Revision ( x.ToString () ))
22- let ver ( x : int ) = Version.SemVer { SemVer.zero with Major = x}
20+ let rev ( x : int ) = Version.Git ( GitVersion.Revision ( string x ))
21+ let ver ( x : int ) = Version.SemVer { SemVer.zero with Major = x }
2322
24- let dep ( p : string , c : Constraint ) : Buckaroo.Dependency = {
25- Package = package p;
26- Constraint = c;
23+ let dep ( p : string , c : Constraint ) : Buckaroo.Dependency =
24+ {
25+ Package = package p
26+ Constraint = c
2727 Targets = None
28- }
28+ }
2929
30- let manifest xs = {
31- Manifest.zero
32- with Dependencies = xs |> List.map dep |> Set.ofList
33- }
30+ let manifest xs =
31+ {
32+ Manifest.zero
33+ with Dependencies = xs |> List.map dep |> Set.ofList
34+ }
3435
3536let lockPackage ( p , r , vs ) : LockedPackage = {
3637 Versions = Set vs
3738 Location = PackageLock.GitHub {
38- Package = { Owner = " test" ; Project = p};
39- Revision = r.ToString ()
39+ Package = { Owner = " test" ; Project = p }
40+ Revision = string r
4041 }
4142 PrivatePackages = Map.empty
4243}
4344
4445let packageLock ( p , r ) : PackageLock = PackageLock.GitHub {
45- Package = { Owner = " test" ; Project = p};
46- Revision = r.ToString ()
46+ Package = { Owner = " test" ; Project = p }
47+ Revision = string r
4748}
4849
4950let lock deps : Lock = {
5051 ManifestHash = " " ;
51- Dependencies = Set[]
52- Packages = deps
52+ Dependencies = Set []
53+ Packages =
54+ deps
5355 |> Seq.map ( fun ( name , r , vs ) -> ( package name, lockPackage ( name, r, vs)))
5456 |> Map.ofSeq
5557}
@@ -61,7 +63,7 @@ let lockBookOf (entries : LockBookEntries) : LockBook =
6163
6264type TestingSourceExplorer ( cookBook : CookBook , lockBook : LockBook ) =
6365 interface ISourceExplorer with
64- member this.FetchVersions ( _ : PackageSources ) ( package : PackageIdentifier ) : AsyncSeq < Version > = asyncSeq {
66+ member this.FetchVersions ( _ : PackageSources ) ( package : PackageIdentifier ) : AsyncSeq < Version > = asyncSeq {
6567 yield !
6668 cookBook
6769 |> Seq.choose ( fun ( p , v , _ ) ->
@@ -123,26 +125,33 @@ let solve (partial : Solution) (cookBook : CookBook) (lockBookEntries : LockBook
123125 let lockBook = lockBookOf lockBookEntries
124126 let console = ConsoleManager ( LoggingLevel.Silent)
125127 let context : TaskContext = {
128+ BuildSystem = BuildSystem.Bazel
126129 Console = console
127130 DownloadManager = DownloadManager( console, " /tmp" )
128131 GitManager = new GitManager( CacheFirst, console, new GitCli( console), " /tmp" )
129132 SourceExplorer = TestingSourceExplorer( cookBook, lockBook)
130133 }
131134
132135 Buckaroo.Solver.solve
133- context partial
134- root style
136+ context
137+ partial
138+ root
139+ style
135140 ( lockBook |> Map.tryFind ( packageLock ( " root" , 0 )))
136141
137142let getLockedRev ( p : string ) ( r : _ ) =
138143 match r with
139144 | Ok solution ->
145+ if not ( Map.containsKey ( package p) solution.Resolutions)
146+ then
147+ failwith ( p + " is not in the solution" )
148+
140149 let ( resolved , _ ) = solution.Resolutions.[ package p]
150+
141151 match resolved.Lock with
142152 | PackageLock.GitHub g -> g.Revision
143153 | _ -> " "
144154 | _ -> " "
145- ()
146155
147156let isOk ( r : _ ) =
148157 match r with
@@ -605,9 +614,53 @@ let ``Solver can handle the simple triangle case`` () =
605614 let solution =
606615 solve
607616 completeSolution
608- cookBook lockBookSpec root
617+ cookBook
618+ lockBookSpec
619+ root
620+ ResolutionStyle.Upgrading
621+ |> Async.RunSynchronously
622+
623+ Assert.Equal ( " 1" , getLockedRev " a" solution)
624+ Assert.Equal ( " 1" , getLockedRev " b" solution)
625+
626+
627+ [<Fact>]
628+ let ``Solver can follow overrides`` () =
629+ let cookBook = [
630+ ( package " x" ,
631+ Set [ ver 1 ],
632+ manifest [( " b" , Exactly ( ver 1 ))])
633+ ( package " y" ,
634+ Set [ ver 1 ],
635+ manifest [])
636+ ]
637+
638+ let lockBookSpec = [
639+ (( " root" , 0 ), [
640+ ( " x" , 1 , Set [ ver 1 ])
641+ ( " y" , 1 , Set [ ver 1 ])
642+ ])
643+ ]
644+
645+ let root = {
646+ manifest [
647+ ( " a" , Exactly ( ver 1 ) )
648+ ] with
649+ Overrides =
650+ Map.empty
651+ |> Map.add ( package " a" ) ( package " x" )
652+ |> Map.add ( package " b" ) ( package " y" )
653+ }
654+
655+ let solution =
656+ solve
657+ Solution.empty
658+ cookBook
659+ lockBookSpec
660+ root
609661 ResolutionStyle.Upgrading
610662 |> Async.RunSynchronously
611663
664+ // We should find a and b in solution, but they point to locations from x and y
612665 Assert.Equal ( " 1" , getLockedRev " a" solution)
613666 Assert.Equal ( " 1" , getLockedRev " b" solution)
0 commit comments