diff --git a/src/System.CommandLine.Suggest.Tests/DotnetSuggestEndToEndTests.cs b/src/System.CommandLine.Suggest.Tests/DotnetSuggestEndToEndTests.cs index d59f69029c..e2dfa6c6dc 100644 --- a/src/System.CommandLine.Suggest.Tests/DotnetSuggestEndToEndTests.cs +++ b/src/System.CommandLine.Suggest.Tests/DotnetSuggestEndToEndTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. +// 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.CommandLine.Invocation; @@ -66,7 +66,7 @@ public void Dispose() } } - private void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome() + private static void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome() { _testRoot = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(_testRoot); @@ -77,7 +77,7 @@ public async Task Test_app_supplies_suggestions() { var stdOut = new StringBuilder(); - await Process.ExecuteAsync( + await ExecuteAsync( _endToEndTestApp.FullName, "[suggest:1] \"a\"", stdOut: value => stdOut.AppendLine(value), @@ -92,7 +92,7 @@ await Process.ExecuteAsync( public async Task Dotnet_suggest_provides_suggestions_for_app() { // run once to trigger a call to dotnet-suggest register - await Process.ExecuteAsync( + await ExecuteAsync( _endToEndTestApp.FullName, "-h", stdOut: s => _output.WriteLine(s), @@ -104,7 +104,7 @@ await Process.ExecuteAsync( var commandLineToComplete = "a"; - await Process.ExecuteAsync( + await ExecuteAsync( _dotnetSuggest.FullName, $"get -e \"{_endToEndTestApp.FullName}\" --position {commandLineToComplete.Length} -- \"{commandLineToComplete}\"", stdOut: value => stdOut.AppendLine(value), @@ -127,7 +127,7 @@ await Process.ExecuteAsync( public async Task Dotnet_suggest_provides_suggestions_for_app_with_only_commandname() { // run once to trigger a call to dotnet-suggest register - await Process.ExecuteAsync( + await ExecuteAsync( _endToEndTestApp.FullName, "-h", stdOut: s => _output.WriteLine(s), @@ -139,7 +139,7 @@ await Process.ExecuteAsync( var commandLineToComplete = "a "; - await Process.ExecuteAsync( + await ExecuteAsync( _dotnetSuggest.FullName, $"get -e \"{_endToEndTestApp.FullName}\" --position {commandLineToComplete.Length} -- \"{commandLineToComplete}\"", stdOut: value => stdOut.AppendLine(value), @@ -157,5 +157,66 @@ await Process.ExecuteAsync( .Should() .Be($"--apple{NewLine}--banana{NewLine}--cherry{NewLine}--durian{NewLine}--help{NewLine}--version{NewLine}-?{NewLine}-h{NewLine}/?{NewLine}/h{NewLine}"); } + + public static async Task ExecuteAsync( + string command, + string args, + Action stdOut = null, + Action stdErr = null, + params (string key, string value)[] environmentVariables) + { + args ??= ""; + + var process = new Diagnostics.Process + { + StartInfo = + { + Arguments = args, + FileName = command, + RedirectStandardError = true, + RedirectStandardOutput = true, + RedirectStandardInput = true, + UseShellExecute = false + } + }; + + if (environmentVariables.Length > 0) + { + for (var i = 0; i < environmentVariables.Length; i++) + { + var (key, value) = environmentVariables[i]; + process.StartInfo.Environment.Add(key, value); + } + } + + if (stdOut != null) + { + process.OutputDataReceived += (sender, eventArgs) => + { + if (eventArgs.Data != null) + { + stdOut(eventArgs.Data); + } + }; + } + + if (stdErr != null) + { + process.ErrorDataReceived += (sender, eventArgs) => + { + if (eventArgs.Data != null) + { + stdErr(eventArgs.Data); + } + }; + } + + process.Start(); + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + return await process.CompleteAsync(); + } } } diff --git a/src/System.CommandLine/Invocation/Process.cs b/src/System.CommandLine/Invocation/Process.cs index 70b509442a..298b1cb757 100644 --- a/src/System.CommandLine/Invocation/Process.cs +++ b/src/System.CommandLine/Invocation/Process.cs @@ -8,24 +8,6 @@ namespace System.CommandLine.Invocation { public static class Process { - public static async Task ExecuteAsync( - string command, - string args, - string? workingDir = null, - Action? stdOut = null, - Action? stdErr = null, - params (string key, string value)[] environmentVariables) - { - var process = StartProcess(command, - args, - workingDir, - stdOut, - stdErr, - environmentVariables); - - return await process.CompleteAsync(); - } - public static async Task CompleteAsync( this Diagnostics.Process process, CancellationToken? cancellationToken = null) => @@ -44,7 +26,7 @@ public static Diagnostics.Process StartProcess( Action? stdErr = null, params (string key, string value)[] environmentVariables) { - args = args ?? ""; + args ??= ""; var process = new Diagnostics.Process { @@ -64,12 +46,12 @@ public static Diagnostics.Process StartProcess( process.StartInfo.WorkingDirectory = workingDir; } - if (environmentVariables?.Length > 0) + if (environmentVariables.Length > 0) { for (var i = 0; i < environmentVariables.Length; i++) { - var tuple = environmentVariables[i]; - process.StartInfo.Environment.Add(tuple.key, tuple.value); + var (key, value) = environmentVariables[i]; + process.StartInfo.Environment.Add(key, value); } }