Skip to content

Commit 4d0b9dd

Browse files
authored
Switch file-based app integration tests to use dotnet.exe only (#7316)
1 parent bac078b commit 4d0b9dd

6 files changed

Lines changed: 39 additions & 226 deletions

File tree

test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
using System.Linq;
88
using System.Threading.Tasks;
99
using FluentAssertions;
10-
using Microsoft.Extensions.CommandLineUtils;
1110
using Microsoft.Internal.NuGet.Testing.SignedPackages.ChildProcess;
12-
using NuGet.CommandLine.XPlat;
1311
using NuGet.Common;
1412
using NuGet.Packaging;
1513
using NuGet.Packaging.Core;
@@ -79,7 +77,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
7977
}
8078
}
8179

82-
// https://github.com/NuGet/Home/issues/14823: This should use `dotnet package add` when it supports file-based apps.
8380
[Fact]
8481
public async Task AddPkg_FileBasedApp()
8582
{
@@ -95,56 +92,23 @@ public async Task AddPkg_FileBasedApp()
9592
Console.WriteLine();
9693
""");
9794

98-
var tempDir = Path.Join(pathContext.WorkingDirectory, "temp");
99-
Directory.CreateDirectory(tempDir);
100-
101-
// Generate DG file.
102-
var dgFile = Path.Join(tempDir, "dg.json");
103-
_fixture.RunDotnetExpectSuccess(fbaDir, $"build app.cs -t:GenerateRestoreGraphFile -p:RestoreGraphOutputPath={ArgumentEscaper.EscapeAndConcatenate([dgFile])}", testOutputHelper: _testOutputHelper);
104-
105-
// Get project content.
106-
var virtualProject = _fixture.GetFileBasedAppVirtualProject(appFile, _testOutputHelper);
107-
_testOutputHelper.WriteLine("before:\n" + virtualProject.Content);
108-
Assert.DoesNotContain("PackageReference", virtualProject.Content);
109-
using var builder = new TestVirtualProjectBuilder(virtualProject);
110-
11195
// Create a package.
11296
var packageX = XPlatTestUtils.CreatePackage();
11397
await SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.PackageSource, PackageSaveMode.Defaultv3, packageX);
11498

11599
// Add the package.
116-
using var outWriter = new StringWriter();
117-
using var errorWriter = new StringWriter();
118-
var testApp = new CommandLineApplication
119-
{
120-
Out = outWriter,
121-
Error = errorWriter,
122-
};
123-
AddPackageReferenceCommand.Register(
124-
testApp,
125-
() => new TestLogger(_testOutputHelper),
126-
() => new AddPackageReferenceCommandRunner(),
127-
() => builder);
128-
int result = testApp.Execute([
129-
"add",
130-
"--project", appFile,
131-
"--package", "packageX",
132-
"--dg-file", dgFile,
133-
]);
134-
135-
var output = outWriter.ToString();
136-
var error = errorWriter.ToString();
137-
138-
_testOutputHelper.WriteLine(output);
139-
_testOutputHelper.WriteLine(error);
140-
141-
Assert.Equal(0, result);
142-
143-
Assert.Empty(error);
144-
145-
var modifiedProjectContent = builder.ModifiedContent;
146-
_testOutputHelper.WriteLine("after:\n" + modifiedProjectContent);
147-
Assert.Contains("""<PackageReference Include="packageX" Version="1.0.0" />""", modifiedProjectContent);
100+
_fixture.RunDotnetExpectSuccess(fbaDir, "package add packageX --file app.cs --version 1.0.0", testOutputHelper: _testOutputHelper);
101+
102+
// Verify the full content of the modified .cs file.
103+
var modifiedContent = File.ReadAllText(appFile);
104+
_testOutputHelper.WriteLine("after:\n" + modifiedContent);
105+
Assert.Equal(
106+
"""
107+
#:package packageX@1.0.0
108+
#:property PublishAot=false
109+
Console.WriteLine();
110+
""",
111+
modifiedContent);
148112
}
149113

150114
[Fact]

test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetIntegrationTestFixture.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
using System.Linq;
1212
using System.Reflection;
1313
using System.Runtime.InteropServices;
14-
using System.Text.Json;
15-
using System.Text.Json.Nodes;
1614
using System.Threading;
1715
using FluentAssertions;
18-
using Microsoft.Build.Locator;
1916
using Microsoft.Internal.NuGet.Testing.SignedPackages.ChildProcess;
2017
using NuGet.Commands;
2118
using NuGet.Common;
@@ -57,9 +54,6 @@ public DotnetIntegrationTestFixture()
5754
SdkDirectory = new DirectoryInfo(sdkPath);
5855
MsBuildSdksPath = Path.Combine(sdkPath, "Sdks");
5956

60-
// https://github.com/NuGet/Home/issues/14823: This can be removed when we migrate to `dotnet.exe`-only integration tests for file-based apps.
61-
MSBuildLocator.RegisterMSBuildPath(sdkPath);
62-
6357
_templateDirectory = new SimpleTestPathContext();
6458
TestDotnetCLiUtility.WriteGlobalJson(_templateDirectory.WorkingDirectory);
6559

@@ -361,18 +355,6 @@ private CommandRunnerResult BuildProjectOrSolution(string workingDirectory, stri
361355
return RunDotnet(workingDirectory, $"msbuild {file} {args}", expectSuccess, testOutputHelper: testOutputHelper);
362356
}
363357

364-
internal (string Content, string ProjectPath, string FilePath) GetFileBasedAppVirtualProject(string entryPointFileFullPath, ITestOutputHelper testOutputHelper)
365-
{
366-
var runApi = RunDotnetExpectSuccess(Path.GetDirectoryName(entryPointFileFullPath), "run-api", testOutputHelper: testOutputHelper, inputAction: writer =>
367-
{
368-
writer.Write($$"""{ "$type": "GetProject", "EntryPointFileFullPath": {{JsonSerializer.Serialize(entryPointFileFullPath)}} }""");
369-
});
370-
var node = JsonNode.Parse(runApi.AllOutput);
371-
return (Content: node["Content"].GetValue<string>(),
372-
ProjectPath: node["ProjectPath"].GetValue<string>(),
373-
FilePath: entryPointFileFullPath);
374-
}
375-
376358
internal TestDirectory CreateTestDirectory()
377359
{
378360
var testDirectory = TestDirectory.Create();

test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetListPackageTests.cs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
using System.Threading.Tasks;
1313
using System.Xml.Linq;
1414
using FluentAssertions;
15-
using Microsoft.Extensions.CommandLineUtils;
1615
using Microsoft.Internal.NuGet.Testing.SignedPackages.ChildProcess;
1716
using Newtonsoft.Json.Linq;
18-
using NuGet.CommandLine.XPlat;
1917
using NuGet.Common;
2018
using NuGet.Configuration;
2119
using NuGet.Frameworks;
@@ -79,7 +77,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
7977
}
8078
}
8179

82-
// https://github.com/NuGet/Home/issues/14823: This should use `dotnet package list` when it supports file-based apps.
8380
[Fact]
8481
public async Task DotnetListPackage_FileBasedApp()
8582
{
@@ -102,45 +99,11 @@ public async Task DotnetListPackage_FileBasedApp()
10299
// Restore.
103100
_fixture.RunDotnetExpectSuccess(fbaDir, "restore app.cs", testOutputHelper: _testOutputHelper);
104101

105-
// Get project content.
106-
var virtualProject = _fixture.GetFileBasedAppVirtualProject(appFile, _testOutputHelper);
107-
using var builder = new TestVirtualProjectBuilder(virtualProject);
108-
109102
// List packages.
110-
using var outWriter = new StringWriter();
111-
using var errorWriter = new StringWriter();
112-
var testApp = new CommandLineApplication
113-
{
114-
Out = outWriter,
115-
Error = errorWriter,
116-
};
117-
var logger = new TestLogger(_testOutputHelper);
118-
var msbuild = new MSBuildAPIUtility(logger, builder);
119-
ListPackageCommand.Register(
120-
testApp,
121-
() => logger,
122-
(_) => { },
123-
() => new ListPackageCommandRunner(msbuild));
124-
int result = testApp.Execute([
125-
"list", appFile,
126-
"--source", pathContext.PackageSource,
127-
"--format", "json",
128-
]);
129-
130-
var output = outWriter.ToString();
131-
var error = errorWriter.ToString();
132-
133-
_testOutputHelper.WriteLine(output);
134-
_testOutputHelper.WriteLine(error);
135-
136-
Assert.Equal(0, result);
137-
138-
Assert.Empty(error);
139-
140-
Assert.Contains("packageX", output);
141-
Assert.Contains("1.0.0", output);
103+
var result = _fixture.RunDotnetExpectSuccess(fbaDir, "package list --file app.cs --format json", testOutputHelper: _testOutputHelper);
142104

143-
Assert.Null(builder.ModifiedContent);
105+
Assert.Contains("packageX", result.AllOutput);
106+
Assert.Contains("1.0.0", result.AllOutput);
144107
}
145108

146109
[PlatformFact(Platform.Windows)]

test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetPackageUpdateTests.cs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#nullable disable
55

66
using System.Collections.Generic;
7-
using System.CommandLine;
87
using System.IO;
98
using System.Linq;
109
using System.Text.Json;
@@ -13,14 +12,10 @@
1312
using System.Xml.XPath;
1413
using FluentAssertions;
1514
using NuGet.CommandLine.Xplat.Tests;
16-
using NuGet.CommandLine.XPlat;
17-
using NuGet.CommandLine.XPlat.Commands.Package.Update;
1815
using NuGet.Frameworks;
1916
using NuGet.ProjectModel;
2017
using NuGet.Test.Utility;
2118
using NuGet.Versioning;
22-
using NuGet.XPlat.FuncTest;
23-
using Test.Utility;
2419
using Xunit;
2520
using Xunit.Abstractions;
2621

@@ -100,7 +95,6 @@ public async Task SingleTfmProject_PackageVersionUpdated()
10095
version.Should().Be("2.0.0");
10196
}
10297

103-
// https://github.com/NuGet/Home/issues/14823: This should use `dotnet package update` when it supports file-based apps.
10498
[Fact]
10599
public async Task FileBasedApp()
106100
{
@@ -124,43 +118,19 @@ public async Task FileBasedApp()
124118
Console.WriteLine();
125119
""");
126120

