Skip to content

Commit

Permalink
Try to use FsCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Oct 14, 2015
1 parent 5d304a9 commit d8d8896
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 67 deletions.
1 change: 1 addition & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ group Test

nuget NUnit.Runners.Net4
nuget NUnit
nuget FSCheck
github forki/FsUnit FsUnit.fs
11 changes: 7 additions & 4 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ NUGET
GITHUB
remote: fsharp/FAKE
specs:
src/app/FakeLib/Globbing/Globbing.fs (80516864689fcf46ed484d6ba2fcd8a32cfd8ac5)
src/app/FakeLib/Globbing/Globbing.fs (d9b4f58aa76d4a0990fff2ac18840a3e527f48c9)
remote: fsprojects/Chessie
specs:
src/Chessie/ErrorHandling.fs (1f23b1caeb1f87e750abc96a25109376771dd090)
GROUP Build
NUGET
remote: https://nuget.org/api/v2
specs:
FAKE (4.4.4)
FAKE (4.6.2)
FSharp.Compiler.Service (0.0.90)
FSharp.Formatting (2.11.0)
FSharp.Compiler.Service (>= 0.0.90 <= 1.3)
FSharpVSPowerTools.Core (1.9.0)
FSharpVSPowerTools.Core (1.9.0)
FSharp.Compiler.Service (>= 0.0.90)
ILRepack (2.0.5)
ILRepack (2.0.8)
Microsoft.Bcl (1.1.10)
Microsoft.Bcl.Build (>= 1.0.14)
Microsoft.Bcl.Build (1.0.21) - import_targets: false
Expand All @@ -35,12 +35,15 @@ NUGET
GITHUB
remote: fsharp/FAKE
specs:
modules/Octokit/Octokit.fsx (80516864689fcf46ed484d6ba2fcd8a32cfd8ac5)
modules/Octokit/Octokit.fsx (d9b4f58aa76d4a0990fff2ac18840a3e527f48c9)
Octokit
GROUP Test
NUGET
remote: https://nuget.org/api/v2
specs:
FsCheck (2.1.0)
FSharp.Core (>= 3.1.2.5)
FSharp.Core (4.0.0.1)
NUnit (2.6.4)
NUnit.Runners.Net4 (2.6.4)
GITHUB
Expand Down
24 changes: 18 additions & 6 deletions src/Paket.Core/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ type PackageName =

member this.GetCompareString() =
match this with
| PackageName id -> id.ToLowerInvariant().Trim()
| PackageName id ->
match id with
| null -> ""
| _ -> id.ToLowerInvariant().Trim()

override this.ToString() =
match this with
| PackageName id -> id
| PackageName id ->
match id with
| null -> ""
| _ -> id

override this.Equals(that) =
match that with
| :? PackageName as that -> this.GetCompareString() = that.GetCompareString()
| _ -> false

override this.GetHashCode() = hash (this.GetCompareString())
override this.GetHashCode() = hash (this.GetCompareString())

interface System.IComparable with
member this.CompareTo that =
Expand All @@ -40,18 +46,24 @@ type GroupName =

member this.GetCompareString() =
match this with
| GroupName id -> id.ToLowerInvariant().Trim()
| GroupName id ->
match id with
| null -> ""
| _ -> id.ToLowerInvariant().Trim()

override this.ToString() =
match this with
| GroupName id -> id
| GroupName id ->
match id with
| null -> ""
| _ -> id

override this.Equals(that) =
match that with
| :? GroupName as that -> this.GetCompareString() = that.GetCompareString()
| _ -> false

override this.GetHashCode() = hash (this.GetCompareString())
override this.GetHashCode() = hash (this.GetCompareString())

interface System.IComparable with
member this.CompareTo that =
Expand Down
6 changes: 3 additions & 3 deletions src/Paket.Core/FrameworkHandling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ type FrameworkIdentifier =
override x.ToString() =
match x with
| DotNetFramework v -> "net" + v.ShortString()
| DNX v -> "dnx" + v.ShortString()
| DNXCore v -> "dnxcore" + v.ShortString()
| DNX v -> "dnx" + v.ShortString()
| DNXCore v -> "dnxcore" + v.ShortString()
| MonoAndroid -> "monoandroid"
| MonoTouch -> "monotouch"
| MonoMac -> "monomac"
Expand All @@ -98,7 +98,7 @@ type FrameworkIdentifier =
| Windows v -> "win" + v
| WindowsPhoneApp v -> "wp" + v
| WindowsPhoneSilverlight v -> "wp" + v
| Silverlight v -> "sl" + v.Replace("v","").Replace(".","")
| Silverlight v -> "sl" + if String.IsNullOrWhiteSpace v then "" else v.Replace("v","").Replace(".","")


