Skip to content

Commit 25a809e

Browse files
committed
Add integration test suite for group conditions
1 parent 3ac6f2e commit 25a809e

29 files changed

+1677
-501
lines changed

Diff for: .paket/Paket.Restore.targets

+497-500
Large diffs are not rendered by default.
+237
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
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)

Diff for: integrationtests/Paket.IntegrationTests/Paket.IntegrationTests.fsproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<None Include="paket.references" />
4141
<Content Include="App.config" />
4242
<Compile Include="AddGithubSpecs.fs" />
43+
<Compile Include="ConditionSpecs.fs" />
4344
</ItemGroup>
4445
<ItemGroup>
4546
<ProjectReference Include="..\..\src\Paket.Core\Paket.Core.fsproj" />
@@ -51,4 +52,4 @@
5152
</Reference>
5253
</ItemGroup>
5354
<Import Project="..\..\.paket\Paket.Restore.targets" />
54-
</Project>
55+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.nupkg
2+
bin/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project>
2+
<!-- Based on https://github.com/NuGet/Home/issues/5525#issuecomment-1179525536 -->
3+
<Target Name="UseExplicitPackageVersions" BeforeTargets="GenerateNuspec">
4+
<ItemGroup>
5+
<_ProjectReferenceWithExplicitPackageVersion Include="@(ProjectReference->'%(FullPath)')" Condition="'%(ProjectReference.PackageVersion)' != ''" />
6+
<_ProjectReferenceWithExactPackageVersion Include="@(ProjectReference->'%(FullPath)')" />
7+
<_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" Condition="'%(Identity)' != '' And '@(_ProjectReferenceWithExplicitPackageVersion)' == '@(_ProjectReferencesWithVersions)'">
8+
<ProjectVersion>@(_ProjectReferenceWithExplicitPackageVersion->'%(PackageVersion)')</ProjectVersion>
9+
</_ProjectReferenceWithReassignedVersion>
10+
<_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" Condition="'%(Identity)' != '' And '@(_ProjectReferenceWithExactPackageVersion)' == '@(_ProjectReferencesWithVersions)'">
11+
<ProjectVersion>[@(_ProjectReferencesWithVersions->'%(ProjectVersion)')]</ProjectVersion>
12+
</_ProjectReferenceWithReassignedVersion>
13+
<_ProjectReferencesWithVersions Remove="@(_ProjectReferenceWithReassignedVersion)" />
14+
<_ProjectReferencesWithVersions Include="@(_ProjectReferenceWithReassignedVersion)" />
15+
</ItemGroup>
16+
</Target>
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaketTest2394.PackageA", "PaketTest2394.PackageA\PaketTest2394.PackageA.csproj", "{A0449AB0-A1EA-4BE6-8D03-701BC5A2FD46}"
4+
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaketTest2394.PackageB", "PaketTest2394.PackageB\PaketTest2394.PackageB.csproj", "{59B3FFCE-AA5A-4AAA-B73D-108A6A15E524}"
6+
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaketTest2394.PackageB.Transient", "PaketTest2394.PackageB.Transient\PaketTest2394.PackageB.Transient.csproj", "{C763B8F6-18BF-4018-B852-CA8EE39C94F5}"
8+
EndProject
9+
Global
10+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
11+
Debug|Any CPU = Debug|Any CPU
12+
Release|Any CPU = Release|Any CPU
13+
EndGlobalSection
14+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
15+
{A0449AB0-A1EA-4BE6-8D03-701BC5A2FD46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
16+
{A0449AB0-A1EA-4BE6-8D03-701BC5A2FD46}.Debug|Any CPU.Build.0 = Debug|Any CPU
17+
{A0449AB0-A1EA-4BE6-8D03-701BC5A2FD46}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{A0449AB0-A1EA-4BE6-8D03-701BC5A2FD46}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{59B3FFCE-AA5A-4AAA-B73D-108A6A15E524}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{59B3FFCE-AA5A-4AAA-B73D-108A6A15E524}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{59B3FFCE-AA5A-4AAA-B73D-108A6A15E524}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{59B3FFCE-AA5A-4AAA-B73D-108A6A15E524}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{C763B8F6-18BF-4018-B852-CA8EE39C94F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{C763B8F6-18BF-4018-B852-CA8EE39C94F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{C763B8F6-18BF-4018-B852-CA8EE39C94F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{C763B8F6-18BF-4018-B852-CA8EE39C94F5}.Release|Any CPU.Build.0 = Release|Any CPU
27+
EndGlobalSection
28+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace PaketTest2394.PackageA;
2+
3+
public static class PackageDescription
4+
{
5+
public static string GetDescription()
6+
{
7+
var assemblyName = typeof(PackageDescription).Assembly.GetName();
8+
return $"PackageA {assemblyName.Version!.ToString(2)}";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<None Include="build/PaketTest2394.PackageA.targets" Pack="true" PackagePath="build\PaketTest2394.PackageA.targets" />
10+
</ItemGroup>
11+
<Target Name="UpdateTargetsFile" BeforeTargets="Build">
12+
<PropertyGroup>
13+
<NewDefineConstants>%24(DefineConstants)%3BPACKAGEA_$(Version.Split('.')[0])</NewDefineConstants>
14+
</PropertyGroup>
15+
<XmlPoke XmlInputPath="build/PaketTest2394.PackageA.targets" Query="//DefineConstants" Value="$(NewDefineConstants)"/>
16+
</Target>
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<PropertyGroup>
3+
<DefineConstants>$(DefineConstants);PACKAGEA_5</DefineConstants>
4+
</PropertyGroup>
5+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace PaketTest2394.PackageB.Transient;
2+
3+
public static class PackageDescription
4+
{
5+
public static string GetDescription()
6+
{
7+
var assemblyName = typeof(PackageDescription).Assembly.GetName();
8+
return $"PackageB.Transient {assemblyName.Version!.ToString(2)}";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace PaketTest2394.PackageB;
2+
3+
public static class PackageDescription
4+
{
5+
public static string GetDescription()
6+
{
7+
var assemblyName = typeof(PackageDescription).Assembly.GetName();
8+
return $"PackageB {assemblyName.Version!.ToString(2)} (references {Transient.PackageDescription.GetDescription()})";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\PaketTest2394.PackageB.Transient\PaketTest2394.PackageB.Transient.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)