Skip to content

Commit 2612e77

Browse files
CopilotmarcpopMSFT
andcommitted
Fix dotnet package remove command to default to current directory
Co-authored-by: marcpopMSFT <[email protected]>
1 parent b7310cb commit 2612e77

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/Cli/dotnet/Commands/Package/Remove/PackageRemoveCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public PackageRemoveCommand(
2525
_arguments = parseResult.GetValue(PackageRemoveCommandParser.CmdPackageArgument).ToList().AsReadOnly();
2626
if (_fileOrDirectory == null)
2727
{
28-
throw new ArgumentNullException(nameof(_fileOrDirectory));
28+
_fileOrDirectory = Environment.CurrentDirectory;
2929
}
3030
if (_arguments.Count != 1)
3131
{
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.DotNet.Cli.Utils;
5+
6+
namespace Microsoft.DotNet.Cli.Package.Remove.Tests
7+
{
8+
public class GivenDotnetPackageRemove : SdkTest
9+
{
10+
public GivenDotnetPackageRemove(ITestOutputHelper log) : base(log)
11+
{
12+
}
13+
14+
[Fact]
15+
public void WhenPackageIsRemovedWithoutProjectArgument()
16+
{
17+
var projectDirectory = _testAssetsManager
18+
.CopyTestAsset("TestAppSimple")
19+
.WithSource().Path;
20+
21+
var packageName = "Newtonsoft.Json";
22+
var add = new DotnetCommand(Log)
23+
.WithWorkingDirectory(projectDirectory)
24+
.Execute("add", "package", packageName);
25+
add.Should().Pass();
26+
27+
// Test the new 'dotnet package remove' command without specifying project
28+
var remove = new DotnetCommand(Log)
29+
.WithWorkingDirectory(projectDirectory)
30+
.Execute("package", "remove", packageName);
31+
32+
remove.Should().Pass();
33+
remove.StdOut.Should().Contain($"Removing PackageReference for package '{packageName}' from project 'TestAppSimple.csproj'.");
34+
remove.StdErr.Should().BeEmpty();
35+
}
36+
37+
[Fact]
38+
public void WhenPackageIsRemovedWithProjectOption()
39+
{
40+
var projectDirectory = _testAssetsManager
41+
.CopyTestAsset("TestAppSimple")
42+
.WithSource().Path;
43+
44+
var packageName = "Newtonsoft.Json";
45+
var add = new DotnetCommand(Log)
46+
.WithWorkingDirectory(projectDirectory)
47+
.Execute("add", "package", packageName);
48+
add.Should().Pass();
49+
50+
// Test the new 'dotnet package remove' command with --project option
51+
var remove = new DotnetCommand(Log)
52+
.WithWorkingDirectory(projectDirectory)
53+
.Execute("package", "remove", packageName, "--project", "TestAppSimple.csproj");
54+
55+
remove.Should().Pass();
56+
remove.StdOut.Should().Contain($"Removing PackageReference for package '{packageName}' from project 'TestAppSimple.csproj'.");
57+
remove.StdErr.Should().BeEmpty();
58+
}
59+
60+
[Fact]
61+
public void WhenNoPackageIsPassedCommandFails()
62+
{
63+
var projectDirectory = _testAssetsManager
64+
.CopyTestAsset("TestAppSimple")
65+
.WithSource()
66+
.Path;
67+
68+
var cmd = new DotnetCommand(Log)
69+
.WithWorkingDirectory(projectDirectory)
70+
.Execute("package", "remove")
71+
.Should()
72+
.Fail();
73+
}
74+
75+
[Fact]
76+
public void WhenMultiplePackagesArePassedCommandFails()
77+
{
78+
var projectDirectory = _testAssetsManager
79+
.CopyTestAsset("TestAppSimple")
80+
.WithSource()
81+
.Path;
82+
83+
var cmd = new DotnetCommand(Log)
84+
.WithWorkingDirectory(projectDirectory)
85+
.Execute("package", "remove", "package1", "package2")
86+
.Should()
87+
.Fail();
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)