// returns a list of compatible platforms that this platform also supports
Expand Down
64 changes: 50 additions & 14 deletions src/Paket.Core/LockFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ module LockFileSerializer =
| [] -> ()
| _ -> yield "FRAMEWORK: " + (String.Join(", ",options.Settings.FrameworkRestrictions)).ToUpper()
for (source, _), packages in sources do

if String.IsNullOrEmpty source then
failwith "Can't serialize empty source"

if not !hasReported then
yield "NUGET"
hasReported := true
Expand All @@ -66,6 +70,8 @@ module LockFileSerializer =
yield " specs:"
for _,_,package in packages |> Seq.sortBy (fun (_,_,p) -> p.Name) do
let (PackageName packageName) = package.Name
if String.IsNullOrEmpty packageName then
failwith "Can't serialize empty package name"

let versionStr =
let s = package.Version.ToString()
Expand All @@ -84,6 +90,9 @@ module LockFileSerializer =
yield sprintf " %s %s - %s" packageName versionStr s

for (PackageName name),v,restrictions in package.Dependencies do
if String.IsNullOrEmpty name then
failwith "Can't serialize empty package name"

let versionStr =
let s = v.ToString()
if s = "" then s else "(" + s + ")"
Expand All @@ -100,13 +109,21 @@ module LockFileSerializer =
let updateHasReported = new List<SingleSourceFileOrigin>()

