blob: 757202380c5f0cfb887adb4a856f3d6b26d8fb96 [file] [log] [blame]
// 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();
}