Skip to content

Commit

Permalink
Merge pull request #1739 from FrankRay78/1738-CommandAppTester-is-tri…
Browse files Browse the repository at this point in the history
…mming-TestConsole-output
  • Loading branch information
patriksvensson authored Jan 21, 2025
2 parents 58bf89a + 8c5264d commit f704f2a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
5 changes: 0 additions & 5 deletions src/Spectre.Console.Testing/Cli/CommandAppResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,5 @@ internal CommandAppResult(int exitCode, string output, CommandContext? context,
Output = output ?? string.Empty;
Context = context;
Settings = settings;

Output = Output
.NormalizeLineEndings()
.TrimLines()
.Trim();
}
}
34 changes: 23 additions & 11 deletions src/Spectre.Console.Testing/Cli/CommandAppTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@ public sealed class CommandAppTester
private Action<CommandApp>? _appConfiguration;
private Action<IConfigurator>? _configuration;

/// <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>
public CommandAppTester(ITypeRegistrar? registrar = null)
/// <param name="settings">The settings.</param>
public CommandAppTester(ITypeRegistrar? registrar = null, CommandAppTesterSettings? settings = null)
{
Registrar = registrar;
TestSettings = settings ?? new CommandAppTesterSettings();
}

/// <summary>
/// Gets or sets the Registrar to use in the CommandApp.
/// Initializes a new instance of the <see cref="CommandAppTester"/> class.
/// </summary>
public ITypeRegistrar? Registrar { get; set; }
/// <param name="settings">The settings.</param>
public CommandAppTester(CommandAppTesterSettings settings)
{
TestSettings = settings;
}

/// <summary>
/// Sets the default command.
Expand Down Expand Up @@ -135,10 +151,8 @@ private CommandAppResult Run(string[] args, TestConsole console, Action<IConfigu

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);
}
Expand Down Expand Up @@ -181,10 +195,8 @@ private async Task<CommandAppResult> RunAsync(string[] args, TestConsole console

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);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Spectre.Console.Testing/Cli/CommandAppTesterSettings.cs
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async override Task<int> ExecuteAsync(CommandContext context, Asynchronou
}
else
{
_console.WriteLine($"Finished executing asynchronously");
_console.Write($"Finished executing asynchronously");
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f704f2a

Please sign in to comment.