Simplify the logic for unicode and colors in output (#2679)
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index 4ef5167..1713f9c 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -398,12 +398,12 @@
///
/// On Windows or when not printing to a terminal, only printable ASCII
/// characters should be used.
+///
+/// Tests should make sure to run the subprocess with or without an attached
+/// terminal to decide if colors will be provided.
bool get canUseAnsiCodes =>
forceColors ||
- (!runningFromTest &&
- !runningAsTest &&
- stdioType(stdout) == StdioType.terminal &&
- stdout.supportsAnsiEscapes);
+ (stdioType(stdout) == StdioType.terminal && stdout.supportsAnsiEscapes);
/// Gets an ANSI escape if those are supported by stdout (or nothing).
String getAnsi(String ansiCode) => canUseAnsiCodes ? ansiCode : '';
@@ -420,7 +420,6 @@
bool get canUseUnicode =>
// The tests support unicode also on windows.
runningFromTest ||
- runningAsTest ||
// When not outputting to terminal we can also use unicode.
stdioType(stdout) != StdioType.terminal ||
!Platform.isWindows ||
diff --git a/test/ascii_tree_test.dart b/test/ascii_tree_test.dart
index 6c2d9be..5d82d49 100644
--- a/test/ascii_tree_test.dart
+++ b/test/ascii_tree_test.dart
@@ -5,10 +5,15 @@
import 'package:pub/src/ascii_tree.dart' as tree;
import 'package:test/test.dart';
+/// Removes ansi color codes from [s].
+String stripColors(String s) {
+ return s.replaceAll(RegExp('\u001b\\[.*?m'), '');
+}
+
void main() {
group('tree.fromFiles', () {
test('no files', () {
- expect(tree.fromFiles([]), equals(''));
+ expect(stripColors(tree.fromFiles([])), equals(''));
});
test('up to ten files in one directory are shown', () {
@@ -24,7 +29,7 @@
'dir/i.dart',
'dir/j.dart'
];
- expect(tree.fromFiles(files), equals("""
+ expect(stripColors(tree.fromFiles(files)), equals("""
'-- dir
|-- a.dart
|-- b.dart
@@ -53,7 +58,7 @@
'dir/j.dart',
'dir/k.dart'
];
- expect(tree.fromFiles(files), equals("""
+ expect(stripColors(tree.fromFiles(files)), equals("""
'-- dir
|-- a.dart
|-- b.dart
@@ -79,7 +84,7 @@
'j.dart',
'k.dart'
];
- expect(tree.fromFiles(files), equals("""
+ expect(stripColors(tree.fromFiles(files)), equals("""
|-- a.dart
|-- b.dart
|-- c.dart
@@ -119,7 +124,7 @@
'test/path_windows_test.dart'
];
- expect(tree.fromFiles(files), equals("""
+ expect(stripColors(tree.fromFiles(files)), equals("""
|-- .gitignore
|-- README.md
|-- TODO
@@ -145,7 +150,7 @@
group('treeFromMap', () {
test('empty map', () {
- expect(tree.fromMap({}), equals(''));
+ expect(stripColors(tree.fromMap({})), equals(''));
});
test('a complex example', () {
@@ -174,7 +179,7 @@
}
};
- expect(tree.fromMap(map), equals("""
+ expect(stripColors(tree.fromMap(map)), equals("""
|-- .gitignore
|-- README.md
|-- TODO
@@ -218,7 +223,7 @@
'l.dart': <String, Map>{},
}
};
- expect(tree.fromMap(map, showAllChildren: true), equals("""
+ expect(stripColors(tree.fromMap(map, showAllChildren: true)), equals("""
'-- dir
|-- a.dart
|-- b.dart