Skip to content

Commit

Permalink
Add why and config commands to dotnet nuget help (#5931)
Browse files Browse the repository at this point in the history
* Add commands Why and Config to dotnet nuget help
  • Loading branch information
martinrrm committed Jul 26, 2024
1 parent 46f80a3 commit 5485ea6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.CommandLine.Parsing;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.CommandLineUtils;
using NuGet.Common;

namespace NuGet.CommandLine.XPlat
Expand Down Expand Up @@ -84,6 +85,14 @@ internal static void ShowHelp(ParseResult parseResult, CliCommand cmd)
cmd.Parse(tokenList).Invoke();
}

internal static void Register(CommandLineApplication app)
{
app.Command("config", configCmd =>
{
configCmd.Description = Strings.Config_Description;
});
}

internal static CliCommand Register(CliCommand app, Func<ILogger> getLogger)
{
var ConfigCmd = new CliCommand(name: "config", description: Strings.Config_Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Help;
using Microsoft.Extensions.CommandLineUtils;

namespace NuGet.CommandLine.XPlat
{
Expand Down Expand Up @@ -33,6 +34,14 @@ internal static class WhyCommand
Arity = ArgumentArity.Zero
};

internal static void Register(CommandLineApplication app)
{
app.Command("why", whyCmd =>
{
whyCmd.Description = Strings.WhyCommand_Description;
});
}

internal static void Register(CliCommand rootCommand, Func<ILoggerWithColor> getLogger)
{
var whyCommand = new CliCommand("why", Strings.WhyCommand_Description);
Expand Down
4 changes: 3 additions & 1 deletion src/NuGet.Core/NuGet.CommandLine.XPlat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.CommandLineUtils;
using NuGet.Commands;
using NuGet.Common;
Expand Down Expand Up @@ -276,6 +275,9 @@ private static CommandLineApplication InitializeApp(string[] args, CommandOutput
VerifyCommand.Register(app, getHidePrefixLogger, setLogLevel, () => new VerifyCommandRunner());
TrustedSignersCommand.Register(app, getHidePrefixLogger, setLogLevel);
SignCommand.Register(app, getHidePrefixLogger, setLogLevel, () => new SignCommandRunner());
// The commands below are implemented with System.CommandLine, and are here only for `dotnet nuget --help`
ConfigCommand.Register(app);
WhyCommand.Register(app);
}

app.FullName = Strings.App_FullName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NuGet.CommandLine.XPlat;
using Xunit;
using Xunit.Abstractions;
using System.Text.RegularExpressions;

namespace NuGet.XPlat.FuncTest
{
Expand Down Expand Up @@ -43,6 +44,24 @@ public static IEnumerable<object[]> FailParsing
}
}

public static IEnumerable<string> HelpCommands => new List<string>
{
"add",
"config",
"delete",
"disable",
"enable",
"list",
"locals",
"push",
"remove",
"sign",
"trust",
"update",
"verify",
"why"
};

[Theory]
[MemberData(nameof(FailParsing))]
public void MainInternal_OnParsingError_ShowsHelp(string[] args)
Expand Down Expand Up @@ -82,5 +101,28 @@ public void MainInternal_OnParsingSuccess_DoesNotShowsHelp(string[] args)
Assert.DoesNotContain("Usage", output);
Assert.Equal(1, exitCode);
}

[Fact]
public void MainInternal_ShowsHelp()
{
// Arrange
var originalConsoleOut = Console.Out;
using var consoleOutput = new StringWriter();
Console.SetOut(consoleOutput);
var log = new TestCommandOutputLogger(_testOutputHelper);

// Act
var exitCode = Program.MainInternal(Array.Empty<string>(), log);
Console.SetOut(originalConsoleOut);

// Assert
var output = consoleOutput.ToString();

var commandPattern = @"^\s{2}(\w+)\s{2,}"; // Matches lines starting with two spaces, a word (command), followed by at least two spaces
IEnumerable<string> matches = Regex.Matches(output, commandPattern, RegexOptions.Multiline).Select(m => m.ToString().Trim());

Assert.Equal(HelpCommands, matches);
Assert.Equal(0, exitCode);
}
}
}

0 comments on commit 5485ea6

Please sign in to comment.