Revert "Revert "Add many more global analyses. (#47875)" (#48080)" (#48081)

This reverts commit 04ea3183ce67c62def5852a5c2615e38188195ca.
diff --git a/dev/bots/test.dart b/dev/bots/test.dart
index d183330..f756d70 100644
--- a/dev/bots/test.dart
+++ b/dev/bots/test.dart
@@ -13,6 +13,7 @@
 
 import 'flutter_compact_formatter.dart';
 import 'run_command.dart';
+import 'utils.dart';
 
 typedef ShardRunner = Future<void> Function();
 
@@ -98,22 +99,28 @@
 /// SHARD=tool_tests bin/cache/dart-sdk/bin/dart dev/bots/test.dart
 /// bin/cache/dart-sdk/bin/dart dev/bots/test.dart --local-engine=host_debug_unopt
 Future<void> main(List<String> args) async {
-  flutterTestArgs.addAll(args);
-  if (Platform.environment.containsKey(CIRRUS_TASK_NAME))
-    print('Running task: ${Platform.environment[CIRRUS_TASK_NAME]}');
-  print('═' * 80);
-  await _runSmokeTests();
-  print('═' * 80);
-  await selectShard(const <String, ShardRunner>{
-    'add_to_app_tests': _runAddToAppTests,
-    'build_tests': _runBuildTests,
-    'framework_coverage': _runFrameworkCoverage,
-    'framework_tests': _runFrameworkTests,
-    'hostonly_devicelab_tests': _runHostOnlyDeviceLabTests,
-    'tool_coverage': _runToolCoverage,
-    'tool_tests': _runToolTests,
-    'web_tests': _runWebTests,
-  });
+  print('$clock STARTING ANALYSIS');
+  try {
+    flutterTestArgs.addAll(args);
+    if (Platform.environment.containsKey(CIRRUS_TASK_NAME))
+      print('Running task: ${Platform.environment[CIRRUS_TASK_NAME]}');
+    print('═' * 80);
+    await _runSmokeTests();
+    print('═' * 80);
+    await selectShard(const <String, ShardRunner>{
+      'add_to_app_tests': _runAddToAppTests,
+      'build_tests': _runBuildTests,
+      'framework_coverage': _runFrameworkCoverage,
+      'framework_tests': _runFrameworkTests,
+      'hostonly_devicelab_tests': _runHostOnlyDeviceLabTests,
+      'tool_coverage': _runToolCoverage,
+      'tool_tests': _runToolTests,
+      'web_tests': _runWebTests,
+    });
+  } on ExitException catch (error) {
+    error.apply();
+  }
+  print('$clock ${bold}Test successful.$reset');
 }
 
 Future<void> _runSmokeTests() async {
@@ -195,12 +202,8 @@
 
   // Verify that we correctly generated the version file.
   final String versionError = await verifyVersion(File(path.join(flutterRoot, 'version')));
-  if (versionError != null) {
-    print(redLine);
-    print(versionError);
-    print(redLine);
-    exit(1);
-  }
+  if (versionError != null)
+    exitWithError(<String>[versionError]);
 }
 
 Future<bq.BigqueryApi> _getBigqueryApi() async {
@@ -624,13 +627,17 @@
   }
   if (useFlutterTestFormatter) {
     final FlutterCompactFormatter formatter = FlutterCompactFormatter();
-    final Stream<String> testOutput = runAndGetStdout(
-      pub,
-      args,
-      workingDirectory: workingDirectory,
-      environment: pubEnvironment,
-      beforeExit: formatter.finish,
-    );
+    Stream<String> testOutput;
+    try {
+      testOutput = runAndGetStdout(
+        pub,
+        args,
+        workingDirectory: workingDirectory,
+        environment: pubEnvironment,
+      );
+    } finally {
+      formatter.finish();
+    }
     await _processTestOutput(formatter, testOutput, tableData);
   } else {
     await runCommand(
@@ -707,26 +714,26 @@
 
     if (outputChecker != null) {
       final String message = outputChecker(output);
-      if (message != null) {
-        print('$redLine');
-        print(message);
-        print('$redLine');
-        exit(1);
-      }
+      if (message != null)
+        exitWithError(<String>[message]);
     }
     return;
   }
 
   if (useFlutterTestFormatter) {
     final FlutterCompactFormatter formatter = FlutterCompactFormatter();
-    final Stream<String> testOutput = runAndGetStdout(
-      flutter,
-      args,
-      workingDirectory: workingDirectory,
-      expectNonZeroExit: expectFailure,
-      beforeExit: formatter.finish,
-      environment: environment,
-    );
+    Stream<String> testOutput;
+    try {
+      testOutput = runAndGetStdout(
+        flutter,
+        args,
+        workingDirectory: workingDirectory,
+        expectNonZeroExit: expectFailure,
+        environment: environment,
+      );
+    } finally {
+      formatter.finish();
+    }
     await _processTestOutput(formatter, testOutput, tableData);
   } else {
     await runCommand(