Skip to content

Commit 4e00994

Browse files
Keboojonsequitur
authored andcommitted
Make System.CommandLine.Invocation.Process internal
Fixes #1213
1 parent 0c7431f commit 4e00994

File tree

2 files changed

+72
-29
lines changed

2 files changed

+72
-29
lines changed

src/System.CommandLine.Suggest.Tests/DotnetSuggestEndToEndTests.cs

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

44
using 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
}

src/System.CommandLine/Invocation/Process.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,6 @@ namespace System.CommandLine.Invocation
88
{
99
public static class Process
1010
{
11-
public static async Task<int> ExecuteAsync(
12-
string command,
13-
string args,
14-
string? workingDir = null,
15-
Action<string>? stdOut = null,
16-
Action<string>? stdErr = null,
17-
params (string key, string value)[] environmentVariables)
18-
{
19-
var process = StartProcess(command,
20-
args,
21-
workingDir,
22-
stdOut,
23-
stdErr,
24-
environmentVariables);
25-
26-
return await process.CompleteAsync();
27-
}
28-
2911
public static async Task<int> CompleteAsync(
3012
this Diagnostics.Process process,
3113
CancellationToken? cancellationToken = null) =>
@@ -44,7 +26,7 @@ public static Diagnostics.Process StartProcess(
4426
Action<string>? stdErr = null,
4527
params (string key, string value)[] environmentVariables)
4628
{
47-
args = args ?? "";
29+
args ??= "";
4830

4931
var process = new Diagnostics.Process
5032
{
@@ -64,12 +46,12 @@ public static Diagnostics.Process StartProcess(
6446
process.StartInfo.WorkingDirectory = workingDir;
6547
}
6648

67-
if (environmentVariables?.Length > 0)
49+
if (environmentVariables.Length > 0)
6850
{
6951
for (var i = 0; i < environmentVariables.Length; i++)
7052
{
71-
var tuple = environmentVariables[i];
72-
process.StartInfo.Environment.Add(tuple.key, tuple.value);
53+
var (key, value) = environmentVariables[i];
54+
process.StartInfo.Environment.Add(key, value);
7355
}
7456
}
7557

0 commit comments

Comments
 (0)