2017-09-05 07:42:24 +00:00
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import ' dart:async ' ;
import ' dart:convert ' ;
import ' package:file/file.dart ' ;
2018-05-21 00:52:33 +00:00
import ' package:file/memory.dart ' ;
2018-08-15 03:33:58 +00:00
2017-09-05 07:42:24 +00:00
import ' package:flutter_tools/src/asset.dart ' ;
import ' package:flutter_tools/src/base/file_system.dart ' ;
2018-05-21 00:52:33 +00:00
import ' package:flutter_tools/src/base/platform.dart ' ;
2017-09-05 07:42:24 +00:00
import ' package:flutter_tools/src/cache.dart ' ;
2019-07-13 18:51:44 +00:00
import ' ../src/common.dart ' ;
import ' ../src/context.dart ' ;
import ' ../src/pubspec_schema.dart ' ;
2017-09-05 07:42:24 +00:00
void main ( ) {
2018-09-05 18:30:55 +00:00
String fixPath ( String path ) {
// The in-memory file system is strict about slashes on Windows being the
// correct way so until https://github.com/google/file.dart/issues/112 is
// fixed we fix them here.
// TODO(dantup): Remove this function once the above issue is fixed and
// rolls into Flutter.
return path ? . replaceAll ( ' / ' , fs . path . separator ) ;
}
2019-02-21 08:27:07 +00:00
void writePubspecFile ( String path , String name , { List < String > assets } ) {
2017-09-05 07:42:24 +00:00
String assetsSection ;
if ( assets = = null ) {
assetsSection = ' ' ;
} else {
2018-09-12 06:29:29 +00:00
final StringBuffer buffer = StringBuffer ( ) ;
2017-09-05 07:42:24 +00:00
buffer . write ( '''
flutter:
assets:
''' );
for ( String asset in assets ) {
buffer . write ( '''
- $asset
''' );
}
assetsSection = buffer . toString ( ) ;
}
2018-09-05 18:30:55 +00:00
fs . file ( fixPath ( path ) )
2017-09-05 07:42:24 +00:00
. . createSync ( recursive: true )
. . writeAsStringSync ( '''
name: $name
dependencies:
flutter:
sdk: flutter
$assetsSection
''' );
}
void establishFlutterRoot ( ) {
Cache . flutterRoot = getFlutterRoot ( ) ;
}
void writePackagesFile ( String packages ) {
2017-10-22 16:11:36 +00:00
fs . file ( ' .packages ' )
2017-09-05 07:42:24 +00:00
. . createSync ( )
. . writeAsStringSync ( packages ) ;
}
2018-10-05 05:54:56 +00:00
Future < void > buildAndVerifyAssets (
2017-09-05 07:42:24 +00:00
List < String > assets ,
List < String > packages ,
String expectedAssetManifest ,
) async {
2018-01-29 17:40:28 +00:00
final AssetBundle bundle = AssetBundleFactory . instance . createBundle ( ) ;
2017-09-05 07:42:24 +00:00
await bundle . build ( manifestPath: ' pubspec.yaml ' ) ;
for ( String packageName in packages ) {
for ( String asset in assets ) {
2018-03-29 20:47:35 +00:00
final String entryKey = Uri . encodeFull ( ' packages/ $ packageName / $ asset ' ) ;
2018-05-21 00:52:33 +00:00
expect ( bundle . entries . containsKey ( entryKey ) , true , reason: ' Cannot find key on bundle: $ entryKey ' ) ;
2017-09-05 07:42:24 +00:00
expect (
2019-05-31 20:17:12 +00:00
utf8 . decode ( await bundle . entries [ entryKey ] . contentsAsBytes ( ) ) ,
2017-09-05 07:42:24 +00:00
asset ,
) ;
}
}
expect (
2019-05-31 20:17:12 +00:00
utf8 . decode ( await bundle . entries [ ' AssetManifest.json ' ] . contentsAsBytes ( ) ) ,
2017-09-05 07:42:24 +00:00
expectedAssetManifest ,
) ;
}
void writeAssets ( String path , List < String > assets ) {
for ( String asset in assets ) {
2018-09-05 18:30:55 +00:00
final String fullPath = fixPath ( fs . path . join ( path , asset ) ) ;
2018-05-21 00:52:33 +00:00
2018-09-05 18:30:55 +00:00
fs . file ( fullPath )
2017-09-05 07:42:24 +00:00
. . createSync ( recursive: true )
. . writeAsStringSync ( asset ) ;
}
}
2018-09-05 18:30:55 +00:00
FileSystem testFileSystem ;
2018-01-24 21:16:23 +00:00
setUp ( ( ) async {
2018-09-12 06:29:29 +00:00
testFileSystem = MemoryFileSystem (
2018-09-05 18:30:55 +00:00
style: platform . isWindows
? FileSystemStyle . windows
: FileSystemStyle . posix ,
) ;
testFileSystem . currentDirectory = testFileSystem . systemTempDirectory . createTempSync ( ' flutter_asset_bundle_test. ' ) ;
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-12 10:53:03 +00:00
group ( ' AssetBundle assets from packages ' , ( ) {
testUsingContext ( ' No assets are bundled when the package has no assets ' , ( ) async {
2017-09-05 07:42:24 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-05 07:42:24 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
writePubspecFile ( ' p/p/pubspec.yaml ' , ' test_package ' ) ;
2018-01-29 17:40:28 +00:00
final AssetBundle bundle = AssetBundleFactory . instance . createBundle ( ) ;
2017-09-05 07:42:24 +00:00
await bundle . build ( manifestPath: ' pubspec.yaml ' ) ;
2018-04-17 04:48:21 +00:00
expect ( bundle . entries . length , 3 ) ; // LICENSE, AssetManifest, FontManifest
2018-02-01 06:51:26 +00:00
const String expectedAssetManifest = ' {} ' ;
2017-09-12 10:53:03 +00:00
expect (
2019-05-31 20:17:12 +00:00
utf8 . decode ( await bundle . entries [ ' AssetManifest.json ' ] . contentsAsBytes ( ) ) ,
2017-09-12 10:53:03 +00:00
expectedAssetManifest ,
) ;
2018-04-17 04:48:21 +00:00
expect (
2019-05-31 20:17:12 +00:00
utf8 . decode ( await bundle . entries [ ' FontManifest.json ' ] . contentsAsBytes ( ) ) ,
2018-04-17 04:48:21 +00:00
' [] ' ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-05 07:42:24 +00:00
2017-09-12 10:53:03 +00:00
testUsingContext ( ' No assets are bundled when the package has an asset that is not listed ' , ( ) async {
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-12 10:53:03 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
writePubspecFile ( ' p/p/pubspec.yaml ' , ' test_package ' ) ;
final List < String > assets = < String > [ ' a/foo ' ] ;
writeAssets ( ' p/p/ ' , assets ) ;
2018-01-29 17:40:28 +00:00
final AssetBundle bundle = AssetBundleFactory . instance . createBundle ( ) ;
2017-09-12 10:53:03 +00:00
await bundle . build ( manifestPath: ' pubspec.yaml ' ) ;
2018-04-17 04:48:21 +00:00
expect ( bundle . entries . length , 3 ) ; // LICENSE, AssetManifest, FontManifest
2018-02-01 06:51:26 +00:00
const String expectedAssetManifest = ' {} ' ;
2017-09-12 10:53:03 +00:00
expect (
2019-05-31 20:17:12 +00:00
utf8 . decode ( await bundle . entries [ ' AssetManifest.json ' ] . contentsAsBytes ( ) ) ,
2017-09-12 10:53:03 +00:00
expectedAssetManifest ,
) ;
2018-04-17 04:48:21 +00:00
expect (
2019-05-31 20:17:12 +00:00
utf8 . decode ( await bundle . entries [ ' FontManifest.json ' ] . contentsAsBytes ( ) ) ,
2018-04-17 04:48:21 +00:00
' [] ' ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-12 10:53:03 +00:00
testUsingContext ( ' One asset is bundled when the package has and lists one asset its pubspec ' , ( ) async {
2017-09-05 07:42:24 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-05 07:42:24 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assets = < String > [ ' a/foo ' ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assets ,
) ;
writeAssets ( ' p/p/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedAssetManifest = ' {"packages/test_package/a/foo": '
2017-09-05 07:42:24 +00:00
' ["packages/test_package/a/foo"]} ' ;
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-10-22 16:11:36 +00:00
2017-09-12 10:53:03 +00:00
testUsingContext ( " One asset is bundled when the package has one asset, listed in the app's pubspec " , ( ) async {
2017-09-12 07:13:41 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-12 07:13:41 +00:00
final List < String > assetEntries = < String > [ ' packages/test_package/a/foo ' ] ;
writePubspecFile (
' pubspec.yaml ' ,
' test ' ,
assets: assetEntries ,
) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
writePubspecFile ( ' p/p/pubspec.yaml ' , ' test_package ' ) ;
final List < String > assets = < String > [ ' a/foo ' ] ;
writeAssets ( ' p/p/lib/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedAssetManifest = ' {"packages/test_package/a/foo": '
2017-09-12 07:13:41 +00:00
' ["packages/test_package/a/foo"]} ' ;
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-05 07:42:24 +00:00
2017-10-22 16:11:36 +00:00
testUsingContext ( ' One asset and its variant are bundled when the package has an asset and a variant, and lists the asset in its pubspec ' , ( ) async {
2017-09-05 07:42:24 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-05 07:42:24 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: < String > [ ' a/foo ' ] ,
) ;
final List < String > assets = < String > [ ' a/foo ' , ' a/v/foo ' ] ;
writeAssets ( ' p/p/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedManifest = ' {"packages/test_package/a/foo": '
2017-09-05 07:42:24 +00:00
' ["packages/test_package/a/foo","packages/test_package/a/v/foo"]} ' ;
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' ] ,
expectedManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-05 07:42:24 +00:00
2017-10-22 16:11:36 +00:00
testUsingContext ( ' One asset and its variant are bundled when the package has an asset and a variant, and the app lists the asset in its pubspec ' , ( ) async {
2017-09-12 07:13:41 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-12 07:13:41 +00:00
writePubspecFile (
' pubspec.yaml ' ,
' test ' ,
assets: < String > [ ' packages/test_package/a/foo ' ] ,
) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
) ;
final List < String > assets = < String > [ ' a/foo ' , ' a/v/foo ' ] ;
writeAssets ( ' p/p/lib/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedManifest = ' {"packages/test_package/a/foo": '
2017-09-12 07:13:41 +00:00
' ["packages/test_package/a/foo","packages/test_package/a/v/foo"]} ' ;
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' ] ,
expectedManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-12 07:13:41 +00:00
2017-10-22 16:11:36 +00:00
testUsingContext ( ' Two assets are bundled when the package has and lists two assets in its pubspec ' , ( ) async {
2017-09-05 07:42:24 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-05 07:42:24 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assets = < String > [ ' a/foo ' , ' a/bar ' ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assets ,
) ;
writeAssets ( ' p/p/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedAssetManifest =
2018-05-21 00:52:33 +00:00
' {"packages/test_package/a/bar":["packages/test_package/a/bar"], '
' "packages/test_package/a/foo":["packages/test_package/a/foo"]} ' ;
2017-09-05 07:42:24 +00:00
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-05 07:42:24 +00:00
2017-09-12 10:53:03 +00:00
testUsingContext ( " Two assets are bundled when the package has two assets, listed in the app's pubspec " , ( ) async {
2017-09-12 07:13:41 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-12 07:13:41 +00:00
final List < String > assetEntries = < String > [
' packages/test_package/a/foo ' ,
' packages/test_package/a/bar ' ,
] ;
writePubspecFile (
' pubspec.yaml ' ,
' test ' ,
assets: assetEntries ,
) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assets = < String > [ ' a/foo ' , ' a/bar ' ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
) ;
writeAssets ( ' p/p/lib/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedAssetManifest =
2018-05-21 00:52:33 +00:00
' {"packages/test_package/a/bar":["packages/test_package/a/bar"], '
' "packages/test_package/a/foo":["packages/test_package/a/foo"]} ' ;
2017-09-12 07:13:41 +00:00
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-12 07:13:41 +00:00
2017-10-22 16:11:36 +00:00
testUsingContext ( ' Two assets are bundled when two packages each have and list an asset their pubspec ' , ( ) async {
2017-09-05 07:42:24 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-05 07:42:24 +00:00
2017-09-12 07:13:41 +00:00
writePubspecFile (
' pubspec.yaml ' ,
' test ' ,
) ;
2017-09-05 07:42:24 +00:00
writePackagesFile ( ' test_package:p/p/lib/ \n test_package2:p2/p/lib/ ' ) ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: < String > [ ' a/foo ' ] ,
) ;
writePubspecFile (
' p2/p/pubspec.yaml ' ,
' test_package2 ' ,
assets: < String > [ ' a/foo ' ] ,
) ;
final List < String > assets = < String > [ ' a/foo ' , ' a/v/foo ' ] ;
writeAssets ( ' p/p/ ' , assets ) ;
writeAssets ( ' p2/p/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedAssetManifest =
2017-09-05 07:42:24 +00:00
' {"packages/test_package/a/foo": '
' ["packages/test_package/a/foo","packages/test_package/a/v/foo"], '
' "packages/test_package2/a/foo": '
' ["packages/test_package2/a/foo","packages/test_package2/a/v/foo"]} ' ;
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' , ' test_package2 ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-10-22 16:11:36 +00:00
2017-09-12 10:53:03 +00:00
testUsingContext ( " Two assets are bundled when two packages each have an asset, listed in the app's pubspec " , ( ) async {
2017-09-12 07:13:41 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-12 07:13:41 +00:00
final List < String > assetEntries = < String > [
' packages/test_package/a/foo ' ,
' packages/test_package2/a/foo ' ,
] ;
writePubspecFile (
' pubspec.yaml ' ,
' test ' ,
assets: assetEntries ,
) ;
writePackagesFile ( ' test_package:p/p/lib/ \n test_package2:p2/p/lib/ ' ) ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
) ;
writePubspecFile (
' p2/p/pubspec.yaml ' ,
' test_package2 ' ,
) ;
final List < String > assets = < String > [ ' a/foo ' , ' a/v/foo ' ] ;
writeAssets ( ' p/p/lib/ ' , assets ) ;
writeAssets ( ' p2/p/lib/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedAssetManifest =
2017-09-12 07:13:41 +00:00
' {"packages/test_package/a/foo": '
' ["packages/test_package/a/foo","packages/test_package/a/v/foo"], '
' "packages/test_package2/a/foo": '
' ["packages/test_package2/a/foo","packages/test_package2/a/v/foo"]} ' ;
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' , ' test_package2 ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-12 07:13:41 +00:00
2017-10-22 16:11:36 +00:00
testUsingContext ( ' One asset is bundled when the app depends on a package, listing in its pubspec an asset from another package ' , ( ) async {
2017-09-12 07:13:41 +00:00
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2017-09-12 07:13:41 +00:00
writePubspecFile (
' pubspec.yaml ' ,
' test ' ,
) ;
writePackagesFile ( ' test_package:p/p/lib/ \n test_package2:p2/p/lib/ ' ) ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
2017-09-12 10:53:03 +00:00
assets: < String > [ ' packages/test_package2/a/foo ' ] ,
2017-09-12 07:13:41 +00:00
) ;
writePubspecFile (
' p2/p/pubspec.yaml ' ,
' test_package2 ' ,
) ;
final List < String > assets = < String > [ ' a/foo ' , ' a/v/foo ' ] ;
writeAssets ( ' p2/p/lib/ ' , assets ) ;
2018-03-14 05:24:49 +00:00
const String expectedAssetManifest =
2017-09-12 07:13:41 +00:00
' {"packages/test_package2/a/foo": '
' ["packages/test_package2/a/foo","packages/test_package2/a/v/foo"]} ' ;
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package2 ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-01-24 21:16:23 +00:00
} ) ;
2017-09-05 07:42:24 +00:00
} ) ;
2018-03-29 20:47:35 +00:00
testUsingContext ( ' Asset paths can contain URL reserved characters ' , ( ) async {
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2018-03-29 20:47:35 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assets = < String > [ ' a/foo ' , ' a/foo[x] ' ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assets ,
) ;
writeAssets ( ' p/p/ ' , assets ) ;
const String expectedAssetManifest =
' {"packages/test_package/a/foo":["packages/test_package/a/foo"], '
' "packages/test_package/a/foo%5Bx%5D":["packages/test_package/a/foo%5Bx%5D"]} ' ;
await buildAndVerifyAssets (
assets ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-03-29 20:47:35 +00:00
} ) ;
2018-05-21 00:52:33 +00:00
group ( ' AssetBundle assets from scanned paths ' , ( ) {
testUsingContext (
' Two assets are bundled when scanning their directory ' , ( ) async {
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2018-05-21 00:52:33 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assetsOnDisk = < String > [ ' a/foo ' , ' a/bar ' ] ;
final List < String > assetsOnManifest = < String > [ ' a/ ' ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assetsOnManifest ,
) ;
writeAssets ( ' p/p/ ' , assetsOnDisk ) ;
const String expectedAssetManifest =
' {"packages/test_package/a/bar":["packages/test_package/a/bar"], '
' "packages/test_package/a/foo":["packages/test_package/a/foo"]} ' ;
await buildAndVerifyAssets (
assetsOnDisk ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-05-21 00:52:33 +00:00
} ) ;
testUsingContext (
' Two assets are bundled when listing one and scanning second directory ' , ( ) async {
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2018-05-21 00:52:33 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assetsOnDisk = < String > [ ' a/foo ' , ' abc/bar ' ] ;
final List < String > assetOnManifest = < String > [ ' a/foo ' , ' abc/ ' ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assetOnManifest ,
) ;
writeAssets ( ' p/p/ ' , assetsOnDisk ) ;
const String expectedAssetManifest =
' {"packages/test_package/abc/bar":["packages/test_package/abc/bar"], '
' "packages/test_package/a/foo":["packages/test_package/a/foo"]} ' ;
await buildAndVerifyAssets (
assetsOnDisk ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-05-21 00:52:33 +00:00
} ) ;
testUsingContext (
' One asset is bundled with variant, scanning wrong directory ' , ( ) async {
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2018-05-21 00:52:33 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assetsOnDisk = < String > [ ' a/foo ' , ' a/b/foo ' , ' a/bar ' ] ;
final List < String > assetOnManifest = < String > [ ' a ' , ' a/bar ' ] ; // can't list 'a' as asset, should be 'a/'
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assetOnManifest ,
) ;
writeAssets ( ' p/p/ ' , assetsOnDisk ) ;
final AssetBundle bundle = AssetBundleFactory . instance . createBundle ( ) ;
await bundle . build ( manifestPath: ' pubspec.yaml ' ) ;
assert ( bundle . entries [ ' AssetManifest.json ' ] = = null , ' Invalid pubspec.yaml should not generate AssetManifest.json ' ) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-05-21 00:52:33 +00:00
} ) ;
} ) ;
group ( ' AssetBundle assets from scanned paths with MemoryFileSystem ' , ( ) {
2018-09-05 18:30:55 +00:00
testUsingContext (
2018-05-21 00:52:33 +00:00
' One asset is bundled with variant, scanning directory ' , ( ) async {
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2018-05-21 00:52:33 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assetsOnDisk = < String > [ ' a/foo ' , ' a/b/foo ' ] ;
final List < String > assetOnManifest = < String > [ ' a/ ' , ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assetOnManifest ,
) ;
writeAssets ( ' p/p/ ' , assetsOnDisk ) ;
const String expectedAssetManifest =
' {"packages/test_package/a/foo":["packages/test_package/a/foo","packages/test_package/a/b/foo"]} ' ;
await buildAndVerifyAssets (
assetsOnDisk ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-05-21 00:52:33 +00:00
} ) ;
2018-09-05 18:30:55 +00:00
testUsingContext (
2018-05-21 00:52:33 +00:00
' No asset is bundled with variant, no assets or directories are listed ' , ( ) async {
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2018-05-21 00:52:33 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assetsOnDisk = < String > [ ' a/foo ' , ' a/b/foo ' ] ;
final List < String > assetOnManifest = < String > [ ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assetOnManifest ,
) ;
writeAssets ( ' p/p/ ' , assetsOnDisk ) ;
const String expectedAssetManifest = ' {} ' ;
await buildAndVerifyAssets (
assetOnManifest ,
< String > [ ' test_package ' ] ,
expectedAssetManifest ,
) ;
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-05-21 00:52:33 +00:00
} ) ;
2018-09-05 18:30:55 +00:00
testUsingContext (
2018-05-21 00:52:33 +00:00
' Expect error generating manifest, wrong non-existing directory is listed ' , ( ) async {
establishFlutterRoot ( ) ;
2018-09-05 18:30:55 +00:00
writeEmptySchemaFile ( fs ) ;
2018-05-21 00:52:33 +00:00
writePubspecFile ( ' pubspec.yaml ' , ' test ' ) ;
writePackagesFile ( ' test_package:p/p/lib/ ' ) ;
final List < String > assetOnManifest = < String > [ ' c/ ' ] ;
writePubspecFile (
' p/p/pubspec.yaml ' ,
' test_package ' ,
assets: assetOnManifest ,
) ;
try {
await buildAndVerifyAssets (
assetOnManifest ,
< String > [ ' test_package ' ] ,
null ,
) ;
final Function watchdog = ( ) async {
assert ( false , ' Code failed to detect missing directory. Test failed. ' ) ;
} ;
watchdog ( ) ;
} catch ( e ) {
// Test successful
}
2018-09-05 18:30:55 +00:00
} , overrides: < Type , Generator > {
FileSystem: ( ) = > testFileSystem ,
2018-05-21 00:52:33 +00:00
} ) ;
} ) ;
2017-09-05 07:42:24 +00:00
}