Skip to content

Commit f704f2a

Browse files
Merge pull request #1739 from FrankRay78/1738-CommandAppTester-is-trimming-TestConsole-output
2 parents 58bf89a + 8c5264d commit f704f2a

File tree

5 files changed

+86
-17
lines changed

5 files changed

+86
-17
lines changed

src/Spectre.Console.Testing/Cli/CommandAppResult.cs

-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,5 @@ internal CommandAppResult(int exitCode, string output, CommandContext? context,
3131
Output = output ?? string.Empty;
3232
Context = context;
3333
Settings = settings;
34-
35-
Output = Output
36-
.NormalizeLineEndings()
37-
.TrimLines()
38-
.Trim();
3934
}
4035
}

src/Spectre.Console.Testing/Cli/CommandAppTester.cs

+23-11
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,35 @@ public sealed class CommandAppTester
88
private Action<CommandApp>? _appConfiguration;
99
private Action<IConfigurator>? _configuration;
1010

11+
/// <summary>
12+
/// Gets or sets the Registrar to use in the CommandApp.
13+
/// </summary>
14+
public ITypeRegistrar? Registrar { get; set; }
15+
16+
/// <summary>
17+
/// Gets or sets the settings for the <see cref="CommandAppTester"/>.
18+
/// </summary>
19+
public CommandAppTesterSettings TestSettings { get; set; }
20+
1121
/// <summary>
1222
/// Initializes a new instance of the <see cref="CommandAppTester"/> class.
1323
/// </summary>
1424
/// <param name="registrar">The registrar.</param>
15-
public CommandAppTester(ITypeRegistrar? registrar = null)
25+
/// <param name="settings">The settings.</param>
26+
public CommandAppTester(ITypeRegistrar? registrar = null, CommandAppTesterSettings? settings = null)
1627
{
1728
Registrar = registrar;
29+
TestSettings = settings ?? new CommandAppTesterSettings();
1830
}
1931

2032
/// <summary>
21-
/// Gets or sets the Registrar to use in the CommandApp.
33+
/// Initializes a new instance of the <see cref="CommandAppTester"/> class.
2234
/// </summary>
23-
public ITypeRegistrar? Registrar { get; set; }
35+
/// <param name="settings">The settings.</param>
36+
public CommandAppTester(CommandAppTesterSettings settings)
37+
{
38+
TestSettings = settings;
39+
}
2440

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

136152
var result = app.Run(args);
137153

138-
var output = console.Output
139-
.NormalizeLineEndings()
140-
.TrimLines()
141-
.Trim();
154+
var output = console.Output.NormalizeLineEndings();
155+
output = TestSettings.TrimConsoleOutput ? output.TrimLines().Trim() : output;
142156

143157
return new CommandAppResult(result, output, context, settings);
144158
}
@@ -181,10 +195,8 @@ private async Task<CommandAppResult> RunAsync(string[] args, TestConsole console
181195

182196
var result = await app.RunAsync(args);
183197

184-
var output = console.Output
185-
.NormalizeLineEndings()
186-
.TrimLines()
187-
.Trim();
198+
var output = console.Output.NormalizeLineEndings();
199+
output = TestSettings.TrimConsoleOutput ? output.TrimLines().Trim() : output;
188200

189201
return new CommandAppResult(result, output, context, settings);
190202
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Spectre.Console.Testing;
2+
3+
/// <summary>
4+
/// Represents the configuration settings for the <see cref="CommandAppTester"/> class.
5+
/// </summary>
6+
public sealed class CommandAppTesterSettings
7+
{
8+
/// <summary>
9+
/// Gets or sets a value indicating whether whitespace should be trimmed from the console output.
10+
/// </summary>
11+
/// <remarks>
12+
/// When enabled, leading and trailing whitespace from the console output and trailing whitespace from each line will be trimmed.
13+
/// </remarks>
14+
public bool TrimConsoleOutput { get; set; } = true;
15+
}

src/Tests/Spectre.Console.Cli.Tests/Data/Commands/AsynchronousCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public async override Task<int> ExecuteAsync(CommandContext context, Asynchronou
2020
}
2121
else
2222
{
23-
_console.WriteLine($"Finished executing asynchronously");
23+
_console.Write($"Finished executing asynchronously");
2424
}
2525

2626
return 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
using System;
3+
4+
namespace Spectre.Console.Tests.Unit.Cli.Testing;
5+
6+
public sealed class CommandAppTesterTests
7+
{
8+
private class CommandAppTesterCommand : Command<OptionalArgumentWithDefaultValueSettings>
9+
{
10+
private readonly IAnsiConsole _console;
11+
12+
public CommandAppTesterCommand(IAnsiConsole console)
13+
{
14+
_console = console;
15+
}
16+
17+
public override int Execute(CommandContext context, OptionalArgumentWithDefaultValueSettings settings)
18+
{
19+
_console.Write(settings.Greeting);
20+
return 0;
21+
}
22+
}
23+
24+
[Theory]
25+
[InlineData(false, " Hello ", " Hello ")]
26+
[InlineData(true, " Hello ", "Hello")]
27+
[InlineData(false, " Hello \n World ", " Hello \n World ")]
28+
[InlineData(true, " Hello \n World ", "Hello\n World")]
29+
public void Should_Respect_Trim_Setting(bool trim, string actual, string expected)
30+
{
31+
// Given
32+
var settings = new CommandAppTesterSettings { TrimConsoleOutput = trim };
33+
34+
var app = new CommandAppTester(settings);
35+
app.SetDefaultCommand<CommandAppTesterCommand>();
36+
app.Configure(config =>
37+
{
38+
config.PropagateExceptions();
39+
});
40+
41+
// When
42+
var result = app.Run(actual);
43+
44+
// Then
45+
result.Output.ShouldBe(expected);
46+
}
47+
}

0 commit comments

Comments
 (0)