Skip to content

Commit 5e1709c

Browse files
authored
Add dotnet package update command (#49287)
2 parents 69a3f7d + 6d89b44 commit 5e1709c

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
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: 47 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,50 @@ 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+
var updatedPackageVersionString = JObject.Parse(listPackageCommandResult.StdOut)
175+
.SelectToken("$.projects[0].frameworks[0].topLevelPackages[?(@.id == 'dotnet-hello')].requestedVersion")
176+
.ToString();
177+
178+
var v1 = NuGetVersion.Parse("1.0.0");
179+
var updatedVersion = NuGetVersion.Parse(updatedPackageVersionString);
180+
181+
updatedVersion.Should().BeGreaterThan(v1);
182+
}
136183
}
137184
}

test/dotnet.Tests/CompletionTests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ _testhost_package() {
10061006
prev="${COMP_WORDS[COMP_CWORD-1]}"
10071007
COMPREPLY=()
10081008

1009-
opts="search add list remove --help"
1009+
opts="search add list remove update --help"
10101010

10111011
if [[ $COMP_CWORD == "$1" ]]; then
10121012
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
@@ -1034,6 +1034,11 @@ _testhost_package() {
10341034
return
10351035
;;
10361036

1037+
(update)
1038+
_testhost_package_update $(($1+1))
1039+
return
1040+
;;
1041+
10371042
esac
10381043

10391044
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
@@ -1126,6 +1131,23 @@ _testhost_package_remove() {
11261131
}
11271132

11281133

1134+
_testhost_package_update() {
1135+
1136+
cur="${COMP_WORDS[COMP_CWORD]}"
1137+
prev="${COMP_WORDS[COMP_CWORD-1]}"
1138+
COMPREPLY=()
1139+
1140+
opts="--project --help"
1141+
1142+
if [[ $COMP_CWORD == "$1" ]]; then
1143+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
1144+
return
1145+
fi
1146+
1147+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
1148+
}
1149+
1150+
11291151
_testhost_project() {
11301152

11311153
cur="${COMP_WORDS[COMP_CWORD]}"

test/dotnet.Tests/CompletionTests/snapshots/pwsh/DotnetCliSnapshotTests.VerifyCompletions.verified.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ Register-ArgumentCompleter -Native -CommandName 'testhost' -ScriptBlock {
573573
[CompletionResult]::new('add', 'add', [CompletionResultType]::ParameterValue, "Add a NuGet package reference to the project.")
574574
[CompletionResult]::new('list', 'list', [CompletionResultType]::ParameterValue, "List all package references of the project or solution.")
575575
[CompletionResult]::new('remove', 'remove', [CompletionResultType]::ParameterValue, "Remove a NuGet package reference from the project.")
576+
[CompletionResult]::new('update', 'update', [CompletionResultType]::ParameterValue, "Update referenced packages in a project or solution.")
576577
)
577578
$completions += $staticCompletions
578579
break
@@ -656,6 +657,15 @@ Register-ArgumentCompleter -Native -CommandName 'testhost' -ScriptBlock {
656657
$completions += $staticCompletions
657658
break
658659
}
660+
'testhost;package;update' {
661+
$staticCompletions = @(
662+
[CompletionResult]::new('--project', '--project', [CompletionResultType]::ParameterName, "Path to a project or solution file, or a directory.")
663+
[CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, "Show command line help.")
664+
[CompletionResult]::new('--help', '-h', [CompletionResultType]::ParameterName, "Show command line help.")
665+
)
666+
$completions += $staticCompletions
667+
break
668+
}
659669
'testhost;project' {
660670
$staticCompletions = @(
661671
[CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, "Show command line help.")

test/dotnet.Tests/CompletionTests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,14 @@ _testhost() {
646646
'*::PACKAGE_NAME -- The package reference to remove.: ' \
647647
&& ret=0
648648
;;
649+
(update)
650+
_arguments "${_arguments_options[@]}" : \
651+
'--project=[Path to a project or solution file, or a directory.]: : ' \
652+
'--help[Show command line help.]' \
653+
'-h[Show command line help.]' \
654+
'*::packages: ' \
655+
&& ret=0
656+
;;
649657
esac
650658
;;
651659
esac
@@ -1640,6 +1648,7 @@ _testhost__package_commands() {
16401648
'add:Add a NuGet package reference to the project.' \
16411649
'list:List all package references of the project or solution.' \
16421650
'remove:Remove a NuGet package reference from the project.' \
1651+
'update:Update referenced packages in a project or solution.' \
16431652
)
16441653
_describe -t commands 'testhost package commands' commands "$@"
16451654
}
@@ -1668,6 +1677,12 @@ _testhost__package__remove_commands() {
16681677
_describe -t commands 'testhost package remove commands' commands "$@"
16691678
}
16701679

1680+
(( $+functions[_testhost__package__update_commands] )) ||
1681+
_testhost__package__update_commands() {
1682+
local commands; commands=()
1683+
_describe -t commands 'testhost package update commands' commands "$@"
1684+
}
1685+
16711686
(( $+functions[_testhost__project_commands] )) ||
16721687
_testhost__project_commands() {
16731688
local commands; commands=(

0 commit comments

Comments
 (0)