From d07a1091a8e4ede2b9609de84b1d2a7029c23622 Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Fri, 25 Mar 2016 20:17:15 -0700 Subject: [PATCH] Added a Help property to DotnetTestParams that indicates when the parsed arguments included the help param. The TestCommand then uses this property of the params to decide whether to continue with the execution or just return. Also added unit tests around this as well as introduced a DotnetTestRunnerFactory to make these unit tests possible and also push the responsibility to figure out which runner to use to the factory. --- .../commands/dotnet-test/DotnetTestParams.cs | 8 ++- .../dotnet-test/DotnetTestRunnerFactory.cs | 19 ++++++ .../dotnet-test/IDotnetTestRunnerFactory.cs | 10 +++ src/dotnet/commands/dotnet-test/Program.cs | 27 ++++++-- .../GivenATestCommand.cs | 68 +++++++++++++++++++ ...ThatWeWantToParseArgumentsForDotnetTest.cs | 15 ++++ test/dotnet-test.UnitTests/project.json | 3 + 7 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 src/dotnet/commands/dotnet-test/DotnetTestRunnerFactory.cs create mode 100644 src/dotnet/commands/dotnet-test/IDotnetTestRunnerFactory.cs create mode 100644 test/dotnet-test.UnitTests/GivenATestCommand.cs diff --git a/src/dotnet/commands/dotnet-test/DotnetTestParams.cs b/src/dotnet/commands/dotnet-test/DotnetTestParams.cs index 25dee31ec..092ce3e45 100644 --- a/src/dotnet/commands/dotnet-test/DotnetTestParams.cs +++ b/src/dotnet/commands/dotnet-test/DotnetTestParams.cs @@ -47,6 +47,8 @@ namespace Microsoft.DotNet.Tools.Test public bool NoBuild { get; set; } + public bool Help { get; set; } + public DotnetTestParams() { _app = new CommandLineApplication(false) @@ -57,6 +59,8 @@ namespace Microsoft.DotNet.Tools.Test }; AddDotnetTestParameters(); + + Help = true; } public void Parse(string[] args) @@ -108,6 +112,8 @@ namespace Microsoft.DotNet.Tools.Test RemainingArguments = _app.RemainingArguments; + Help = false; + return 0; }); @@ -150,7 +156,7 @@ namespace Microsoft.DotNet.Tools.Test _app.Option("--no-build", "Do not build project before testing", CommandOptionType.NoValue); _projectPath = _app.Argument( "", - "The project to test, defaults to the current directory. Can be a path to a project.json or a project directory."); + "The project to test, defaults to the current directory. Can be a path to a project.json or a project directory."); } } } diff --git a/src/dotnet/commands/dotnet-test/DotnetTestRunnerFactory.cs b/src/dotnet/commands/dotnet-test/DotnetTestRunnerFactory.cs new file mode 100644 index 000000000..32ef694ec --- /dev/null +++ b/src/dotnet/commands/dotnet-test/DotnetTestRunnerFactory.cs @@ -0,0 +1,19 @@ +// 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. + +namespace Microsoft.DotNet.Tools.Test +{ + public class DotnetTestRunnerFactory : IDotnetTestRunnerFactory + { + public IDotnetTestRunner Create(int? port) + { + IDotnetTestRunner dotnetTestRunner = new ConsoleTestRunner(); + if (port.HasValue) + { + dotnetTestRunner = new DesignTimeRunner(); + } + + return dotnetTestRunner; + } + } +} diff --git a/src/dotnet/commands/dotnet-test/IDotnetTestRunnerFactory.cs b/src/dotnet/commands/dotnet-test/IDotnetTestRunnerFactory.cs new file mode 100644 index 000000000..8dfc9599f --- /dev/null +++ b/src/dotnet/commands/dotnet-test/IDotnetTestRunnerFactory.cs @@ -0,0 +1,10 @@ +// 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. + +namespace Microsoft.DotNet.Tools.Test +{ + public interface IDotnetTestRunnerFactory + { + IDotnetTestRunner Create(int? port); + } +} diff --git a/src/dotnet/commands/dotnet-test/Program.cs b/src/dotnet/commands/dotnet-test/Program.cs index 46dda477f..d89fd6e9c 100644 --- a/src/dotnet/commands/dotnet-test/Program.cs +++ b/src/dotnet/commands/dotnet-test/Program.cs @@ -14,7 +14,14 @@ namespace Microsoft.DotNet.Tools.Test { public class TestCommand { - public static int Run(string[] args) + private readonly IDotnetTestRunnerFactory _dotnetTestRunnerFactory; + + public TestCommand(IDotnetTestRunnerFactory testRunnerFactory) + { + _dotnetTestRunnerFactory = testRunnerFactory; + } + + public int DoRun(string[] args) { DebugHelper.HandleDebugSwitch(ref args); @@ -24,6 +31,11 @@ namespace Microsoft.DotNet.Tools.Test try { + if (dotnetTestParams.Help) + { + return 0; + } + // Register for parent process's exit event if (dotnetTestParams.ParentProcessId.HasValue) { @@ -36,11 +48,7 @@ namespace Microsoft.DotNet.Tools.Test var testRunner = projectContext.ProjectFile.TestRunner; - IDotnetTestRunner dotnetTestRunner = new ConsoleTestRunner(); - if (dotnetTestParams.Port.HasValue) - { - dotnetTestRunner = new DesignTimeRunner(); - } + IDotnetTestRunner dotnetTestRunner = _dotnetTestRunnerFactory.Create(dotnetTestParams.Port); return dotnetTestRunner.RunTests(projectContext, dotnetTestParams); } @@ -56,6 +64,13 @@ namespace Microsoft.DotNet.Tools.Test } } + public static int Run(string[] args) + { + var testCommand = new TestCommand(new DotnetTestRunnerFactory()); + + return testCommand.DoRun(args); + } + private static void RegisterForParentProcessExit(int id) { var parentProcess = Process.GetProcesses().FirstOrDefault(p => p.Id == id); diff --git a/test/dotnet-test.UnitTests/GivenATestCommand.cs b/test/dotnet-test.UnitTests/GivenATestCommand.cs new file mode 100644 index 000000000..2d0e5c405 --- /dev/null +++ b/test/dotnet-test.UnitTests/GivenATestCommand.cs @@ -0,0 +1,68 @@ +// 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 FluentAssertions; +using Microsoft.DotNet.Tools.Test; +using Moq; +using Xunit; +using Microsoft.DotNet.ProjectModel; + +namespace Microsoft.Dotnet.Tools.Test.Tests +{ + public class GivenATestCommand + { + private static readonly string ProjectJsonPath = Path.Combine( + AppContext.BaseDirectory, + "TestAssets", + "TestProjects", + "ProjectWithTests", + "project.json"); + + private TestCommand _testCommand; + private Mock _dotnetTestRunnerFactoryMock; + private Mock _dotnetTestRunnerMock; + + public GivenATestCommand() + { + _dotnetTestRunnerMock = new Mock(); + _dotnetTestRunnerMock + .Setup(d => d.RunTests(It.IsAny(), It.IsAny())) + .Returns(0); + + _dotnetTestRunnerFactoryMock = new Mock(); + _dotnetTestRunnerFactoryMock.Setup(d => d.Create(null)).Returns(_dotnetTestRunnerMock.Object); + + _testCommand = new TestCommand(_dotnetTestRunnerFactoryMock.Object); + } + + [Fact] + public void It_does_not_create_a_runner_if_the_args_include_help() + { + var result = _testCommand.DoRun(new[] {"--help"}); + + result.Should().Be(0); + _dotnetTestRunnerFactoryMock.Verify(d => d.Create(It.IsAny()), Times.Never); + } + + [Fact] + public void It_creates_a_runner_if_the_args_do_no_include_help() + { + var result = _testCommand.DoRun(new[] { ProjectJsonPath }); + + result.Should().Be(0); + _dotnetTestRunnerFactoryMock.Verify(d => d.Create(It.IsAny()), Times.Once); + } + + [Fact] + public void It_runs_the_tests_through_the_DotnetTestRunner() + { + var result = _testCommand.DoRun(new[] { ProjectJsonPath }); + + _dotnetTestRunnerMock.Verify( + d => d.RunTests(It.IsAny(), It.IsAny()), + Times.Once); + } + } +} diff --git a/test/dotnet-test.UnitTests/GivenThatWeWantToParseArgumentsForDotnetTest.cs b/test/dotnet-test.UnitTests/GivenThatWeWantToParseArgumentsForDotnetTest.cs index bac983f04..202ee4ea3 100644 --- a/test/dotnet-test.UnitTests/GivenThatWeWantToParseArgumentsForDotnetTest.cs +++ b/test/dotnet-test.UnitTests/GivenThatWeWantToParseArgumentsForDotnetTest.cs @@ -192,5 +192,20 @@ namespace Microsoft.Dotnet.Tools.Test.Tests { _emptyDotnetTestParams.NoBuild.Should().BeFalse(); } + + [Fact] + public void It_sets_Help_to_false_when_help_is_not_passed_in() + { + _dotnetTestFullParams.Help.Should().BeFalse(); + } + + [Fact] + public void It_sets_Help_to_true_when_help_is_passed_in() + { + var dotnetTestParams = new DotnetTestParams(); + dotnetTestParams.Parse(new[] { "--help" }); + + dotnetTestParams.Help.Should().BeTrue(); + } } } diff --git a/test/dotnet-test.UnitTests/project.json b/test/dotnet-test.UnitTests/project.json index 34fa3a683..b593502d0 100644 --- a/test/dotnet-test.UnitTests/project.json +++ b/test/dotnet-test.UnitTests/project.json @@ -23,5 +23,8 @@ ] } }, + "content": [ + "../../TestAssets/TestProjects/ProjectWithTests/project.json" + ], "testRunner": "xunit" }