chore: refactored test dummy program
All checks were successful
default / default (8.0) (push) Successful in 46s

Refactored the dummy program to always use async commands. Also added a Terminal abstractions to help the problem with char and byte interop when writing or reading data from the std pipes.
This commit is contained in:
Louis Seubert 2024-05-05 20:39:36 +02:00
parent 54185e7877
commit 7e67992a18
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD
13 changed files with 159 additions and 51 deletions

View file

@ -0,0 +1,12 @@
using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy;
internal abstract class AsyncOutputCommand<T> : AsyncCommand<T> where T : OutputCommandSettings
{
}
internal abstract class OutputCommandSettings : CommandSettings
{
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut;
}

View file

@ -2,20 +2,21 @@ using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands; namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class EchoCommand : Command<EchoCommand.Settings> internal sealed class EchoCommand : AsyncOutputCommand<EchoCommand.Settings>
{ {
public sealed class Settings : CommandSettings public sealed class Settings : OutputCommandSettings
{ {
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut; [CommandOption("--separator <char>")] public string Separator { get; init; } = " ";
[CommandOption("--separator <sep>")] public string Separator { get; init; } = " ";
[CommandArgument(0, "[line]")] public string[] Items { get; init; } = []; [CommandArgument(0, "[line]")] public string[] Items { get; init; } = [];
} }
public override int Execute(CommandContext context, Settings settings) public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{ {
foreach (var writer in settings.Target.GetWriters()) using var tty = Terminal.Connect();
foreach (var writer in tty.GetWriters(settings.Target))
{ {
writer.WriteLine(string.Join(settings.Separator, settings.Items)); await writer.WriteLineAsync(string.Join(settings.Separator, settings.Items));
} }
return 0; return 0;

View file

@ -4,30 +4,30 @@ using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands; namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class EchoStdinCommand : Command<EchoStdinCommand.Settings> internal sealed class EchoStdinCommand : AsyncOutputCommand<EchoStdinCommand.Settings>
{ {
public sealed class Settings : CommandSettings public sealed class Settings : OutputCommandSettings
{ {
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut;
[CommandOption("--length")] public long Length { get; init; } = long.MaxValue; [CommandOption("--length")] public long Length { get; init; } = long.MaxValue;
} }
public override int Execute(CommandContext context, Settings settings) public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{ {
using var buffer = MemoryPool<char>.Shared.Rent(81920); using var tty = Terminal.Connect();
using var buffer = MemoryPool<byte>.Shared.Rent(81920);
var count = 0L; var count = 0L;
while (count < settings.Length) while (count < settings.Length)
{ {
var bytesWanted = (int)Math.Min(buffer.Memory.Length, settings.Length - count); var bytesWanted = (int)Math.Min(buffer.Memory.Length, settings.Length - count);
var bytesRead = Console.In.Read(buffer.Memory.Span[..bytesWanted]); var bytesRead = await tty.Stdin.BaseStream.ReadAsync(buffer.Memory[..bytesWanted]);
if (bytesRead <= 0) if (bytesRead <= 0)
break; break;
foreach (var writer in settings.Target.GetWriters()) foreach (var writer in tty.GetWriters(settings.Target))
{ {
writer.Write(buffer.Memory.Span[..bytesRead]); await writer.BaseStream.WriteAsync(buffer.Memory[..bytesRead]);
} }
count += bytesRead; count += bytesRead;

View file

@ -2,18 +2,25 @@ using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands; namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class EnvironmentCommand : Command<EnvironmentCommand.Settings> internal sealed class EnvironmentCommand : AsyncOutputCommand<EnvironmentCommand.Settings>
{ {
public sealed class Settings : CommandSettings public sealed class Settings : OutputCommandSettings
{ {
[CommandArgument(0, "<ARGUMENT>")] public string[] Variables { get; init; } = []; [CommandArgument(0, "<ARGUMENT>")] public string[] Variables { get; init; } = [];
} }
public override int Execute(CommandContext context, Settings settings) public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{ {
using var tty = Terminal.Connect();
foreach (var name in settings.Variables) foreach (var name in settings.Variables)
{ {
Console.Out.WriteLine(Environment.GetEnvironmentVariable(name) ?? string.Empty); var value = Environment.GetEnvironmentVariable(name) ?? string.Empty;
foreach (var writer in tty.GetWriters(settings.Target))
{
await writer.WriteLineAsync(value);
}
} }
return 0; return 0;

View file

@ -2,16 +2,19 @@ using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands; namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class ExitCommand : Command<ExitCommand.Settings> internal sealed class ExitCommand : AsyncCommand<ExitCommand.Settings>
{ {
public sealed class Settings : CommandSettings public sealed class Settings : CommandSettings
{ {
[CommandArgument(1, "<code>")] public int Code { get; init; } [CommandArgument(1, "<code>")] public int Code { get; init; }
} }
public override int Execute(CommandContext context, Settings settings) public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{ {
Console.Error.WriteLine($"Exit code set to {settings.Code}"); using var tty = Terminal.Connect();
await tty.Stderr.WriteLineAsync($"Exit code set to {settings.Code}");
return settings.Code; return settings.Code;
} }
} }

View file

@ -5,33 +5,31 @@ using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands; namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class GenerateBlobCommand : Command<GenerateBlobCommand.Settings> internal sealed class GenerateBlobCommand : AsyncOutputCommand<GenerateBlobCommand.Settings>
{ {
private readonly Random _random = new(1234567); private readonly Random _random = new(1234567);
public sealed class Settings : CommandSettings public sealed class Settings : OutputCommandSettings
{ {
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut;
[CommandOption("--length")] public long Length { get; init; } = 100_000; [CommandOption("--length")] public long Length { get; init; } = 100_000;
[CommandOption("--buffer")] public int BufferSize { get; init; } = 1024; [CommandOption("--buffer")] public int BufferSize { get; init; } = 1024;
} }
public override int Execute(CommandContext context, Settings settings) public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{ {
using var tty = Terminal.Connect();
using var bytes = MemoryPool<byte>.Shared.Rent(settings.BufferSize); using var bytes = MemoryPool<byte>.Shared.Rent(settings.BufferSize);
using var chars = MemoryPool<char>.Shared.Rent(settings.BufferSize);
var total = 0L; var total = 0L;
while (total < settings.Length) while (total < settings.Length)
{ {
_random.NextBytes(bytes.Memory.Span); _random.NextBytes(bytes.Memory.Span);
Encoding.UTF8.GetChars(bytes.Memory.Span, chars.Memory.Span); var count = (int)Math.Min(bytes.Memory.Length, settings.Length - total);
foreach (var writer in tty.GetWriters(settings.Target))
var count = (int)Math.Min(chars.Memory.Length, settings.Length - total);
foreach (var writer in settings.Target.GetWriters())
{ {
writer.Write(chars.Memory[..count]); await writer.BaseStream.WriteAsync(bytes.Memory[..count]);
} }
total += count; total += count;

View file

@ -5,20 +5,21 @@ using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands; namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class GenerateClobCommand : Command<GenerateClobCommand.Settings> internal sealed class GenerateClobCommand : AsyncOutputCommand<GenerateClobCommand.Settings>
{ {
private readonly Random _random = new(1234567); private readonly Random _random = new(1234567);
private readonly char[] _chars = Enumerable.Range(32, 94).Select(i => (char)i).ToArray(); private readonly char[] _chars = Enumerable.Range(32, 94).Select(i => (char)i).ToArray();
public sealed class Settings : CommandSettings public sealed class Settings : OutputCommandSettings
{ {
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut;
[CommandOption("--length")] public int Length { get; init; } = 100_000; [CommandOption("--length")] public int Length { get; init; } = 100_000;
[CommandOption("--lines")] public int LinesCount { get; init; } = 1; [CommandOption("--lines")] public int LinesCount { get; init; } = 1;
} }
public override int Execute(CommandContext context, Settings settings) public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{ {
using var tty = Terminal.Connect();
var buffer = new StringBuilder(settings.Length); var buffer = new StringBuilder(settings.Length);
for (var line = 0; line < settings.LinesCount; line++) for (var line = 0; line < settings.LinesCount; line++)
@ -30,9 +31,9 @@ internal sealed class GenerateClobCommand : Command<GenerateClobCommand.Settings
buffer.Append(_chars[_random.Next(0, _chars.Length)]); buffer.Append(_chars[_random.Next(0, _chars.Length)]);
} }
foreach (var writer in settings.Target.GetWriters()) foreach (var writer in tty.GetWriters(settings.Target))
{ {
writer.WriteLine(buffer.ToString()); await writer.WriteLineAsync(buffer.ToString());
} }
} }

View file

@ -5,28 +5,33 @@ using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands; namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class LengthCommand : Command<LengthCommand.Settings> internal sealed class LengthCommand : AsyncOutputCommand<LengthCommand.Settings>
{ {
public sealed class Settings : CommandSettings public sealed class Settings : OutputCommandSettings
{ {
} }
public override int Execute(CommandContext context, Settings settings) public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{ {
using var tty = Terminal.Connect();
using var buffer = MemoryPool<byte>.Shared.Rent(81920); using var buffer = MemoryPool<byte>.Shared.Rent(81920);
using var stdin = Console.OpenStandardInput();
var count = 0L; var count = 0L;
while (true) while (true)
{ {
var bytesRead = stdin.Read(buffer.Memory.Span); var bytesRead = await tty.Stdin.BaseStream.ReadAsync(buffer.Memory);
if (bytesRead <= 0) if (bytesRead <= 0)
break; break;
count += bytesRead; count += bytesRead;
} }
Console.Out.WriteLine(count.ToString(CultureInfo.InvariantCulture)); foreach (var writer in tty.GetWriters(settings.Target))
{
await writer.WriteLineAsync(count.ToString(CultureInfo.InvariantCulture));
}
return 0; return 0;
} }
} }

View file

@ -0,0 +1,33 @@
using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class SleepCommand : AsyncCommand<SleepCommand.Settings>
{
public sealed class Settings : CommandSettings
{
[CommandArgument(0, "[duration]")] public TimeSpan Duration { get; init; } = TimeSpan.FromSeconds(1);
}
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
using var tty = Terminal.Connect();
try
{
await Console.Out.WriteLineAsync($"Sleeping for {settings.Duration}...");
await Console.Out.FlushAsync(CancellationToken.None);
await Task.Delay(settings.Duration, tty.CancellationToken);
}
catch (OperationCanceledException)
{
await Console.Out.WriteLineAsync("Canceled.");
await Console.Out.FlushAsync(CancellationToken.None);
}
await Console.Out.WriteLineAsync("Done.");
await Console.Out.FlushAsync(CancellationToken.None);
return 0;
}
}

View file

@ -2,15 +2,21 @@ using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands; namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class WorkingDirectoryCommand : Command<WorkingDirectoryCommand.Settings> internal sealed class WorkingDirectoryCommand : AsyncOutputCommand<WorkingDirectoryCommand.Settings>
{ {
public sealed class Settings : CommandSettings public sealed class Settings : OutputCommandSettings
{ {
} }
public override int Execute(CommandContext context, Settings settings) public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{ {
Console.Out.WriteLine(Directory.GetCurrentDirectory()); using var tty = Terminal.Connect();
foreach (var writer in tty.GetWriters(settings.Target))
{
await writer.WriteLineAsync(Directory.GetCurrentDirectory());
}
return 0; return 0;
} }
} }

View file

@ -10,12 +10,12 @@ public enum OutputTarget
internal static class OutputTargetExtensions internal static class OutputTargetExtensions
{ {
public static IEnumerable<TextWriter> GetWriters(this OutputTarget target) public static IEnumerable<StreamWriter> GetWriters(this Terminal terminal, OutputTarget target)
{ {
if (target.HasFlag(OutputTarget.StdOut)) if (target.HasFlag(OutputTarget.StdOut))
yield return Console.Out; yield return terminal.Stdout;
if (target.HasFlag(OutputTarget.StdErr)) if (target.HasFlag(OutputTarget.StdErr))
yield return Console.Error; yield return terminal.Stderr;
} }
} }

View file

@ -33,6 +33,7 @@ public static class Program
configuration.AddCommand<WorkingDirectoryCommand>("cwd"); configuration.AddCommand<WorkingDirectoryCommand>("cwd");
configuration.AddCommand<ExitCommand>("exit"); configuration.AddCommand<ExitCommand>("exit");
configuration.AddCommand<LengthCommand>("length"); configuration.AddCommand<LengthCommand>("length");
configuration.AddCommand<SleepCommand>("sleep");
configuration.AddBranch("generate", static generate => configuration.AddBranch("generate", static generate =>
{ {
generate.AddCommand<GenerateBlobCommand>("blob"); generate.AddCommand<GenerateBlobCommand>("blob");

View file

@ -0,0 +1,41 @@
namespace Geekeey.Extensions.Process.Tests.Dummy;
internal sealed class Terminal : IDisposable
{
private readonly CancellationTokenSource _cts = new();
public Terminal()
{
Console.CancelKeyPress += Cancel;
}
public StreamReader Stdin { get; } = new(Console.OpenStandardInput(), leaveOpen: false);
public StreamWriter Stdout { get; } = new(Console.OpenStandardOutput(), leaveOpen: false);
public StreamWriter Stderr { get; } = new(Console.OpenStandardError(), leaveOpen: false);
public CancellationToken CancellationToken => _cts.Token;
public static Terminal Connect()
{
return new Terminal();
}
private void Cancel(object? sender, ConsoleCancelEventArgs args)
{
args.Cancel = true;
_cts.Cancel();
}
public void Dispose()
{
Stdout.BaseStream.Flush();
Stdout.Dispose();
Stderr.BaseStream.Flush();
Stderr.Dispose();
Stdin.Dispose();
Console.CancelKeyPress -= Cancel;
_cts.Dispose();
}
}