From 9802c1cde7874ae6497a76a9208cd109af909bda Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Mon, 16 Dec 2024 17:07:17 -0800 Subject: [PATCH] Normalize localizations with dartfmt in presubmit check (#160395) Makes the presubmit check independent of whether the files in the repo are dartfmt formatted or not. --- dev/bots/analyze.dart | 21 +++++++++++++++------ dev/bots/pubspec.yaml | 3 ++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/dev/bots/analyze.dart b/dev/bots/analyze.dart index c0bfff7184..2e1b40c63f 100644 --- a/dev/bots/analyze.dart +++ b/dev/bots/analyze.dart @@ -14,6 +14,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/visitor.dart'; import 'package:collection/equality.dart'; import 'package:crypto/crypto.dart'; +import 'package:dart_style/dart_style.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; @@ -1042,6 +1043,8 @@ Future verifyIntegrationTestTimeouts(String workingDirectory) async { } } +final DartFormatter _formatter = DartFormatter(languageVersion: DartFormatter.latestLanguageVersion); + Future verifyInternationalizations(String workingDirectory, String dartExecutable) async { final EvalResult materialGenResult = await _evalCommand( dartExecutable, @@ -1067,24 +1070,30 @@ Future verifyInternationalizations(String workingDirectory, String dartExe final String expectedMaterialResult = await File(materialLocalizationsFile).readAsString(); final String expectedCupertinoResult = await File(cupertinoLocalizationsFile).readAsString(); - if (materialGenResult.stdout.trim() != expectedMaterialResult.trim()) { + // Normalize both with the Dart formatter. + final String formattedMaterialGenResult = _formatter.format(materialGenResult.stdout.trim()); + final String formattedExpectedMaterialResult = _formatter.format(expectedMaterialResult.trim()); + + if (formattedMaterialGenResult != formattedExpectedMaterialResult) { foundError([ '<<<<<<< $materialLocalizationsFile', - expectedMaterialResult.trim(), + formattedExpectedMaterialResult, '=======', - materialGenResult.stdout.trim(), + formattedMaterialGenResult, '>>>>>>> gen_localizations', 'The contents of $materialLocalizationsFile are different from that produced by gen_localizations.', '', 'Did you forget to run gen_localizations.dart after updating a .arb file?', ]); } - if (cupertinoGenResult.stdout.trim() != expectedCupertinoResult.trim()) { + final String formattedCupertinoGenResult = _formatter.format(cupertinoGenResult.stdout.trim()); + final String formattedExpectedCupertinoResult = _formatter.format(expectedCupertinoResult.trim()); + if (formattedCupertinoGenResult != formattedExpectedCupertinoResult) { foundError([ '<<<<<<< $cupertinoLocalizationsFile', - expectedCupertinoResult.trim(), + formattedExpectedCupertinoResult, '=======', - cupertinoGenResult.stdout.trim(), + formattedCupertinoGenResult, '>>>>>>> gen_localizations', 'The contents of $cupertinoLocalizationsFile are different from that produced by gen_localizations.', '', diff --git a/dev/bots/pubspec.yaml b/dev/bots/pubspec.yaml index f6145e7dd3..47c9ecbc5e 100644 --- a/dev/bots/pubspec.yaml +++ b/dev/bots/pubspec.yaml @@ -8,6 +8,7 @@ dependencies: analyzer: 6.11.0 args: 2.6.0 crypto: 3.0.6 + dart_style: 2.3.7 intl: 0.19.0 file: 7.0.1 flutter_devicelab: @@ -77,4 +78,4 @@ dependencies: dev_dependencies: test_api: 0.7.4 -# PUBSPEC CHECKSUM: cbf1 +# PUBSPEC CHECKSUM: e284