mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-16 12:13:22 +08:00
Trimming of TestConsole output by CommandAppTester is configurable
This commit is contained in:
parent
58bf89a56a
commit
8c5264d117
@ -31,10 +31,5 @@ public sealed class CommandAppResult
|
|||||||
Output = output ?? string.Empty;
|
Output = output ?? string.Empty;
|
||||||
Context = context;
|
Context = context;
|
||||||
Settings = settings;
|
Settings = settings;
|
||||||
|
|
||||||
Output = Output
|
|
||||||
.NormalizeLineEndings()
|
|
||||||
.TrimLines()
|
|
||||||
.Trim();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,20 +8,36 @@ public sealed class CommandAppTester
|
|||||||
private Action<CommandApp>? _appConfiguration;
|
private Action<CommandApp>? _appConfiguration;
|
||||||
private Action<IConfigurator>? _configuration;
|
private Action<IConfigurator>? _configuration;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="CommandAppTester"/> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="registrar">The registrar.</param>
|
|
||||||
public CommandAppTester(ITypeRegistrar? registrar = null)
|
|
||||||
{
|
|
||||||
Registrar = registrar;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the Registrar to use in the CommandApp.
|
/// Gets or sets the Registrar to use in the CommandApp.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ITypeRegistrar? Registrar { get; set; }
|
public ITypeRegistrar? Registrar { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the settings for the <see cref="CommandAppTester"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CommandAppTesterSettings TestSettings { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="CommandAppTester"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="registrar">The registrar.</param>
|
||||||
|
/// <param name="settings">The settings.</param>
|
||||||
|
public CommandAppTester(ITypeRegistrar? registrar = null, CommandAppTesterSettings? settings = null)
|
||||||
|
{
|
||||||
|
Registrar = registrar;
|
||||||
|
TestSettings = settings ?? new CommandAppTesterSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="CommandAppTester"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="settings">The settings.</param>
|
||||||
|
public CommandAppTester(CommandAppTesterSettings settings)
|
||||||
|
{
|
||||||
|
TestSettings = settings;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the default command.
|
/// Sets the default command.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -135,10 +151,8 @@ public sealed class CommandAppTester
|
|||||||
|
|
||||||
var result = app.Run(args);
|
var result = app.Run(args);
|
||||||
|
|
||||||
var output = console.Output
|
var output = console.Output.NormalizeLineEndings();
|
||||||
.NormalizeLineEndings()
|
output = TestSettings.TrimConsoleOutput ? output.TrimLines().Trim() : output;
|
||||||
.TrimLines()
|
|
||||||
.Trim();
|
|
||||||
|
|
||||||
return new CommandAppResult(result, output, context, settings);
|
return new CommandAppResult(result, output, context, settings);
|
||||||
}
|
}
|
||||||
@ -181,10 +195,8 @@ public sealed class CommandAppTester
|
|||||||
|
|
||||||
var result = await app.RunAsync(args);
|
var result = await app.RunAsync(args);
|
||||||
|
|
||||||
var output = console.Output
|
var output = console.Output.NormalizeLineEndings();
|
||||||
.NormalizeLineEndings()
|
output = TestSettings.TrimConsoleOutput ? output.TrimLines().Trim() : output;
|
||||||
.TrimLines()
|
|
||||||
.Trim();
|
|
||||||
|
|
||||||
return new CommandAppResult(result, output, context, settings);
|
return new CommandAppResult(result, output, context, settings);
|
||||||
}
|
}
|
||||||
|
15
src/Spectre.Console.Testing/Cli/CommandAppTesterSettings.cs
Normal file
15
src/Spectre.Console.Testing/Cli/CommandAppTesterSettings.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
namespace Spectre.Console.Testing;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents the configuration settings for the <see cref="CommandAppTester"/> class.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class CommandAppTesterSettings
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether whitespace should be trimmed from the console output.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// When enabled, leading and trailing whitespace from the console output and trailing whitespace from each line will be trimmed.
|
||||||
|
/// </remarks>
|
||||||
|
public bool TrimConsoleOutput { get; set; } = true;
|
||||||
|
}
|
@ -20,7 +20,7 @@ public sealed class AsynchronousCommand : AsyncCommand<AsynchronousCommandSettin
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_console.WriteLine($"Finished executing asynchronously");
|
_console.Write($"Finished executing asynchronously");
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Spectre.Console.Tests.Unit.Cli.Testing;
|
||||||
|
|
||||||
|
public sealed class CommandAppTesterTests
|
||||||
|
{
|
||||||
|
private class CommandAppTesterCommand : Command<OptionalArgumentWithDefaultValueSettings>
|
||||||
|
{
|
||||||
|
private readonly IAnsiConsole _console;
|
||||||
|
|
||||||
|
public CommandAppTesterCommand(IAnsiConsole console)
|
||||||
|
{
|
||||||
|
_console = console;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int Execute(CommandContext context, OptionalArgumentWithDefaultValueSettings settings)
|
||||||
|
{
|
||||||
|
_console.Write(settings.Greeting);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(false, " Hello ", " Hello ")]
|
||||||
|
[InlineData(true, " Hello ", "Hello")]
|
||||||
|
[InlineData(false, " Hello \n World ", " Hello \n World ")]
|
||||||
|
[InlineData(true, " Hello \n World ", "Hello\n World")]
|
||||||
|
public void Should_Respect_Trim_Setting(bool trim, string actual, string expected)
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var settings = new CommandAppTesterSettings { TrimConsoleOutput = trim };
|
||||||
|
|
||||||
|
var app = new CommandAppTester(settings);
|
||||||
|
app.SetDefaultCommand<CommandAppTesterCommand>();
|
||||||
|
app.Configure(config =>
|
||||||
|
{
|
||||||
|
config.PropagateExceptions();
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
var result = app.Run(actual);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.Output.ShouldBe(expected);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user