From 5d2da26e745d23417e0e8b9c26e4151d639a3a18 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Fri, 22 Apr 2016 23:40:39 -0500 Subject: [PATCH] Adding dotnet run tests for passing arguments to the app. --- Microsoft.DotNet.Cli.sln | 19 ++++++ .../Commands/RunCommand.cs | 20 ++++-- test/dotnet-run.Tests/RunTests.cs | 65 ++++++++++++++++--- 3 files changed, 90 insertions(+), 14 deletions(-) diff --git a/Microsoft.DotNet.Cli.sln b/Microsoft.DotNet.Cli.sln index 8d17ab89e..1fed5abc3 100644 --- a/Microsoft.DotNet.Cli.sln +++ b/Microsoft.DotNet.Cli.sln @@ -103,6 +103,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-dependency-tool-invo EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.Compiler.Common.Tests", "test\Microsoft.DotNet.Compiler.Common.Tests\Microsoft.DotNet.Compiler.Common.Tests.xproj", "{44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-run.Tests", "test\dotnet-run.Tests\dotnet-run.Tests.xproj", "{35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}" +EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Performance", "test\Performance\Performance.xproj", "{6A3095FF-A7C5-4300-85A9-C025C384401D}" EndProject Global @@ -725,6 +727,22 @@ Global {44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU {44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU {44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5}.RelWithDebInfo|x64.Build.0 = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Debug|x64.ActiveCfg = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Debug|x64.Build.0 = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Release|Any CPU.Build.0 = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Release|x64.ActiveCfg = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Release|x64.Build.0 = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.RelWithDebInfo|x64.Build.0 = Release|Any CPU {6A3095FF-A7C5-4300-85A9-C025C384401D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {6A3095FF-A7C5-4300-85A9-C025C384401D}.Debug|Any CPU.Build.0 = Debug|Any CPU {6A3095FF-A7C5-4300-85A9-C025C384401D}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -789,6 +807,7 @@ Global {1AB5B24B-B317-4142-A5D1-A6E84F15BA34} = {ADA7052B-884B-4776-8B8D-D04191D0AA70} {C26A48BB-193F-450C-AB09-4D3324C78188} = {1AB5B24B-B317-4142-A5D1-A6E84F15BA34} {44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} {6A3095FF-A7C5-4300-85A9-C025C384401D} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} EndGlobalSection EndGlobal diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs index 7319e416e..6b6d2f094 100644 --- a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs @@ -1,8 +1,9 @@ // 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 Microsoft.DotNet.Cli.Utils; +using System.Linq; using System.Threading.Tasks; +using Microsoft.DotNet.Cli.Utils; namespace Microsoft.DotNet.Tools.Test.Utilities { @@ -20,22 +21,18 @@ namespace Microsoft.DotNet.Tools.Test.Utilities private string ConfigurationOption => string.IsNullOrEmpty(_configuration) ? "" : $"-c {_configuration}"; - private string PreserveTemporaryOption => _preserveTemporary ? $"-t \"{_projectPath}\"" : ""; - private string AppArgsArgument => _appArgs; public RunCommand( string projectPath, string framework = "", string configuration = "", - bool preserveTemporary = false, string appArgs = "") : base("dotnet") { _projectPath = projectPath; _framework = framework; _configuration = configuration; - _preserveTemporary = preserveTemporary; _appArgs = appArgs; } @@ -57,6 +54,17 @@ namespace Microsoft.DotNet.Tools.Test.Utilities return base.ExecuteAsync(args); } - private string BuildArgs() => $"{ProjectPathOption} {FrameworkOption} {ConfigurationOption} {PreserveTemporaryOption} {AppArgsArgument}"; + private string BuildArgs() + { + return string.Join(" ", + new[] + { + ProjectPathOption, + FrameworkOption, + ConfigurationOption, + AppArgsArgument, + } + .Where(s => !string.IsNullOrEmpty(s))); + } } } diff --git a/test/dotnet-run.Tests/RunTests.cs b/test/dotnet-run.Tests/RunTests.cs index 41de0dd2e..4cb26bc11 100644 --- a/test/dotnet-run.Tests/RunTests.cs +++ b/test/dotnet-run.Tests/RunTests.cs @@ -82,18 +82,67 @@ namespace Microsoft.DotNet.Tools.Run.Tests .HaveStdOutContaining("Hélló Wórld!"); } - private void CopyProjectToTempDir(string projectDir, TempDirectory tempDir) + [Fact] + public void ItPassesArgumentsToTheApp() { - // copy all the files to temp dir - foreach (var file in Directory.EnumerateFiles(projectDir)) - { - tempDir.CopyFile(file); - } + TestInstance instance = TestAssetsManager.CreateTestInstance("TestAppWithArgs") + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(instance.TestRoot) + .ExecuteWithCapturedOutput("one --two -three") + .Should() + .Pass() + .And + .HaveStdOutContaining( + JoinWithNewlines( + "Hello World!", + "I was passed 3 args:", + "arg: [one]", + "arg: [--two]", + "arg: [-three]")); } - private string GetProjectPath(TempDirectory projectDir) + [Fact] + public void ItPassesAllArgsAfterUnexpectedArg() { - return Path.Combine(projectDir.Path, "project.json"); + TestInstance instance = TestAssetsManager.CreateTestInstance("TestAppWithArgs") + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(instance.TestRoot) + .ExecuteWithCapturedOutput("Hello -f") + .Should() + .Pass() + .And + .HaveStdOutContaining( + JoinWithNewlines( + "Hello World!", + "I was passed 2 args:", + "arg: [Hello]", + "arg: [-f]")); + } + + [Fact] + public void ItHandlesArgSeparatorCorrectly() + { + TestInstance instance = TestAssetsManager.CreateTestInstance("TestAppWithArgs") + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(instance.TestRoot) + .ExecuteWithCapturedOutput("-- one two") + .Should() + .Pass() + .And + .HaveStdOutContaining( + JoinWithNewlines( + "Hello World!", + "I was passed 2 args:", + "arg: [one]", + "arg: [two]")); + } + + private static string JoinWithNewlines(params string[] values) + { + return string.Join(Environment.NewLine, values); } } }