mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Refactor (#94)
This commit is contained in:
@@ -1,44 +1,112 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.Commands;
|
||||
using CliFx.Tests.Utils;
|
||||
using CliFx.Tests.Utils.Extensions;
|
||||
using CliWrap;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public class DirectivesSpecs
|
||||
public class DirectivesSpecs : SpecsBase
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public DirectivesSpecs(ITestOutputHelper output) => _output = output;
|
||||
public DirectivesSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Preview_directive_can_be_specified_to_print_provided_arguments_as_they_were_parsed()
|
||||
public async Task Debug_directive_can_be_specified_to_interrupt_execution_until_a_debugger_is_attached()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
var stdOutBuffer = new StringBuilder();
|
||||
|
||||
var command = Cli.Wrap("dotnet")
|
||||
.WithArguments(a => a
|
||||
.Add(Dummy.Program.Location)
|
||||
.Add("[debug]")) | stdOutBuffer;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
// This has a timeout just in case the execution hangs forever
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
|
||||
var task = command.ExecuteAsync(cts.Token);
|
||||
|
||||
// We can't attach a debugger programmatically, so the application
|
||||
// will hang indefinitely.
|
||||
// To work around it, we will wait until the application writes
|
||||
// something to the standard output and then kill it.
|
||||
while (true)
|
||||
{
|
||||
if (stdOutBuffer.Length > 0)
|
||||
{
|
||||
cts.Cancel();
|
||||
break;
|
||||
}
|
||||
|
||||
await Task.Delay(100, cts.Token);
|
||||
}
|
||||
|
||||
await task;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// It's expected to fail
|
||||
}
|
||||
|
||||
var stdOut = stdOutBuffer.ToString();
|
||||
|
||||
// Assert
|
||||
stdOut.Should().Contain("Attach debugger to");
|
||||
|
||||
TestOutput.WriteLine(stdOut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Preview_directive_can_be_specified_to_print_command_input()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
// language=cs
|
||||
@"
|
||||
[Command(""cmd"")]
|
||||
public class Command : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
");
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<NamedCommand>()
|
||||
.UseConsole(console)
|
||||
.AddCommand(commandType)
|
||||
.UseConsole(FakeConsole)
|
||||
.AllowPreviewMode()
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"[preview]", "named", "param", "-abc", "--option", "foo"},
|
||||
new Dictionary<string, string>()
|
||||
new[] {"[preview]", "cmd", "param", "-abc", "--option", "foo"},
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_QOP"] = "hello",
|
||||
["ENV_KIL"] = "world"
|
||||
}
|
||||
);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"named", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]"
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
"cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]",
|
||||
"ENV_QOP", "=", "\"hello\"",
|
||||
"ENV_KIL", "=", "\"world\""
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user