From 1328997b08fa19f366a50aa7f1fa1195e60abf3f Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Thu, 26 Oct 2023 23:18:17 -0700 Subject: [PATCH] give `throwsToolExit` a more useful description (#136694) Fixes https://github.com/flutter/flutter/issues/136698. Alters how `throwToolExit` creates its matcher. This results is an improved description of the matcher. The mismatch description isn't improved by this, but I writing an entirely custom matcher to fix this isn't ideal either. We can instead mitigate the issue by augmenting the `toString` implementation of `ToolExit` to include the exit code, if it is non-null. With these changes, the first few lines of output from a test would look like this: ``` Expected: throws with `exitCode`: <42> and `message`: contains 'message' Actual: Never> Which: threw ToolExit: ``` --- packages/flutter_tools/lib/src/base/common.dart | 2 +- .../test/general.shard/resident_runner_test.dart | 2 +- .../debug_adapter/flutter_adapter_test.dart | 2 +- packages/flutter_tools/test/src/common.dart | 15 ++++++++------- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/common.dart b/packages/flutter_tools/lib/src/base/common.dart index ecac504655..4ee40046c5 100644 --- a/packages/flutter_tools/lib/src/base/common.dart +++ b/packages/flutter_tools/lib/src/base/common.dart @@ -21,5 +21,5 @@ class ToolExit implements Exception { final int? exitCode; @override - String toString() => 'Exception: $message'; // TODO(ianh): Really this should say "Error". + String toString() => 'Error: $message'; } diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 2af0b71530..65b6e4428d 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -1298,7 +1298,7 @@ flutter: await residentRunner.runSourceGenerators(); - expect(testLogger.errorText, allOf(contains('Exception'))); + expect(testLogger.errorText, contains('Error')); expect(testLogger.statusText, isEmpty); })); diff --git a/packages/flutter_tools/test/integration.shard/debug_adapter/flutter_adapter_test.dart b/packages/flutter_tools/test/integration.shard/debug_adapter/flutter_adapter_test.dart index 8de1a2bddf..9127bd1cef 100644 --- a/packages/flutter_tools/test/integration.shard/debug_adapter/flutter_adapter_test.dart +++ b/packages/flutter_tools/test/integration.shard/debug_adapter/flutter_adapter_test.dart @@ -184,7 +184,7 @@ void main() { final String output = _uniqueOutputLines(outputEvents); expect(output, contains('this code does not compile')); - expect(output, contains('Exception: Failed to build')); + expect(output, contains('Error: Failed to build')); expect(output, contains('Exited (1)')); }); diff --git a/packages/flutter_tools/test/src/common.dart b/packages/flutter_tools/test/src/common.dart index 5de5ce51fc..a5d05f6ca6 100644 --- a/packages/flutter_tools/test/src/common.dart +++ b/packages/flutter_tools/test/src/common.dart @@ -93,19 +93,20 @@ Future capturedConsolePrint(Future Function() body) async { final Matcher throwsAssertionError = throwsA(isA()); /// Matcher for functions that throw [ToolExit]. +/// +/// [message] is matched using the [contains] matcher. Matcher throwsToolExit({ int? exitCode, Pattern? message }) { - Matcher matcher = _isToolExit; + TypeMatcher result = const TypeMatcher(); + if (exitCode != null) { - matcher = allOf(matcher, (ToolExit e) => e.exitCode == exitCode); + result = result.having((ToolExit e) => e.exitCode, 'exitCode', equals(exitCode)); } if (message != null) { - matcher = allOf(matcher, (ToolExit e) => e.message?.contains(message) ?? false); + result = result.having((ToolExit e) => e.message, 'message', contains(message)); } - return throwsA(matcher); -} -/// Matcher for [ToolExit]s. -final TypeMatcher _isToolExit = isA(); + return throwsA(result); +} /// Matcher for functions that throw [UsageException]. Matcher throwsUsageException({Pattern? message }) {