127-
// Get project content.
128-
var virtualProject = _testFixture.GetFileBasedAppVirtualProject(appFile, _testOutputHelper);
129-
_testOutputHelper.WriteLine("before:\n" + virtualProject.Content);
130-
Assert.Contains("""<PackageReference Include="NuGet.Internal.Test.a" Version="1.0.0" />""", virtualProject.Content);
131-
using var builder = new TestVirtualProjectBuilder(virtualProject);
132-
133121
// Update the package.
134-
using var outWriter = new StringWriter();
135-
using var errorWriter = new StringWriter();
136-
var rootCommand = new RootCommand();
137-
PackageUpdateCommand.Register(
138-
rootCommand,
139-
new Option<bool>("--interactive"),
140-
(args, ct) =>
141-
{
142-
var msbuildUtility = new MSBuildAPIUtility(new TestLogger(_testOutputHelper), builder);
143-
var packageUpdateIO = new PackageUpdateIO(args.Project, msbuildUtility, new TestEnvironmentVariableReader(_envVars));
144-
return PackageUpdateCommandRunner.Run(args, new TestCommandOutputLogger(_testOutputHelper), packageUpdateIO, ct);
145-
});
146-
int result = rootCommand.Parse([
147-
"update",
148-
"--project", appFile,
149-
]).Invoke(new() { Output = outWriter, Error = errorWriter });
150-
151-
var output = outWriter.ToString();
152-
var error = errorWriter.ToString();
153-
154-
_testOutputHelper.WriteLine(output);
155-
_testOutputHelper.WriteLine(error);
122+
_testFixture.RunDotnetExpectSuccess(fbaDir, "package update --file app.cs", testOutputHelper: _testOutputHelper, environmentVariables: _envVars);
156123

