Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 140 additions & 10 deletions buckaroo-tests/Solver.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open FSharp.Control

open Buckaroo.Console
open Buckaroo.Tasks
open Buckaroo.Tests

type CookBook = List<PackageIdentifier * Set<Version> * Manifest>
type LockBookEntries = List<(string*int) * List<string*int*Set<Version>>>
Expand Down Expand Up @@ -119,7 +120,7 @@ type TestingSourceExplorer (cookBook : CookBook, lockBook : LockBook) =
}


let solve (cookBook : CookBook) (lockBookEntries : LockBookEntries) root style =
let solve (partial : Solution) (cookBook : CookBook) (lockBookEntries : LockBookEntries) root style =
let lockBook = lockBookOf lockBookEntries
let console = new ConsoleManager(LoggingLevel.Silent);
let context : TaskContext = {
Expand All @@ -130,7 +131,8 @@ let solve (cookBook : CookBook) (lockBookEntries : LockBookEntries) root style =
}

Buckaroo.Solver.solve
context root style
context partial
root style
(lockBook |> Map.tryFind (packageLock ("root", 0)))

let getLockedRev (p : string) (r: Resolution) =
Expand Down Expand Up @@ -164,7 +166,7 @@ let ``Solver handles simple case`` () =

let root = manifest [("a", Exactly (br "a") )]
let solution =
solve
solve Solution.empty
cookBook [] root
ResolutionStyle.Quick
|> Async.RunSynchronously
Expand All @@ -189,7 +191,8 @@ let ``Solver can backtrack to resolve simple conflicts`` () =

let root = manifest [("a", Exactly (br "a") )]
let solution =
solve cookBook [] root ResolutionStyle.Quick
solve Solution.empty
cookBook [] root ResolutionStyle.Quick
|> Async.RunSynchronously

Assert.Equal ("1", getLockedRev "a" solution)
Expand All @@ -214,7 +217,9 @@ let ``Solver can compute version intersections`` () =
]

let solution =
solve spec [] root ResolutionStyle.Quick
solve
Solution.empty
spec [] root ResolutionStyle.Quick
|> Async.RunSynchronously

Assert.Equal ("1", getLockedRev "a" solution)
Expand All @@ -241,7 +246,8 @@ let ``Solver can compute intersection of branches`` () =
]

let solution =
solve spec [] root ResolutionStyle.Quick
solve Solution.empty
spec [] root ResolutionStyle.Quick
|> Async.RunSynchronously

Assert.Equal ("3", getLockedRev "a" solution)
Expand All @@ -268,7 +274,8 @@ let ``Solver fails if package cant satisfy all constraints`` () =
]

let solution =
solve spec [] root ResolutionStyle.Quick
solve Solution.empty
spec [] root ResolutionStyle.Quick
|> Async.RunSynchronously

Assert.False (isOk solution)
Expand Down Expand Up @@ -299,7 +306,8 @@ let ``Solver picks package that satisfies all constraints`` () =
]

let solution =
solve spec [] root ResolutionStyle.Quick
solve Solution.empty
spec [] root ResolutionStyle.Quick
|> Async.RunSynchronously

Assert.Equal ("3", getLockedRev "b" solution)
Expand Down Expand Up @@ -327,7 +335,8 @@ let ``Solver deduces that a package can satisfy multiple constraints`` () =
]

let solution =
solve spec [] root ResolutionStyle.Quick
solve Solution.empty
spec [] root ResolutionStyle.Quick
|> Async.RunSynchronously

Assert.Equal ("2", getLockedRev "b" solution)
Expand Down Expand Up @@ -361,7 +370,8 @@ let ``Solver handles negated constraints also`` () =
]

let solution =
solve spec [] root ResolutionStyle.Quick
solve Solution.empty
spec [] root ResolutionStyle.Quick
|> Async.RunSynchronously

Assert.Equal ("4", getLockedRev "b" solution)
Expand Down Expand Up @@ -396,6 +406,7 @@ let ``Solver uses lockfile as hint in Quick`` () =
let root = manifest [("a", Exactly (br "a") )]
let solution =
solve
Solution.empty
cookBook lockBook root
ResolutionStyle.Quick
|> Async.RunSynchronously
Expand Down Expand Up @@ -433,10 +444,129 @@ let ``Solver doesnt use lockfile as hint in Upgrade`` () =
let root = manifest [("a", Exactly (br "a") )]
let solution =
solve
Solution.empty
cookBook lockBook root
ResolutionStyle.Upgrading
|> Async.RunSynchronously

