Skip to content

Commit

Permalink
chore: Refactor CLI.cs to use ConcurrentQueue for message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
devantler committed Aug 11, 2024
1 parent f858dc6 commit 91c714f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
16 changes: 8 additions & 8 deletions Devantler.CLIRunner.Tests/CLITests/RunAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class RunAsyncTests
/// </summary>
/// <returns></returns>
[Fact]
public async Task RunAsync_GivenValidCommand_ReturnsZeroExitCodeAndStdout()
{
public async Task RunAsync_GivenValidCommand_ReturnsZeroExitCodeAndStdout()
{
// Arrange
Environment.SetEnvironmentVariable("DEBUG", "true");
var command = new Command("echo")
Expand All @@ -38,8 +38,8 @@ public async Task RunAsync_GivenValidCommand_ReturnsZeroExitCodeAndStdout()
/// </summary>
/// <returns></returns>
[Fact]
public async Task RunAsync_GivenInvalidCommand_ReturnsOneExitCodeAndNoOutput()
{
public async Task RunAsync_GivenInvalidCommand_ReturnsOneExitCodeAndNoOutput()
{
// Arrange
var command = new Command("ech")
.WithArguments("Hello, World!");
Expand All @@ -60,8 +60,8 @@ public async Task RunAsync_GivenInvalidCommand_ReturnsOneExitCodeAndNoOutput()
/// </summary>
/// <returns></returns>
[Fact]
public async Task RunAsync_GivenInvalidArgument_ReturnsOneExitCodeAndStderr()
{
public async Task RunAsync_GivenInvalidArgument_ReturnsOneExitCodeAndStderr()
{
// Arrange
var command = new Command("cat")
.WithArguments("--invalid")
Expand All @@ -81,8 +81,8 @@ public async Task RunAsync_GivenInvalidArgument_ReturnsOneExitCodeAndStderr()
/// Tests that the <see cref="CLI.RunAsync"/> method throws an <see cref="ArgumentNullException"/> when the command is null
/// </summary>
[Fact]
public async Task RunAsync_GivenNullCommand_ReturnsArgumentNullException()
{
public async Task RunAsync_GivenNullCommand_ReturnsArgumentNullException()
{
// Arrange
Command command = null!;
var cancellationToken = CancellationToken.None;
Expand Down
12 changes: 9 additions & 3 deletions Devantler.CLIRunner/CLI.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System.Text;
using CliWrap;
using CliWrap.EventStream;
Expand All @@ -22,7 +23,7 @@ public static class CLI
{
ArgumentNullException.ThrowIfNull(command);
bool isFaulty = false;
StringBuilder result = new();
ConcurrentQueue<string> messageQueue = new();
try
{
await foreach (var cmdEvent in command.WithValidation(validation).ListenAsync(cancellationToken: cancellationToken))
Expand All @@ -38,14 +39,14 @@ public static class CLI
{
Console.WriteLine(stdOut.Text);
}
_ = result.AppendLine(stdOut.Text);
messageQueue.Enqueue(stdOut.Text);
break;
case StandardErrorCommandEvent stdErr:
if (!silent)
{
Console.WriteLine(stdErr.Text);
}
_ = result.AppendLine(stdErr.Text);
messageQueue.Enqueue(stdErr.Text);
break;
case ExitedCommandEvent exited:
if (System.Diagnostics.Debugger.IsAttached || Environment.GetEnvironmentVariable("DEBUG") is not null)
Expand All @@ -62,6 +63,11 @@ public static class CLI
{
isFaulty = true;
}
StringBuilder result = new();
while (messageQueue.TryDequeue(out string? message))
{
_ = result.AppendLine(message);
}
return isFaulty ? (1, result.ToString()) : (0, result.ToString());
}
}

0 comments on commit 91c714f

Please sign in to comment.