mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-12-16 16:15:47 +08:00
* TestConsole can now be configured and accessed in CommandAppTester * Add test with mocked user inputs for interactive command * Add documentation for using the CommandAppTester Co-authored-by: Patrik Svensson <patriksvensson@users.noreply.github.com> Co-authored-by: Marek Magath <Marek.Magath@solarwinds.com>
This commit is contained in:
@@ -8,6 +8,11 @@ public sealed class CommandAppTester
|
||||
private Action<CommandApp>? _appConfiguration;
|
||||
private Action<IConfigurator>? _configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the test console used by both the CommandAppTester and CommandApp.
|
||||
/// </summary>
|
||||
public TestConsole Console { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Registrar to use in the CommandApp.
|
||||
/// </summary>
|
||||
@@ -23,10 +28,15 @@ public sealed class CommandAppTester
|
||||
/// </summary>
|
||||
/// <param name="registrar">The registrar.</param>
|
||||
/// <param name="settings">The settings.</param>
|
||||
public CommandAppTester(ITypeRegistrar? registrar = null, CommandAppTesterSettings? settings = null)
|
||||
/// <param name="console">The test console that overrides the default one.</param>
|
||||
public CommandAppTester(
|
||||
ITypeRegistrar? registrar = null,
|
||||
CommandAppTesterSettings? settings = null,
|
||||
TestConsole? console = null)
|
||||
{
|
||||
Registrar = registrar;
|
||||
TestSettings = settings ?? new CommandAppTesterSettings();
|
||||
Console = console ?? new TestConsole().Width(int.MaxValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -36,6 +46,7 @@ public sealed class CommandAppTester
|
||||
public CommandAppTester(CommandAppTesterSettings settings)
|
||||
{
|
||||
TestSettings = settings;
|
||||
Console = new TestConsole().Width(int.MaxValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -85,25 +96,23 @@ public sealed class CommandAppTester
|
||||
public CommandAppFailure RunAndCatch<T>(params string[] args)
|
||||
where T : Exception
|
||||
{
|
||||
var console = new TestConsole().Width(int.MaxValue);
|
||||
|
||||
try
|
||||
{
|
||||
Run(args, console, c => c.PropagateExceptions());
|
||||
Run(args, Console, c => c.PropagateExceptions());
|
||||
throw new InvalidOperationException("Expected an exception to be thrown, but there was none.");
|
||||
}
|
||||
catch (T ex)
|
||||
{
|
||||
if (ex is CommandAppException commandAppException && commandAppException.Pretty != null)
|
||||
{
|
||||
console.Write(commandAppException.Pretty);
|
||||
Console.Write(commandAppException.Pretty);
|
||||
}
|
||||
else
|
||||
{
|
||||
console.WriteLine(ex.Message);
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
return new CommandAppFailure(ex, console.Output);
|
||||
return new CommandAppFailure(ex, Console.Output);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -120,8 +129,7 @@ public sealed class CommandAppTester
|
||||
/// <returns>The result.</returns>
|
||||
public CommandAppResult Run(params string[] args)
|
||||
{
|
||||
var console = new TestConsole().Width(int.MaxValue);
|
||||
return Run(args, console);
|
||||
return Run(args, Console);
|
||||
}
|
||||
|
||||
private CommandAppResult Run(string[] args, TestConsole console, Action<IConfigurator>? config = null)
|
||||
@@ -164,8 +172,7 @@ public sealed class CommandAppTester
|
||||
/// <returns>The result.</returns>
|
||||
public async Task<CommandAppResult> RunAsync(params string[] args)
|
||||
{
|
||||
var console = new TestConsole().Width(int.MaxValue);
|
||||
return await RunAsync(args, console);
|
||||
return await RunAsync(args, Console);
|
||||
}
|
||||
|
||||
private async Task<CommandAppResult> RunAsync(string[] args, TestConsole console, Action<IConfigurator>? config = null)
|
||||
|
||||
Reference in New Issue
Block a user