Auto-format Framework (#160545)

This auto-formats all *.dart files in the repository outside of the
`engine` subdirectory and enforces that these files stay formatted with
a presubmit check.

**Reviewers:** Please carefully review all the commits except for the
one titled "formatted". The "formatted" commit was auto-generated by
running `dev/tools/format.sh -a -f`. The other commits were hand-crafted
to prepare the repo for the formatting change. I recommend reviewing the
commits one-by-one via the "Commits" tab and avoiding Github's "Files
changed" tab as it will likely slow down your browser because of the
size of this PR.

---------

Co-authored-by: Kate Lovett <katelovett@google.com>
Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
This commit is contained in:
Michael Goderbauer
2024-12-19 12:06:21 -08:00
committed by GitHub
parent 8e0993eda8
commit 5491c8c146
4411 changed files with 455108 additions and 415991 deletions

View File

@@ -24,14 +24,21 @@ class CustomerTest {
continue;
}
final bool isUnknownDirective = _TestDirective.values.firstWhereOrNull((_TestDirective d) => line.startsWith(d.name)) == null;
final bool isUnknownDirective =
_TestDirective.values.firstWhereOrNull((_TestDirective d) => line.startsWith(d.name)) ==
null;
if (isUnknownDirective) {
throw FormatException('${errorPrefix}Unexpected directive:\n$line');
}
_maybeAddTestConfig(line, directive: _TestDirective.contact, directiveValues: contacts);
_maybeAddTestConfig(line, directive: _TestDirective.fetch, directiveValues: fetch);
_maybeAddTestConfig(line, directive: _TestDirective.setup, directiveValues: setup, platformAgnostic: false);
_maybeAddTestConfig(
line,
directive: _TestDirective.setup,
directiveValues: setup,
platformAgnostic: false,
);
final String updatePrefix = _directive(_TestDirective.update);
if (line.startsWith(updatePrefix)) {
@@ -41,45 +48,71 @@ class CustomerTest {
final String iterationsPrefix = _directive(_TestDirective.iterations);
if (line.startsWith(iterationsPrefix)) {
if (iterations != null) {
throw FormatException('Cannot specify "${_TestDirective.iterations.name}" directive multiple times.');
throw FormatException(
'Cannot specify "${_TestDirective.iterations.name}" directive multiple times.',
);
}
iterations = int.parse(line.substring(iterationsPrefix.length));
if (iterations < 1) {
throw FormatException('The "${_TestDirective.iterations.name}" directive must have a positive integer value.');
throw FormatException(
'The "${_TestDirective.iterations.name}" directive must have a positive integer value.',
);
}
}
if (line.startsWith(_directive(_TestDirective.test)) || line.startsWith('${_TestDirective.test.name}.')) {
if (line.startsWith(_directive(_TestDirective.test)) ||
line.startsWith('${_TestDirective.test.name}.')) {
hasTests = true;
}
_maybeAddTestConfig(line, directive: _TestDirective.test, directiveValues: test, platformAgnostic: false);
_maybeAddTestConfig(
line,
directive: _TestDirective.test,
directiveValues: test,
platformAgnostic: false,
);
}
if (contacts.isEmpty) {
throw FormatException('${errorPrefix}No "${_TestDirective.contact.name}" directives specified. At least one contact e-mail address must be specified.');
throw FormatException(
'${errorPrefix}No "${_TestDirective.contact.name}" directives specified. At least one contact e-mail address must be specified.',
);
}
for (final String email in contacts) {
if (!email.contains(_email) || email.endsWith('@example.com')) {
throw FormatException('${errorPrefix}The following e-mail address appears to be an invalid e-mail address: $email');
throw FormatException(
'${errorPrefix}The following e-mail address appears to be an invalid e-mail address: $email',
);
}
}
if (fetch.isEmpty) {
throw FormatException('${errorPrefix}No "${_TestDirective.fetch.name}" directives specified. Two lines are expected: "git clone https://github.com/USERNAME/REPOSITORY.git tests" and "git -C tests checkout HASH".');
throw FormatException(
'${errorPrefix}No "${_TestDirective.fetch.name}" directives specified. Two lines are expected: "git clone https://github.com/USERNAME/REPOSITORY.git tests" and "git -C tests checkout HASH".',
);
}
if (fetch.length < 2) {
throw FormatException('${errorPrefix}Only one "${_TestDirective.fetch.name}" directive specified. Two lines are expected: "git clone https://github.com/USERNAME/REPOSITORY.git tests" and "git -C tests checkout HASH".');
throw FormatException(
'${errorPrefix}Only one "${_TestDirective.fetch.name}" directive specified. Two lines are expected: "git clone https://github.com/USERNAME/REPOSITORY.git tests" and "git -C tests checkout HASH".',
);
}
if (!fetch[0].contains(_fetch1)) {
throw FormatException('${errorPrefix}First "${_TestDirective.fetch.name}" directive does not match expected pattern (expected "git clone https://github.com/USERNAME/REPOSITORY.git tests").');
throw FormatException(
'${errorPrefix}First "${_TestDirective.fetch.name}" directive does not match expected pattern (expected "git clone https://github.com/USERNAME/REPOSITORY.git tests").',
);
}
if (!fetch[1].contains(_fetch2)) {
throw FormatException('${errorPrefix}Second "${_TestDirective.fetch.name}" directive does not match expected pattern (expected "git -C tests checkout HASH").');
throw FormatException(
'${errorPrefix}Second "${_TestDirective.fetch.name}" directive does not match expected pattern (expected "git -C tests checkout HASH").',
);
}
if (update.isEmpty) {
throw FormatException('${errorPrefix}No "${_TestDirective.update.name}" directives specified. At least one directory must be specified. (It can be "." to just upgrade the root of the repository.)');
throw FormatException(
'${errorPrefix}No "${_TestDirective.update.name}" directives specified. At least one directory must be specified. (It can be "." to just upgrade the root of the repository.)',
);
}
if (!hasTests) {
throw FormatException('${errorPrefix}No "${_TestDirective.test.name}" directives specified. At least one command must be specified to run tests.');
throw FormatException(
'${errorPrefix}No "${_TestDirective.test.name}" directives specified. At least one command must be specified to run tests.',
);
}
return CustomerTest._(
List<String>.unmodifiable(contacts),
@@ -101,9 +134,15 @@ class CustomerTest {
);
// (e-mail regexp from HTML standard)
static final RegExp _email = RegExp(r"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");
static final RegExp _fetch1 = RegExp(r'^git(?: -c core.longPaths=true)? clone https://github.com/[-a-zA-Z0-9]+/[-_a-zA-Z0-9]+.git tests$');
static final RegExp _fetch2 = RegExp(r'^git(?: -c core.longPaths=true)? -C tests checkout [0-9a-f]+$');
static final RegExp _email = RegExp(
r"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$",
);
static final RegExp _fetch1 = RegExp(
r'^git(?: -c core.longPaths=true)? clone https://github.com/[-a-zA-Z0-9]+/[-_a-zA-Z0-9]+.git tests$',
);
static final RegExp _fetch2 = RegExp(
r'^git(?: -c core.longPaths=true)? -C tests checkout [0-9a-f]+$',
);
final List<String> contacts;
final List<String> fetch;
@@ -118,9 +157,8 @@ class CustomerTest {
required List<String> directiveValues,
bool platformAgnostic = true,
}) {
final List<_PlatformType> platforms = platformAgnostic
? <_PlatformType>[_PlatformType.all]
: _PlatformType.values;
final List<_PlatformType> platforms =
platformAgnostic ? <_PlatformType>[_PlatformType.all] : _PlatformType.values;
for (final _PlatformType platform in platforms) {
final String directiveName = _directive(directive, platform: platform);
if (line.startsWith(directiveName) && platform.conditionMet) {
@@ -129,10 +167,7 @@ class CustomerTest {
}
}
static String _directive(
_TestDirective directive, {
_PlatformType platform = _PlatformType.all,
}) {
static String _directive(_TestDirective directive, {_PlatformType platform = _PlatformType.all}) {
return switch (platform) {
_PlatformType.all => '${directive.name}=',
_ => '${directive.name}.${platform.name}=',
@@ -148,19 +183,12 @@ enum _PlatformType {
posix;
bool get conditionMet => switch (this) {
_PlatformType.all => true,
_PlatformType.windows => Platform.isWindows,
_PlatformType.macos => Platform.isMacOS,
_PlatformType.linux => Platform.isLinux,
_PlatformType.posix => Platform.isLinux || Platform.isMacOS,
};
_PlatformType.all => true,
_PlatformType.windows => Platform.isWindows,
_PlatformType.macos => Platform.isMacOS,
_PlatformType.linux => Platform.isLinux,
_PlatformType.posix => Platform.isLinux || Platform.isMacOS,
};
}
enum _TestDirective {
contact,
fetch,
setup,
update,
test,
iterations,
}
enum _TestDirective { contact, fetch, setup, update, test, iterations }

View File

@@ -34,7 +34,9 @@ Future<bool> runTests({
final String s = files.length == 1 ? '' : 's';
if (numberShards > 1) {
final String ss = shardedFiles.length == 1 ? '' : 's';
print('${files.length} file$s specified. ${shardedFiles.length} test$ss in shard #$shardIndex ($numberShards shards total).');
print(
'${files.length} file$s specified. ${shardedFiles.length} test$ss in shard #$shardIndex ($numberShards shards total).',
);
} else {
print('${files.length} file$s specified.');
}
@@ -78,14 +80,21 @@ Future<bool> runTests({
bool success = true;
final Directory checkout = Directory.systemTemp.createTempSync('flutter_customer_testing.${path.basenameWithoutExtension(file.path)}.');
final Directory checkout = Directory.systemTemp.createTempSync(
'flutter_customer_testing.${path.basenameWithoutExtension(file.path)}.',
);
if (verbose) {
print('Created temporary directory: ${checkout.path}');
}
try {
assert(instructions.fetch.isNotEmpty);
for (final String fetchCommand in instructions.fetch) {
success = await shell(fetchCommand, checkout, verbose: verbose, silentFailure: skipOnFetchFailure);
success = await shell(
fetchCommand,
checkout,
verbose: verbose,
silentFailure: skipOnFetchFailure,
);
if (!success) {
if (skipOnFetchFailure) {
if (verbose) {
@@ -105,34 +114,38 @@ Future<bool> runTests({
if (verbose) {
print('Running setup command: $setupCommand');
}
success = await shell(
setupCommand,
customerRepo,
verbose: verbose,
);
success = await shell(setupCommand, customerRepo, verbose: verbose);
if (!success) {
failure('Setup command failed: $setupCommand');
break;
}
}
for (final Directory updateDirectory in instructions.update) {
final Directory resolvedUpdateDirectory = Directory(path.join(customerRepo.path, updateDirectory.path));
final Directory resolvedUpdateDirectory = Directory(
path.join(customerRepo.path, updateDirectory.path),
);
if (verbose) {
print('Updating code in ${resolvedUpdateDirectory.path}...');
}
if (!File(path.join(resolvedUpdateDirectory.path, 'pubspec.yaml')).existsSync()) {
failure('The directory ${updateDirectory.path}, which was specified as an update directory, does not contain a "pubspec.yaml" file.');
failure(
'The directory ${updateDirectory.path}, which was specified as an update directory, does not contain a "pubspec.yaml" file.',
);
success = false;
break;
}
success = await shell('flutter packages get', resolvedUpdateDirectory, verbose: verbose);
if (!success) {
failure('Could not run "flutter pub get" in ${updateDirectory.path}, which was specified as an update directory.');
failure(
'Could not run "flutter pub get" in ${updateDirectory.path}, which was specified as an update directory.',
);
break;
}
success = await shell('dart fix --apply', resolvedUpdateDirectory, verbose: verbose);
if (!success) {
failure('Could not run "dart fix" in ${updateDirectory.path}, which was specified as an update directory.');
failure(
'Could not run "dart fix" in ${updateDirectory.path}, which was specified as an update directory.',
);
break;
}
}
@@ -143,7 +156,9 @@ Future<bool> runTests({
if (instructions.iterations != null && instructions.iterations! < repeat) {
if (verbose) {
final String s = instructions.iterations == 1 ? '' : 's';
print('Limiting to ${instructions.iterations} round$s rather than $repeat rounds because of "iterations" directive.');
print(
'Limiting to ${instructions.iterations} round$s rather than $repeat rounds because of "iterations" directive.',
);
}
repeat = instructions.iterations!;
}
@@ -156,14 +171,18 @@ Future<bool> runTests({
testCount += 1;
success = await shell(testCommand, customerRepo, verbose: verbose);
if (!success) {
failure('One or more tests from ${path.basenameWithoutExtension(file.path)} failed.');
failure(
'One or more tests from ${path.basenameWithoutExtension(file.path)} failed.',
);
break;
}
}
}
stopwatch.stop();
// Always print test runtime for debugging.
print('Tests finished in ${(stopwatch.elapsed.inSeconds / repeat).toStringAsFixed(2)} seconds per iteration.');
print(
'Tests finished in ${(stopwatch.elapsed.inSeconds / repeat).toStringAsFixed(2)} seconds per iteration.',
);
}
}
} finally {
@@ -195,20 +214,40 @@ Future<bool> runTests({
final RegExp _spaces = RegExp(r' +');
Future<bool> shell(String command, Directory directory, { bool verbose = false, bool silentFailure = false, void Function()? failedCallback }) async {
Future<bool> shell(
String command,
Directory directory, {
bool verbose = false,
bool silentFailure = false,
void Function()? failedCallback,
}) async {
if (verbose) {
print('>> $command');
}
Process process;
if (Platform.isWindows) {
process = await Process.start('CMD.EXE', <String>['/S', '/C', command], workingDirectory: directory.path);
process = await Process.start('CMD.EXE', <String>[
'/S',
'/C',
command,
], workingDirectory: directory.path);
} else {
final List<String> segments = command.trim().split(_spaces);
process = await Process.start(segments.first, segments.skip(1).toList(), workingDirectory: directory.path);
process = await Process.start(
segments.first,
segments.skip(1).toList(),
workingDirectory: directory.path,
);
}
final List<String> output = <String>[];
utf8.decoder.bind(process.stdout).transform(const LineSplitter()).listen(verbose ? printLog : output.add);
utf8.decoder.bind(process.stderr).transform(const LineSplitter()).listen(verbose ? printLog : output.add);
utf8.decoder
.bind(process.stdout)
.transform(const LineSplitter())
.listen(verbose ? printLog : output.add);
utf8.decoder
.bind(process.stderr)
.transform(const LineSplitter())
.listen(verbose ? printLog : output.add);
final bool success = await process.exitCode == 0;
if (success || silentFailure) {
return success;

View File

@@ -17,45 +17,32 @@ Future<void> main(List<String> arguments) async {
// Return true if successful, false if failed.
Future<bool> run(List<String> arguments) async {
final ArgParser argParser = ArgParser(
allowTrailingOptions: false,
usageLineLength: 72,
)
..addOption(
'repeat',
defaultsTo: '1',
help: 'How many times to run each test. Set to a high value to look for flakes. If a test specifies a number of iterations, the lower of the two values is used.',
valueHelp: 'count',
)
..addOption(
'shards',
defaultsTo: '1',
help: 'How many shards to split the tests into. Used in continuous integration.',
valueHelp: 'count',
)
..addOption(
'shard-index',
defaultsTo: '0',
help: 'The current shard to run the tests with the range [0 .. shards - 1]. Used in continuous integration.',
valueHelp: 'count',
)
..addFlag(
'skip-on-fetch-failure',
help: 'Whether to skip tests that we fail to download.',
)
..addFlag(
'skip-template',
help: 'Whether to skip tests named "template.test".',
)
..addFlag(
'verbose',
help: 'Describe what is happening in detail.',
)
..addFlag(
'help',
negatable: false,
help: 'Print this help message.',
);
final ArgParser argParser =
ArgParser(allowTrailingOptions: false, usageLineLength: 72)
..addOption(
'repeat',
defaultsTo: '1',
help:
'How many times to run each test. Set to a high value to look for flakes. If a test specifies a number of iterations, the lower of the two values is used.',
valueHelp: 'count',
)
..addOption(
'shards',
defaultsTo: '1',
help: 'How many shards to split the tests into. Used in continuous integration.',
valueHelp: 'count',
)
..addOption(
'shard-index',
defaultsTo: '0',
help:
'The current shard to run the tests with the range [0 .. shards - 1]. Used in continuous integration.',
valueHelp: 'count',
)
..addFlag('skip-on-fetch-failure', help: 'Whether to skip tests that we fail to download.')
..addFlag('skip-template', help: 'Whether to skip tests named "template.test".')
..addFlag('verbose', help: 'Describe what is happening in detail.')
..addFlag('help', negatable: false, help: 'Print this help message.');
void printHelp() {
print('run_tests.dart [options...] path/to/file1.test path/to/file2.test...');
@@ -82,14 +69,20 @@ Future<bool> run(List<String> arguments) async {
final bool help = parsedArguments['help'] as bool;
final int? numberShards = int.tryParse(parsedArguments['shards'] as String);
final int? shardIndex = int.tryParse(parsedArguments['shard-index'] as String);
final List<File> files = parsedArguments
.rest
.expand((String path) => Glob(path).listFileSystemSync(const LocalFileSystem()))
.whereType<File>()
.where((File file) => !skipTemplate || path.basename(file.path) != 'template.test')
.toList();
final List<File> files =
parsedArguments.rest
.expand((String path) => Glob(path).listFileSystemSync(const LocalFileSystem()))
.whereType<File>()
.where((File file) => !skipTemplate || path.basename(file.path) != 'template.test')
.toList();
if (help || repeat == null || files.isEmpty || numberShards == null || numberShards <= 0 || shardIndex == null || shardIndex < 0) {
if (help ||
repeat == null ||
files.isEmpty ||
numberShards == null ||
numberShards <= 0 ||
shardIndex == null ||
shardIndex < 0) {
printHelp();
if (verbose) {
if (repeat == null) {
@@ -98,17 +91,23 @@ Future<bool> run(List<String> arguments) async {
if (numberShards == null) {
print('Error: Could not parse shards count ("${parsedArguments['shards']}")');
} else if (numberShards < 1) {
print('Error: The specified shards count ($numberShards) is less than 1. It must be greater than zero.');
print(
'Error: The specified shards count ($numberShards) is less than 1. It must be greater than zero.',
);
}
if (shardIndex == null) {
print('Error: Could not parse shard index ("${parsedArguments['shard-index']}")');
} else if (shardIndex < 0) {
print('Error: The specified shard index ($shardIndex) is negative. It must be in the range [0 .. shards - 1].');
print(
'Error: The specified shard index ($shardIndex) is negative. It must be in the range [0 .. shards - 1].',
);
}
if (parsedArguments.rest.isEmpty) {
print('Error: No file arguments specified.');
} else if (files.isEmpty) {
print('Error: File arguments ("${parsedArguments.rest.join('", "')}") did not identify any real files.');
print(
'Error: File arguments ("${parsedArguments.rest.join('", "')}") did not identify any real files.',
);
}
}
return help;
@@ -117,7 +116,7 @@ Future<bool> run(List<String> arguments) async {
if (shardIndex > numberShards - 1) {
print(
'Error: The specified shard index ($shardIndex) is more than the specified number of shards ($numberShards). '
'It must be in the range [0 .. shards - 1].'
'It must be in the range [0 .. shards - 1].',
);
return false;
}

View File

@@ -27,14 +27,18 @@ test.posix=./test_utilities/bin/flutter_test_runner.sh app_flutter
test.posix=./test_utilities/bin/flutter_test_runner.sh repo_dashboard
test.windows=.\test_utilities\bin\flutter_test_runner.bat repo_dashboard
''';
final File registryFile = MemoryFileSystem().file('flutter_cocoon.test')..writeAsStringSync(registryContent);
final File registryFile = MemoryFileSystem().file('flutter_cocoon.test')
..writeAsStringSync(registryContent);
final CustomerTest test = CustomerTest(registryFile);
expect(test.contacts, containsAll(<String>['abc@gmail.com']));
expect(
test.fetch,
containsAllInOrder(
<String>['git clone https://github.com/flutter/cocoon.git tests', 'git -C tests checkout abc123']));
test.fetch,
containsAllInOrder(<String>[
'git clone https://github.com/flutter/cocoon.git tests',
'git -C tests checkout abc123',
]),
);
expect(test.setup.first, 'flutter --version');
if (Platform.isLinux || Platform.isMacOS) {
expect(test.setup.length, 3);
@@ -50,7 +54,10 @@ test.windows=.\test_utilities\bin\flutter_test_runner.bat repo_dashboard
} else if (Platform.isWindows) {
expect(test.setup.length, 2);
expect(test.setup[1], 'flutter doctor');
expect(test.tests, containsAllInOrder(<String>['.\test_utilities\bin\flutter_test_runner.bat repo_dashboard']));
expect(
test.tests,
containsAllInOrder(<String>['.\test_utilities\bin\flutter_test_runner.bat repo_dashboard']),
);
}
});
@@ -64,7 +71,8 @@ test.posix=./test_utilities/bin/flutter_test_runner.sh app_flutter
test.windows=.\test_utilities\bin\flutter_test_runner.bat repo_dashboard
unknownfield=super not cool
''';
final File registryFile = MemoryFileSystem().file('abc.test')..writeAsStringSync(registryContent);
final File registryFile = MemoryFileSystem().file('abc.test')
..writeAsStringSync(registryContent);
expect(() => CustomerTest(registryFile), throwsFormatException);
});
@@ -75,7 +83,8 @@ contact=abc@gmail.com
update=.
fetch=git clone https://github.com/flutter/cocoon.git tests
''';
final File registryFile = MemoryFileSystem().file('abc.test')..writeAsStringSync(registryContent);
final File registryFile = MemoryFileSystem().file('abc.test')
..writeAsStringSync(registryContent);
expect(() => CustomerTest(registryFile), throwsFormatException);
});
@@ -88,7 +97,8 @@ fetch=git clone https://github.com/flutter/cocoon.git tests
test.posix=./test_utilities/bin/flutter_test_runner.sh app_flutter
test.windows=.\test_utilities\bin\flutter_test_runner.bat repo_dashboard
''';
final File registryFile = MemoryFileSystem().file('abc.test')..writeAsStringSync(registryContent);
final File registryFile = MemoryFileSystem().file('abc.test')
..writeAsStringSync(registryContent);
expect(() => CustomerTest(registryFile), throwsFormatException);
});
@@ -100,7 +110,8 @@ fetch=git clone https://github.com/flutter/cocoon.git tests
test.posix=./test_utilities/bin/flutter_test_runner.sh app_flutter
test.windows=.\test_utilities\bin\flutter_test_runner.bat repo_dashboard
''';
final File registryFile = MemoryFileSystem().file('abc.test')..writeAsStringSync(registryContent);
final File registryFile = MemoryFileSystem().file('abc.test')
..writeAsStringSync(registryContent);
expect(() => CustomerTest(registryFile), throwsFormatException);
});