Skip to content

Commit 8e4f704

Browse files
authored
[CLI tool] in flutter test, consider --flavor when validating the cached asset bundle (#150461)
Fixes flutter/flutter#150296 **Context.** `flutter test` has its own code path for writing flutter app [assets](https://docs.flutter.dev/ui/assets/assets-and-images). flutter/flutter#132985 introduced [flavor-conditional asset bundling ](https://docs.flutter.dev/deployment/flavors#conditionally-bundling-assets-based-on-flavor), which lets users control which assets get bundled based on `--flavor`. `--flavor` is supported in `flutter test`. **Bug and fix.** `--flavor` isn't considered when deciding whether we need to rebuild this asset bundle: https://github.com/flutter/flutter/blob/5e448f4ce57723ac0792ae822ebac69df3188ba1/packages/flutter_tools/lib/src/commands/test.dart#L709 This PR address this by writing the value of `--flavor` to a file in the build directory and checking that when validating the cached asset bundle.
1 parent 0674f46 commit 8e4f704

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
695695
if (build != 0) {
696696
throwToolExit('Error: Failed to build asset bundle');
697697
}
698-
if (_needRebuild(assetBundle.entries)) {
698+
if (_needsRebuild(assetBundle.entries, flavor)) {
699699
await writeBundle(
700700
globals.fs.directory(globals.fs.path.join('build', 'unit_test_assets')),
701701
assetBundle.entries,
@@ -708,14 +708,25 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
708708
projectDir: globals.fs.currentDirectory,
709709
buildMode: buildMode,
710710
);
711+
712+
final File cachedFlavorFile = globals.fs.file(
713+
globals.fs.path.join('build', 'test_cache', 'flavor.txt'),
714+
);
715+
if (cachedFlavorFile.existsSync()) {
716+
await cachedFlavorFile.delete();
717+
}
718+
if (flavor != null) {
719+
cachedFlavorFile.createSync(recursive: true);
720+
cachedFlavorFile.writeAsStringSync(flavor);
721+
}
711722
}
712723
}
713724

714-
bool _needRebuild(Map<String, AssetBundleEntry> entries) {
725+
bool _needsRebuild(Map<String, AssetBundleEntry> entries, String? flavor) {
715726
// TODO(andrewkolos): This logic might fail in the future if we change the
716-
// schema of the contents of the asset manifest file and the user does not
717-
// perform a `flutter clean` after upgrading.
718-
// See https://github.com/flutter/flutter/issues/128563.
727+
// schema of the contents of the asset manifest file and the user does not
728+
// perform a `flutter clean` after upgrading.
729+
// See https://github.com/flutter/flutter/issues/128563.
719730
final File manifest = globals.fs.file(globals.fs.path.join('build', 'unit_test_assets', 'AssetManifest.bin'));
720731
if (!manifest.existsSync()) {
721732
return true;
@@ -736,6 +747,17 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
736747
return true;
737748
}
738749
}
750+
751+
final File cachedFlavorFile = globals.fs.file(
752+
globals.fs.path.join('build', 'test_cache', 'flavor.txt'),
753+
);
754+
final String? cachedFlavor = cachedFlavorFile.existsSync()
755+
? cachedFlavorFile.readAsStringSync()
756+
: null;
757+
if (cachedFlavor != flavor) {
758+
return true;
759+
}
760+
739761
return false;
740762
}
741763
}

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:convert';
77

88
import 'package:args/command_runner.dart';
99
import 'package:file/memory.dart';
10+
import 'package:file_testing/file_testing.dart';
1011
import 'package:flutter_tools/src/base/async_guard.dart';
1112
import 'package:flutter_tools/src/base/common.dart';
1213
import 'package:flutter_tools/src/base/file_system.dart';
@@ -1178,6 +1179,59 @@ dev_dependencies:
11781179
DeviceManager: () => _FakeDeviceManager(<Device>[]),
11791180
});
11801181

1182+
testUsingContext('correctly considers --flavor when validating the cached asset bundle', () async {
1183+
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
1184+
fs.file('vanilla.txt').writeAsStringSync('vanilla');
1185+
fs.file('flavorless.txt').writeAsStringSync('flavorless');
1186+
fs.file('pubspec.yaml').writeAsStringSync('''
1187+
flutter:
1188+
assets:
1189+
- path: vanilla.txt
1190+
flavors:
1191+
- vanilla
1192+
- flavorless.txt
1193+
dev_dependencies:
1194+
flutter_test:
1195+
sdk: flutter
1196+
integration_test:
1197+
sdk: flutter''');
1198+
final TestCommand testCommand = TestCommand(testRunner: testRunner);
1199+
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);
1200+
1201+
const List<String> buildArgsFlavorless = <String>[
1202+
'test',
1203+
'--no-pub',
1204+
];
1205+
1206+
const List<String> buildArgsVanilla = <String>[
1207+
'test',
1208+
'--no-pub',
1209+
'--flavor',
1210+
'vanilla',
1211+
];
1212+
1213+
final File builtVanillaAssetFile = fs.file(
1214+
fs.path.join('build', 'unit_test_assets', 'vanilla.txt'),
1215+
);
1216+
final File builtFlavorlessAssetFile = fs.file(
1217+
fs.path.join('build', 'unit_test_assets', 'flavorless.txt'),
1218+
);
1219+
1220+
await commandRunner.run(buildArgsVanilla);
1221+
await commandRunner.run(buildArgsFlavorless);
1222+
1223+
expect(builtVanillaAssetFile, isNot(exists));
1224+
expect(builtFlavorlessAssetFile, exists);
1225+
1226+
await commandRunner.run(buildArgsVanilla);
1227+
1228+
expect(builtVanillaAssetFile, exists);
1229+
}, overrides: <Type, Generator>{
1230+
FileSystem: () => fs,
1231+
ProcessManager: () => FakeProcessManager.empty(),
1232+
DeviceManager: () => _FakeDeviceManager(<Device>[]),
1233+
});
1234+
11811235
testUsingContext("Don't build the asset manifest if --no-test-assets if informed", () async {
11821236
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
11831237

0 commit comments

Comments
 (0)