Assert.Equal ("2", getLockedRev "a" solution)
Assert.Equal ("2", getLockedRev "b" solution)
()

[<Fact>]
let ``Solver does not upgrade if a complete solution is supplied`` () =
let cookBook = [
(package "a",
Set[ver 2; br "a"],
manifest [])
(package "a",
Set[ver 1; br "a"],
manifest [])
(package "b",
Set[ver 2; br "a"],
manifest [])
(package "b",
Set[ver 1; br "a"],
manifest [])
(package "c",
Set[ver 2; br "a"],
manifest [])
(package "c",
Set[ver 1; br "a"],
manifest [])
]

let lockBookSpec = [
(("root", 0), [
("a", 1, Set[ver 1; br "a"])
("b", 1, Set[ver 1; br "a"])
("c", 1, Set[ver 1; br "a"])
])
]

let root = manifest [
("a", Exactly (br "a") )
("b", Exactly (br "a") )
("c", Exactly (br "a") )
]

let lockBook = lockBookOf lockBookSpec

let rootLock = lockBook |> Map.find (packageLock ("root", 0))
let explorer = TestingSourceExplorer(cookBook, lockBook)

let completeSolution =
Solver.fromLock explorer rootLock
|> Async.RunSynchronously


let solution =
solve
completeSolution
cookBook lockBookSpec root
ResolutionStyle.Upgrading
|> Async.RunSynchronously

Assert.Equal ("1", getLockedRev "a" solution)
Assert.Equal ("1", getLockedRev "b" solution)
Assert.Equal ("1", getLockedRev "c" solution)
()

[<Fact>]
let ``Solver upgrades completes partial solution with latest packages`` () =
let cookBook = [
(package "a",
Set[ver 2; br "a"],
manifest [])
(package "a",
Set[ver 1; br "a"],
manifest [])
(package "b",
Set[ver 2; br "a"],
manifest [])
(package "b",
Set[ver 1; br "a"],
manifest [])
(package "c",
Set[ver 2; br "a"],
manifest [])
(package "c",
Set[ver 1; br "a"],
manifest [])
]

let lockBookSpec = [
(("root", 0), [
("a", 1, Set[ver 1; br "a"])
("b", 1, Set[ver 1; br "a"])
("c", 1, Set[ver 1; br "a"])
])
]

let root = manifest [
("a", Exactly (br "a") )
("b", Exactly (br "a") )
("c", Exactly (br "a") )
]

let lockBook = lockBookOf lockBookSpec
let rootLock = lockBook |> Map.find (packageLock ("root", 0))

let explorer = TestingSourceExplorer(cookBook, lockBook)
let completeSolution =
Solver.fromLock explorer rootLock
|> Async.RunSynchronously

let partialSolution = Set[package "b"] |> Solver.unlock completeSolution

let solution =
solve
partialSolution
cookBook lockBookSpec root
ResolutionStyle.Upgrading
|> Async.RunSynchronously

Assert.Equal ("1", getLockedRev "a" solution)
Assert.Equal ("2", getLockedRev "b" solution)
Assert.Equal ("1", getLockedRev "c" solution)
()
32 changes: 16 additions & 16 deletions buckaroo/AddCommand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ open Buckaroo

let task (context : Tasks.TaskContext) dependencies = async {
context.Console.Write (
(text "Adding ") +
(text "Adding ") +
(
dependencies
|> Seq.map Dependency.showRich
dependencies
|> Seq.map Dependency.showRich
|> RichOutput.concat (text " ")
)
)

let! manifest = Tasks.readManifest "."
let newManifest = {
manifest with
Dependencies =
manifest.Dependencies
|> Seq.append dependencies
let newManifest = {
manifest with
Dependencies =
manifest.Dependencies
|> Seq.append dependencies
|> Set.ofSeq;
}

if manifest = newManifest
then
if manifest = newManifest
then
return ()
else
else
let! maybeLock = async {
if File.Exists(Constants.LockFileName)
then
Expand All @@ -37,15 +37,15 @@ let task (context : Tasks.TaskContext) dependencies = async {
return None
}

let! resolution = Solver.solve context newManifest ResolutionStyle.Quick maybeLock
let! resolution = Solver.solve context Solution.empty newManifest ResolutionStyle.Quick maybeLock

match resolution with
| Resolution.Ok solution ->
| Resolution.Ok solution ->
do! Tasks.writeManifest newManifest
do! Tasks.writeLock (Lock.fromManifestAndSolution newManifest solution)
do! InstallCommand.task context
| _ ->
| _ ->
()

context.Console.Write ("Success. " |> text |> foreground ConsoleColor.Green)
}
Loading