Improve canUseSpecialChars on Linux and Mac (#2545)
Fix output to non-terminals which are unlikely to support ascii escape
characters. Prior implementation was overly optimistic for linux and mac
and always allowed ascii because the SDK implementation checking support
is contrarily overly restrictive. Add at least a check for a interactive
terminal to fix some false positives.
diff --git a/pkgs/test_core/CHANGELOG.md b/pkgs/test_core/CHANGELOG.md
index 27324b6..61d8b60 100644
--- a/pkgs/test_core/CHANGELOG.md
+++ b/pkgs/test_core/CHANGELOG.md
@@ -5,6 +5,9 @@
* Require a function definition named `main` directly in a test suite and
provide a more direct error message than a failing compiler output.
* Suppress skip reason messages in the compact and failures-only reporters.
+* Improve fidelity of checks for using ascii characters. Check the SDK reported
+ support on windows, and assume ascii support for all terminals on linux since
+ the SDK reported support is much more narrow.
* Fix default coverage filter when running in a workspace package. Default
filter now includes all the workspace's package.
* Add support for reading test package version within pub workspaces.
diff --git a/pkgs/test_core/lib/src/util/io.dart b/pkgs/test_core/lib/src/util/io.dart
index e0c8e27..4a1fdc4 100644
--- a/pkgs/test_core/lib/src/util/io.dart
+++ b/pkgs/test_core/lib/src/util/io.dart
@@ -95,11 +95,22 @@
? Platform.environment['_UNITTEST_TEMP_DIR']!
: Directory.systemTemp.path;
-/// Whether or not the current terminal supports ansi escape codes.
+/// Whether [stdout] supports ANSI escape codes.
///
/// Otherwise only printable ASCII characters should be used.
-bool get canUseSpecialChars =>
- (!Platform.isWindows || stdout.supportsAnsiEscapes) && !inTestTests;
+bool get canUseSpecialChars {
+ if (inTestTests) return false;
+
+ if (Platform.isWindows) return stdout.supportsAnsiEscapes;
+
+ // On Linux and Mac, `supportsAnsiEscapes` always returns `false` on most
+ // modern terminals, see https://github.com/dart-lang/sdk/issues/31606 for
+ // details.
+ //
+ // Instead of relying on `supportsAnsiEscapes`, we assume that all modern
+ // terminals support colors and check that `stdout` is a tty.
+ return stdioType(stdout) == StdioType.terminal;
+}
/// Detect whether we're running in a Github Actions context.
///