1
- // Copyright (c) .NET Foundation and contributors. All rights reserved.
1
+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System . CommandLine . Invocation ;
@@ -66,7 +66,7 @@ public void Dispose()
66
66
}
67
67
}
68
68
69
- private void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome ( )
69
+ private static void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome ( )
70
70
{
71
71
_testRoot = Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) ) ;
72
72
Directory . CreateDirectory ( _testRoot ) ;
@@ -77,7 +77,7 @@ public async Task Test_app_supplies_suggestions()
77
77
{
78
78
var stdOut = new StringBuilder ( ) ;
79
79
80
- await Process . ExecuteAsync (
80
+ await ExecuteAsync (
81
81
_endToEndTestApp . FullName ,
82
82
"[suggest:1] \" a\" " ,
83
83
stdOut : value => stdOut . AppendLine ( value ) ,
@@ -92,7 +92,7 @@ await Process.ExecuteAsync(
92
92
public async Task Dotnet_suggest_provides_suggestions_for_app ( )
93
93
{
94
94
// run once to trigger a call to dotnet-suggest register
95
- await Process . ExecuteAsync (
95
+ await ExecuteAsync (
96
96
_endToEndTestApp . FullName ,
97
97
"-h" ,
98
98
stdOut : s => _output . WriteLine ( s ) ,
@@ -104,7 +104,7 @@ await Process.ExecuteAsync(
104
104
105
105
var commandLineToComplete = "a" ;
106
106
107
- await Process . ExecuteAsync (
107
+ await ExecuteAsync (
108
108
_dotnetSuggest . FullName ,
109
109
$ "get -e \" { _endToEndTestApp . FullName } \" --position { commandLineToComplete . Length } -- \" { commandLineToComplete } \" ",
110
110
stdOut : value => stdOut . AppendLine ( value ) ,
@@ -127,7 +127,7 @@ await Process.ExecuteAsync(
127
127
public async Task Dotnet_suggest_provides_suggestions_for_app_with_only_commandname ( )
128
128
{
129
129
// run once to trigger a call to dotnet-suggest register
130
- await Process . ExecuteAsync (
130
+ await ExecuteAsync (
131
131
_endToEndTestApp . FullName ,
132
132
"-h" ,
133
133
stdOut : s => _output . WriteLine ( s ) ,
@@ -139,7 +139,7 @@ await Process.ExecuteAsync(
139
139
140
140
var commandLineToComplete = "a " ;
141
141
142
- await Process . ExecuteAsync (
142
+ await ExecuteAsync (
143
143
_dotnetSuggest . FullName ,
144
144
$ "get -e \" { _endToEndTestApp . FullName } \" --position { commandLineToComplete . Length } -- \" { commandLineToComplete } \" ",
145
145
stdOut : value => stdOut . AppendLine ( value ) ,
@@ -157,5 +157,66 @@ await Process.ExecuteAsync(
157
157
. Should ( )
158
158
. Be ( $ "--apple{ NewLine } --banana{ NewLine } --cherry{ NewLine } --durian{ NewLine } --help{ NewLine } --version{ NewLine } -?{ NewLine } -h{ NewLine } /?{ NewLine } /h{ NewLine } ") ;
159
159
}
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
+ }
160
221
}
161
222
}
0 commit comments