Skip to content

Commit f4c42b8

Browse files
committed
Add dotnet package update command
1 parent 8eb971c commit f4c42b8

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/Cli/dotnet/Parser.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static class Parser
119119
// Argument
120120
public static readonly Argument<string> DotnetSubCommand = new("subcommand") { Arity = ArgumentArity.ZeroOrOne, Hidden = true };
121121

122-
private static Command ConfigureCommandLine(Command rootCommand)
122+
private static Command ConfigureCommandLine(RootCommand rootCommand)
123123
{
124124
for (int i = rootCommand.Options.Count - 1; i >= 0; i--)
125125
{
@@ -156,6 +156,9 @@ private static Command ConfigureCommandLine(Command rootCommand)
156156
// Add argument
157157
rootCommand.Arguments.Add(DotnetSubCommand);
158158

159+
// NuGet implements several commands in its own repo. Add them to the .NET SDK via the provided API.
160+
NuGet.CommandLine.XPlat.NuGetCommands.Add(rootCommand);
161+
159162
rootCommand.SetAction(parseResult =>
160163
{
161164
if (parseResult.GetValue(DiagOption) && parseResult.Tokens.Count == 1)

test/dotnet.Tests/CommandTests/NuGet/GivenANuGetCommand.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Microsoft.DotNet.Cli;
77
using Microsoft.DotNet.Cli.Commands.NuGet;
88
using Moq;
9+
using Newtonsoft.Json.Linq;
10+
using NuGet.Versioning;
911

1012
namespace Microsoft.DotNet.Tools.Run.Tests
1113
{
@@ -133,5 +135,51 @@ public void ItHasAWhySubcommand()
133135
.And.NotHaveStdErr()
134136
.And.HaveStdOutContaining("has the following dependency");
135137
}
138+
139+
[Fact]
140+
public void ItCanUpdatePackages()
141+
{
142+
// Arrange
143+
var testAssetName = "TestAppSimple";
144+
var testAsset = _testAssetsManager
145+
.CopyTestAsset(testAssetName)
146+
.WithSource();
147+
var projectDirectory = testAsset.Path;
148+
149+
NuGetConfigWriter.Write(projectDirectory, TestContext.Current.TestPackages);
150+
151+
new DotnetCommand(Log, "package", "add", "[email protected]")
152+
.WithWorkingDirectory(projectDirectory)
153+
.Execute()
154+
.Should()
155+
.Pass()
156+
.And.NotHaveStdErr();
157+
158+
// Act
159+
var commandResult = new DotnetCommand(Log, "package", "update", "dotnet-hello")
160+
.WithWorkingDirectory(projectDirectory)
161+
.Execute()
162+
.Should()
163+
.Pass()
164+
.And.NotHaveStdErr();
165+
166+
// Assert
167+
var listPackageCommandResult = new DotnetCommand(Log, "package", "list", "--format", "json")
168+
.WithWorkingDirectory(projectDirectory)
169+
.Execute();
170+
listPackageCommandResult.Should()
171+
.Pass()
172+
.And.NotHaveStdErr();
173+
174+
_ = listPackageCommandResult.StdOut;
175+
var updatedPackageVersionString = JObject.Parse(listPackageCommandResult.StdOut)
176+
.SelectToken("$.projects[0].frameworks[0].topLevelPackages[?(@.id == 'dotnet-hello')].requestedVersion")
177+
.ToString();
178+
179+
var v1 = NuGetVersion.Parse("1.0.0");
180+
var updatedVersion = NuGetVersion.Parse(updatedPackageVersionString);
181+
182+
updatedVersion.Should().BeGreaterThan(v1);
183+
}
136184
}
137185
}

0 commit comments

Comments
 (0)