more test commands

This commit is contained in:
Louis Seubert 2024-04-28 21:36:31 +02:00
parent 833bc2bd9c
commit acd6edaa63
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD
6 changed files with 126 additions and 7 deletions

View file

@ -7,12 +7,10 @@ internal sealed class EchoCommand : Command<EchoCommand.Settings>
public sealed class Settings : CommandSettings public sealed class Settings : CommandSettings
{ {
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut; [CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut;
[CommandOption("--separator <sep>")] 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 int Execute(CommandContext context, Settings settings)
{ {
foreach (var writer in settings.Target.GetWriters()) foreach (var writer in settings.Target.GetWriters())

View file

@ -9,7 +9,6 @@ internal sealed class EchoStdinCommand : Command<EchoStdinCommand.Settings>
public sealed class Settings : CommandSettings public sealed class Settings : CommandSettings
{ {
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut; [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;
} }
@ -17,10 +16,10 @@ internal sealed class EchoStdinCommand : Command<EchoStdinCommand.Settings>
{ {
using var buffer = MemoryPool<char>.Shared.Rent(81920); using var buffer = MemoryPool<char>.Shared.Rent(81920);
var totalBytesRead = 0L; var count = 0L;
while (totalBytesRead < settings.Length) while (count < settings.Length)
{ {
var bytesWanted = (int)Math.Min(buffer.Memory.Length, settings.Length - totalBytesRead); var bytesWanted = (int)Math.Min(buffer.Memory.Length, settings.Length - count);
var bytesRead = Console.In.Read(buffer.Memory.Span[..bytesWanted]); var bytesRead = Console.In.Read(buffer.Memory.Span[..bytesWanted]);
if (bytesRead <= 0) if (bytesRead <= 0)
@ -31,7 +30,7 @@ internal sealed class EchoStdinCommand : Command<EchoStdinCommand.Settings>
writer.Write(buffer.Memory.Span[..bytesRead]); writer.Write(buffer.Memory.Span[..bytesRead]);
} }
totalBytesRead += bytesRead; count += bytesRead;
} }
return 0; return 0;

View file

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

View file

@ -0,0 +1,41 @@
using System.Buffers;
using System.Text;
using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class GenerateClobCommand : Command<GenerateClobCommand.Settings>
{
private readonly Random _random = new(1234567);
private readonly char[] _chars = Enumerable.Range(32, 94).Select(i => (char)i).ToArray();
public sealed class Settings : CommandSettings
{
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut;
[CommandOption("--length")] public int Length { get; init; } = 100_000;
[CommandOption("--lines")] public int LinesCount { get; init; } = 1;
}
public override int Execute(CommandContext context, Settings settings)
{
var buffer = new StringBuilder(settings.Length);
for (var line = 0; line < settings.LinesCount; line++)
{
buffer.Clear();
for (var i = 0; i < settings.Length; i++)
{
buffer.Append(_chars[_random.Next(0, _chars.Length)]);
}
foreach (var writer in settings.Target.GetWriters())
{
writer.WriteLine(buffer.ToString());
}
}
return 0;
}
}

View file

@ -0,0 +1,32 @@
using System.Buffers;
using System.Globalization;
using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class LengthCommand : Command<LengthCommand.Settings>
{
public sealed class Settings : CommandSettings
{
}
public override int Execute(CommandContext context, Settings settings)
{
using var buffer = MemoryPool<byte>.Shared.Rent(81920);
using var stdin = Console.OpenStandardInput();
var count = 0L;
while (true)
{
var bytesRead = stdin.Read(buffer.Memory.Span);
if (bytesRead <= 0)
break;
count += bytesRead;
}
Console.Out.WriteLine(count.ToString(CultureInfo.InvariantCulture));
return 0;
}
}

View file

@ -30,7 +30,14 @@ public static class Program
configuration.AddCommand<EchoStdinCommand>("echo-stdin"); configuration.AddCommand<EchoStdinCommand>("echo-stdin");
configuration.AddCommand<EnvironmentCommand>("env"); configuration.AddCommand<EnvironmentCommand>("env");
configuration.AddCommand<WorkingDirectoryCommand>("cwd"); configuration.AddCommand<WorkingDirectoryCommand>("cwd");
configuration.AddCommand<WorkingDirectoryCommand>("cwd");
configuration.AddCommand<ExitCommand>("exit"); configuration.AddCommand<ExitCommand>("exit");
configuration.AddCommand<LengthCommand>("length");
configuration.AddBranch("generate", static generate =>
{
generate.AddCommand<GenerateBlobCommand>("blob");
generate.AddCommand<GenerateClobCommand>("clob");
});
} }
} }
} }