From b6c7df447d5f8d579231c1bbc5aad57da31470b0 Mon Sep 17 00:00:00 2001 From: Tae Hyung Kim Date: Tue, 18 Apr 2023 11:42:07 -0700 Subject: [PATCH] l10n.yaml's nullable-getter option should default to true (#124353) Currently, nullable-getter defaults to false when l10n.yaml is not present, which is not the same behavior as when an l10n.yaml file is present and nullable-getter is not set. Fixes #120457. --- .../src/commands/generate_localizations.dart | 1 + .../hermetic/generate_localizations_test.dart | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/packages/flutter_tools/lib/src/commands/generate_localizations.dart b/packages/flutter_tools/lib/src/commands/generate_localizations.dart index 7323aa0f1b..a021bc718b 100644 --- a/packages/flutter_tools/lib/src/commands/generate_localizations.dart +++ b/packages/flutter_tools/lib/src/commands/generate_localizations.dart @@ -179,6 +179,7 @@ class GenerateLocalizationsCommand extends FlutterCommand { ); argParser.addFlag( 'nullable-getter', + defaultsTo: true, help: 'Whether or not the localizations class getter is nullable.\n' '\n' 'By default, this value is set to true so that ' 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 e57e5ecb93..ac1695cfad 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 @@ -319,4 +319,36 @@ untranslated-messages-file: lib/l10n/untranslated.json FileSystem: () => fileSystem, ProcessManager: () => FakeProcessManager.any(), }); + + testUsingContext('nullable-getter defaults to true', () 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']); + + final Directory outputDirectory = fileSystem.directory(fileSystem.path.join('.dart_tool', 'flutter_gen', 'gen_l10n')); + expect(outputDirectory.existsSync(), isTrue); + expect(outputDirectory.childFile('app_localizations.dart').existsSync(), isTrue); + expect( + outputDirectory.childFile('app_localizations.dart').readAsStringSync(), + contains('static AppLocalizations? of(BuildContext context)'), + ); + }, overrides: { + FileSystem: () => fileSystem, + ProcessManager: () => FakeProcessManager.any(), + }); }