1- // Copyright (c) .NET Foundation and contributors. All rights reserved.
1+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
44using System . CommandLine . Invocation ;
@@ -66,7 +66,7 @@ public void Dispose()
6666 }
6767 }
6868
69- private void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome ( )
69+ private static void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome ( )
7070 {
7171 _testRoot = Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) ) ;
7272 Directory . CreateDirectory ( _testRoot ) ;
@@ -77,7 +77,7 @@ public async Task Test_app_supplies_suggestions()
7777 {
7878 var stdOut = new StringBuilder ( ) ;
7979
80- await Process . ExecuteAsync (
80+ await ExecuteAsync (
8181 _endToEndTestApp . FullName ,
8282 "[suggest:1] \" a\" " ,
8383 stdOut : value => stdOut . AppendLine ( value ) ,
@@ -92,7 +92,7 @@ await Process.ExecuteAsync(
9292 public async Task Dotnet_suggest_provides_suggestions_for_app ( )
9393 {
9494 // run once to trigger a call to dotnet-suggest register
95- await Process . ExecuteAsync (
95+ await ExecuteAsync (
9696 _endToEndTestApp . FullName ,
9797 "-h" ,
9898 stdOut : s => _output . WriteLine ( s ) ,
@@ -104,7 +104,7 @@ await Process.ExecuteAsync(
104104
105105 var commandLineToComplete = "a" ;
106106
107- await Process . ExecuteAsync (
107+ await ExecuteAsync (
108108 _dotnetSuggest . FullName ,
109109 $ "get -e \" { _endToEndTestApp . FullName } \" --position { commandLineToComplete . Length } -- \" { commandLineToComplete } \" ",
110110 stdOut : value => stdOut . AppendLine ( value ) ,
@@ -127,7 +127,7 @@ await Process.ExecuteAsync(
127127 public async Task Dotnet_suggest_provides_suggestions_for_app_with_only_commandname ( )
128128 {
129129 // run once to trigger a call to dotnet-suggest register
130- await Process . ExecuteAsync (
130+ await ExecuteAsync (
131131 _endToEndTestApp . FullName ,
132132 "-h" ,
133133 stdOut : s => _output . WriteLine ( s ) ,
@@ -139,7 +139,7 @@ await Process.ExecuteAsync(
139139
140140 var commandLineToComplete = "a " ;
141141
142- await Process . ExecuteAsync (
142+ await ExecuteAsync (
143143 _dotnetSuggest . FullName ,
144144 $ "get -e \" { _endToEndTestApp . FullName } \" --position { commandLineToComplete . Length } -- \" { commandLineToComplete } \" ",
145145 stdOut : value => stdOut . AppendLine ( value ) ,
@@ -157,5 +157,66 @@ await Process.ExecuteAsync(
157157 . Should ( )
158158 . Be ( $ "--apple{ NewLine } --banana{ NewLine } --cherry{ NewLine } --durian{ NewLine } --help{ NewLine } --version{ NewLine } -?{ NewLine } -h{ NewLine } /?{ NewLine } /h{ NewLine } ") ;
159159 }
160+
161+ public static async Task < int > ExecuteAsync (
162+ string command ,
163+ string args ,
164+ Action < string > stdOut = null ,
165+ Action < string > stdErr = null ,
166+ params ( string key , string value ) [ ] environmentVariables )
167+ {
168+ args ??= "" ;
169+
170+ var process = new Diagnostics . Process
171+ {
172+ StartInfo =
173+ {
174+ Arguments = args ,
175+ FileName = command ,
176+ RedirectStandardError = true ,
177+ RedirectStandardOutput = true ,
178+ RedirectStandardInput = true ,
179+ UseShellExecute = false
180+ }
181+ } ;
182+
183+ if ( environmentVariables . Length > 0 )
184+ {
185+ for ( var i = 0 ; i < environmentVariables . Length ; i ++ )
186+ {
187+ var ( key , value ) = environmentVariables [ i ] ;
188+ process . StartInfo . Environment . Add ( key , value ) ;
189+ }
190+ }
191+
192+ if ( stdOut != null )
193+ {
194+ process . OutputDataReceived += ( sender , eventArgs ) =>
195+ {
196+ if ( eventArgs . Data != null )
197+ {
198+ stdOut ( eventArgs . Data ) ;
199+ }
200+ } ;
201+ }
202+
203+ if ( stdErr != null )
204+ {
205+ process . ErrorDataReceived += ( sender , eventArgs ) =>
206+ {
207+ if ( eventArgs . Data != null )
208+ {
209+ stdErr ( eventArgs . Data ) ;
210+ }
211+ } ;
212+ }
213+
214+ process . Start ( ) ;
215+
216+ process . BeginOutputReadLine ( ) ;
217+ process . BeginErrorReadLine ( ) ;
218+
219+ return await process . CompleteAsync ( ) ;
220+ }
160221 }
161222}
0 commit comments