Skip to content

Add --ansi <auto|on|off> CLI option for explicit ANSI control (#5081)#8493

Merged
Evangelink merged 2 commits into
mainfrom
dev/amauryleve/ansi-cli-option
May 22, 2026
Merged

Add --ansi <auto|on|off> CLI option for explicit ANSI control (#5081)#8493
Evangelink merged 2 commits into
mainfrom
dev/amauryleve/ansi-cli-option

Conversation

@Evangelink
Copy link
Copy Markdown
Member

Implements the user-facing --ansi option with auto/on/off values (plus true|enable|1 / false|disable|0 aliases). The new option fulfils issue #5081 by mapping --ansi on to the existing internal AnsiMode.ForceAnsi, which emits ANSI escape codes (including cursor movement) even when stdout is redirected (for example, in mytest.exe | Out-Host pipelines).

Closes #5081.

What

  • New --ansi <auto|on|off> option on the Terminal test reporter extension.
  • Accepted argument aliases: on/true/enable/1 and off/false/disable/0; case-insensitive; invalid values are rejected with a localized message.
  • When both --ansi and --no-ansi are specified, --ansi wins (documented in the help text and verified by integration tests).
  • --ansi auto explicitly opts back into auto-detection and overrides a sibling --no-ansi.
  • New internal CommandLineOptionArgumentValidator helper exposes IsValidBooleanArgument / IsValidBooleanAutoArgument / IsOnValue / IsOffValue / IsAutoValue so future on|off|auto options can share the same parsing.

Tests

  • New unit tests for the validator and the Terminal options provider (CommandLineOptionArgumentValidatorTests, TerminalTestReporterCommandLineOptionsProviderTests).
  • New integration tests in AnsiOptionTests exercising on/off/auto behavior, alias handling, precedence vs --no-ansi, and invalid-argument rejection.
  • Updated MTP and MSTest acceptance --help / --info expectations to include the new option.

Notes

This was previously bundled into #8461 (TestRun.Current / PlannedTests). Split out per code-review request.

Implements the user-facing --ansi option with auto/on/off values (plus
true|enable|1 / false|disable|0 aliases). The new option fulfils issue
#5081 by mapping --ansi on to the existing internal AnsiMode.ForceAnsi,
which emits ANSI escape codes (including cursor movement) even when
stdout is redirected (for example, in 'mytest.exe | Out-Host' pipelines).

- New internal CommandLineOptionArgumentValidator helper exposes
  IsValidBooleanArgument / IsValidBooleanAutoArgument / IsOnValue /
  IsOffValue / IsAutoValue for reuse by future on|off|auto options.
- --no-ansi continues to work unchanged. When both --ansi and --no-ansi
  are specified, --ansi wins (documented in the help text).
- Updated MTP and MSTest acceptance test --help and --info expectations
  to include the new option.
- Added unit tests for the validator and the provider plus integration
  tests verifying the on/off/auto behavior, alias handling, and invalid
  argument rejection.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an explicit --ansi <auto|on|off> CLI option to the Terminal test reporter (Microsoft.Testing.Platform), including parsing/validation helpers, localized help/error strings, and updated acceptance/unit tests to cover the new behavior and help output.

Changes:

  • Introduces --ansi <auto|on|off> (with true|enable|1 and false|disable|0 aliases) and wires it to AnsiMode.ForceAnsi/NoAnsi/auto detection.
  • Adds CommandLineOptionArgumentValidator helper for reusable boolean/boolean+auto argument validation.
  • Updates localized resources and --help/--info acceptance expectations; adds new unit + acceptance coverage for the new option.
Show a summary per file
File Description
test/UnitTests/Microsoft.Testing.Platform.UnitTests/OutputDevice/Terminal/TerminalTestReporterCommandLineOptionsProviderTests.cs Unit tests for --ansi option arity/visibility and accepted/rejected argument values.
test/UnitTests/Microsoft.Testing.Platform.UnitTests/CommandLine/CommandLineOptionArgumentValidatorTests.cs Unit tests for shared on/off/auto argument validation helper.
test/IntegrationTests/MSTest.Acceptance.IntegrationTests/HelpInfoTests.cs Updates MSTest host --help expectations to include --ansi.
test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/HelpInfoTests.cs Updates MTP host --help/--info expectations to include --ansi option details.
test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/HelpInfoAllExtensionsTests.cs Updates “all extensions” --help/--info expectations to include --ansi.
test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/AnsiOptionTests.cs New acceptance tests covering --ansi on/off/auto behavior, precedence, and invalid argument handling.
src/Platform/Microsoft.Testing.Platform/Resources/PlatformResources.resx Adds localized resource strings for --ansi description and invalid-argument message.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.cs.xlf Regenerated localization entry for new --ansi strings (Czech).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.de.xlf Regenerated localization entry for new --ansi strings (German).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.es.xlf Regenerated localization entry for new --ansi strings (Spanish).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.fr.xlf Regenerated localization entry for new --ansi strings (French).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.it.xlf Regenerated localization entry for new --ansi strings (Italian).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ja.xlf Regenerated localization entry for new --ansi strings (Japanese).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ko.xlf Regenerated localization entry for new --ansi strings (Korean).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pl.xlf Regenerated localization entry for new --ansi strings (Polish).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pt-BR.xlf Regenerated localization entry for new --ansi strings (Portuguese - Brazil).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ru.xlf Regenerated localization entry for new --ansi strings (Russian).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.tr.xlf Regenerated localization entry for new --ansi strings (Turkish).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hans.xlf Regenerated localization entry for new --ansi strings (Chinese - Simplified).
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hant.xlf Regenerated localization entry for new --ansi strings (Chinese - Traditional).
src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TerminalTestReporterCommandLineOptionsProvider.cs Adds --ansi option definition + argument validation using the shared validator helper.
src/Platform/Microsoft.Testing.Platform/OutputDevice/TerminalOutputDevice.cs Implements --ansi precedence rules and maps values to AnsiMode selection.
src/Platform/Microsoft.Testing.Platform/CommandLine/CommandLineOptionArgumentValidator.cs New shared helper for on/off/auto argument validation and alias handling.

Copilot's findings

  • Files reviewed: 23/23 changed files
  • Comments generated: 2

- AnsiOption_MissingArgument_FailsCommandLineValidation: assert the actual
  arity-validation message (CommandLineOptionsValidator rejects --ansi without
  a value before per-occurrence validation runs, so the custom
  TerminalAnsiOptionInvalidArgument text is not used here).
- AnsiOption_Auto_OverridesNoAnsi_AndFollowsEnvironmentDetection: rewrite to
  prove the override deterministically by forcing GITHUB_ACTIONS=true and
  clearing known LLM env vars so '--ansi auto' maps to SimpleAnsi and emits
  ESC, demonstrating it won over the auto-injected --no-ansi.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Evangelink Evangelink merged commit 8359a8a into main May 22, 2026
75 of 77 checks passed
@Evangelink Evangelink deleted the dev/amauryleve/ansi-cli-option branch May 22, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --ansi option to force Ansi output

2 participants