157-
Assert.Equal(0, result);
158-
159-
Assert.Empty(error);
160-
161-
var modifiedProjectContent = builder.ModifiedContent;
162-
_testOutputHelper.WriteLine("after:\n" + modifiedProjectContent);
163-
Assert.Contains("""<PackageReference Include="NuGet.Internal.Test.a" Version="2.0.0" />""", modifiedProjectContent);
124+
// Verify the full content of the modified .cs file.
125+
var modifiedContent = File.ReadAllText(appFile);
126+
_testOutputHelper.WriteLine("after:\n" + modifiedContent);
127+
Assert.Equal(
128+
"""
129+
#:property PublishAot=false
130+
#:package NuGet.Internal.Test.a@2.0.0
131+
Console.WriteLine();
132+
""",
133+
modifiedContent);
164134
}
165135

166136
[Fact]

test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetRemovePackageTests.cs

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.IO;
5-
using System.Threading.Tasks;
6-
using Microsoft.Extensions.CommandLineUtils;
7-
using NuGet.CommandLine.XPlat;
8-
using NuGet.Test.Utility;
9-
using NuGet.XPlat.FuncTest;
105
using Xunit;
116
using Xunit.Abstractions;
127

@@ -18,9 +13,8 @@ public sealed class DotnetRemovePackageTests(DotnetIntegrationTestFixture fixtur
1813
private readonly DotnetIntegrationTestFixture _fixture = fixture;
1914
private readonly ITestOutputHelper _testOutputHelper = testOutputHelper;
2015

21-
// https://github.com/NuGet/Home/issues/14823: This should use `dotnet package remove` when it supports file-based apps.
2216
[Fact]
23-
public async Task RemovePkg_FileBasedApp()
17+
public void RemovePkg_FileBasedApp()
2418
{
2519
using var pathContext = _fixture.CreateSimpleTestPathContext();
2620

@@ -34,43 +28,16 @@ public async Task RemovePkg_FileBasedApp()
3428
Console.WriteLine();
3529
""");
3630

37-
// Get project content.
38-
var virtualProject = _fixture.GetFileBasedAppVirtualProject(appFile, _testOutputHelper);
39-
_testOutputHelper.WriteLine("before:\n" + virtualProject.Content);
40-
Assert.Contains("""<PackageReference Include="packageX" Version="1.0.0" />""", virtualProject.Content);
41-
using var builder = new TestVirtualProjectBuilder(virtualProject);
42-
4331
// Remove the package.
44-
using var outWriter = new StringWriter();
45-
using var errorWriter = new StringWriter();
46-
var testApp = new CommandLineApplication
47-
{
48-
Out = outWriter,
49-
Error = errorWriter,
50-
};
51-
RemovePackageReferenceCommand.Register(
52-
testApp,
53-
() => new TestLogger(_testOutputHelper),
54-
() => new RemovePackageReferenceCommandRunner(),
55-
() => builder);
56-
int result = testApp.Execute([
57-
"remove",
58-
"--project", appFile,
59-
"--package", "packageX",
60-
]);
61-
62-
var output = outWriter.ToString();
63-
var error = errorWriter.ToString();
64-
65-
_testOutputHelper.WriteLine(output);
66-
_testOutputHelper.WriteLine(error);
32+
_fixture.RunDotnetExpectSuccess(fbaDir, "package remove packageX --file app.cs", testOutputHelper: _testOutputHelper);
6733

68-
Assert.Equal(0, result);
69-
70-
Assert.Empty(error);
71-
72-
var modifiedProjectContent = builder.ModifiedContent;
73-
_testOutputHelper.WriteLine("after:\n" + modifiedProjectContent);
74-
Assert.DoesNotContain("PackageReference", modifiedProjectContent);
34+
// Verify the full content of the modified .cs file.
35+
var modifiedContent = File.ReadAllText(appFile);
36+
_testOutputHelper.WriteLine("after:\n" + modifiedContent);
37+
Assert.Equal(
38+
"""
39+
Console.WriteLine();
40+
""",
41+
modifiedContent);
7542
}
7643
}

0 commit comments

Comments
 (0)