Trimming of TestConsole output by CommandAppTester is configurable

This commit is contained in:
Frank Ray
2025-01-21 08:37:54 +00:00
parent 58bf89a56a
commit 8c5264d117
5 changed files with 92 additions and 23 deletions

View File

@@ -31,10 +31,5 @@ public sealed class CommandAppResult
Output = output ?? string.Empty;
Context = context;
Settings = settings;
Output = Output
.NormalizeLineEndings()
.TrimLines()
.Trim();
}
}

View File

@@ -8,20 +8,36 @@ public sealed class CommandAppTester
private Action<CommandApp>? _appConfiguration;
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>
/// Gets or sets the Registrar to use in the CommandApp.
/// </summary>
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>
/// Sets the default command.
/// </summary>
@@ -135,10 +151,8 @@ public sealed class CommandAppTester
var result = app.Run(args);
var output = console.Output
.NormalizeLineEndings()
.TrimLines()
.Trim();
var output = console.Output.NormalizeLineEndings();
output = TestSettings.TrimConsoleOutput ? output.TrimLines().Trim() : output;
return new CommandAppResult(result, output, context, settings);
}
@@ -181,10 +195,8 @@ public sealed class CommandAppTester
var result = await app.RunAsync(args);
var output = console.Output
.NormalizeLineEndings()
.TrimLines()
.Trim();
var output = console.Output.NormalizeLineEndings();
output = TestSettings.TrimConsoleOutput ? output.TrimLines().Trim() : output;
return new CommandAppResult(result, output, context, settings);
}

View 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;
}