Skip to content

Commit

Permalink
Make System.CommandLine.Invocation.Process internal
Browse files Browse the repository at this point in the history
Fixes #1213
  • Loading branch information
Keboo authored and jonsequitur committed Apr 15, 2021
1 parent 0c7431f commit 4e00994
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 29 deletions.
75 changes: 68 additions & 7 deletions src/System.CommandLine.Suggest.Tests/DotnetSuggestEndToEndTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void Dispose()
}
}

private void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome()
private static void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome()
{
_testRoot = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(_testRoot);
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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<int> ExecuteAsync(
string command,
string args,
Action<string> stdOut = null,
Action<string> 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();
}
}
}
26 changes: 4 additions & 22 deletions src/System.CommandLine/Invocation/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,6 @@ namespace System.CommandLine.Invocation
{
public static class Process
{
public static async Task<int> ExecuteAsync(
string command,
string args,
string? workingDir = null,
Action<string>? stdOut = null,
Action<string>? 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<int> CompleteAsync(
this Diagnostics.Process process,
CancellationToken? cancellationToken = null) =>
Expand All @@ -44,7 +26,7 @@ public static Diagnostics.Process StartProcess(
Action<string>? stdErr = null,
params (string key, string value)[] environmentVariables)
{
args = args ?? "";
args ??= "";

var process = new Diagnostics.Process
{
Expand All @@ -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);
}
}

Expand Down

1 comment on commit 4e00994

@elgonzo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe i am confused, but it seems the commit message here does not actually describe the code changes in the commit. System.CommandLine.Invocation.Process is still public.

Please sign in to comment.