Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommandAppTester is trimming TestConsole output, requiring expected outputs to be manually adjusted #1738

Closed
FrankRay78 opened this issue Jan 18, 2025 · 3 comments · Fixed by #1739
Assignees
Labels
area-Testing-Framework Spectre.Console testing framework for developers. bug Something isn't working
Milestone

Comments

@FrankRay78
Copy link
Contributor

Information

  • OS: Windows 10
  • Version: Spectre.Console 0.49.1, XUnit 2.9.3
  • Terminal: Windows Terminal

Describe the bug
CommandAppTester is trimming TestConsole output, requiring expected outputs to be manually adjusted.

To Reproduce
Run the below, self-contained, unit test:

using Spectre.Console;
using Spectre.Console.Cli;
using Spectre.Console.Testing;
using Xunit;


public class HelloCommand : Command
{
    private IAnsiConsole console;

    public HelloCommand(IAnsiConsole console)
    {
        this.console = console;
    }

    public override int Execute(CommandContext context)
    {
        console.Write(
            new FigletText(" -- Hello -- ")
                //.Centered()
                .Color(Color.Red));

        return 0;
    }
}

public class Tests
{
    [Fact]
    public void Should_Output_Centered_FigletText()
    {
        // Given
        var app = new CommandAppTester();
        app.Configure(config => config.PropagateExceptions());

        app.SetDefaultCommand<HelloCommand>();

        // When
        var result = app.Run();

        // Then
        Assert.Equal(0, result.ExitCode);
    }
}

Expected behavior
Whitespace is trimmed from the TestConsole output (see screenshot below)

Screenshots

Image

Additional context
Here is the culprit:

Image

nb. I don't see a good reason for the literal command output to be manipulated after the command has executed (please tell me if you know why this is necessary). We saw a similar thing happening in the CLI command parser stripping out whitespace, which we eventually abandoned because it was causing inaccurate discarding of whitespace.


Please upvote 👍 this issue if you are interested in it.

@FrankRay78 FrankRay78 added bug Something isn't working needs triage labels Jan 18, 2025
@FrankRay78 FrankRay78 added the area-Testing-Framework Spectre.Console testing framework for developers. label Jan 18, 2025
@github-project-automation github-project-automation bot moved this to Todo 🕑 in Spectre Console Jan 18, 2025
@patriksvensson
Copy link
Contributor

We could make it configurable.

@FrankRay78
Copy link
Contributor Author

PR #1739 raised @patriksvensson.

@github-project-automation github-project-automation bot moved this from Todo 🕑 to Done 🚀 in Spectre Console Jan 21, 2025
@FrankRay78
Copy link
Contributor Author

Check out this bad boy @patriksvensson ... no trim works perfectly!

https://github.com/FrankRay78/SpeedTestConsole/blob/main/src/SpeedTestConsole.Tests/SpeedTestConsoleTests.cs

    [Fact]
    public async Task Should_Display_Help_When_Run_With_No_Arguments()
    {
        // Given
        var app = new CommandAppTester(new CommandAppTesterSettings { TrimConsoleOutput = false });
        app.Configure(Program.ConfigureAction);

        // When
        var result = await app.RunAsync();

        // Then
        Assert.Equal(0, result.ExitCode);
        await Verify(result.Output);
    }

https://github.com/FrankRay78/SpeedTestConsole/blob/main/src/SpeedTestConsole.Tests/Expectations/SpeedTestConsoleTests.Should_Display_Help_When_Run_With_No_Arguments.verified.txt

   _____                            __   __                  __           ______                                  __      
  / ___/    ____   ___   ___   ____/ /  / /_  ___    _____  / /_         / ____/  ____    ____    _____  ____    / /  ___ 
  \__ \    / __ \ / _ \ / _ \ / __  /  / __/ / _ \  / ___/ / __/        / /      / __ \  / __ \  / ___/ / __ \  / /  / _ \
 ___/ /   / /_/ //  __//  __// /_/ /  / /_  /  __/ (__  ) / /_         / /___   / /_/ / / / / / (__  ) / /_/ / / /  /  __/
/____/   / .___/ \___/ \___/ \__,_/   \__/  \___/ /____/  \__/         \____/   \____/ /_/ /_/ /____/  \____/ /_/   \___/ 
        /_/                                                                                                               

USAGE:
    SpeedTestConsole [OPTIONS] 

OPTIONS:
    -h, --help    Prints help information

COMMANDS:
    servers     Show the nearest speed test servers    
    download    Perform an internet download speed test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Testing-Framework Spectre.Console testing framework for developers. bug Something isn't working
Projects
Status: Done 🚀
Development

Successfully merging a pull request may close this issue.

2 participants