mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using FluentAssertions;
|
|
using Xunit;
|
|
|
|
namespace CliFx.Tests
|
|
{
|
|
public partial class CancellationSpecs
|
|
{
|
|
[Fact]
|
|
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
|
|
{
|
|
// Arrange
|
|
using var cts = new CancellationTokenSource();
|
|
|
|
await using var stdOut = new MemoryStream();
|
|
var console = new VirtualConsole(output: stdOut, cancellationToken: cts.Token);
|
|
|
|
var application = new CliApplicationBuilder()
|
|
.AddCommand(typeof(CancellableCommand))
|
|
.UseConsole(console)
|
|
.Build();
|
|
|
|
// Act
|
|
cts.CancelAfter(TimeSpan.FromSeconds(0.2));
|
|
|
|
var exitCode = await application.RunAsync(
|
|
new[] {"cancel"},
|
|
new Dictionary<string, string>());
|
|
|
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
|
|
|
// Assert
|
|
exitCode.Should().NotBe(0);
|
|
stdOutData.Should().Be("Cancellation requested");
|
|
}
|
|
}
|
|
} |