From 453dd7174cc6555e3e9eb5f006c86e6ec221a0d2 Mon Sep 17 00:00:00 2001 From: flutteractionsbot <154381524+flutteractionsbot@users.noreply.github.com> Date: Tue, 29 Apr 2025 21:15:41 -0700 Subject: [PATCH] [CP-beta]feat: Arbitrary format options for localizations generation (#102983) (#168035) This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: What is the link to the issue this cherry-pick is addressing? < Replace with issue link here > ### Changelog Description: Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples < Replace with changelog description here > ### Impact Description: What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch) < Replace with impact description here > ### Workaround: Is there a workaround for this issue? < Replace with workaround here > ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: What are the steps to validate that this fix works? < Replace with validation steps here > --- .../localizations/localizations_utils.dart | 2 +- .../hermetic/generate_localizations_test.dart | 80 ++++++++++++++++++- .../generate_localizations_test.dart | 16 ++-- 3 files changed, 86 insertions(+), 12 deletions(-) diff --git a/packages/flutter_tools/lib/src/localizations/localizations_utils.dart b/packages/flutter_tools/lib/src/localizations/localizations_utils.dart index 2830240108..6389b7fbef 100644 --- a/packages/flutter_tools/lib/src/localizations/localizations_utils.dart +++ b/packages/flutter_tools/lib/src/localizations/localizations_utils.dart @@ -356,7 +356,7 @@ class LocalizationOptions { syntheticPackage = syntheticPackage ?? !featureFlags.isExplicitPackageDependenciesEnabled, requiredResourceAttributes = requiredResourceAttributes ?? false, nullableGetter = nullableGetter ?? true, - format = format ?? false, + format = format ?? true, useEscaping = useEscaping ?? false, suppressWarnings = suppressWarnings ?? false, relaxSyntax = relaxSyntax ?? false, diff --git a/packages/flutter_tools/test/commands.shard/hermetic/generate_localizations_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/generate_localizations_test.dart index d937de7adc..6619c631fd 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/generate_localizations_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/generate_localizations_test.dart @@ -202,7 +202,7 @@ flutter: fileSystem: fileSystem, logger: logger, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ); await createTestCommandRunner(command).run(['gen-l10n']); @@ -241,7 +241,7 @@ flutter: fileSystem: fileSystem, logger: logger, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ); await createTestCommandRunner(command).run(['gen-l10n']); expect(command.usage, contains(' If this value is set to false, then ')); @@ -299,6 +299,43 @@ flutter: }, ); + testUsingContext( + 'dart format is not run when --no-format is passed', + () async { + final File arbFile = fileSystem.file(fileSystem.path.join('lib', 'l10n', 'app_en.arb')) + ..createSync(recursive: true); + arbFile.writeAsStringSync(''' +{ + "helloWorld": "Hello, World!", + "@helloWorld": { + "description": "Sample description" + } +}'''); + final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync(); + pubspecFile.writeAsStringSync(BasicProjectWithFlutterGen().pubspec); + + final GenerateLocalizationsCommand command = GenerateLocalizationsCommand( + fileSystem: fileSystem, + logger: logger, + artifacts: artifacts, + processManager: processManager, + ); + + await createTestCommandRunner(command).run(['gen-l10n', '--no-format']); + + final Directory outputDirectory = fileSystem.directory(fileSystem.path.join('lib', 'l10n')); + expect(outputDirectory.existsSync(), true); + expect(outputDirectory.childFile('app_localizations_en.dart').existsSync(), true); + expect(outputDirectory.childFile('app_localizations.dart').existsSync(), true); + expect(processManager, hasNoRemainingExpectations); + }, + overrides: { + FeatureFlags: enableExplicitPackageDependencies, + FileSystem: () => fileSystem, + ProcessManager: () => FakeProcessManager.any(), + }, + ); + testUsingContext( 'dart format is run when format: true is passed into l10n.yaml', () async { @@ -348,6 +385,45 @@ format: true }, ); + testUsingContext( + 'dart format is not running when format: false is passed into l10n.yaml', + () async { + final File arbFile = fileSystem.file(fileSystem.path.join('lib', 'l10n', 'app_en.arb')) + ..createSync(recursive: true); + arbFile.writeAsStringSync(''' +{ + "helloWorld": "Hello, World!", + "@helloWorld": { + "description": "Sample description" + } +}'''); + final File configFile = fileSystem.file('l10n.yaml')..createSync(); + configFile.writeAsStringSync(''' +format: false +'''); + final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync(); + pubspecFile.writeAsStringSync(BasicProjectWithFlutterGen().pubspec); + final GenerateLocalizationsCommand command = GenerateLocalizationsCommand( + fileSystem: fileSystem, + logger: logger, + artifacts: artifacts, + processManager: processManager, + ); + await createTestCommandRunner(command).run(['gen-l10n']); + + final Directory outputDirectory = fileSystem.directory(fileSystem.path.join('lib', 'l10n')); + expect(outputDirectory.existsSync(), true); + expect(outputDirectory.childFile('app_localizations_en.dart').existsSync(), true); + expect(outputDirectory.childFile('app_localizations.dart').existsSync(), true); + expect(processManager, hasNoRemainingExpectations); + }, + overrides: { + FeatureFlags: enableExplicitPackageDependencies, + FileSystem: () => fileSystem, + ProcessManager: () => FakeProcessManager.any(), + }, + ); + // Regression test for https://github.com/flutter/flutter/issues/119594 testUsingContext( 'dart format is working when the untranslated messages file is produced', diff --git a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart index fcd5f21e60..1a4b8babb7 100644 --- a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart +++ b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart @@ -76,7 +76,6 @@ void main() { late MemoryFileSystem fs; late BufferLogger logger; late Artifacts artifacts; - late ProcessManager processManager; late String defaultL10nPathString; late String syntheticPackagePath; late String syntheticL10nPackagePath; @@ -152,7 +151,6 @@ void main() { fs = MemoryFileSystem.test(); logger = BufferLogger.test(); artifacts = Artifacts.test(); - processManager = FakeProcessManager.empty(); defaultL10nPathString = fs.path.join('lib', 'l10n'); syntheticPackagePath = fs.path.join('.dart_tool', 'flutter_gen'); @@ -757,7 +755,7 @@ flutter: projectDir: projectDir, dependenciesDir: fs.currentDirectory, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ); }); @@ -780,7 +778,7 @@ flutter: projectDir: fs.currentDirectory, dependenciesDir: fs.currentDirectory, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ); }); @@ -809,7 +807,7 @@ flutter: projectDir: fs.currentDirectory, dependenciesDir: fs.currentDirectory, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ); expect(generator.inputDirectory.path, '/lib/l10n/'); @@ -880,7 +878,7 @@ flutter: projectDir: fs.currentDirectory, dependenciesDir: fs.currentDirectory, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ), throwsToolExit( message: @@ -916,7 +914,7 @@ flutter:\r projectDir: fs.currentDirectory, dependenciesDir: fs.currentDirectory, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ); final String content = getInPackageGeneratedFileContent(locale: 'en'); expect(content, contains('\r\n')); @@ -940,7 +938,7 @@ flutter:\r projectDir: fs.currentDirectory, dependenciesDir: fs.currentDirectory, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ); expect(fs.file('/lib/l10n/app_localizations_en.dart').readAsStringSync(), ''' @@ -973,7 +971,7 @@ class AppLocalizationsEn extends AppLocalizations { projectDir: fs.currentDirectory, dependenciesDir: fs.currentDirectory, artifacts: artifacts, - processManager: processManager, + processManager: FakeProcessManager.any(), ); expect(fs.file('/lib/l10n/app_localizations_en.dart').readAsStringSync(), '''