|
| 1 | +module Paket.IntegrationTests.ConditionSpecs |
| 2 | + |
| 3 | +open System.Text.RegularExpressions |
| 4 | +open System.Xml.Linq |
| 5 | +open System.Xml.XPath |
| 6 | +open Fake |
| 7 | +open System |
| 8 | +open NUnit.Framework |
| 9 | +open FsUnit |
| 10 | +open System |
| 11 | +open System.IO |
| 12 | +open System.Diagnostics |
| 13 | +open Paket |
| 14 | +open Paket.Domain |
| 15 | + |
| 16 | +let preparePackages workingDir = |
| 17 | + let packagesDir = workingDir @@ "Packages" |
| 18 | + |
| 19 | + [1..5] |
| 20 | + |> List.filter (fun v -> fileExists (workingDir @@ "Packages" @@ $"PaketTest2394.PackageA.%d{v}.0.0.nupkg") |> not) |
| 21 | + |> List.map (fun v -> directDotnet false $"pack -o . -p:Version=%d{v}.0" packagesDir) |
| 22 | + |> ignore |
| 23 | + |
| 24 | +let prepareBuildProps projectDir = |
| 25 | + let propsFile = (projectDir @@ "Directory.Build.props") |
| 26 | + let xmlDoc = XDocument.Load propsFile |
| 27 | + let propertyGroup = xmlDoc.Descendants("PropertyGroup") |
| 28 | + |> Seq.head |
| 29 | + propertyGroup.Add(XElement("PaketExePath", paketToolPath |> snd)) |
| 30 | + xmlDoc.Save propsFile |
| 31 | + |
| 32 | +let dotnetRun projectDir project configuration = |
| 33 | + match configuration with |
| 34 | + | Some config -> directDotnet false $"run --project %s{project} --configuration %s{config}" projectDir |
| 35 | + | None -> directDotnet false $"run --project %s{project}" projectDir |
| 36 | + |> Seq.map (_.Message) |
| 37 | + |
| 38 | +let dotnetPack projectDir project configuration = |
| 39 | + match configuration with |
| 40 | + | Some config -> directDotnet false $"pack %s{project} --configuration %s{config} -o ." projectDir |
| 41 | + | None -> directDotnet false $"pack %s{project} -o ." projectDir |
| 42 | + |> ignore |
| 43 | + |
| 44 | +let private shouldIncludeVersionedString (pattern: string) (version: int) (inputs: string seq) = |
| 45 | + let expected = pattern.Replace("XX", version.ToString()) |
| 46 | + let regex = $"""^%s{Regex.Escape(pattern).Replace("XX", "(\d)")}$""" |
| 47 | + |
| 48 | + inputs |
| 49 | + |> Seq.filter (fun input -> Regex.IsMatch(input, regex)) |
| 50 | + |> Seq.iter (fun input -> Assert.That(input, Is.EqualTo expected)) |
| 51 | + |
| 52 | +let private shouldIncludePackageA v i = shouldIncludeVersionedString "PackageA XX.0" v i |
| 53 | +let private shouldIncludePackageB v i = shouldIncludeVersionedString "PackageB XX.0 (references PackageB.Transient XX.0)" v i |
| 54 | +let private shouldIncludePackageBTransient v i = shouldIncludeVersionedString "PackageB.Transient XX.0" v i |
| 55 | +let private shouldIncludeConstant v i = shouldIncludeVersionedString "Constant PACKAGEA_XX set" v i |
| 56 | + |
| 57 | +[<Test>] |
| 58 | +let ``#2394 default group with no condition`` () = |
| 59 | + let scenario = "i002394-group-conditions" |
| 60 | + preparePackages (originalScenarioPath scenario) |
| 61 | + |
| 62 | + use __ = prepare scenario |
| 63 | + let root = scenarioTempPath scenario |
| 64 | + let projectDir = root @@ "TestProjects" |
| 65 | + prepareBuildProps projectDir |
| 66 | + |
| 67 | + directPaketInPath "install" projectDir |> ignore |
| 68 | + let output = dotnetRun projectDir "MainGroup.fsproj" None |
| 69 | + dotnetPack projectDir "MainGroup.fsproj" None |
| 70 | + |
| 71 | + output |> shouldIncludePackageA 1 |
| 72 | + output |> shouldIncludePackageB 1 |
| 73 | + output |> shouldIncludePackageBTransient 1 |
| 74 | + output |> shouldIncludeConstant 1 |
| 75 | + |
| 76 | + let nupkgPath = projectDir @@ "MainGroup.1.0.0.nupkg" |
| 77 | + |
| 78 | + if File.Exists nupkgPath |> not then Assert.Fail $"Expected '%s{nupkgPath}' to exist" |
| 79 | + let nuspec = NuGetCache.getNuSpecFromNupkg nupkgPath |
| 80 | + let dependencies = nuspec.Dependencies.Value |
| 81 | + |> List.map (fun (n, v, _) -> n.Name, v.Range.ToString()) |
| 82 | + |
| 83 | + let expected = [("PaketTest2394.PackageA", ">= 1.0 < 2.0"); ("PaketTest2394.PackageB", ">= 1.0 < 2.0")] |
| 84 | + Assert.That(dependencies, Is.SupersetOf expected) |
| 85 | + |
| 86 | +[<Test>] |
| 87 | +let ``#2394 alternate group with no condition`` () = |
| 88 | + let scenario = "i002394-group-conditions" |
| 89 | + preparePackages (originalScenarioPath scenario) |
| 90 | + |
| 91 | + use __ = prepare scenario |
| 92 | + let root = scenarioTempPath scenario |
| 93 | + let projectDir = root @@ "TestProjects" |
| 94 | + prepareBuildProps projectDir |
| 95 | + |
| 96 | + directPaketInPath "install" projectDir |> ignore |
| 97 | + let output = dotnetRun projectDir "NonConditionalGroup.fsproj" None |
| 98 | + dotnetPack projectDir "NonConditionalGroup.fsproj" None |
| 99 | + |
| 100 | + output |> shouldIncludePackageA 2 |
| 101 | + output |> shouldIncludePackageB 2 |
| 102 | + output |> shouldIncludePackageBTransient 2 |
| 103 | + output |> shouldIncludeConstant 2 |
| 104 | + |
| 105 | + let nupkgPath = projectDir @@ "NonConditionalGroup.1.0.0.nupkg" |
| 106 | + |
| 107 | + if File.Exists nupkgPath |> not then Assert.Fail $"Expected '%s{nupkgPath}' to exist" |
| 108 | + let nuspec = NuGetCache.getNuSpecFromNupkg nupkgPath |
| 109 | + let dependencies = nuspec.Dependencies.Value |
| 110 | + |> List.map (fun (n, v, _) -> n.Name, v.Range.ToString()) |
| 111 | + |
| 112 | + let expected = [("PaketTest2394.PackageA", ">= 2.0 < 3.0"); ("PaketTest2394.PackageB", ">= 2.0 < 3.0")] |
| 113 | + Assert.That(dependencies, Is.SupersetOf expected) |
| 114 | + |
| 115 | +[<Test>] |
| 116 | +let ``#2394 group with fixed property condition`` () = |
| 117 | + let scenario = "i002394-group-conditions" |
| 118 | + preparePackages (originalScenarioPath scenario) |
| 119 | + |
| 120 | + use __ = prepare scenario |
| 121 | + let root = scenarioTempPath scenario |
| 122 | + let projectDir = root @@ "TestProjects" |
| 123 | + prepareBuildProps projectDir |
| 124 | + |
| 125 | + directPaketInPath "install" projectDir |> ignore |
| 126 | + let output = dotnetRun projectDir "FixedProperty.fsproj" None |
| 127 | + dotnetPack projectDir "FixedProperty.fsproj" None |
| 128 | + |
| 129 | + output |> shouldIncludePackageA 3 |
| 130 | + output |> shouldIncludePackageB 3 |
| 131 | + output |> shouldIncludePackageBTransient 3 |
| 132 | + output |> shouldIncludeConstant 3 |
| 133 | + |
| 134 | + let nupkgPath = projectDir @@ "FixedProperty.1.0.0.nupkg" |
| 135 | + |
| 136 | + if File.Exists nupkgPath |> not then Assert.Fail $"Expected '%s{nupkgPath}' to exist" |
| 137 | + let nuspec = NuGetCache.getNuSpecFromNupkg nupkgPath |
| 138 | + let dependencies = nuspec.Dependencies.Value |
| 139 | + |> List.map (fun (n, v, _) -> n.Name, v.Range.ToString()) |
| 140 | + |
| 141 | + let expected = [("PaketTest2394.PackageA", ">= 3.0 < 4.0"); ("PaketTest2394.PackageB", ">= 3.0 < 4.0")] |
| 142 | + Assert.That(dependencies, Is.SupersetOf expected) |
| 143 | + |
| 144 | +[<Test>] |
| 145 | +let ``#2394 mix dependencies from multiple groups with conditions`` () = |
| 146 | + let scenario = "i002394-group-conditions" |
| 147 | + preparePackages (originalScenarioPath scenario) |
| 148 | + |
| 149 | + use __ = prepare scenario |
| 150 | + let root = scenarioTempPath scenario |
| 151 | + let projectDir = root @@ "TestProjects" |
| 152 | + prepareBuildProps projectDir |
| 153 | + |
| 154 | + directPaketInPath "install" projectDir |> ignore |
| 155 | + let output = dotnetRun projectDir "MixedProperties.fsproj" None |
| 156 | + dotnetPack projectDir "MixedProperties.fsproj" None |
| 157 | + |
| 158 | + output |> shouldIncludePackageA 4 |
| 159 | + output |> shouldIncludePackageB 5 |
| 160 | + output |> shouldIncludePackageBTransient 5 |
| 161 | + output |> shouldIncludeConstant 4 |
| 162 | + |
| 163 | + let expected = ["PackageA 4.0"; "PackageB 5.0 (references PackageB.Transient 5.0)"; "PackageB.Transient 5.0"; "Constant PACKAGEA_4 set"] |
| 164 | + let rejected = ["PackageA 1.0"; "PackageB.Transient 1.0"; "Constant PACKAGEA_1 set" |
| 165 | + "PackageA 2.0"; "PackageB.Transient 2.0"; "Constant PACKAGEA_2 set" |
| 166 | + "PackageA 3.0"; "PackageB.Transient 3.0"; "Constant PACKAGEA_3 set" |
| 167 | + "PackageA 5.0"; "PackageB.Transient 4.0"; "Constant PACKAGEA_5 set"] |
| 168 | + Assert.That(output, Is.SupersetOf expected) |
| 169 | + Assert.That(output, Is.Not.SubsetOf rejected) |
| 170 | + |
| 171 | + let nupkgPath = projectDir @@ "MixedProperties.1.0.0.nupkg" |
| 172 | + |
| 173 | + if File.Exists nupkgPath |> not then Assert.Fail $"Expected '%s{nupkgPath}' to exist" |
| 174 | + let nuspec = NuGetCache.getNuSpecFromNupkg nupkgPath |
| 175 | + let dependencies = nuspec.Dependencies.Value |
| 176 | + |> List.map (fun (n, v, _) -> n.Name, v.Range.ToString()) |
| 177 | + |
| 178 | + let expected = [("PaketTest2394.PackageA", ">= 4.0 < 5.0"); ("PaketTest2394.PackageB", ">= 5.0 < 6.0")] |
| 179 | + Assert.That(dependencies, Is.SupersetOf expected) |
| 180 | + |
| 181 | +[<Test>] |
| 182 | +let ``#2394 project with dynamic condition based on configuration 1`` () = |
| 183 | + let scenario = "i002394-group-conditions" |
| 184 | + preparePackages (originalScenarioPath scenario) |
| 185 | + |
| 186 | + use __ = prepare scenario |
| 187 | + let root = scenarioTempPath scenario |
| 188 | + let projectDir = root @@ "TestProjects" |
| 189 | + prepareBuildProps projectDir |
| 190 | + |
| 191 | + directPaketInPath "install" projectDir |> ignore |
| 192 | + let output = dotnetRun projectDir "ConfigurationDependent.fsproj" None |
| 193 | + dotnetPack projectDir "ConfigurationDependent.fsproj" None |
| 194 | + |
| 195 | + output |> shouldIncludePackageA 3 |
| 196 | + output |> shouldIncludePackageB 3 |
| 197 | + output |> shouldIncludePackageBTransient 3 |
| 198 | + output |> shouldIncludeConstant 3 |
| 199 | + |
| 200 | + let nupkgPath = projectDir @@ "ConfigurationDependent.1.0.0.nupkg" |
| 201 | + |
| 202 | + if File.Exists nupkgPath |> not then Assert.Fail $"Expected '%s{nupkgPath}' to exist" |
| 203 | + let nuspec = NuGetCache.getNuSpecFromNupkg nupkgPath |
| 204 | + let dependencies = nuspec.Dependencies.Value |
| 205 | + |> List.map (fun (n, v, _) -> n.Name, v.Range.ToString()) |
| 206 | + |
| 207 | + let expected = [("PaketTest2394.PackageA", ">= 3.0 < 4.0"); ("PaketTest2394.PackageB", ">= 3.0 < 4.0")] |
| 208 | + Assert.That(dependencies, Is.SupersetOf expected) |
| 209 | + |
| 210 | +[<Test>] |
| 211 | +let ``#2394 project with dynamic condition based on configuration 2`` () = |
| 212 | + let scenario = "i002394-group-conditions" |
| 213 | + preparePackages (originalScenarioPath scenario) |
| 214 | + |
| 215 | + use __ = prepare scenario |
| 216 | + let root = scenarioTempPath scenario |
| 217 | + let projectDir = root @@ "TestProjects" |
| 218 | + prepareBuildProps projectDir |
| 219 | + |
| 220 | + directPaketInPath "install" projectDir |> ignore |
| 221 | + let output = dotnetRun projectDir "ConfigurationDependent.fsproj" (Some "Alternate") |
| 222 | + dotnetPack projectDir "ConfigurationDependent.fsproj" (Some "Alternate") |
| 223 | + |
| 224 | + output |> shouldIncludePackageA 4 |
| 225 | + output |> shouldIncludePackageB 4 |
| 226 | + output |> shouldIncludePackageBTransient 4 |
| 227 | + output |> shouldIncludeConstant 4 |
| 228 | + |
| 229 | + let nupkgPath = projectDir @@ "ConfigurationDependent.1.0.0.nupkg" |
| 230 | + |
| 231 | + if File.Exists nupkgPath |> not then Assert.Fail $"Expected '%s{nupkgPath}' to exist" |
| 232 | + let nuspec = NuGetCache.getNuSpecFromNupkg nupkgPath |
| 233 | + let dependencies = nuspec.Dependencies.Value |
| 234 | + |> List.map (fun (n, v, _) -> n.Name, v.Range.ToString()) |
| 235 | + |
| 236 | + let expected = [("PaketTest2394.PackageA", ">= 4.0 < 5.0"); ("PaketTest2394.PackageB", ">= 4.0 < 5.0")] |
| 237 | + Assert.That(dependencies, Is.SupersetOf expected) |
0 commit comments