testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Add --ansi option to force Ansi output

Open powercode opened this issue 10 months ago • 9 comments

Summary

Add the complement to --no-ansi, that forces --ansi output, even if output is redirected.

Background and Motivation

When running the tests from a PowerShell script, I may want to display the test output but not have it as output from the PowerShell function.

mytest.exe | Out-Host
if ($LASTEXITCODE){
   throw "test failed"
}

This removes all coloring from the output, even though it's logically equivalent to passing it directly to the terminal for display.

Proposed Feature

Add --ansi parameter that forces ansi output, regardless of redirection. Let the user decide.

Alternative Designs

Always output ansi unless the user explicitly uses --no-ansi. That has the drawback of potentially breaking existing scripts.

powercode avatar Feb 19 '25 22:02 powercode

It's probably a quick and easy update so I don't see why not! Thanks for the suggestion @powercode.

cc @mariam-abdulla as it might require some porting to dotnet test.

Evangelink avatar Feb 20 '25 16:02 Evangelink

Forcing ANSI should only be done if you know if you're running inside of a terminal that can handle it. The standard console host (CONHOST), for example, does not support ANSI:

Image

bradwilson avatar Apr 12 '25 18:04 bradwilson

@bradwilson I agree and that's why this is not the default behavior. The default is trying to auto-detect if we can use ansi or not. We then have flags to force using or not using ANSI.

Evangelink avatar Apr 14 '25 07:04 Evangelink

moving to 3.10, I think we need a better design for this, having --ansi and --no-ansi, leads to many combinations with similar options like --no-color and so on.

nohwnd avatar May 05 '25 12:05 nohwnd

@nohwnd what about --ansi <auto|enable|disable>, --color <enable|disable> (this one may need some deps on ansi?).

I think gradle (java) had something interesting where cli has only part of the commands, for extra granularity you needed the config file. Maybe a similar design could help so we don't have to support it all as CLI.

Evangelink avatar May 05 '25 18:05 Evangelink

FWIW, xUnit.net uses on and off for switches in MTP. https://github.com/xunit/xunit/blob/main/src/xunit.v3.runner.inproc.console/TestingPlatform/CommandLineOptionsProvider.cs

bradwilson avatar May 05 '25 19:05 bradwilson

yes those are all good options, terminal logger does something similar with handling auto, and on/off true/false 0/1.

Then it is just question of deprecating no-ansi no-color or not, but we cannot do that right away anyway.

nohwnd avatar May 06 '25 11:05 nohwnd

@nohwnd I moved to "Future" milestone. But feel free to move to "2.0.0" as a breaking change (and if you did, please add it to https://github.com/microsoft/testfx/issues/5762 and add the "Breaking !!" label)

Youssef1313 avatar Jul 11 '25 09:07 Youssef1313

Given the v2 was missed, what about introducing the new API and deprecating the old ones (but keeping them working)? It feels like something we can implement quickly.

Evangelink avatar Nov 28 '25 17:11 Evangelink