mirror of
https://github.com/flutter/packages.git
synced 2025-06-30 23:03:11 +08:00
[tool] Conditionalize color on stdout
(#4436)
Refactors colorization to a centralized utility file, and makes it all conditional on `stdout` having ANSI escape support. This makes the output more readable on LUCI, which unlike the Cirrus log display doesn't handle ANSI. Fixes https://github.com/flutter/flutter/issues/89392
This commit is contained in:
100
script/tool/test/common/output_utils_test.dart
Normal file
100
script/tool/test/common/output_utils_test.dart
Normal file
@ -0,0 +1,100 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_plugin_tools/src/common/output_utils.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('with color support', () {
|
||||
setUp(() {
|
||||
useColorForOutput = true;
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
useColorForOutput = stdout.supportsAnsiEscapes;
|
||||
});
|
||||
|
||||
test('colorize works', () async {
|
||||
const String message = 'a message';
|
||||
|
||||
expect(
|
||||
colorizeString(message, Styles.MAGENTA), '\x1B[35m$message\x1B[0m');
|
||||
});
|
||||
|
||||
test('printSuccess is green', () async {
|
||||
const String message = 'a message';
|
||||
|
||||
expect(await _capturePrint(() => printSuccess(message)),
|
||||
'\x1B[32m$message\x1B[0m');
|
||||
});
|
||||
|
||||
test('printWarning is yellow', () async {
|
||||
const String message = 'a message';
|
||||
|
||||
expect(await _capturePrint(() => printWarning(message)),
|
||||
'\x1B[33m$message\x1B[0m');
|
||||
});
|
||||
|
||||
test('printError is red', () async {
|
||||
const String message = 'a message';
|
||||
|
||||
expect(await _capturePrint(() => printError(message)),
|
||||
'\x1B[31m$message\x1B[0m');
|
||||
});
|
||||
});
|
||||
|
||||
group('without color support', () {
|
||||
setUp(() {
|
||||
useColorForOutput = false;
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
useColorForOutput = stdout.supportsAnsiEscapes;
|
||||
});
|
||||
|
||||
test('colorize no-ops', () async {
|
||||
const String message = 'a message';
|
||||
|
||||
expect(colorizeString(message, Styles.MAGENTA), message);
|
||||
});
|
||||
|
||||
test('printSuccess just prints', () async {
|
||||
const String message = 'a message';
|
||||
|
||||
expect(await _capturePrint(() => printSuccess(message)), message);
|
||||
});
|
||||
|
||||
test('printWarning just prints', () async {
|
||||
const String message = 'a message';
|
||||
|
||||
expect(await _capturePrint(() => printWarning(message)), message);
|
||||
});
|
||||
|
||||
test('printError just prints', () async {
|
||||
const String message = 'a message';
|
||||
|
||||
expect(await _capturePrint(() => printError(message)), message);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Run the command [runner] with the given [args] and return
|
||||
/// what was printed.
|
||||
/// A custom [errorHandler] can be used to handle the runner error as desired without throwing.
|
||||
Future<String> _capturePrint(void Function() printFunction) async {
|
||||
final StringBuffer output = StringBuffer();
|
||||
final ZoneSpecification spec = ZoneSpecification(
|
||||
print: (_, __, ___, String message) {
|
||||
output.write(message);
|
||||
},
|
||||
);
|
||||
await Zone.current
|
||||
.fork(specification: spec)
|
||||
.run<Future<void>>(() async => printFunction());
|
||||
|
||||
return output.toString();
|
||||
}
|
@ -9,6 +9,7 @@ import 'package:args/command_runner.dart';
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter_plugin_tools/src/common/core.dart';
|
||||
import 'package:flutter_plugin_tools/src/common/output_utils.dart';
|
||||
import 'package:flutter_plugin_tools/src/common/package_looping_command.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:test/test.dart';
|
||||
@ -82,6 +83,8 @@ void main() {
|
||||
late Directory thirdPartyPackagesDir;
|
||||
|
||||
setUp(() {
|
||||
// Correct color handling is part of the behavior being tested here.
|
||||
useColorForOutput = true;
|
||||
fileSystem = MemoryFileSystem();
|
||||
mockPlatform = MockPlatform();
|
||||
packagesDir = createPackagesDirectory(fileSystem: fileSystem);
|
||||
@ -90,6 +93,11 @@ void main() {
|
||||
.childDirectory('packages');
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
// Restore the default behavior.
|
||||
useColorForOutput = io.stdout.supportsAnsiEscapes;
|
||||
});
|
||||
|
||||
/// Creates a TestPackageLoopingCommand instance that uses [gitDiffResponse]
|
||||
/// for git diffs, and logs output to [printOutput].
|
||||
TestPackageLoopingCommand createTestCommand({
|
||||
|
@ -525,8 +525,8 @@ void main() {
|
||||
'Publishing all packages that have changed relative to "HEAD~"'),
|
||||
contains('Running `pub publish ` in ${plugin1.path}...'),
|
||||
contains('Running `pub publish ` in ${plugin2.path}...'),
|
||||
contains('plugin1 - \x1B[32mpublished\x1B[0m'),
|
||||
contains('plugin2/plugin2 - \x1B[32mpublished\x1B[0m'),
|
||||
contains('plugin1 - published'),
|
||||
contains('plugin2/plugin2 - published'),
|
||||
]));
|
||||
expect(
|
||||
processRunner.recordedCalls,
|
||||
|
Reference in New Issue
Block a user