From 0815b0d039b641ae1546a2ceedfdd7a9577b6f34 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Fri, 4 Feb 2022 17:11:10 -0800 Subject: [PATCH 1/2] Enable command arguments from launch profiles Fixes #23565 --- .../TestAppWithLaunchSettings/Program.cs | 8 ++++++++ .../Properties/launchSettings.json | 1 + .../BuiltInCommand.cs | 4 ++++ src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs | 6 ++++++ .../Microsoft.DotNet.Cli.Utils/ICommand.cs | 2 ++ .../dotnet/commands/dotnet-run/RunCommand.cs | 4 ++++ .../GivenDotnetRunRunsCsProj.cs | 20 +++++++++++++++++++ 7 files changed, 45 insertions(+) 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..f837df9cfc76 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 (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..21198c02aa9b 100644 --- a/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs +++ b/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs @@ -688,5 +688,25 @@ public void ItIncludesEnvironmentVariablesSpecifiedInLaunchSettings() .And .HaveStdOutContaining(expectedValue); } + + [Fact] + public void ItIncludesCommandArgumentSpecifiedInLaunchSettings() + { + 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); + } } } From 56f066b2a31bc556806ad368c0259bb92fe10e46 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Tue, 8 Feb 2022 15:32:51 -0800 Subject: [PATCH 2/2] Change the behavior to have CLI arguments override the launch profile --- .../dotnet/commands/dotnet-run/RunCommand.cs | 2 +- .../GivenDotnetRunRunsCsProj.cs | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs b/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs index f837df9cfc76..342f60ab1b17 100644 --- a/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs +++ b/src/Cli/dotnet/commands/dotnet-run/RunCommand.cs @@ -72,7 +72,7 @@ public int Execute() //NOTE: MSBuild variables are not expanded like they are in VS targetCommand.EnvironmentVariable(entry.Key, value); } - if (launchSettings.CommandLineArgs != null) + if (String.IsNullOrEmpty(targetCommand.CommandArgs) && launchSettings.CommandLineArgs != null) { targetCommand.SetCommandArgs(launchSettings.CommandLineArgs); } diff --git a/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs b/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs index 21198c02aa9b..053941e9db5f 100644 --- a/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs +++ b/src/Tests/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs @@ -690,7 +690,7 @@ public void ItIncludesEnvironmentVariablesSpecifiedInLaunchSettings() } [Fact] - public void ItIncludesCommandArgumentSpecifiedInLaunchSettings() + public void ItIncludesCommandArgumentsSpecifiedInLaunchSettings() { var expectedValue = "TestAppCommandLineArguments"; var secondExpectedValue = "SecondTestAppCommandLineArguments"; @@ -708,5 +708,25 @@ public void ItIncludesCommandArgumentSpecifiedInLaunchSettings() .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); + } } }