From b24e9f4cec893f8068384975c30dfe56d179a451 Mon Sep 17 00:00:00 2001 From: Peter Huene Date: Wed, 16 May 2018 14:29:53 -0700 Subject: [PATCH] Add unit tests for `dotnet complete`. This commit adds a few simple unit tests to cover the `dotnet complete` command. It only checks the top-level output, integration with the `new` command from the templating engine, and the custom `nuget` command parser that is solely intended for use with `dotnet complete`. --- .../dotnet-complete/CompleteCommand.cs | 12 +- .../CommandTests/CompleteCommandTests.cs | 194 ++++++++++++++++++ 2 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 test/dotnet.Tests/CommandTests/CompleteCommandTests.cs diff --git a/src/dotnet/commands/dotnet-complete/CompleteCommand.cs b/src/dotnet/commands/dotnet-complete/CompleteCommand.cs index a6061d37d..e3571f13b 100644 --- a/src/dotnet/commands/dotnet-complete/CompleteCommand.cs +++ b/src/dotnet/commands/dotnet-complete/CompleteCommand.cs @@ -12,6 +12,16 @@ namespace Microsoft.DotNet.Cli { public static int Run(string[] args) { + return RunWithReporter(args, Reporter.Output); + } + + public static int RunWithReporter(string [] args, IReporter reporter) + { + if (reporter == null) + { + throw new ArgumentNullException(nameof(reporter)); + } + try { DebugHelper.HandleDebugSwitch(ref args); @@ -28,7 +38,7 @@ namespace Microsoft.DotNet.Cli foreach (var suggestion in suggestions) { - Console.WriteLine(suggestion); + reporter.WriteLine(suggestion); } } catch (Exception) diff --git a/test/dotnet.Tests/CommandTests/CompleteCommandTests.cs b/test/dotnet.Tests/CommandTests/CompleteCommandTests.cs new file mode 100644 index 000000000..c9323ea45 --- /dev/null +++ b/test/dotnet.Tests/CommandTests/CompleteCommandTests.cs @@ -0,0 +1,194 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.IO; +using System.Linq; +using FluentAssertions; +using Microsoft.DotNet.Cli; +using Microsoft.DotNet.Tools.Test.Utilities; +using Xunit; + +namespace Microsoft.DotNet.Tests.Commands +{ + public class CompleteCommandTests : TestBase + { + [Fact] + public void GivenOnlyDotnetItSuggestsTopLevelCommandsAndOptions() + { + var expected = new string[] { + "--diagnostics", + "--help", + "--info", + "--list-runtimes", + "--list-sdks", + "--version", + "-d", + "-h", + "add", + "build", + "build-server", + "clean", + "help", + "list", + "migrate", + "msbuild", + "new", + "nuget", + "pack", + "publish", + "remove", + "restore", + "run", + "sln", + "store", + "test", + "tool", + "vstest" + }; + + var reporter = new BufferedReporter(); + CompleteCommand.RunWithReporter(new[] { "dotnet " }, reporter).Should().Be(0); + reporter.Lines.Should().Equal(expected.OrderBy(c => c)); + } + + [Fact] + public void GivenASlashItSuggestsTopLevelOptions() + { + var expected = new string[] { + "--diagnostics", + "--help", + "--info", + "--list-runtimes", + "--list-sdks", + "--version", + "-d", + "-h", + "build-server" // This should be removed when completion is based on "starts with" rather than "contains". + // See https://github.com/dotnet/cli/issues/8958. + }; + + var reporter = new BufferedReporter(); + CompleteCommand.RunWithReporter(new[] { "dotnet -" }, reporter).Should().Be(0); + reporter.Lines.Should().Equal(expected.OrderBy(c => c)); + } + + [Fact] + public void GivenNewCommandItDisplaysCompletions() + { + var expected = new string[] { + "--force", + "--help", + "--install", + "--language", + "--list", + "--name", + "--nuget-source", + "--output", + "--type", + "--uninstall", + "-h", + "-i", + "-l", + "-lang", + "-n", + "-o", + "-u" + }; + + var reporter = new BufferedReporter(); + CompleteCommand.RunWithReporter(new[] { "dotnet new " }, reporter).Should().Be(0); + reporter.Lines.Should().Equal(expected.OrderBy(c => c)); + } + + [Fact] + public void GivenNuGetCommandItDisplaysCompletions() + { + var expected = new string[] { + "--help", + "--verbosity", + "--version", + "-h", + "-v", + "delete", + "locals", + "push", + }; + + var reporter = new BufferedReporter(); + CompleteCommand.RunWithReporter(new[] { "dotnet nuget " }, reporter).Should().Be(0); + reporter.Lines.Should().Equal(expected.OrderBy(c => c)); + } + + [Fact] + public void GivenNuGetDeleteCommandItDisplaysCompletions() + { + var expected = new string[] { + "--api-key", + "--force-english-output", + "--help", + "--no-service-endpoint", + "--non-interactive", + "--source", + "-h", + "-k", + "-s" + }; + + var reporter = new BufferedReporter(); + CompleteCommand.RunWithReporter(new[] { "dotnet nuget delete " }, reporter).Should().Be(0); + reporter.Lines.Should().Equal(expected.OrderBy(c => c)); + } + + [Fact] + public void GivenNuGetLocalsCommandItDisplaysCompletions() + { + var expected = new string[] { + "--clear", + "--force-english-output", + "--help", + "--list", + "-c", + "-h", + "-l", + "all", + "global-packages", + "http-cache", + "temp" + }; + + var reporter = new BufferedReporter(); + CompleteCommand.RunWithReporter(new[] { "dotnet nuget locals " }, reporter).Should().Be(0); + reporter.Lines.Should().Equal(expected.OrderBy(c => c)); + } + + [Fact] + public void GivenNuGetPushCommandItDisplaysCompletions() + { + var expected = new string[] { + "--api-key", + "--disable-buffering", + "--force-english-output", + "--help", + "--no-service-endpoint", + "--no-symbols", + "--source", + "--symbol-api-key", + "--symbol-source", + "--timeout", + "-d", + "-h", + "-k", + "-n", + "-s", + "-sk", + "-ss", + "-t" + }; + + var reporter = new BufferedReporter(); + CompleteCommand.RunWithReporter(new[] { "dotnet nuget push " }, reporter).Should().Be(0); + reporter.Lines.Should().Equal(expected.OrderBy(c => c)); + } + } +}