Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Internal.NuGet.Testing.SignedPackages.ChildProcess;
using NuGet.CommandLine.XPlat;
using NuGet.Common;
using NuGet.Packaging;
using NuGet.Packaging.Core;
Expand Down Expand Up @@ -79,7 +77,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
}
}

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

var tempDir = Path.Join(pathContext.WorkingDirectory, "temp");
Directory.CreateDirectory(tempDir);

// Generate DG file.
var dgFile = Path.Join(tempDir, "dg.json");
_fixture.RunDotnetExpectSuccess(fbaDir, $"build app.cs -t:GenerateRestoreGraphFile -p:RestoreGraphOutputPath={ArgumentEscaper.EscapeAndConcatenate([dgFile])}", testOutputHelper: _testOutputHelper);

// Get project content.
var virtualProject = _fixture.GetFileBasedAppVirtualProject(appFile, _testOutputHelper);
_testOutputHelper.WriteLine("before:\n" + virtualProject.Content);
Assert.DoesNotContain("PackageReference", virtualProject.Content);
using var builder = new TestVirtualProjectBuilder(virtualProject);

// Create a package.
var packageX = XPlatTestUtils.CreatePackage();
await SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.PackageSource, PackageSaveMode.Defaultv3, packageX);

// Add the package.
using var outWriter = new StringWriter();
using var errorWriter = new StringWriter();
var testApp = new CommandLineApplication
{
Out = outWriter,
Error = errorWriter,
};
AddPackageReferenceCommand.Register(
testApp,
() => new TestLogger(_testOutputHelper),
() => new AddPackageReferenceCommandRunner(),
() => builder);
int result = testApp.Execute([
"add",
"--project", appFile,
"--package", "packageX",
"--dg-file", dgFile,
]);

var output = outWriter.ToString();
var error = errorWriter.ToString();

_testOutputHelper.WriteLine(output);
_testOutputHelper.WriteLine(error);

Assert.Equal(0, result);

Assert.Empty(error);

var modifiedProjectContent = builder.ModifiedContent;
_testOutputHelper.WriteLine("after:\n" + modifiedProjectContent);
Assert.Contains("""<PackageReference Include="packageX" Version="1.0.0" />""", modifiedProjectContent);
_fixture.RunDotnetExpectSuccess(fbaDir, "package add packageX --file app.cs --version 1.0.0", testOutputHelper: _testOutputHelper);

// Verify the full content of the modified .cs file.
var modifiedContent = File.ReadAllText(appFile);
_testOutputHelper.WriteLine("after:\n" + modifiedContent);
Assert.Equal(
"""
#:package [email protected]
#:property PublishAot=false
Console.WriteLine();
""",
modifiedContent);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using FluentAssertions;
using Microsoft.Build.Locator;
using Microsoft.Internal.NuGet.Testing.SignedPackages.ChildProcess;
using NuGet.Commands;
using NuGet.Common;
Expand Down Expand Up @@ -57,9 +54,6 @@ public DotnetIntegrationTestFixture()
SdkDirectory = new DirectoryInfo(sdkPath);
MsBuildSdksPath = Path.Combine(sdkPath, "Sdks");

// https://github.com/NuGet/Home/issues/14823: This can be removed when we migrate to `dotnet.exe`-only integration tests for file-based apps.
MSBuildLocator.RegisterMSBuildPath(sdkPath);

_templateDirectory = new SimpleTestPathContext();
TestDotnetCLiUtility.WriteGlobalJson(_templateDirectory.WorkingDirectory);

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

internal (string Content, string ProjectPath, string FilePath) GetFileBasedAppVirtualProject(string entryPointFileFullPath, ITestOutputHelper testOutputHelper)
{
var runApi = RunDotnetExpectSuccess(Path.GetDirectoryName(entryPointFileFullPath), "run-api", testOutputHelper: testOutputHelper, inputAction: writer =>
{
writer.Write($$"""{ "$type": "GetProject", "EntryPointFileFullPath": {{JsonSerializer.Serialize(entryPointFileFullPath)}} }""");
});
var node = JsonNode.Parse(runApi.AllOutput);
return (Content: node["Content"].GetValue<string>(),
ProjectPath: node["ProjectPath"].GetValue<string>(),
FilePath: entryPointFileFullPath);
}

internal TestDirectory CreateTestDirectory()
{
var testDirectory = TestDirectory.Create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
using System.Threading.Tasks;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Internal.NuGet.Testing.SignedPackages.ChildProcess;
using Newtonsoft.Json.Linq;
using NuGet.CommandLine.XPlat;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Frameworks;
Expand Down Expand Up @@ -79,7 +77,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
}
}

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

// Get project content.
var virtualProject = _fixture.GetFileBasedAppVirtualProject(appFile, _testOutputHelper);
using var builder = new TestVirtualProjectBuilder(virtualProject);

// List packages.
using var outWriter = new StringWriter();
using var errorWriter = new StringWriter();
var testApp = new CommandLineApplication
{
Out = outWriter,
Error = errorWriter,
};
var logger = new TestLogger(_testOutputHelper);
var msbuild = new MSBuildAPIUtility(logger, builder);
ListPackageCommand.Register(
testApp,
() => logger,
(_) => { },
() => new ListPackageCommandRunner(msbuild));
int result = testApp.Execute([
"list", appFile,
"--source", pathContext.PackageSource,
"--format", "json",
]);