[ for (owner,project,origin), files in files |> List.groupBy (fun f -> f.Owner, f.Project, f.Origin) do

match origin with
| GitHubLink ->
if not (updateHasReported.Contains(GitHubLink)) then
yield "GITHUB"
updateHasReported.Remove (HttpLink "") |> ignore
updateHasReported.Remove GistLink |> ignore
updateHasReported.Add GitHubLink

if String.IsNullOrEmpty owner || owner.Contains "/" || owner.Contains "\r" || owner.Contains "\n" then
failwithf "Can't serialize owner %s" owner

if String.IsNullOrEmpty project || project.Contains "/" || project.Contains "\r" || project.Contains "\n" then
failwithf "Can't serialize project %s" project

yield sprintf " remote: %s/%s" owner project
yield " specs:"
| GistLink ->
Expand All @@ -115,6 +132,13 @@ module LockFileSerializer =
updateHasReported.Remove GitHubLink |> ignore
updateHasReported.Remove (HttpLink "") |> ignore
updateHasReported.Add GistLink

if String.IsNullOrEmpty owner || owner.Contains "/" || owner.Contains "\r" || owner.Contains "\n" then
failwithf "Can't serialize owner %s" owner

if String.IsNullOrEmpty project || project.Contains "/" || project.Contains "\r" || project.Contains "\n" then
failwithf "Can't serialize project %s" project

yield sprintf " remote: %s/%s" owner project
yield " specs:"
| HttpLink url ->
Expand All @@ -123,12 +147,19 @@ module LockFileSerializer =
updateHasReported.Remove GitHubLink |> ignore
updateHasReported.Remove GistLink |> ignore
updateHasReported.Add (HttpLink "")
if String.IsNullOrEmpty url || System.Uri.IsWellFormedUriString(url,UriKind.RelativeOrAbsolute) |> not then
failwithf "Can't serialize url: %s" url

yield sprintf " remote: " + url
yield " specs:"

for file in files |> Seq.sortBy (fun f -> f.Owner.ToLower(),f.Project.ToLower(),f.Name.ToLower()) do

let path = file.Name.TrimStart '/'
let path =
match file.Name with
| null -> ""
| name -> name.TrimStart '/'

match String.IsNullOrEmpty(file.Commit) with
| false ->
match file.AuthKey with
Expand Down Expand Up @@ -208,7 +239,9 @@ module LockFileParser =
let Parse(lockFileLines) =
let remove textToRemove (source:string) = source.Replace(textToRemove, "")
let removeBrackets = remove "(" >> remove ")"
([{ GroupName = Constants.MainDependencyGroup; RepositoryType = None; RemoteUrl = None; Packages = []; SourceFiles = []; Options = InstallOptions.Default; LastWasPackage = false }], lockFileLines)
let startGroup = { GroupName = Constants.MainDependencyGroup; RepositoryType = None; RemoteUrl = None; Packages = []; SourceFiles = []; Options = InstallOptions.Default; LastWasPackage = false }

([startGroup], lockFileLines)
||> Seq.fold(fun state line ->
match state with
| [] -> failwithf "error"
Expand Down Expand Up @@ -236,7 +269,7 @@ module LockFileParser =
| Some remote ->
let parts = details.Split([|" - "|],StringSplitOptions.None)
let parts' = parts.[0].Split ' '
let version = parts'.[1] |> removeBrackets
let version = if parts'.Length < 2 then "" else parts'.[1] |> removeBrackets
let optionsString =
if parts.Length < 2 then "" else
if parts.[1] <> "" && parts.[1].Contains(":") |> not then
Expand All @@ -257,13 +290,13 @@ module LockFileParser =
| NugetDependency (name, v) ->
let parts = v.Split([|" - "|],StringSplitOptions.None)
let version = parts.[0]
if currentGroup.LastWasPackage then
if currentGroup.LastWasPackage then
match currentGroup.Packages with
| currentPackage :: otherPackages ->
{ currentGroup with
Packages = { currentPackage with
Dependencies = Set.add (PackageName name, DependenciesFileParser.parseVersionRequirement version, []) currentPackage.Dependencies
} :: otherPackages } ::otherGroups
} :: otherPackages } ::otherGroups
| [] -> failwithf "cannot set a dependency to %s %s - no package has been specified." name v
else
match currentGroup.SourceFiles with
Expand All @@ -272,19 +305,19 @@ module LockFileParser =
SourceFiles =
{ currentFile with
Dependencies = Set.add (PackageName name, VersionRequirement.AllReleases) currentFile.Dependencies
} :: rest } ::otherGroups
| [] -> failwith "cannot set a dependency to %s %s- no remote file has been specified." name v
} :: rest } ::otherGroups
| [] -> failwithf "cannot set a dependency to %s %s - no remote file has been specified." name v
| SourceFile(origin, details) ->
match origin with
| GitHubLink | GistLink ->
match currentGroup.RemoteUrl |> Option.map(fun s -> s.Split '/') with
| Some [| owner; project |] ->
let path, commit, authKey =
match details.Split ' ' with
| [| filePath; commit; authKey |] -> filePath, commit |> removeBrackets, (Some authKey)
| [| filePath; commit |] -> filePath, commit |> removeBrackets, None
| _ -> failwith "invalid file source details."
{ currentGroup with
| [| filePath; commit; authKey |] -> filePath, commit |> removeBrackets, (Some authKey)
| [| filePath; commit |] -> filePath, commit |> removeBrackets, None
| _ -> failwithf "invalid file source details: %s" details
{ currentGroup with
LastWasPackage = false
SourceFiles = { Commit = commit
Owner = owner
Expand Down Expand Up @@ -439,9 +472,12 @@ type LockFile(fileName:string,groups: Map<GroupName,LockFileGroup>) =
override __.ToString() =
String.Join
(Environment.NewLine,
[|let mainGroup = groups.[Constants.MainDependencyGroup]
yield LockFileSerializer.serializePackages mainGroup.Options mainGroup.Resolution
yield LockFileSerializer.serializeSourceFiles mainGroup.RemoteFiles
[|match groups |> Map.tryFind Constants.MainDependencyGroup with
| Some mainGroup ->
yield LockFileSerializer.serializePackages mainGroup.Options mainGroup.Resolution
yield LockFileSerializer.serializeSourceFiles mainGroup.RemoteFiles
| None -> ()

for g in groups do
if g.Key <> Constants.MainDependencyGroup then
yield "GROUP " + g.Value.Name.ToString()
Expand Down
4 changes: 2 additions & 2 deletions src/Paket.Core/Requirements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ type InstallSettings =
| Some x -> yield "version_in_path: " + x.ToString().ToLower()
| None -> ()
match this.ReferenceCondition with
| Some x -> yield "condition: " + x.ToUpper()
| None -> ()
| Some x when x <> null -> yield "condition: " + x.ToUpper()
| _ -> ()
match this.CreateBindingRedirects with
| Some true -> yield "redirects: on"
| Some false -> yield "redirects: off"
Expand Down
33 changes: 18 additions & 15 deletions src/Paket.Core/SemVer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ type PreRelease =
[<CustomEquality; CustomComparison; StructuredFormatDisplay("{AsString}")>]
type SemVerInfo =
{ /// MAJOR version when you make incompatible API changes.
Major : int
Major : uint32
/// MINOR version when you add functionality in a backwards-compatible manner.
Minor : int
Minor : uint32
/// PATCH version when you make backwards-compatible bug fixes.
Patch : int
Patch : uint32
/// The optional PreRelease version
PreRelease : PreRelease option
/// The optional build no.
Expand All @@ -103,7 +103,7 @@ type SemVerInfo =
let build =
if String.IsNullOrEmpty x.Build |> not && x.Build <> "0" then "." + x.Build
else ""

let pre =
match x.PreRelease with
| Some preRelease -> sprintf "-%s" preRelease.Origin
Expand All @@ -113,8 +113,8 @@ type SemVerInfo =

override x.ToString() =
match x.Original with
| Some version -> version.Trim()
| None -> x.Normalize()
| Some version when version <> null -> version.Trim()
| _ -> x.Normalize()

member x.AsString
with get() = x.ToString()
Expand Down Expand Up @@ -161,7 +161,7 @@ module SemVer =

/// sanity check to make sure that all of the integers in the string are positive.
/// because we use raw substrings with dashes this is very complex :(
version.Split([|'.'|]) |> Array.iter (fun s -> match Int32.TryParse s with | true, s when s < 0 -> failwith "no negatives!" | _ -> ignore ())
version.Split([|'.'|]) |> Array.iter (fun s -> match Int32.TryParse s with | true, s when s < 0 -> failwithf "no negative versions allowed: %s" version | _ -> ignore ())

if version.Contains("!") then
failwithf "Invalid character found in %s" version
Expand All @@ -171,7 +171,7 @@ module SemVer =
let firstDash = version.IndexOf("-")
let plusIndex = version.IndexOf("+")

let majorMinorPatch =
let majorMinorPatch =
let firstSigil = if firstDash > 0 then firstDash else plusIndex
match firstSigil with
| -1 -> version
Expand All @@ -181,7 +181,7 @@ module SemVer =
match firstDash, plusIndex with
| -1, _ -> ""
| d, p when p = -1 -> version.Substring(d+1)
| d, p -> version.Substring(d+1, (version.Length - 1 - p) )
| d, p -> version.Substring(d+1, (version.Length - 1 - p) )

/// there can only be one piece of build metadata, and it is signified by a + and then any number of dot-separated alphanumeric groups.
/// this just greedily takes the whole remaining string :(
Expand All @@ -192,12 +192,15 @@ module SemVer =
| n -> version.Substring(plusIndex + 1)

let major, minor, patch, build =
match majorMinorPatch.Split([|'.'|]) with
| [|M; m; p; b|] -> int M, int m, int p, b
| [|M; m; p; |] -> int M, int m, int p, "0"
| [|M; m;|] -> int M, int m, 0, "0"
| [|M;|] -> int M, 0, 0, "0"
| _ -> 0, 0, 0, "0"
try
match majorMinorPatch.Split([|'.'|]) with
| [|M; m; p; b|] -> uint32 M, uint32 m, uint32 p, b
| [|M; m; p; |] -> uint32 M, uint32 m, uint32 p, "0"
| [|M; m;|] -> uint32 M, uint32 m, 0u, "0"
| [|M;|] -> uint32 M, 0u, 0u, "0"
| _ -> 0u, 0u, 0u, "0"
with
| _ -> 0u, 0u, 0u, "0"

{ Major = major
Minor = minor
Expand Down
4 changes: 3 additions & 1 deletion src/Paket.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ module String =
Some (input.Substring(prefix.Length))
else None

let quoted(text:string) = (if text.Contains(" ") then "\"" + text + "\"" else text)
let inline quoted(text:string) =
if String.IsNullOrEmpty text then "" else
if text.Contains(" ") then "\"" + text + "\"" else text

// MonadPlus - "or else"
let inline (++) x y =
Expand Down
Loading

0 comments on commit d8d8896

Please sign in to comment.