Skip to content

Commit 8c11aa0

Browse files
authored
add flavor-conditional asset bundling support to flutter test (#140944)
Fixes flutter/flutter#140932
1 parent 1a8de10 commit 8c11aa0

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

packages/flutter_tools/lib/src/commands/test.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
347347

348348
String? testAssetDirectory;
349349
if (buildTestAssets) {
350-
await _buildTestAsset();
350+
await _buildTestAsset(flavor: buildInfo.flavor);
351351
testAssetDirectory = globals.fs.path.
352352
join(flutterProject.directory.path, 'build', 'unit_test_assets');
353353
}
@@ -563,9 +563,14 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
563563
.replace(query: queryPart.isEmpty ? null : queryPart);
564564
}
565565

566-
Future<void> _buildTestAsset() async {
566+
Future<void> _buildTestAsset({
567+
required String? flavor,
568+
}) async {
567569
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
568-
final int build = await assetBundle.build(packagesPath: '.packages');
570+
final int build = await assetBundle.build(
571+
packagesPath: '.packages',
572+
flavor: flavor,
573+
);
569574
if (build != 0) {
570575
throwToolExit('Error: Failed to build asset bundle');
571576
}

packages/flutter_tools/test/commands.shard/hermetic/test_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,45 @@ dev_dependencies:
888888
DeviceManager: () => _FakeDeviceManager(<Device>[]),
889889
});
890890

891+
testUsingContext('builds asset bundle using --flavor', () async {
892+
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
893+
fs.file('vanilla.txt').writeAsStringSync('vanilla');
894+
fs.file('orange.txt').writeAsStringSync('orange');
895+
fs.file('pubspec.yaml').writeAsStringSync('''
896+
flutter:
897+
assets:
898+
- path: vanilla.txt
899+
flavors:
900+
- vanilla
901+
- path: orange.txt
902+
flavors:
903+
- orange
904+
dev_dependencies:
905+
flutter_test:
906+
sdk: flutter
907+
integration_test:
908+
sdk: flutter''');
909+
final TestCommand testCommand = TestCommand(testRunner: testRunner);
910+
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);
911+
912+
await commandRunner.run(const <String>[
913+
'test',
914+
'--no-pub',
915+
'--flavor',
916+
'vanilla',
917+
]);
918+
919+
final bool vanillaExists = await fs.isFile(globals.fs.path.join('build', 'unit_test_assets', 'vanilla.txt'));
920+
expect(vanillaExists, true, reason: 'vanilla.txt should be bundled');
921+
final bool orangeExists = await fs.isFile(globals.fs.path.join('build', 'unit_test_assets', 'orange.txt'));
922+
expect(orangeExists, false, reason: 'orange.txt should not be bundled');
923+
924+
}, overrides: <Type, Generator>{
925+
FileSystem: () => fs,
926+
ProcessManager: () => FakeProcessManager.any(),
927+
DeviceManager: () => _FakeDeviceManager(<Device>[]),
928+
});
929+
891930
testUsingContext("Don't build the asset manifest if --no-test-assets if informed", () async {
892931
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
893932

0 commit comments

Comments
 (0)