-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathDotnetPackageUpdateTests.cs
More file actions
428 lines (357 loc) · 17.8 KB
/
DotnetPackageUpdateTests.cs
File metadata and controls
428 lines (357 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable disable
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.XPath;
using FluentAssertions;
using NuGet.CommandLine.Xplat.Tests;
using NuGet.Frameworks;
using NuGet.ProjectModel;
using NuGet.Test.Utility;
using NuGet.Versioning;
using Xunit;
using Xunit.Abstractions;
namespace Dotnet.Integration.Test
{
[Collection(DotnetIntegrationCollection.Name)]
public class DotnetPackageUpdateTests : IClassFixture<PackageSearchRunnerFixture>
{
private readonly DotnetIntegrationTestFixture _testFixture;
private readonly ITestOutputHelper _testOutputHelper;
private readonly IReadOnlyDictionary<string, string> _envVars;
// The .NET SDK downloads reference assembly packages for target frameworks it can't find ref assemblies for
// locally. Ideally this should be solved, so it's not necessary to download the packages every time the test
// runs. At a minimum, this should be changed to use package source mapping, once update package supports PSM.
private const string NugetConfigFormat = """
<configuration>
<packageSources>
<clear />
<add key="test" value="{0}" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<packageSourceMapping>
<clear />
</packageSourceMapping>
</configuration>
""";
public DotnetPackageUpdateTests(DotnetIntegrationTestFixture testFixture, ITestOutputHelper testOutputHelper)
{
_testFixture = testFixture;
_testOutputHelper = testOutputHelper;
_envVars = new Dictionary<string, string>()
{
["DOTNET_HOST_PATH"] = _testFixture.TestDotnetCli
};
}
[Fact]
public async Task SingleTfmProject_PackageVersionUpdated()
{
// Arrange
using var testContext = new SimpleTestPathContext();
File.WriteAllText(testContext.NuGetConfig, string.Format(NugetConfigFormat, testContext.PackageSource));
var a1 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "1.0.0");
var a2 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "2.0.0");
SimpleTestPackageContext[] packages = [a1, a2];
await SimpleTestPackageUtility.CreatePackagesAsync(testContext.PackageSource, packages);
var csprojContents = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Internal.Test.a" Version="1.0.0" />
</ItemGroup>
</Project>
""";
var csprojPath = Path.Combine(testContext.SolutionRoot, "my.csproj");
File.WriteAllText(csprojPath, csprojContents);
// Act
var result = _testFixture.RunDotnetExpectSuccess(
workingDirectory: testContext.SolutionRoot,
args: $"package update NuGet.Internal.Test.a",
testOutputHelper: _testOutputHelper,
environmentVariables: _envVars);
// Assert
result.ExitCode.Should().Be(0);
string version = GetPackageReferenceVersion(csprojPath, "NuGet.Internal.Test.a");
version.Should().Be("2.0.0");
}
[Fact]
public async Task FileBasedApp()
{
using var pathContext = _testFixture.CreateSimpleTestPathContext();
// Create packages.
var a1 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "1.0.0");
var a2 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "2.0.0");
SimpleTestPackageContext[] packages = [a1, a2];
await SimpleTestPackageUtility.CreatePackagesAsync(pathContext.PackageSource, packages);
// Create the file-based app.
var fbaDir = Path.Join(pathContext.SolutionRoot, "fba");
Directory.CreateDirectory(fbaDir);
var appFile = Path.Join(fbaDir, "app.cs");
File.WriteAllText(appFile, """
#:property PublishAot=false
#:package [email protected]
Console.WriteLine();
""");
// Update the package.
_testFixture.RunDotnetExpectSuccess(fbaDir, "package update --file app.cs", testOutputHelper: _testOutputHelper, environmentVariables: _envVars);
// Verify the full content of the modified .cs file.
var modifiedContent = File.ReadAllText(appFile);
_testOutputHelper.WriteLine("after:\n" + modifiedContent);
Assert.Equal(
"""
#:property PublishAot=false
#:package [email protected]
Console.WriteLine();
""",
modifiedContent);
}
[Fact]
public async Task SingleTfmCpmProject_PackageVersionUpdated()
{
// Arrange
using var testContext = new SimpleTestPathContext();
File.WriteAllText(testContext.NuGetConfig, string.Format(NugetConfigFormat, testContext.PackageSource));
var a1 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "1.0.0");
var a2 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "2.0.0");
SimpleTestPackageContext[] packages = [a1, a2];
await SimpleTestPackageUtility.CreatePackagesAsync(testContext.PackageSource, packages);
var csprojContents = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Internal.Test.a" />
</ItemGroup>
</Project>
""";
var csprojPath = Path.Combine(testContext.SolutionRoot, "my.csproj");
File.WriteAllText(csprojPath, csprojContents);
var packagesPropsContents = """
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="NuGet.Internal.Test.a" Version="1.0.0" />
</ItemGroup>
</Project>
""";
var packagesPropsPath = Path.Combine(testContext.SolutionRoot, "Directory.Packages.props");
File.WriteAllText(packagesPropsPath, packagesPropsContents);
// Act
var result = _testFixture.RunDotnetExpectSuccess(
workingDirectory: testContext.SolutionRoot,
args: $"package update NuGet.Internal.Test.a",
testOutputHelper: _testOutputHelper,
environmentVariables: _envVars);
// Assert
result.ExitCode.Should().Be(0);
string packageReferenceVersion = GetPackageReferenceVersion(csprojPath, "NuGet.Internal.Test.a");
packageReferenceVersion.Should().BeNullOrEmpty();
string packageVersionVersion = GetPackageVersionVersion(csprojPath, "NuGet.Internal.Test.a");
packageVersionVersion.Should().Be("2.0.0");
}
[Fact]
public async Task MultiTfmProject_PackageVersionUpdated()
{
// Arrange
using var testContext = new SimpleTestPathContext();
File.WriteAllText(testContext.NuGetConfig, string.Format(NugetConfigFormat, testContext.PackageSource));
var a1 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "1.0.0");
var a2 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "2.0.0");
SimpleTestPackageContext[] packages = [a1, a2];
await SimpleTestPackageUtility.CreatePackagesAsync(testContext.PackageSource, packages);
var csprojContents = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Internal.Test.a" Version="1.0.0" />
</ItemGroup>
</Project>
""";
var csprojPath = Path.Combine(testContext.SolutionRoot, "my.csproj");
File.WriteAllText(csprojPath, csprojContents);
// Act
var result = _testFixture.RunDotnetExpectSuccess(
workingDirectory: testContext.SolutionRoot,
args: $"package update NuGet.Internal.Test.a",
testOutputHelper: _testOutputHelper,
environmentVariables: _envVars);
// Assert
result.ExitCode.Should().Be(0);
string version = GetPackageReferenceVersion(csprojPath, "NuGet.Internal.Test.a");
version.Should().Be("2.0.0");
}
[Fact]
public async Task MultiTfmProjectWithConditionalPackageRef_PackageVersionUpdated()
{
// Arrange
using var testContext = new SimpleTestPathContext();
File.WriteAllText(testContext.NuGetConfig, string.Format(NugetConfigFormat, testContext.PackageSource));
var a1 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "1.0.0");
var a2 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "2.0.0");
SimpleTestPackageContext[] packages = [a1, a2];
await SimpleTestPackageUtility.CreatePackagesAsync(testContext.PackageSource, packages);
var csprojContents = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Internal.Test.a" Version="1.0.0" Condition=" '$(TargetFramework)' == 'net8.0' " />
</ItemGroup>
</Project>
""";
var csprojPath = Path.Combine(testContext.SolutionRoot, "my.csproj");
File.WriteAllText(csprojPath, csprojContents);
// Act
var result = _testFixture.RunDotnetExpectSuccess(
workingDirectory: testContext.SolutionRoot,
args: $"package update NuGet.Internal.Test.a",
testOutputHelper: _testOutputHelper,
environmentVariables: _envVars);
// Assert
result.ExitCode.Should().Be(0);
XDocument csproj = XDocument.Load(csprojPath);
var packageReferenceA = csproj.XPathSelectElements("//PackageReference[@Include='NuGet.Internal.Test.a']").ToList();
packageReferenceA.Count.Should().Be(1);
packageReferenceA[0].Attribute("Version").Value.Should().Be("2.0.0");
packageReferenceA[0].Attribute("Condition").Value.Should().Be(" '$(TargetFramework)' == 'net8.0' ");
}
[Fact]
public async Task SingleTfmProject_CommitsRestore()
{
// Arrange
using var testContext = new SimpleTestPathContext();
File.WriteAllText(testContext.NuGetConfig, string.Format(NugetConfigFormat, testContext.PackageSource));
var a1 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "1.0.0");
var a2 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "2.0.0");
SimpleTestPackageContext[] packages = [a1, a2];
await SimpleTestPackageUtility.CreatePackagesAsync(testContext.PackageSource, packages);
var csprojContents = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Internal.Test.a" Version="1.0.0" />
</ItemGroup>
</Project>
""";
var csprojPath = Path.Combine(testContext.SolutionRoot, "my.csproj");
File.WriteAllText(csprojPath, csprojContents);
// Act
var result = _testFixture.RunDotnetExpectSuccess(
workingDirectory: testContext.SolutionRoot,
args: $"package update NuGet.Internal.Test.a",
testOutputHelper: _testOutputHelper,
environmentVariables: _envVars);
// Assert
result.ExitCode.Should().Be(0);
string assetsFilePath = Path.Combine(testContext.SolutionRoot, "obj", "project.assets.json");
LockFile assetsFile = new LockFileFormat().Read(assetsFilePath);
assetsFile.Libraries[0].Name.Should().Be("NuGet.Internal.Test.a");
assetsFile.Libraries[0].Version.Should().Be(new NuGetVersion("2.0.0"));
}
[Fact]
public void InvalidProjectFile_OutputsMeaningfulError()
{
// Arrange
using var testContext = new SimpleTestPathContext();
var csprojContents = "<Invalid Xml";
var csprojPath = Path.Combine(testContext.SolutionRoot, "my.csproj");
File.WriteAllText(csprojPath, csprojContents);
// Act
var result = _testFixture.RunDotnetExpectFailure(
workingDirectory: testContext.SolutionRoot,
args: $"package update NuGet.Internal.Test.a",
testOutputHelper: _testOutputHelper,
environmentVariables: _envVars);
// Assert
result.Output.Should().Contain("MSB4025").And.Contain(csprojPath);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task SolutionWithTwoProjects_UpdateAll_UpdatesBothProjects(bool useSlnx)
{
// Arrange
using var testContext = new SimpleTestPathContext();
File.WriteAllText(testContext.NuGetConfig, string.Format(NugetConfigFormat, testContext.PackageSource));
var a1 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "1.0.0");
var a2 = new SimpleTestPackageContext("NuGet.Internal.Test.a", "2.0.0");
SimpleTestPackageContext[] packages = [a1, a2];
await SimpleTestPackageUtility.CreatePackagesAsync(testContext.PackageSource, packages);
var solution = new SimpleTestSolutionContext(testContext.SolutionRoot, useSlnx);
var project1 = SimpleTestProjectContext.CreateNETCore(
"Project1",
testContext.SolutionRoot,
NuGetFramework.Parse("net9.0"));
project1.AddPackageToAllFrameworks(a1);
var project2 = SimpleTestProjectContext.CreateNETCore(
"Project2",
testContext.SolutionRoot,
NuGetFramework.Parse("net9.0"));
project2.AddPackageToAllFrameworks(a1);
solution.Projects.Add(project1);
solution.Projects.Add(project2);
solution.Create();
// Act
var result = _testFixture.RunDotnetExpectSuccess(
workingDirectory: testContext.SolutionRoot,
args: $"package update",
testOutputHelper: _testOutputHelper,
environmentVariables: _envVars);
// Assert
result.ExitCode.Should().Be(0);
string version1 = GetPackageReferenceVersion(project1.ProjectPath, "NuGet.Internal.Test.a");
version1.Should().Be("2.0.0");
string version2 = GetPackageReferenceVersion(project2.ProjectPath, "NuGet.Internal.Test.a");
version2.Should().Be("2.0.0");
}
private string GetItemVersion(string projectPath, string itemType, string packageName)
{
var result = _testFixture.RunDotnetExpectSuccess(
workingDirectory: Path.GetDirectoryName(projectPath),
args: $"msbuild {Path.GetFileName(projectPath)} -getItem:{itemType}",
testOutputHelper: _testOutputHelper,
environmentVariables: _envVars);
using JsonDocument document = JsonDocument.Parse(result.Output);
JsonElement root = document.RootElement;
if (root.TryGetProperty("Items", out JsonElement items) &&
items.TryGetProperty(itemType, out JsonElement itemElements))
{
foreach (JsonElement item in itemElements.EnumerateArray())
{
if (item.TryGetProperty("Identity", out JsonElement identity) &&
identity.GetString() == packageName)
{
if (item.TryGetProperty("Version", out JsonElement version))
{
return version.GetString();
}
}
}
}
return null;
}
private string GetPackageReferenceVersion(string projectPath, string packageName)
{
return GetItemVersion(projectPath, "PackageReference", packageName);
}
private string GetPackageVersionVersion(string projectPath, string packageName)
{
return GetItemVersion(projectPath, "PackageVersion", packageName);
}
}
}