var output = outWriter.ToString();
var error = errorWriter.ToString();

_testOutputHelper.WriteLine(output);
_testOutputHelper.WriteLine(error);

Assert.Equal(0, result);

Assert.Empty(error);

Assert.Contains("packageX", output);
Assert.Contains("1.0.0", output);
var result = _fixture.RunDotnetExpectSuccess(fbaDir, "package list --file app.cs --format json", testOutputHelper: _testOutputHelper);

Assert.Null(builder.ModifiedContent);
Assert.Contains("packageX", result.AllOutput);
Assert.Contains("1.0.0", result.AllOutput);
}

[PlatformFact(Platform.Windows)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable disable

using System.Collections.Generic;
using System.CommandLine;
using System.IO;
using System.Linq;
using System.Text.Json;
Expand All @@ -13,14 +12,10 @@
using System.Xml.XPath;
using FluentAssertions;
using NuGet.CommandLine.Xplat.Tests;
using NuGet.CommandLine.XPlat;
using NuGet.CommandLine.XPlat.Commands.Package.Update;
using NuGet.Frameworks;
using NuGet.ProjectModel;
using NuGet.Test.Utility;
using NuGet.Versioning;
using NuGet.XPlat.FuncTest;
using Test.Utility;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -100,7 +95,6 @@ public async Task SingleTfmProject_PackageVersionUpdated()
version.Should().Be("2.0.0");
}

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

// Get project content.
var virtualProject = _testFixture.GetFileBasedAppVirtualProject(appFile, _testOutputHelper);
_testOutputHelper.WriteLine("before:\n" + virtualProject.Content);
Assert.Contains("""<PackageReference Include="NuGet.Internal.Test.a" Version="1.0.0" />""", virtualProject.Content);
using var builder = new TestVirtualProjectBuilder(virtualProject);

// Update the package.
using var outWriter = new StringWriter();
using var errorWriter = new StringWriter();
var rootCommand = new RootCommand();
PackageUpdateCommand.Register(
rootCommand,
new Option<bool>("--interactive"),
(args, ct) =>
{
var msbuildUtility = new MSBuildAPIUtility(new TestLogger(_testOutputHelper), builder);
var packageUpdateIO = new PackageUpdateIO(args.Project, msbuildUtility, new TestEnvironmentVariableReader(_envVars));
return PackageUpdateCommandRunner.Run(args, new TestCommandOutputLogger(_testOutputHelper), packageUpdateIO, ct);
});
int result = rootCommand.Parse([
"update",
"--project", appFile,
]).Invoke(new() { Output = outWriter, Error = errorWriter });

var output = outWriter.ToString();
var error = errorWriter.ToString();

_testOutputHelper.WriteLine(output);
_testOutputHelper.WriteLine(error);
_testFixture.RunDotnetExpectSuccess(fbaDir, "package update --file app.cs", testOutputHelper: _testOutputHelper, environmentVariables: _envVars);

Assert.Equal(0, result);

Assert.Empty(error);

var modifiedProjectContent = builder.ModifiedContent;
_testOutputHelper.WriteLine("after:\n" + modifiedProjectContent);
Assert.Contains("""<PackageReference Include="NuGet.Internal.Test.a" Version="2.0.0" />""", modifiedProjectContent);
// 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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.CommandLineUtils;
using NuGet.CommandLine.XPlat;
using NuGet.Test.Utility;
using NuGet.XPlat.FuncTest;
using Xunit;
using Xunit.Abstractions;

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

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

Expand All @@ -34,43 +28,16 @@ public async Task RemovePkg_FileBasedApp()
Console.WriteLine();
""");

// Get project content.
var virtualProject = _fixture.GetFileBasedAppVirtualProject(appFile, _testOutputHelper);
_testOutputHelper.WriteLine("before:\n" + virtualProject.Content);
Assert.Contains("""<PackageReference Include="packageX" Version="1.0.0" />""", virtualProject.Content);
using var builder = new TestVirtualProjectBuilder(virtualProject);

// Remove the package.
using var outWriter = new StringWriter();
using var errorWriter = new StringWriter();
var testApp = new CommandLineApplication
{
Out = outWriter,
Error = errorWriter,
};
RemovePackageReferenceCommand.Register(
testApp,
() => new TestLogger(_testOutputHelper),
() => new RemovePackageReferenceCommandRunner(),
() => builder);
int result = testApp.Execute([
"remove",
"--project", appFile,
"--package", "packageX",
]);

var output = outWriter.ToString();
var error = errorWriter.ToString();

_testOutputHelper.WriteLine(output);
_testOutputHelper.WriteLine(error);
_fixture.RunDotnetExpectSuccess(fbaDir, "package remove packageX --file app.cs", testOutputHelper: _testOutputHelper);

Assert.Equal(0, result);

Assert.Empty(error);

var modifiedProjectContent = builder.ModifiedContent;
_testOutputHelper.WriteLine("after:\n" + modifiedProjectContent);
Assert.DoesNotContain("PackageReference", modifiedProjectContent);
// Verify the full content of the modified .cs file.
var modifiedContent = File.ReadAllText(appFile);
_testOutputHelper.WriteLine("after:\n" + modifiedContent);
Assert.Equal(
"""
Console.WriteLine();
""",
modifiedContent);
}
}
Loading