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);
BinaryMessages.setMockMessageHandler('flutter/assets', (ByteData message) {
final String key = utf8.decode(message.buffer.asUint8List()); if (_allowedAssetKeys.isNotEmpty) {
if (_allowedKeys.contains(key)) { BinaryMessages.setMockMessageHandler('flutter/assets', (ByteData message) {
final File asset = File(path.join(assetFolderPath, key)); final String key = utf8.decode(message.buffer.asUint8List());
final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync()); if (_allowedAssetKeys.contains(key)) {
return Future<ByteData>.value(encoded.buffer.asByteData()); final File asset = File(path.join(assetFolderPath, key));
} final Uint8List encoded = Uint8List.fromList(asset.readAsBytesSync());
}); return Future<ByteData>.value(encoded.buffer.asByteData());
}
});
}
} }
void _ensureInitialized(String assetFolderPath) { void _ensureInitialized(String assetFolderPath) {
if (_allowedKeys == null) { if (_allowedAssetKeys != null) {
final File manifestFile = File( return;
path.join(assetFolderPath, 'AssetManifest.json')); }
final Map<String, dynamic> manifest = json.decode( final File manifestFile = File(
manifestFile.readAsStringSync()); path.join(assetFolderPath, 'AssetManifest.json'));
_allowedKeys = <String>{ // If the file does not exist, it means there is no asset declared in
'AssetManifest.json', // the project.
}; if (!manifestFile.existsSync()) {
for (List<dynamic> value in manifest.values) { _allowedAssetKeys = <String>{};
final List<String> strList = List<String>.from(value); return;
_allowedKeys.addAll(strList); }
} final Map<String, dynamic> manifest = json.decode(
manifestFile.readAsStringSync());
_allowedAssetKeys = <String>{
'AssetManifest.json',
};
for (List<dynamic> value in manifest.values) {
final List<String> strList = List<String>.from(value);
_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