Unverified Commit e2dfd73c authored by chunhtai's avatar chunhtai Committed by GitHub

only build asset when there is asset declared in pubspec (#31804)

parent 514fb2c7
...@@ -740,7 +740,8 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { ...@@ -740,7 +740,8 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
@override @override
int get microtaskCount => _currentFakeAsync.microtaskCount; int get microtaskCount => _currentFakeAsync.microtaskCount;
static Set<String> _allowedKeys; /// A whitelist [Set] that is used in mocking the asset message channel.
static Set<String> _allowedAssetKeys;
void _mockFlutterAssets() { void _mockFlutterAssets() {
if (!Platform.environment.containsKey('UNIT_TEST_ASSETS')) { if (!Platform.environment.containsKey('UNIT_TEST_ASSETS')) {
...@@ -748,29 +749,39 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { ...@@ -748,29 +749,39 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
} }
final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS']; final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS'];
_ensureInitialized(assetFolderPath); _ensureInitialized(assetFolderPath);
if (_allowedAssetKeys.isNotEmpty) {
BinaryMessages.setMockMessageHandler('flutter/assets', (ByteData message) { BinaryMessages.setMockMessageHandler('flutter/assets', (ByteData message) {
final String key = utf8.decode(message.buffer.asUint8List()); final String key = utf8.decode(message.buffer.asUint8List());
if (_allowedKeys.contains(key)) { if (_allowedAssetKeys.contains(key)) {
final File asset = File(path.join(assetFolderPath, key)); final File asset = File(path.join(assetFolderPath, key));
final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync()); final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync());
return Future<ByteData>.value(encoded.buffer.asByteData()); return Future<ByteData>.value(encoded.buffer.asByteData());
} }
}); });
} }
}
void _ensureInitialized(String assetFolderPath) { void _ensureInitialized(String assetFolderPath) {
if (_allowedKeys == null) { if (_allowedAssetKeys != null) {
return;
}
final File manifestFile = File( final File manifestFile = File(
path.join(assetFolderPath, 'AssetManifest.json')); path.join(assetFolderPath, 'AssetManifest.json'));
// If the file does not exist, it means there is no asset declared in
// the project.
if (!manifestFile.existsSync()) {
_allowedAssetKeys = <String>{};
return;
}
final Map<String, dynamic> manifest = json.decode( final Map<String, dynamic> manifest = json.decode(
manifestFile.readAsStringSync()); manifestFile.readAsStringSync());
_allowedKeys = <String>{ _allowedAssetKeys = <String>{
'AssetManifest.json', 'AssetManifest.json',
}; };
for (List<dynamic> value in manifest.values) { for (List<dynamic> value in manifest.values) {
final List<String> strList = List<String>.from(value); final List<String> strList = List<String>.from(value);
_allowedKeys.addAll(strList); _allowedAssetKeys.addAll(strList);
}
} }
} }
......
...@@ -127,13 +127,14 @@ class TestCommand extends FastFlutterCommand { ...@@ -127,13 +127,14 @@ class TestCommand extends FastFlutterCommand {
await pubGet(context: PubContext.getVerifyContext(name), skipPubspecYamlCheck: true); await pubGet(context: PubContext.getVerifyContext(name), skipPubspecYamlCheck: true);
} }
final bool buildTestAssets = argResults['test-assets']; final bool buildTestAssets = argResults['test-assets'];
if (buildTestAssets) {
await _buildTestAsset();
}
final List<String> names = argResults['name']; final List<String> names = argResults['name'];
final List<String> plainNames = argResults['plain-name']; final List<String> plainNames = argResults['plain-name'];
final FlutterProject flutterProject = FlutterProject.current(); final FlutterProject flutterProject = FlutterProject.current();
if (buildTestAssets && flutterProject.manifest.assets.isNotEmpty) {
await _buildTestAsset();
}
Iterable<String> files = argResults.rest.map<String>((String testPath) => fs.path.absolute(testPath)).toList(); Iterable<String> files = argResults.rest.map<String>((String testPath) => fs.path.absolute(testPath)).toList();
final bool startPaused = argResults['start-paused']; final bool startPaused = argResults['start-paused'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment