2016-11-15 15:01:00 +00:00
|
|
|
// Copyright 2016 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:convert';
|
2017-06-22 23:45:57 +00:00
|
|
|
|
|
|
|
import 'package:file/file.dart';
|
2018-09-05 18:30:32 +00:00
|
|
|
import 'package:file/memory.dart';
|
2017-06-22 23:45:57 +00:00
|
|
|
|
2016-11-15 15:01:00 +00:00
|
|
|
import 'package:flutter_tools/src/asset.dart';
|
2017-01-07 00:51:44 +00:00
|
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
2018-09-05 18:30:32 +00:00
|
|
|
import 'package:flutter_tools/src/base/platform.dart';
|
2017-03-10 17:39:01 +00:00
|
|
|
import 'package:flutter_tools/src/cache.dart';
|
2017-06-22 23:45:57 +00:00
|
|
|
|
2017-03-10 17:39:01 +00:00
|
|
|
import 'src/common.dart';
|
2017-06-22 23:45:57 +00:00
|
|
|
import 'src/context.dart';
|
2017-03-10 17:39:01 +00:00
|
|
|
|
2018-02-02 22:27:29 +00:00
|
|
|
void main() {
|
2017-03-10 17:39:01 +00:00
|
|
|
setUpAll(() {
|
|
|
|
Cache.flutterRoot = getFlutterRoot();
|
|
|
|
});
|
|
|
|
|
2016-12-13 21:43:28 +00:00
|
|
|
group('AssetBundle.build', () {
|
2018-09-05 18:30:32 +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:32 +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
|
|
|
});
|
|
|
|
|
2018-01-29 17:40:28 +00:00
|
|
|
testUsingContext('nonempty', () async {
|
|
|
|
final AssetBundle ab = AssetBundleFactory.instance.createBundle();
|
2016-12-13 21:43:28 +00:00
|
|
|
expect(await ab.build(), 0);
|
|
|
|
expect(ab.entries.length, greaterThan(0));
|
2018-09-05 18:30:32 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2016-12-13 21:43:28 +00:00
|
|
|
});
|
2017-09-26 21:14:20 +00:00
|
|
|
|
|
|
|
testUsingContext('empty pubspec', () async {
|
|
|
|
fs.file('pubspec.yaml')
|
|
|
|
..createSync()
|
|
|
|
..writeAsStringSync('');
|
|
|
|
|
2018-01-29 17:40:28 +00:00
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
2017-09-26 21:14:20 +00:00
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
|
|
|
expect(bundle.entries.length, 1);
|
2018-02-01 06:51:26 +00:00
|
|
|
const String expectedAssetManifest = '{}';
|
2017-09-26 21:14:20 +00:00
|
|
|
expect(
|
2019-05-31 20:17:12 +00:00
|
|
|
utf8.decode(await bundle.entries['AssetManifest.json'].contentsAsBytes()),
|
2017-09-26 21:14:20 +00:00
|
|
|
expectedAssetManifest,
|
|
|
|
);
|
2018-09-05 18:30:32 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
2018-01-24 21:16:23 +00:00
|
|
|
});
|
2019-03-27 04:40:53 +00:00
|
|
|
|
|
|
|
testUsingContext('wildcard directories are updated when filesystem changes', () async {
|
|
|
|
fs.file('.packages').createSync();
|
|
|
|
fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
|
|
fs.file('pubspec.yaml')
|
|
|
|
..createSync()
|
|
|
|
..writeAsStringSync(r'''
|
|
|
|
name: example
|
|
|
|
flutter:
|
|
|
|
assets:
|
|
|
|
- assets/foo/
|
|
|
|
''');
|
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
|
|
|
// Expected assets:
|
|
|
|
// - asset manifest
|
|
|
|
// - font manifest
|
|
|
|
// - license file
|
|
|
|
// - assets/foo/bar.txt
|
|
|
|
expect(bundle.entries.length, 4);
|
|
|
|
expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), false);
|
|
|
|
|
|
|
|
// Adding a file should update the stat of the directory, but instead
|
|
|
|
// we need to fully recreate it.
|
|
|
|
fs.directory(fs.path.join('assets', 'foo')).deleteSync(recursive: true);
|
|
|
|
fs.file(fs.path.join('assets', 'foo', 'fizz.txt')).createSync(recursive: true);
|
|
|
|
fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync();
|
|
|
|
|
|
|
|
expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), true);
|
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
|
|
|
// Expected assets:
|
|
|
|
// - asset manifest
|
|
|
|
// - font manifest
|
|
|
|
// - license file
|
|
|
|
// - assets/foo/bar.txt
|
|
|
|
// - assets/foo/fizz.txt
|
|
|
|
expect(bundle.entries.length, 5);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
|
|
|
});
|
2019-03-29 21:26:56 +00:00
|
|
|
|
|
|
|
testUsingContext('handle removal of wildcard directories', () async {
|
|
|
|
fs.file('.packages').createSync();
|
|
|
|
fs.file(fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
|
|
fs.file('pubspec.yaml')
|
|
|
|
..createSync()
|
|
|
|
..writeAsStringSync(r'''
|
|
|
|
name: example
|
|
|
|
flutter:
|
|
|
|
assets:
|
|
|
|
- assets/foo/
|
|
|
|
''');
|
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
|
|
|
// Expected assets:
|
|
|
|
// - asset manifest
|
|
|
|
// - font manifest
|
|
|
|
// - license file
|
|
|
|
// - assets/foo/bar.txt
|
|
|
|
expect(bundle.entries.length, 4);
|
|
|
|
expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), false);
|
|
|
|
|
|
|
|
// Delete the wildcard directory and update pubspec file.
|
|
|
|
fs.directory(fs.path.join('assets', 'foo')).deleteSync(recursive: true);
|
|
|
|
fs.file('pubspec.yaml')
|
|
|
|
..createSync()
|
|
|
|
..writeAsStringSync(r'''
|
|
|
|
name: example''');
|
|
|
|
|
|
|
|
// Even though the previous file was removed, it is left in the
|
|
|
|
// asset manifest and not updated. This is due to the devfs not
|
|
|
|
// supporting file deletion.
|
|
|
|
expect(bundle.needsBuild(manifestPath: 'pubspec.yaml'), true);
|
|
|
|
await bundle.build(manifestPath: 'pubspec.yaml');
|
|
|
|
// Expected assets:
|
|
|
|
// - asset manifest
|
|
|
|
// - font manifest
|
|
|
|
// - license file
|
|
|
|
// - assets/foo/bar.txt
|
|
|
|
expect(bundle.entries.length, 4);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
FileSystem: () => testFileSystem,
|
|
|
|
});
|
2016-12-13 21:43:28 +00:00
|
|
|
});
|
2017-06-22 23:45:57 +00:00
|
|
|
|
2016-11-15 15:01:00 +00:00
|
|
|
}
|