add flavor-conditional asset bundling support to flutter test
(#140944)
Fixes https://github.com/flutter/flutter/issues/140932
This commit is contained in:
parent
1a8de105cf
commit
8c11aa030d
@ -347,7 +347,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
|
|||||||
|
|
||||||
String? testAssetDirectory;
|
String? testAssetDirectory;
|
||||||
if (buildTestAssets) {
|
if (buildTestAssets) {
|
||||||
await _buildTestAsset();
|
await _buildTestAsset(flavor: buildInfo.flavor);
|
||||||
testAssetDirectory = globals.fs.path.
|
testAssetDirectory = globals.fs.path.
|
||||||
join(flutterProject.directory.path, 'build', 'unit_test_assets');
|
join(flutterProject.directory.path, 'build', 'unit_test_assets');
|
||||||
}
|
}
|
||||||
@ -563,9 +563,14 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
|
|||||||
.replace(query: queryPart.isEmpty ? null : queryPart);
|
.replace(query: queryPart.isEmpty ? null : queryPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _buildTestAsset() async {
|
Future<void> _buildTestAsset({
|
||||||
|
required String? flavor,
|
||||||
|
}) async {
|
||||||
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
|
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
|
||||||
final int build = await assetBundle.build(packagesPath: '.packages');
|
final int build = await assetBundle.build(
|
||||||
|
packagesPath: '.packages',
|
||||||
|
flavor: flavor,
|
||||||
|
);
|
||||||
if (build != 0) {
|
if (build != 0) {
|
||||||
throwToolExit('Error: Failed to build asset bundle');
|
throwToolExit('Error: Failed to build asset bundle');
|
||||||
}
|
}
|
||||||
|
@ -888,6 +888,45 @@ dev_dependencies:
|
|||||||
DeviceManager: () => _FakeDeviceManager(<Device>[]),
|
DeviceManager: () => _FakeDeviceManager(<Device>[]),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testUsingContext('builds asset bundle using --flavor', () async {
|
||||||
|
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
|
||||||
|
fs.file('vanilla.txt').writeAsStringSync('vanilla');
|
||||||
|
fs.file('orange.txt').writeAsStringSync('orange');
|
||||||
|
fs.file('pubspec.yaml').writeAsStringSync('''
|
||||||
|
flutter:
|
||||||
|
assets:
|
||||||
|
- path: vanilla.txt
|
||||||
|
flavors:
|
||||||
|
- vanilla
|
||||||
|
- path: orange.txt
|
||||||
|
flavors:
|
||||||
|
- orange
|
||||||
|
dev_dependencies:
|
||||||
|
flutter_test:
|
||||||
|
sdk: flutter
|
||||||
|
integration_test:
|
||||||
|
sdk: flutter''');
|
||||||
|
final TestCommand testCommand = TestCommand(testRunner: testRunner);
|
||||||
|
final CommandRunner<void> commandRunner = createTestCommandRunner(testCommand);
|
||||||
|
|
||||||
|
await commandRunner.run(const <String>[
|
||||||
|
'test',
|
||||||
|
'--no-pub',
|
||||||
|
'--flavor',
|
||||||
|
'vanilla',
|
||||||
|
]);
|
||||||
|
|
||||||
|
final bool vanillaExists = await fs.isFile(globals.fs.path.join('build', 'unit_test_assets', 'vanilla.txt'));
|
||||||
|
expect(vanillaExists, true, reason: 'vanilla.txt should be bundled');
|
||||||
|
final bool orangeExists = await fs.isFile(globals.fs.path.join('build', 'unit_test_assets', 'orange.txt'));
|
||||||
|
expect(orangeExists, false, reason: 'orange.txt should not be bundled');
|
||||||
|
|
||||||
|
}, overrides: <Type, Generator>{
|
||||||
|
FileSystem: () => fs,
|
||||||
|
ProcessManager: () => FakeProcessManager.any(),
|
||||||
|
DeviceManager: () => _FakeDeviceManager(<Device>[]),
|
||||||
|
});
|
||||||
|
|
||||||
testUsingContext("Don't build the asset manifest if --no-test-assets if informed", () async {
|
testUsingContext("Don't build the asset manifest if --no-test-assets if informed", () async {
|
||||||
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
|
final FakeFlutterTestRunner testRunner = FakeFlutterTestRunner(0);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user