Skip to content

Fix dotnet package remove command when project is not specified #49314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -25,7 +25,7 @@ public PackageRemoveCommand(
_arguments = parseResult.GetValue(PackageRemoveCommandParser.CmdPackageArgument).ToList().AsReadOnly();
if (_fileOrDirectory == null)
{
throw new ArgumentNullException(nameof(_fileOrDirectory));
_fileOrDirectory = Environment.CurrentDirectory;
}
if (_arguments.Count != 1)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.Cli.Package.Remove.Tests
{
public class GivenDotnetPackageRemove : SdkTest
{
public GivenDotnetPackageRemove(ITestOutputHelper log) : base(log)
{
}

[Fact]
public void WhenPackageIsRemovedWithoutProjectArgument()
{
var projectDirectory = _testAssetsManager
.CopyTestAsset("TestAppSimple")
.WithSource().Path;

var packageName = "Newtonsoft.Json";
var add = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("add", "package", packageName);
add.Should().Pass();

// Test the new 'dotnet package remove' command without specifying project
var remove = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("package", "remove", packageName);

remove.Should().Pass();
remove.StdOut.Should().Contain($"Removing PackageReference for package '{packageName}' from project 'TestAppSimple.csproj'.");
remove.StdErr.Should().BeEmpty();
}

[Fact]
public void WhenPackageIsRemovedWithProjectOption()
{
var projectDirectory = _testAssetsManager
.CopyTestAsset("TestAppSimple")
.WithSource().Path;

var packageName = "Newtonsoft.Json";
var add = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("add", "package", packageName);
add.Should().Pass();

// Test the new 'dotnet package remove' command with --project option
var remove = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("package", "remove", packageName, "--project", "TestAppSimple.csproj");

remove.Should().Pass();
remove.StdOut.Should().Contain($"Removing PackageReference for package '{packageName}' from project 'TestAppSimple.csproj'.");
remove.StdErr.Should().BeEmpty();
}

[Fact]
public void WhenNoPackageIsPassedCommandFails()
{
var projectDirectory = _testAssetsManager
.CopyTestAsset("TestAppSimple")
.WithSource()
.Path;

var cmd = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("package", "remove")
.Should()
.Fail();
}

[Fact]
public void WhenMultiplePackagesArePassedCommandFails()
{
var projectDirectory = _testAssetsManager
.CopyTestAsset("TestAppSimple")
.WithSource()
.Path;

var cmd = new DotnetCommand(Log)
.WithWorkingDirectory(projectDirectory)
.Execute("package", "remove", "package1", "package2")
.Should()
.Fail();
}
}
}
Loading