Make Assets more robust across platforms (#14246)
This commit is contained in:
committed by
GitHub
parent
9654659c72
commit
84580b545a
@@ -6,7 +6,6 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
|
||||
import 'package:flutter_tools/src/asset.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
@@ -92,6 +91,28 @@ $fontsSection
|
||||
..writeAsStringSync(font);
|
||||
}
|
||||
|
||||
// These tests do not use a memory file system because we want to ensure that
|
||||
// asset bundles work correctly on Windows and Posix systems.
|
||||
Directory tempDir;
|
||||
Directory oldCurrentDir;
|
||||
|
||||
setUp(() async {
|
||||
tempDir = await fs.systemTempDirectory.createTemp('asset_bundle_tests');
|
||||
oldCurrentDir = fs.currentDirectory;
|
||||
fs.currentDirectory = tempDir;
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
fs.currentDirectory = oldCurrentDir;
|
||||
try {
|
||||
tempDir?.deleteSync(recursive: true);
|
||||
tempDir = null;
|
||||
} on FileSystemException catch (e) {
|
||||
// Do nothing, windows sometimes has trouble deleting.
|
||||
print('Ignored exception during tearDown: $e');
|
||||
}
|
||||
});
|
||||
|
||||
group('AssetBundle fonts from packages', () {
|
||||
testUsingContext('App includes neither font manifest nor fonts when no defines fonts', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -104,7 +125,7 @@ $fontsSection
|
||||
await bundle.build(manifestPath: 'pubspec.yaml');
|
||||
expect(bundle.entries.length, 2); // LICENSE, AssetManifest
|
||||
expect(bundle.entries.containsKey('FontManifest.json'), false);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('App font uses font file from package', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -129,7 +150,7 @@ $fontsSection
|
||||
<String>['test_package'],
|
||||
expectedFontManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('App font uses local font file and package font file', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -158,7 +179,7 @@ $fontsSection
|
||||
<String>['test_package'],
|
||||
expectedFontManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('App uses package font with own font file', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -188,7 +209,7 @@ $fontsSection
|
||||
<String>['test_package'],
|
||||
expectedFontManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('App uses package font with font file from another package', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -219,7 +240,7 @@ $fontsSection
|
||||
<String>['test_package2'],
|
||||
expectedFontManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('App uses package font with properties and own font file', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -251,7 +272,7 @@ $fontsSection
|
||||
<String>['test_package'],
|
||||
expectedFontManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('App uses local font and package font with own font file.', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -287,10 +308,6 @@ $fontsSection
|
||||
<String>['test_package'],
|
||||
expectedFontManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Map<Type, Generator> get contextOverrides {
|
||||
return <Type, Generator>{FileSystem: () => new MemoryFileSystem()};
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
|
||||
import 'package:flutter_tools/src/asset.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
@@ -93,6 +92,28 @@ $assetsSection
|
||||
}
|
||||
}
|
||||
|
||||
// These tests do not use a memory file system because we want to ensure that
|
||||
// asset bundles work correctly on Windows and Posix systems.
|
||||
Directory tempDir;
|
||||
Directory oldCurrentDir;
|
||||
|
||||
setUp(() async {
|
||||
tempDir = await fs.systemTempDirectory.createTemp('asset_bundle_tests');
|
||||
oldCurrentDir = fs.currentDirectory;
|
||||
fs.currentDirectory = tempDir;
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
fs.currentDirectory = oldCurrentDir;
|
||||
try {
|
||||
tempDir?.deleteSync(recursive: true);
|
||||
tempDir = null;
|
||||
} on FileSystemException catch (e) {
|
||||
// Do nothing, windows sometimes has trouble deleting.
|
||||
print('Ignored exception during tearDown: $e');
|
||||
}
|
||||
});
|
||||
|
||||
group('AssetBundle assets from packages', () {
|
||||
testUsingContext('No assets are bundled when the package has no assets', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -109,7 +130,7 @@ $assetsSection
|
||||
UTF8.decode(await bundle.entries['AssetManifest.json'].contentsAsBytes()),
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('No assets are bundled when the package has an asset that is not listed', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -130,7 +151,7 @@ $assetsSection
|
||||
expectedAssetManifest,
|
||||
);
|
||||
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('One asset is bundled when the package has and lists one asset its pubspec', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -154,7 +175,7 @@ $assetsSection
|
||||
<String>['test_package'],
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext("One asset is bundled when the package has one asset, listed in the app's pubspec", () async {
|
||||
establishFlutterRoot();
|
||||
@@ -178,7 +199,7 @@ $assetsSection
|
||||
<String>['test_package'],
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
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 {
|
||||
establishFlutterRoot();
|
||||
@@ -202,7 +223,7 @@ $assetsSection
|
||||
<String>['test_package'],
|
||||
expectedManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
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 {
|
||||
establishFlutterRoot();
|
||||
@@ -229,7 +250,7 @@ $assetsSection
|
||||
<String>['test_package'],
|
||||
expectedManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('Two assets are bundled when the package has and lists two assets in its pubspec', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -254,7 +275,7 @@ $assetsSection
|
||||
<String>['test_package'],
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext("Two assets are bundled when the package has two assets, listed in the app's pubspec", () async {
|
||||
establishFlutterRoot();
|
||||
@@ -286,7 +307,7 @@ $assetsSection
|
||||
<String>['test_package'],
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('Two assets are bundled when two packages each have and list an asset their pubspec', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -322,7 +343,7 @@ $assetsSection
|
||||
<String>['test_package', 'test_package2'],
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext("Two assets are bundled when two packages each have an asset, listed in the app's pubspec", () async {
|
||||
establishFlutterRoot();
|
||||
@@ -361,7 +382,7 @@ $assetsSection
|
||||
<String>['test_package', 'test_package2'],
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('One asset is bundled when the app depends on a package, listing in its pubspec an asset from another package', () async {
|
||||
establishFlutterRoot();
|
||||
@@ -392,10 +413,6 @@ $assetsSection
|
||||
<String>['test_package2'],
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Map<Type, Generator> get contextOverrides {
|
||||
return <Type, Generator>{FileSystem: () => new MemoryFileSystem()};
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
|
||||
import 'package:flutter_tools/src/asset.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
@@ -76,6 +75,28 @@ void main() {
|
||||
});
|
||||
|
||||
group('AssetBundle.build', () {
|
||||
// These tests do not use a memory file system because we want to ensure that
|
||||
// asset bundles work correctly on Windows and Posix systems.
|
||||
Directory tempDir;
|
||||
Directory oldCurrentDir;
|
||||
|
||||
setUp(() async {
|
||||
tempDir = await fs.systemTempDirectory.createTemp('asset_bundle_tests');
|
||||
oldCurrentDir = fs.currentDirectory;
|
||||
fs.currentDirectory = tempDir;
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
fs.currentDirectory = oldCurrentDir;
|
||||
try {
|
||||
tempDir?.deleteSync(recursive: true);
|
||||
tempDir = null;
|
||||
} on FileSystemException catch (e) {
|
||||
// Do nothing, windows sometimes has trouble deleting.
|
||||
print('Ignored exception during tearDown: $e');
|
||||
}
|
||||
});
|
||||
|
||||
test('nonempty', () async {
|
||||
final AssetBundle ab = new AssetBundle();
|
||||
expect(await ab.build(), 0);
|
||||
@@ -95,7 +116,7 @@ void main() {
|
||||
UTF8.decode(await bundle.entries['AssetManifest.json'].contentsAsBytes()),
|
||||
expectedAssetManifest,
|
||||
);
|
||||
}, overrides: <Type, Generator>{FileSystem: () => new MemoryFileSystem(),});
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
|
||||
import 'package:flutter_tools/src/asset.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
@@ -17,6 +16,28 @@ import 'src/common.dart';
|
||||
import 'src/context.dart';
|
||||
|
||||
void main() {
|
||||
// These tests do not use a memory file system because we want to ensure that
|
||||
// asset bundles work correctly on Windows and Posix systems.
|
||||
Directory tempDir;
|
||||
Directory oldCurrentDir;
|
||||
|
||||
setUp(() async {
|
||||
tempDir = await fs.systemTempDirectory.createTemp('asset_bundle_tests');
|
||||
oldCurrentDir = fs.currentDirectory;
|
||||
fs.currentDirectory = tempDir;
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
fs.currentDirectory = oldCurrentDir;
|
||||
try {
|
||||
tempDir?.deleteSync(recursive: true);
|
||||
tempDir = null;
|
||||
} on FileSystemException catch (e) {
|
||||
// Do nothing, windows sometimes has trouble deleting.
|
||||
print('Ignored exception during tearDown: $e');
|
||||
}
|
||||
});
|
||||
|
||||
group('AssetBundle asset variants', () {
|
||||
testUsingContext('main asset and variants', () async {
|
||||
// Setting flutterRoot here so that it picks up the MemoryFileSystem's
|
||||
@@ -59,19 +80,17 @@ flutter:
|
||||
expect(UTF8.decode(await bundle.entries[asset].contentsAsBytes()), asset);
|
||||
}
|
||||
|
||||
fs.file('/a/b/c/foo').deleteSync();
|
||||
fs.file('a/b/c/foo').deleteSync();
|
||||
bundle = new AssetBundle();
|
||||
await bundle.build(manifestPath: 'pubspec.yaml');
|
||||
|
||||
// Now the main asset file, /a/b/c/foo, does not exist. This is OK because
|
||||
// the /a/b/c/*/foo variants do exist.
|
||||
expect(bundle.entries.containsKey('/a/b/c/foo'), false);
|
||||
expect(bundle.entries.containsKey('a/b/c/foo'), false);
|
||||
for (String asset in assets.skip(1)) {
|
||||
expect(bundle.entries.containsKey(asset), true);
|
||||
expect(UTF8.decode(await bundle.entries[asset].contentsAsBytes()), asset);
|
||||
}
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => new MemoryFileSystem(),
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -69,7 +69,9 @@ flutter:
|
||||
- a/bar
|
||||
''';
|
||||
final FlutterManifest flutterManifest = await FlutterManifest.createFromString(manifest);
|
||||
expect(flutterManifest.assets, <String>['a/foo', 'a/bar']);
|
||||
expect(flutterManifest.assets.length, 2);
|
||||
expect(flutterManifest.assets[0], Uri.parse('a/foo'));
|
||||
expect(flutterManifest.assets[1], Uri.parse('a/bar'));
|
||||
});
|
||||
|
||||
test('has one font family with one asset', () async {
|
||||
@@ -97,7 +99,7 @@ flutter:
|
||||
final List<FontAsset> assets = font.fontAssets;
|
||||
expect(assets.length, 1);
|
||||
final FontAsset fontAsset = assets[0];
|
||||
expect(fontAsset.asset, 'a/bar');
|
||||
expect(fontAsset.assetUri.path, 'a/bar');
|
||||
expect(fontAsset.weight, isNull);
|
||||
expect(fontAsset.style, isNull);
|
||||
});
|
||||
@@ -130,11 +132,11 @@ flutter:
|
||||
final List<FontAsset> assets = font.fontAssets;
|
||||
expect(assets.length, 2);
|
||||
final FontAsset fontAsset0 = assets[0];
|
||||
expect(fontAsset0.asset, 'a/bar');
|
||||
expect(fontAsset0.assetUri.path, 'a/bar');
|
||||
expect(fontAsset0.weight, isNull);
|
||||
expect(fontAsset0.style, isNull);
|
||||
final FontAsset fontAsset1 = assets[1];
|
||||
expect(fontAsset1.asset, 'a/bar');
|
||||
expect(fontAsset1.assetUri.path, 'a/bar');
|
||||
expect(fontAsset1.weight, 400);
|
||||
expect(fontAsset1.style, isNull);
|
||||
});
|
||||
@@ -168,11 +170,11 @@ flutter:
|
||||
final List<FontAsset> assets = font.fontAssets;
|
||||
expect(assets.length, 2);
|
||||
final FontAsset fontAsset0 = assets[0];
|
||||
expect(fontAsset0.asset, 'a/bar');
|
||||
expect(fontAsset0.assetUri.path, 'a/bar');
|
||||
expect(fontAsset0.weight, isNull);
|
||||
expect(fontAsset0.style, isNull);
|
||||
final FontAsset fontAsset1 = assets[1];
|
||||
expect(fontAsset1.asset, 'a/bar');
|
||||
expect(fontAsset1.assetUri.path, 'a/bar');
|
||||
expect(fontAsset1.weight, 400);
|
||||
expect(fontAsset1.style, 'italic');
|
||||
});
|
||||
@@ -214,11 +216,11 @@ flutter:
|
||||
final List<FontAsset> fooAassets = fooFont.fontAssets;
|
||||
expect(fooAassets.length, 2);
|
||||
final FontAsset fooFontAsset0 = fooAassets[0];
|
||||
expect(fooFontAsset0.asset, 'a/bar');
|
||||
expect(fooFontAsset0.assetUri.path, 'a/bar');
|
||||
expect(fooFontAsset0.weight, isNull);
|
||||
expect(fooFontAsset0.style, isNull);
|
||||
final FontAsset fooFontAsset1 = fooAassets[1];
|
||||
expect(fooFontAsset1.asset, 'a/bar');
|
||||
expect(fooFontAsset1.assetUri.path, 'a/bar');
|
||||
expect(fooFontAsset1.weight, 400);
|
||||
expect(fooFontAsset1.style, 'italic');
|
||||
|
||||
@@ -229,11 +231,11 @@ flutter:
|
||||
final List<FontAsset> barAssets = barFont.fontAssets;
|
||||
expect(barAssets.length, 2);
|
||||
final FontAsset barFontAsset0 = barAssets[0];
|
||||
expect(barFontAsset0.asset, 'a/baz');
|
||||
expect(barFontAsset0.assetUri.path, 'a/baz');
|
||||
expect(barFontAsset0.weight, isNull);
|
||||
expect(barFontAsset0.style, isNull);
|
||||
final FontAsset barFontAsset1 = barAssets[1];
|
||||
expect(barFontAsset1.asset, 'a/baz');
|
||||
expect(barFontAsset1.assetUri.path, 'a/baz');
|
||||
expect(barFontAsset1.weight, 400);
|
||||
expect(barFontAsset1.style, 'italic');
|
||||
});
|
||||
@@ -274,11 +276,11 @@ flutter:
|
||||
final List<FontAsset> fooAassets = fooFont.fontAssets;
|
||||
expect(fooAassets.length, 2);
|
||||
final FontAsset fooFontAsset0 = fooAassets[0];
|
||||
expect(fooFontAsset0.asset, 'a/bar');
|
||||
expect(fooFontAsset0.assetUri.path, 'a/bar');
|
||||
expect(fooFontAsset0.weight, isNull);
|
||||
expect(fooFontAsset0.style, isNull);
|
||||
final FontAsset fooFontAsset1 = fooAassets[1];
|
||||
expect(fooFontAsset1.asset, 'a/bar');
|
||||
expect(fooFontAsset1.assetUri.path, 'a/bar');
|
||||
expect(fooFontAsset1.weight, 400);
|
||||
expect(fooFontAsset1.style, 'italic');
|
||||
});
|
||||
@@ -314,11 +316,11 @@ flutter:
|
||||
final List<FontAsset> fooAassets = fooFont.fontAssets;
|
||||
expect(fooAassets.length, 2);
|
||||
final FontAsset fooFontAsset0 = fooAassets[0];
|
||||
expect(fooFontAsset0.asset, 'a/bar');
|
||||
expect(fooFontAsset0.assetUri.path, 'a/bar');
|
||||
expect(fooFontAsset0.weight, isNull);
|
||||
expect(fooFontAsset0.style, isNull);
|
||||
final FontAsset fooFontAsset1 = fooAassets[1];
|
||||
expect(fooFontAsset1.asset, 'a/bar');
|
||||
expect(fooFontAsset1.assetUri.path, 'a/bar');
|
||||
expect(fooFontAsset1.weight, 400);
|
||||
expect(fooFontAsset1.style, 'italic');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user