diff --git a/src/Assets/TestProjects/TestAppWithLaunchSettings/Program.cs b/src/Assets/TestProjects/TestAppWithLaunchSettings/Program.cs index 6292216dd517..e6eb56ebe15e 100644 --- a/src/Assets/TestProjects/TestAppWithLaunchSettings/Program.cs +++ b/src/Assets/TestProjects/TestAppWithLaunchSettings/Program.cs @@ -11,6 +11,14 @@ public static void Main(string[] args) { Console.WriteLine("Hello world"); Console.WriteLine($"MyCoolEnvironmentVariableKey={Environment.GetEnvironmentVariable("MyCoolEnvironmentVariableKey")}"); + if (args.Length > 0) + { + Console.WriteLine(args[0]); + } + if (args.Length > 1) + { + Console.WriteLine(args[1]); + } } } } diff --git a/src/Assets/TestProjects/TestAppWithLaunchSettings/Properties/launchSettings.json b/src/Assets/TestProjects/TestAppWithLaunchSettings/Properties/launchSettings.json index d9381bec04f0..5ac0ee3cff41 100644 --- a/src/Assets/TestProjects/TestAppWithLaunchSettings/Properties/launchSettings.json +++ b/src/Assets/TestProjects/TestAppWithLaunchSettings/Properties/launchSettings.json @@ -3,6 +3,7 @@ "TestAppWithLaunchSettings": { "commandName": "Project", "dotnetRunMessages": "true", + "commandLineArgs": "TestAppCommandLineArguments SecondTestAppCommandLineArguments", "environmentVariables": { "MyCoolEnvironmentVariableKey": "MyCoolEnvironmentVariableValue" } diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs index ea6af494ae1c..828653881c82 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs +++ b/src/Cli/Microsoft.DotNet.Cli.Utils/BuiltInCommand.cs @@ -183,5 +183,9 @@ public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false, { throw new NotImplementedException(); } + public ICommand SetCommandArgs(string commandArgs) + { + throw new NotImplementedException(); + } } } diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs index 33cb6e93465d..e225d56884b5 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs +++ b/src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs @@ -188,6 +188,12 @@ public ICommand OnErrorLine(Action handler) public string CommandArgs => _process.StartInfo.Arguments; + public ICommand SetCommandArgs(string commandArgs) + { + _process.StartInfo.Arguments = commandArgs; + return this; + } + private string FormatProcessInfo(ProcessStartInfo info) { if (string.IsNullOrWhiteSpace(info.Arguments)) diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/ICommand.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/ICommand.cs index 5529f0567515..f6d453bf2e38 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/ICommand.cs +++ b/src/Cli/Microsoft.DotNet.Cli.Utils/ICommand.cs @@ -26,6 +26,8 @@ public interface ICommand ICommand OnErrorLine(Action handler); + ICommand SetCommandArgs(string commandArgs); + string CommandName { get; } string CommandArgs { get; } diff --git a/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs b/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs index 20e8d1c594ce..342f60ab1b17 100644 --- a/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs +++ b/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs @@ -72,6 +72,10 @@ public int Execute() //NOTE: MSBuild variables are not expanded like they are in VS targetCommand.EnvironmentVariable(entry.Key, value); } + if (String.IsNullOrEmpty(targetCommand.CommandArgs) && launchSettings.CommandLineArgs != null) + { + targetCommand.SetCommandArgs(launchSettings.CommandLineArgs); + } } // Ignore Ctrl-C for the remainder of the command's execution diff --git a/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs b/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs index 1373c5e749a9..053941e9db5f 100644 --- a/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs +++ b/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs @@ -688,5 +688,45 @@ public void ItIncludesEnvironmentVariablesSpecifiedInLaunchSettings() .And .HaveStdOutContaining(expectedValue); } + + [Fact] + public void ItIncludesCommandArgumentsSpecifiedInLaunchSettings() + { + var expectedValue = "TestAppCommandLineArguments"; + var secondExpectedValue = "SecondTestAppCommandLineArguments"; + var testAppName = "TestAppWithLaunchSettings"; + var testInstance = _testAssetsManager.CopyTestAsset(testAppName) + .WithSource(); + + new DotnetCommand(Log, "run") + .WithWorkingDirectory(testInstance.Path) + .Execute() + .Should() + .Pass() + .And + .HaveStdOutContaining(expectedValue) + .And + .HaveStdOutContaining(secondExpectedValue); + } + + [Fact] + public void ItCLIArgsOverrideCommandArgumentsSpecifiedInLaunchSettings() + { + var expectedValue = "TestAppCommandLineArguments"; + var secondExpectedValue = "SecondTestAppCommandLineArguments"; + var testAppName = "TestAppWithLaunchSettings"; + var testInstance = _testAssetsManager.CopyTestAsset(testAppName) + .WithSource(); + + new DotnetCommand(Log, "run", "-- test") + .WithWorkingDirectory(testInstance.Path) + .Execute() + .Should() + .Pass() + .And + .NotHaveStdOutContaining(expectedValue) + .And + .NotHaveStdOutContaining(secondExpectedValue); + } } }