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" }