Clean up bots output (#36864)

This commit is contained in:
Ian Hickson
2019-08-20 14:53:39 -07:00
committed by GitHub
parent 441b36526d
commit e0a31de61b
3 changed files with 42 additions and 63 deletions

View File

@@ -10,24 +10,37 @@ import 'package:path/path.dart' as path;
final bool hasColor = stdout.supportsAnsiEscapes;
final String bold = hasColor ? '\x1B[1m' : '';
final String red = hasColor ? '\x1B[31m' : '';
final String green = hasColor ? '\x1B[32m' : '';
final String yellow = hasColor ? '\x1B[33m' : '';
final String cyan = hasColor ? '\x1B[36m' : '';
final String bold = hasColor ? '\x1B[1m' : ''; // used for shard titles
final String red = hasColor ? '\x1B[31m' : ''; // used for errors
final String green = hasColor ? '\x1B[32m' : ''; // used for section titles, commands
final String yellow = hasColor ? '\x1B[33m' : ''; // unused
final String cyan = hasColor ? '\x1B[36m' : ''; // used for paths
final String reverse = hasColor ? '\x1B[7m' : ''; // used for clocks
final String reset = hasColor ? '\x1B[0m' : '';
final String redLine = '$red━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━$reset';
const String arrow = '⏩';
const String clock = '🕐';
const Duration _kLongTimeout = Duration(minutes: 45);
String get clock {
final DateTime now = DateTime.now();
return '$reverse▌'
'${now.hour.toString().padLeft(2, "0")}:'
'${now.minute.toString().padLeft(2, "0")}:'
'${now.second.toString().padLeft(2, "0")}'
'▐$reset';
}
String elapsedTime(DateTime start) {
return DateTime.now().difference(start).toString();
String prettyPrintDuration(Duration duration) {
String result = '';
final int minutes = duration.inMinutes;
if (minutes > 0)
result += '${minutes}min ';
final int seconds = duration.inSeconds - minutes * 60;
final int milliseconds = duration.inMilliseconds - (seconds * 1000 + minutes * 60 * 1000);
result += '$seconds.${milliseconds.toString().padLeft(3, "0")}s';
return result;
}
void printProgress(String action, String workingDir, String command) {
print('$arrow $action: cd $cyan$workingDir$reset; $yellow$command$reset');
print('$clock $action: cd $cyan$workingDir$reset; $green$command$reset');
}
Stream<String> runAndGetStdout(String executable, List<String> arguments, {
@@ -36,7 +49,6 @@ Stream<String> runAndGetStdout(String executable, List<String> arguments, {
bool expectNonZeroExit = false,
int expectedExitCode,
String failureMessage,
Duration timeout = _kLongTimeout,
Function beforeExit,
}) async* {
final String commandDescription = '${path.relative(executable, from: workingDirectory)} ${arguments.join(' ')}';
@@ -44,7 +56,7 @@ Stream<String> runAndGetStdout(String executable, List<String> arguments, {
printProgress('RUNNING', relativeWorkingDir, commandDescription);
final DateTime start = DateTime.now();
final Stopwatch time = Stopwatch()..start();
final Process process = await Process.start(executable, arguments,
workingDirectory: workingDirectory,
environment: environment,
@@ -56,21 +68,18 @@ Stream<String> runAndGetStdout(String executable, List<String> arguments, {
yield line;
}
final int exitCode = await process.exitCode.timeout(timeout, onTimeout: () {
stderr.writeln('Process timed out after $timeout');
return expectNonZeroExit ? 0 : 1;
});
print('$clock ELAPSED TIME: $bold${elapsedTime(start)}$reset for $commandDescription in $relativeWorkingDir: ');
final int exitCode = await process.exitCode;
print('$clock ELAPSED TIME: ${prettyPrintDuration(time.elapsed)} for $green$commandDescription$reset in $cyan$relativeWorkingDir$reset');
if ((exitCode == 0) == expectNonZeroExit || (expectedExitCode != null && exitCode != expectedExitCode)) {
if (failureMessage != null) {
print(failureMessage);
}
print(
'$redLine\n'
'${bold}ERROR:$red Last command exited with $exitCode (expected: ${expectNonZeroExit ? (expectedExitCode ?? 'non-zero') : 'zero'}).$reset\n'
'${bold}Command:$cyan $commandDescription$reset\n'
'${bold}Relative working directory:$red $relativeWorkingDir$reset\n'
'$redLine'
'${bold}ERROR: ${red}Last command exited with $exitCode (expected: ${expectNonZeroExit ? (expectedExitCode ?? 'non-zero') : 'zero'}).$reset\n'
'${bold}Command: $green$commandDescription$reset\n'
'${bold}Relative working directory: $cyan$relativeWorkingDir$reset\n'
'$redLine'
);
beforeExit?.call();
exit(1);
@@ -87,7 +96,6 @@ Future<void> runCommand(String executable, List<String> arguments, {
CapturedOutput output,
bool skip = false,
bool expectFlaky = false,
Duration timeout = _kLongTimeout,
bool Function(String) removeLine,
}) async {
assert((outputMode == OutputMode.capture) == (output != null),
@@ -102,7 +110,7 @@ Future<void> runCommand(String executable, List<String> arguments, {
}
printProgress('RUNNING', relativeWorkingDir, commandDescription);
final DateTime start = DateTime.now();
final Stopwatch time = Stopwatch()..start();
final Process process = await Process.start(executable, arguments,
workingDirectory: workingDirectory,
environment: environment,
@@ -129,11 +137,8 @@ Future<void> runCommand(String executable, List<String> arguments, {
break;
}
final int exitCode = await process.exitCode.timeout(timeout, onTimeout: () {
stderr.writeln('Process timed out after $timeout');
return (expectNonZeroExit || expectFlaky) ? 0 : 1;
});
print('$clock ELAPSED TIME: $bold${elapsedTime(start)}$reset for $commandDescription in $relativeWorkingDir: ');
final int exitCode = await process.exitCode;
print('$clock ELAPSED TIME: ${prettyPrintDuration(time.elapsed)} for $green$commandDescription$reset in $cyan$relativeWorkingDir$reset');
if (output != null) {
output.stdout = _flattenToString(await savedStdout);
@@ -141,9 +146,9 @@ Future<void> runCommand(String executable, List<String> arguments, {
}
// If the test is flaky we don't care about the actual exit.
if (expectFlaky) {
if (expectFlaky)
return;
}
if ((exitCode == 0) == expectNonZeroExit || (expectedExitCode != null && exitCode != expectedExitCode)) {
if (failureMessage != null) {
print(failureMessage);
@@ -162,10 +167,10 @@ Future<void> runCommand(String executable, List<String> arguments, {
}
print(
'$redLine\n'
'${bold}ERROR:$red Last command exited with $exitCode (expected: ${expectNonZeroExit ? (expectedExitCode ?? 'non-zero') : 'zero'}).$reset\n'
'${bold}Command:$cyan $commandDescription$reset\n'
'${bold}Relative working directory:$red $relativeWorkingDir$reset\n'
'$redLine'
'${bold}ERROR: ${red}Last command exited with $exitCode (expected: ${expectNonZeroExit ? (expectedExitCode ?? 'non-zero') : 'zero'}).$reset\n'
'${bold}Command: $green$commandDescription$reset\n'
'${bold}Relative working directory: $cyan$relativeWorkingDir$reset\n'
'$redLine'
);
exit(1);
}