Add --dry-run to dev/bots/test.dart. (#158956)

Closes https://github.com/flutter/flutter/issues/158884.

/cc @reidbaker.

Example output:

```sh
% SHARD=tool_integration_tests SUBSHARD=6_6 dart dev/bots/test.dart --dry-run 
▌13:59:18▐ STARTING ANALYSIS
▌13:59:18▐ --dry-run enabled. Tests will not actually be executed.
▌13:59:18▐ SHARD=tool_integration_tests
▌13:59:18▐ SUBSHARD=6_6
▌13:59:18▐ |> bin/flutter: 
  --version
▌13:59:18▐ |> bin/cache/dart-sdk/bin/dart (packages/flutter_tools): 
  run
  test
  --reporter=expanded
  --file-reporter=json:/var/folders/qw/qw_3qd1x4kz5w975jhdq4k58007b7h/T/metrics_1731621558619861.json
  --test-randomize-ordering-seed=20241114
  -j1
  test/integration.shard/shader_compiler_test.dart
  test/integration.shard/overall_experience_test.dart
  test/integration.shard/expression_evaluation_test.dart
  test/integration.shard/isolated/native_assets_without_cbuild_assemble_test.dart
  test/integration.shard/isolated/native_assets_test.dart
  test/integration.shard/isolated/native_assets_agp_version_test.dart
  test/integration.shard/coverage_collection_test.dart
  test/integration.shard/devtools_uri_test.dart
  test/integration.shard/deprecated_gradle_settings_test.dart
  test/integration.shard/batch_entrypoint_test.dart
  test/integration.shard/bash_entrypoint_test.dart
  test/integration.shard/background_isolate_test.dart
```
This commit is contained in:
Matan Lurey
2024-11-20 13:01:54 -08:00
committed by GitHub
parent f2446f2dfb
commit bf36437cae
5 changed files with 106 additions and 28 deletions

View File

@@ -68,9 +68,27 @@ const String CIRRUS_TASK_NAME = 'CIRRUS_TASK_NAME';
final Map<String,String> localEngineEnv = <String, String>{};
/// The arguments to pass to `flutter test` (typically the local engine
/// configuration) -- prefilled with the arguments passed to test.dart.
/// configuration) -- prefilled with the arguments passed to test.dart.
final List<String> flutterTestArgs = <String>[];
/// Whether execution should be simulated for debugging purposes.
///
/// When `true`, calls to [runCommand] print to [io.stdout] instead of running
/// the process. This is useful for determing what an invocation of `test.dart`
/// _might_ due if not invoked with `--dry-run`, or otherwise determine what the
/// different test shards and sub-shards are configured as.
bool get dryRun => _dryRun ?? false;
/// Switches [dryRun] to `true`.
///
/// Expected to be called at most once during execution of a process.
void enableDryRun() {
if (_dryRun != null) {
throw StateError('Should only be called at most once');
}
_dryRun = true;
}
bool? _dryRun;
const int kESC = 0x1B;
const int kOpenSquareBracket = 0x5B;
@@ -135,6 +153,10 @@ final List<String> _pendingLogs = <String>[];
Timer? _hideTimer; // When this is null, the output is verbose.
void foundError(List<String> messages) {
if (dryRun) {
printProgress(messages.join('\n'));
return;
}
assert(messages.isNotEmpty);
// Make the error message easy to notice in the logs by
// wrapping it in a red box.
@@ -413,6 +435,10 @@ Future<void> runDartTest(String workingDirectory, {
removeLine: useBuildRunner ? (String line) => line.startsWith('[INFO]') : null,
);
if (dryRun) {
return;
}
final TestFileReporterResults test = TestFileReporterResults.fromFile(metricFile); // --file-reporter name
final File info = fileSystem.file(path.join(flutterRoot, 'error.log'));
info.writeAsStringSync(json.encode(test.errors));
@@ -508,7 +534,9 @@ Future<void> runFlutterTest(String workingDirectory, {
// metriciFile is a transitional file that needs to be deleted once it is parsed.
// TODO(godofredoc): Ensure metricFile is parsed and aggregated before deleting.
// https://github.com/flutter/flutter/issues/146003
metricFile.deleteSync();
if (!dryRun) {
metricFile.deleteSync();
}
if (outputChecker != null) {
final String? message = outputChecker(result);
@@ -619,27 +647,33 @@ List<T> selectIndexOfTotalSubshard<T>(List<T> tests, {String subshardKey = kSubs
}
Future<void> _runFromList(Map<String, ShardRunner> items, String key, String name, int positionInTaskName) async {
String? item = Platform.environment[key];
if (item == null && Platform.environment.containsKey(CIRRUS_TASK_NAME)) {
final List<String> parts = Platform.environment[CIRRUS_TASK_NAME]!.split('-');
assert(positionInTaskName < parts.length);
item = parts[positionInTaskName];
}
if (item == null) {
for (final String currentItem in items.keys) {
printProgress('$bold$key=$currentItem$reset');
await items[currentItem]!();
try {
String? item = Platform.environment[key];
if (item == null && Platform.environment.containsKey(CIRRUS_TASK_NAME)) {
final List<String> parts = Platform.environment[CIRRUS_TASK_NAME]!.split('-');
assert(positionInTaskName < parts.length);
item = parts[positionInTaskName];
}
} else {
printProgress('$bold$key=$item$reset');
if (!items.containsKey(item)) {
foundError(<String>[
'${red}Invalid $name: $item$reset',
'The available ${name}s are: ${items.keys.join(", ")}',
]);
return;
if (item == null) {
for (final String currentItem in items.keys) {
printProgress('$bold$key=$currentItem$reset');
await items[currentItem]!();
}
} else {
printProgress('$bold$key=$item$reset');
if (!items.containsKey(item)) {
foundError(<String>[
'${red}Invalid $name: $item$reset',
'The available ${name}s are: ${items.keys.join(", ")}',
]);
return;
}
await items[item]!();
}
} catch (_) {
if (!dryRun) {
rethrow;
}
await items[item]!();
}
}