mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

Provides support for conditional bundling of assets through the existing `--flavor` option for `flutter build` and `flutter run`. Closes https://github.com/flutter/flutter/issues/21682. Resolves https://github.com/flutter/flutter/issues/136092 ## Change Within the `assets` section pubspec.yaml, the user can now specify one or more `flavors` that an asset belongs to. Consider this example: ```yaml # pubspec.yaml flutter: assets: - assets/normal-asset.png - path: assets/vanilla/ice-cream.png flavors: - vanilla - path: assets/strawberry/ice-cream.png flavors: - strawberry ``` With this pubspec, * `flutter run --flavor vanilla` will not include `assets/strawberry/ice-cream.png` in the build output. * `flutter run --flavor strawberry` will not include `assets/vanilla/ice-cream.png`. * `flutter run` will only include `assets/normal-asset.png`. ## Open questions * Should this be supported for all platforms, or should this change be limited to ones with documented `--flavor` support (Android, iOS, and (implicitly) MacOS)? This PR currently only enables this feature for officially supported platforms. ## Design thoughts, what this PR does not do, etc. ### This does not provide an automatic mapping/resolution of asset keys/paths to others based on flavor at runtime. The implementation in this PR represents a simplest approach. Notably, it does not give Flutter the ability to dynamically choose an asset based on flavor using a single asset key. For example, one can't use `Image.asset('config.json')` to dynamically choose between different "flavors" of `config.json` (such as `dev-flavor/config.json` or `prod-flavor/config.json`). However, a user could always implement such a mechanism in their project or in a library by examining the flavor at runtime. ### When multiple entries affect the same file and 1) at least one of these entries have a `flavors` list provided and 2) these lists are not equivalent, we always consider the manifest to be ambiguous and will throw a `ToolExit`. <details> For example, these manifests would all be considered ambiguous: ```yaml assets: - assets/ - path: assets/vanilla.png flavors: - vanilla assets: - path: assets/vanilla/ flavors: - vanilla - path: assets/vanilla/cherry.png flavor: - cherry # Thinking towards the future where we might add glob/regex support and more conditions other than flavor: assets: - path: assets/vanilla/** flavors: - vanilla - path: assets/**/ios/** platforms: - ios # Ambiguous in the case of assets like "assets/vanilla/ios/icon.svg" since we # don't know if flavor `vanilla` and platform `ios` should be combined using or-logic or and-logic. ``` See [this review comment thread](https://github.com/flutter/flutter/pull/132985#discussion_r1381909942) for the full story on how I arrived at this decision. </details> ### This does not support Android's multidimensional flavors feature (in an intuitive way) <details> Conder this excerpt from a Flutter project's android/app/build.gradle file: ```groovy android { // ... flavorDimensions "mode", "api" productFlavors { free { dimension "mode" applicationIdSuffix ".free" } premium { dimension "mode" applicationIdSuffix ".premium" } minApi23 { dimension "api" versionNameSuffix "-minApi23" } minApi21 { dimension "api" versionNameSuffix "-minApi21" } } } ``` In this setup, the following values are valid `--flavor` are valid `freeMinApi21`, `freeMinApi23`, `premiumMinApi21`, and `premiumMinApi23`. We call these values "flavor combinations". Consider the following from the Android documentation[^1]: > In addition to the source set directories you can create for each individual product flavor and build variant, you can also create source set directories for each combination of product flavors. For example, you can create and add Java sources to the src/demoMinApi24/java/ directory, and Gradle uses those sources only when building a variant that combines those two product flavors. > > Source sets you create for product flavor combinations have a higher priority than source sets that belong to each individual product flavor. To learn more about source sets and how Gradle merges resources, read the section about how to [create source sets](https://developer.android.com/build/build-variants#sourcesets). This feature will not behave in this way. If a user utilizes this feature and also Android's multidimensional flavors feature, they will have to list out all flavor combinations that contain the flavor they want to limit an asset to: ```yaml assets: - assets/free/ flavors: - freeMinApi21 - freeMinApi23 ``` This is mostly due to a technical limitation in the hot-reload feature of `flutter run`. During a hot reload, the tool will try to update the asset bundle on the device, but the tool does not know the flavors contained within the flavor combination (that the user passes to `--flavor`). Gradle is the source of truth of what flavors were involved in the build, and `flutter run` currently does not access to that information since it's an implementation detail of the build process. We could bubble up this information, but it would require a nontrivial amount of engineering work, and it's unclear how desired this functionality is. It might not be worth implementing. </details> See https://flutter.dev/go/flavor-specific-assets for the (outdated) design document. <summary>Pre-launch Checklist</summary> </details> [^1]: https://developer.android.com/build/build-variants#flavor-dimensions
1129 lines
41 KiB
Dart
1129 lines
41 KiB
Dart
// Copyright 2014 The Flutter 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';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:file/memory.dart';
|
|
import 'package:flutter_tools/src/artifacts.dart';
|
|
import 'package:flutter_tools/src/asset.dart';
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
|
import 'package:flutter_tools/src/base/logger.dart';
|
|
import 'package:flutter_tools/src/base/platform.dart';
|
|
import 'package:flutter_tools/src/base/user_messages.dart';
|
|
import 'package:flutter_tools/src/build_info.dart';
|
|
import 'package:flutter_tools/src/bundle_builder.dart';
|
|
import 'package:flutter_tools/src/cache.dart';
|
|
import 'package:flutter_tools/src/devfs.dart';
|
|
import 'package:flutter_tools/src/globals.dart' as globals;
|
|
import 'package:flutter_tools/src/project.dart';
|
|
import 'package:standard_message_codec/standard_message_codec.dart';
|
|
|
|
import '../src/common.dart';
|
|
import '../src/context.dart';
|
|
|
|
const String shaderLibDir = '/./shader_lib';
|
|
|
|
void main() {
|
|
group('AssetBundle.build', () {
|
|
late Logger logger;
|
|
late FileSystem testFileSystem;
|
|
late Platform platform;
|
|
|
|
setUp(() async {
|
|
testFileSystem = MemoryFileSystem(
|
|
style: globals.platform.isWindows
|
|
? FileSystemStyle.windows
|
|
: FileSystemStyle.posix,
|
|
);
|
|
testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
|
|
logger = BufferLogger.test();
|
|
platform = FakePlatform(operatingSystem: globals.platform.operatingSystem);
|
|
});
|
|
|
|
testUsingContext('nonempty', () async {
|
|
final AssetBundle ab = AssetBundleFactory.instance.createBundle();
|
|
expect(await ab.build(packagesPath: '.packages'), 0);
|
|
expect(ab.entries.length, greaterThan(0));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('empty pubspec', () async {
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync('');
|
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages');
|
|
expect(bundle.entries.keys,
|
|
unorderedEquals(<String>['AssetManifest.json', 'AssetManifest.bin'])
|
|
);
|
|
const String expectedJsonAssetManifest = '{}';
|
|
const Map<Object, Object> expectedBinAssetManifest = <Object, Object>{};
|
|
expect(
|
|
utf8.decode(await bundle.entries['AssetManifest.json']!.contentsAsBytes()),
|
|
expectedJsonAssetManifest,
|
|
);
|
|
expect(
|
|
const StandardMessageCodec().decodeMessage(ByteData.sublistView(Uint8List.fromList(await bundle.entries['AssetManifest.bin']!.contentsAsBytes()))),
|
|
expectedBinAssetManifest
|
|
);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('wildcard directories do not include subdirectories', () async {
|
|
globals.fs.file('.packages').createSync();
|
|
globals.fs.file('pubspec.yaml').writeAsStringSync(
|
|
'''
|
|
name: test
|
|
dependencies:
|
|
flutter:
|
|
sdk: flutter
|
|
flutter:
|
|
assets:
|
|
- assets/foo/
|
|
- assets/bar/lizard.png
|
|
'''
|
|
);
|
|
|
|
final List<String> assets = <String>[
|
|
'assets/foo/dog.png',
|
|
'assets/foo/sub/cat.png',
|
|
'assets/bar/lizard.png',
|
|
'assets/bar/sheep.png'
|
|
];
|
|
|
|
for (final String asset in assets) {
|
|
final File assetFile = globals.fs.file(
|
|
globals.fs.path.joinAll(asset.split('/'))
|
|
);
|
|
assetFile.createSync(recursive: true);
|
|
}
|
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages');
|
|
|
|
expect(bundle.entries.keys, unorderedEquals(<String>[
|
|
'AssetManifest.json',
|
|
'AssetManifest.bin',
|
|
'FontManifest.json',
|
|
'NOTICES.Z',
|
|
'assets/foo/dog.png',
|
|
'assets/bar/lizard.png'
|
|
]));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('wildcard directories are updated when filesystem changes', () async {
|
|
final File packageFile = globals.fs.file('.packages')..createSync();
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/foo/
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages');
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['AssetManifest.json',
|
|
'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z', 'assets/foo/bar.txt']));
|
|
// Simulate modifying the files by updating the filestat time manually.
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'fizz.txt'))
|
|
..createSync(recursive: true)
|
|
..setLastModifiedSync(packageFile.lastModifiedSync().add(const Duration(hours: 1)));
|
|
|
|
expect(bundle.needsBuild(), true);
|
|
await bundle.build(packagesPath: '.packages');
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['AssetManifest.json',
|
|
'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z', 'assets/foo/bar.txt',
|
|
'assets/foo/fizz.txt']));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('handle removal of wildcard directories', () async {
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
final File pubspec = globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/foo/
|
|
''');
|
|
globals.fs.file('.packages').createSync();
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages');
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['AssetManifest.json',
|
|
'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z', 'assets/foo/bar.txt']));
|
|
expect(bundle.needsBuild(), false);
|
|
|
|
// Delete the wildcard directory and update pubspec file.
|
|
final DateTime modifiedTime = pubspec.lastModifiedSync().add(const Duration(hours: 1));
|
|
globals.fs.directory(globals.fs.path.join('assets', 'foo')).deleteSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example''')
|
|
..setLastModifiedSync(modifiedTime);
|
|
|
|
// touch .packages to make sure its change time is after pubspec.yaml's
|
|
globals.fs.file('.packages')
|
|
.setLastModifiedSync(modifiedTime);
|
|
|
|
// 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(), true);
|
|
await bundle.build(packagesPath: '.packages');
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['AssetManifest.json',
|
|
'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z', 'assets/foo/bar.txt']));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
// https://github.com/flutter/flutter/issues/42723
|
|
testUsingContext('Test regression for mistyped file', () async {
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
// Create a directory in the same path to test that we're only looking at File
|
|
// objects.
|
|
globals.fs.directory(globals.fs.path.join('assets', 'foo', 'bar')).createSync();
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/foo/
|
|
''');
|
|
globals.fs.file('.packages').createSync();
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages');
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['AssetManifest.json',
|
|
'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z', 'assets/foo/bar.txt']));
|
|
expect(bundle.needsBuild(), false);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('deferred assets are parsed', () async {
|
|
globals.fs.file('.packages').createSync();
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
globals.fs.file(globals.fs.path.join('assets', 'bar', 'barbie.txt')).createSync(recursive: true);
|
|
globals.fs.file(globals.fs.path.join('assets', 'wild', 'dash.txt')).createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/foo/
|
|
deferred-components:
|
|
- name: component1
|
|
assets:
|
|
- assets/bar/barbie.txt
|
|
- assets/wild/
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.defaultInstance(
|
|
logger: globals.logger,
|
|
fileSystem: globals.fs,
|
|
platform: globals.platform,
|
|
splitDeferredAssets: true,
|
|
).createBundle();
|
|
await bundle.build(packagesPath: '.packages', deferredComponentsEnabled: true);
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['AssetManifest.json',
|
|
'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z', 'assets/foo/bar.txt']));
|
|
expect(bundle.deferredComponentsEntries.length, 1);
|
|
expect(bundle.deferredComponentsEntries['component1']!.length, 2);
|
|
expect(bundle.needsBuild(), false);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('deferred assets are parsed regularly when splitDeferredAssets Disabled', () async {
|
|
globals.fs.file('.packages').createSync();
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
globals.fs.file(globals.fs.path.join('assets', 'bar', 'barbie.txt')).createSync(recursive: true);
|
|
globals.fs.file(globals.fs.path.join('assets', 'wild', 'dash.txt')).createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/foo/
|
|
deferred-components:
|
|
- name: component1
|
|
assets:
|
|
- assets/bar/barbie.txt
|
|
- assets/wild/
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages');
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['assets/foo/bar.txt',
|
|
'assets/bar/barbie.txt', 'assets/wild/dash.txt', 'AssetManifest.json',
|
|
'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z']));
|
|
expect(bundle.deferredComponentsEntries.isEmpty, true);
|
|
expect(bundle.needsBuild(), false);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('deferred assets wildcard parsed', () async {
|
|
final File packageFile = globals.fs.file('.packages')..createSync();
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
globals.fs.file(globals.fs.path.join('assets', 'bar', 'barbie.txt')).createSync(recursive: true);
|
|
globals.fs.file(globals.fs.path.join('assets', 'wild', 'dash.txt')).createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/foo/
|
|
deferred-components:
|
|
- name: component1
|
|
assets:
|
|
- assets/bar/barbie.txt
|
|
- assets/wild/
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.defaultInstance(
|
|
logger: globals.logger,
|
|
fileSystem: globals.fs,
|
|
platform: globals.platform,
|
|
splitDeferredAssets: true,
|
|
).createBundle();
|
|
await bundle.build(packagesPath: '.packages', deferredComponentsEnabled: true);
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['assets/foo/bar.txt',
|
|
'AssetManifest.json', 'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z']));
|
|
expect(bundle.deferredComponentsEntries.length, 1);
|
|
expect(bundle.deferredComponentsEntries['component1']!.length, 2);
|
|
expect(bundle.needsBuild(), false);
|
|
|
|
// Simulate modifying the files by updating the filestat time manually.
|
|
globals.fs.file(globals.fs.path.join('assets', 'wild', 'fizz.txt'))
|
|
..createSync(recursive: true)
|
|
..setLastModifiedSync(packageFile.lastModifiedSync().add(const Duration(hours: 1)));
|
|
|
|
expect(bundle.needsBuild(), true);
|
|
await bundle.build(packagesPath: '.packages', deferredComponentsEnabled: true);
|
|
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['assets/foo/bar.txt',
|
|
'AssetManifest.json', 'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z']));
|
|
expect(bundle.deferredComponentsEntries.length, 1);
|
|
expect(bundle.deferredComponentsEntries['component1']!.length, 3);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
group('flavors feature', () {
|
|
Future<ManifestAssetBundle> buildBundleWithFlavor(String? flavor) async {
|
|
final ManifestAssetBundle bundle = ManifestAssetBundle(
|
|
logger: logger,
|
|
fileSystem: testFileSystem,
|
|
platform: platform,
|
|
splitDeferredAssets: true,
|
|
);
|
|
|
|
await bundle.build(
|
|
packagesPath: '.packages',
|
|
flutterProject: FlutterProject.fromDirectoryTest(testFileSystem.currentDirectory),
|
|
flavor: flavor,
|
|
);
|
|
return bundle;
|
|
}
|
|
|
|
late final String? previousCacheFlutterRootValue;
|
|
|
|
setUpAll(() {
|
|
previousCacheFlutterRootValue = Cache.flutterRoot;
|
|
Cache.flutterRoot = Cache.defaultFlutterRoot(platform: platform, fileSystem: testFileSystem, userMessages: UserMessages());
|
|
});
|
|
|
|
tearDownAll(() => Cache.flutterRoot = previousCacheFlutterRootValue);
|
|
|
|
testWithoutContext('correctly bundles assets given a simple asset manifest with flavors', () async {
|
|
testFileSystem.file('.packages').createSync();
|
|
testFileSystem.file(testFileSystem.path.join('assets', 'common', 'image.png')).createSync(recursive: true);
|
|
testFileSystem.file(testFileSystem.path.join('assets', 'vanilla', 'ice-cream.png')).createSync(recursive: true);
|
|
testFileSystem.file(testFileSystem.path.join('assets', 'strawberry', 'ice-cream.png')).createSync(recursive: true);
|
|
testFileSystem.file(testFileSystem.path.join('assets', 'orange', 'ice-cream.png')).createSync(recursive: true);
|
|
testFileSystem.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/common/
|
|
- path: assets/vanilla/
|
|
flavors:
|
|
- vanilla
|
|
- path: assets/strawberry/
|
|
flavors:
|
|
- strawberry
|
|
- path: assets/orange/ice-cream.png
|
|
flavors:
|
|
- orange
|
|
''');
|
|
|
|
ManifestAssetBundle bundle;
|
|
bundle = await buildBundleWithFlavor(null);
|
|
expect(bundle.entries.keys, contains('assets/common/image.png'));
|
|
expect(bundle.entries.keys, isNot(contains('assets/vanilla/ice-cream.png')));
|
|
expect(bundle.entries.keys, isNot(contains('assets/strawberry/ice-cream.png')));
|
|
expect(bundle.entries.keys, isNot(contains('assets/orange/ice-cream.png')));
|
|
|
|
bundle = await buildBundleWithFlavor('strawberry');
|
|
expect(bundle.entries.keys, contains('assets/common/image.png'));
|
|
expect(bundle.entries.keys, isNot(contains('assets/vanilla/ice-cream.png')));
|
|
expect(bundle.entries.keys, contains('assets/strawberry/ice-cream.png'));
|
|
expect(bundle.entries.keys, isNot(contains('assets/orange/ice-cream.png')));
|
|
|
|
bundle = await buildBundleWithFlavor('orange');
|
|
expect(bundle.entries.keys, contains('assets/common/image.png'));
|
|
expect(bundle.entries.keys, isNot(contains('assets/vanilla/ice-cream.png')));
|
|
expect(bundle.entries.keys, isNot(contains('assets/strawberry/ice-cream.png')));
|
|
expect(bundle.entries.keys, contains('assets/orange/ice-cream.png'));
|
|
});
|
|
|
|
testWithoutContext('throws a tool exit when a non-flavored folder contains a flavored asset', () async {
|
|
testFileSystem.file('.packages').createSync();
|
|
testFileSystem.file(testFileSystem.path.join('assets', 'unflavored.png')).createSync(recursive: true);
|
|
testFileSystem.file(testFileSystem.path.join('assets', 'vanillaOrange.png')).createSync(recursive: true);
|
|
|
|
testFileSystem.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/
|
|
- path: assets/vanillaOrange.png
|
|
flavors:
|
|
- vanilla
|
|
- orange
|
|
''');
|
|
|
|
expect(
|
|
buildBundleWithFlavor(null),
|
|
throwsToolExit(message: 'Multiple assets entries include the file '
|
|
'"assets/vanillaOrange.png", but they specify different lists of flavors.\n'
|
|
'An entry with the path "assets/" does not specify any flavors.\n'
|
|
'An entry with the path "assets/vanillaOrange.png" specifies the flavor(s): "vanilla", "orange".\n\n'
|
|
'Consider organizing assets with different flavors into different directories.'),
|
|
);
|
|
});
|
|
|
|
testWithoutContext('throws a tool exit when a flavored folder contains a flavorless asset', () async {
|
|
testFileSystem.file('.packages').createSync();
|
|
testFileSystem.file(testFileSystem.path.join('vanilla', 'vanilla.png')).createSync(recursive: true);
|
|
testFileSystem.file(testFileSystem.path.join('vanilla', 'flavorless.png')).createSync(recursive: true);
|
|
|
|
testFileSystem.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- path: vanilla/
|
|
flavors:
|
|
- vanilla
|
|
- vanilla/flavorless.png
|
|
''');
|
|
expect(
|
|
buildBundleWithFlavor(null),
|
|
throwsToolExit(message: 'Multiple assets entries include the file '
|
|
'"vanilla/flavorless.png", but they specify different lists of flavors.\n'
|
|
'An entry with the path "vanilla/" specifies the flavor(s): "vanilla".\n'
|
|
'An entry with the path "vanilla/flavorless.png" does not specify any flavors.\n\n'
|
|
'Consider organizing assets with different flavors into different directories.'),
|
|
);
|
|
});
|
|
|
|
testWithoutContext('tool exits when two file-explicit entries give the same asset different flavors', () {
|
|
testFileSystem.file('.packages').createSync();
|
|
testFileSystem.file('orange.png').createSync(recursive: true);
|
|
testFileSystem.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- path: orange.png
|
|
flavors:
|
|
- orange
|
|
- path: orange.png
|
|
flavors:
|
|
- mango
|
|
''');
|
|
|
|
expect(
|
|
buildBundleWithFlavor(null),
|
|
throwsToolExit(message: 'Multiple assets entries include the file '
|
|
'"orange.png", but they specify different lists of flavors.\n'
|
|
'An entry with the path "orange.png" specifies the flavor(s): "orange".\n'
|
|
'An entry with the path "orange.png" specifies the flavor(s): "mango".'),
|
|
);
|
|
});
|
|
|
|
testWithoutContext('throws ToolExit when flavor from file-level declaration has different flavor from containing folder flavor declaration', () async {
|
|
testFileSystem.file('.packages').createSync();
|
|
testFileSystem.file(testFileSystem.path.join('vanilla', 'actually-strawberry.png')).createSync(recursive: true);
|
|
testFileSystem.file(testFileSystem.path.join('vanilla', 'vanilla.png')).createSync(recursive: true);
|
|
|
|
testFileSystem.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- path: vanilla/
|
|
flavors:
|
|
- vanilla
|
|
- path: vanilla/actually-strawberry.png
|
|
flavors:
|
|
- strawberry
|
|
''');
|
|
expect(
|
|
buildBundleWithFlavor(null),
|
|
throwsToolExit(message: 'Multiple assets entries include the file '
|
|
'"vanilla/actually-strawberry.png", but they specify different lists of flavors.\n'
|
|
'An entry with the path "vanilla/" specifies the flavor(s): "vanilla".\n'
|
|
'An entry with the path "vanilla/actually-strawberry.png" '
|
|
'specifies the flavor(s): "strawberry".'),
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
group('AssetBundle.build (web builds)', () {
|
|
late FileSystem testFileSystem;
|
|
|
|
setUp(() async {
|
|
testFileSystem = MemoryFileSystem(
|
|
style: globals.platform.isWindows
|
|
? FileSystemStyle.windows
|
|
: FileSystemStyle.posix,
|
|
);
|
|
testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
|
|
});
|
|
|
|
testUsingContext('empty pubspec', () async {
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync('');
|
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript);
|
|
|
|
expect(bundle.entries.keys,
|
|
unorderedEquals(<String>[
|
|
'AssetManifest.json',
|
|
'AssetManifest.bin',
|
|
'AssetManifest.bin.json',
|
|
])
|
|
);
|
|
expect(
|
|
utf8.decode(await bundle.entries['AssetManifest.json']!.contentsAsBytes()),
|
|
'{}',
|
|
);
|
|
expect(
|
|
utf8.decode(await bundle.entries['AssetManifest.bin.json']!.contentsAsBytes()),
|
|
'""',
|
|
);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('pubspec contains an asset', () async {
|
|
globals.fs.file('.packages').createSync();
|
|
globals.fs.file('pubspec.yaml').writeAsStringSync(r'''
|
|
name: test
|
|
dependencies:
|
|
flutter:
|
|
sdk: flutter
|
|
flutter:
|
|
assets:
|
|
- assets/bar/lizard.png
|
|
''');
|
|
globals.fs.file(
|
|
globals.fs.path.joinAll(<String>['assets', 'bar', 'lizard.png'])
|
|
).createSync(recursive: true);
|
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript);
|
|
|
|
expect(bundle.entries.keys,
|
|
unorderedEquals(<String>[
|
|
'AssetManifest.json',
|
|
'AssetManifest.bin',
|
|
'AssetManifest.bin.json',
|
|
'FontManifest.json',
|
|
'NOTICES', // not .Z
|
|
'assets/bar/lizard.png',
|
|
])
|
|
);
|
|
final Map<Object?, Object?> manifestJson = json.decode(
|
|
utf8.decode(
|
|
await bundle.entries['AssetManifest.json']!.contentsAsBytes()
|
|
)
|
|
) as Map<Object?, Object?>;
|
|
expect(manifestJson, isNotEmpty);
|
|
expect(manifestJson['assets/bar/lizard.png'], isNotNull);
|
|
|
|
final Uint8List manifestBinJsonBytes = base64.decode(
|
|
json.decode(
|
|
utf8.decode(
|
|
await bundle.entries['AssetManifest.bin.json']!.contentsAsBytes()
|
|
)
|
|
) as String
|
|
);
|
|
|
|
final Uint8List manifestBinBytes = Uint8List.fromList(
|
|
await bundle.entries['AssetManifest.bin']!.contentsAsBytes()
|
|
);
|
|
|
|
expect(manifestBinJsonBytes, equals(manifestBinBytes),
|
|
reason: 'JSON-encoded binary content should be identical to BIN file.');
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => testFileSystem,
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
});
|
|
|
|
testUsingContext('Failed directory delete shows message', () async {
|
|
final FileExceptionHandler handler = FileExceptionHandler();
|
|
final FileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);
|
|
|
|
final Directory directory = fileSystem.directory('foo')
|
|
..createSync();
|
|
handler.addError(directory, FileSystemOp.delete, const FileSystemException('Expected Error Text'));
|
|
|
|
await writeBundle(
|
|
directory,
|
|
<String, DevFSContent>{},
|
|
<String, AssetKind>{},
|
|
loggerOverride: testLogger,
|
|
targetPlatform: TargetPlatform.android,
|
|
);
|
|
|
|
expect(testLogger.warningText, contains('Expected Error Text'));
|
|
});
|
|
|
|
testUsingContext('does not unnecessarily recreate asset manifest, font manifest, license', () async {
|
|
globals.fs.file('.packages').createSync();
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt')).createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/foo/bar.txt
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
await bundle.build(packagesPath: '.packages');
|
|
|
|
final DevFSStringContent? assetManifest = bundle.entries['AssetManifest.json']
|
|
as DevFSStringContent?;
|
|
final DevFSStringContent? fontManifest = bundle.entries['FontManifest.json']
|
|
as DevFSStringContent?;
|
|
final DevFSStringContent? license = bundle.entries['NOTICES']
|
|
as DevFSStringContent?;
|
|
|
|
await bundle.build(packagesPath: '.packages');
|
|
|
|
expect(assetManifest, bundle.entries['AssetManifest.json']);
|
|
expect(fontManifest, bundle.entries['FontManifest.json']);
|
|
expect(license, bundle.entries['NOTICES']);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('inserts dummy file into additionalDependencies when '
|
|
'wildcards are used', () async {
|
|
globals.fs.file('.packages').createSync();
|
|
globals.fs.file(globals.fs.path.join('assets', 'bar.txt')).createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 0);
|
|
expect(bundle.additionalDependencies.single.path, contains('DOES_NOT_EXIST_RERUN_FOR_WILDCARD'));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
testUsingContext('Does not insert dummy file into additionalDependencies '
|
|
'when wildcards are not used', () async {
|
|
globals.fs.file('.packages').createSync();
|
|
globals.fs.file(globals.fs.path.join('assets', 'bar.txt')).createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- assets/bar.txt
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 0);
|
|
expect(bundle.additionalDependencies, isEmpty);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
});
|
|
|
|
|
|
group('Shaders: ', () {
|
|
late MemoryFileSystem fileSystem;
|
|
late Artifacts artifacts;
|
|
late String impellerc;
|
|
late Directory output;
|
|
late String assetsPath;
|
|
late String shaderPath;
|
|
late String outputPath;
|
|
|
|
setUp(() {
|
|
artifacts = Artifacts.test();
|
|
fileSystem = MemoryFileSystem.test();
|
|
impellerc = artifacts.getHostArtifact(HostArtifact.impellerc).path;
|
|
|
|
fileSystem.file(impellerc).createSync(recursive: true);
|
|
|
|
output = fileSystem.directory('asset_output')..createSync(recursive: true);
|
|
assetsPath = 'assets';
|
|
shaderPath = fileSystem.path.join(assetsPath, 'shader.frag');
|
|
outputPath = fileSystem.path.join(output.path, assetsPath, 'shader.frag');
|
|
fileSystem.file(shaderPath).createSync(recursive: true);
|
|
});
|
|
|
|
testUsingContext('Including a shader triggers the shader compiler', () async {
|
|
fileSystem.file('.packages').createSync();
|
|
fileSystem.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
shaders:
|
|
- assets/shader.frag
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 0);
|
|
|
|
await writeBundle(
|
|
output,
|
|
bundle.entries,
|
|
bundle.entryKinds,
|
|
loggerOverride: testLogger,
|
|
targetPlatform: TargetPlatform.android,
|
|
);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
Artifacts: () => artifacts,
|
|
FileSystem: () => fileSystem,
|
|
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
|
|
FakeCommand(
|
|
command: <String>[
|
|
impellerc,
|
|
'--sksl',
|
|
'--iplr',
|
|
'--sl=$outputPath',
|
|
'--spirv=$outputPath.spirv',
|
|
'--input=/$shaderPath',
|
|
'--input-type=frag',
|
|
'--include=/$assetsPath',
|
|
'--include=$shaderLibDir',
|
|
],
|
|
onRun: () {
|
|
fileSystem.file(outputPath).createSync(recursive: true);
|
|
fileSystem.file('$outputPath.spirv').createSync(recursive: true);
|
|
},
|
|
),
|
|
]),
|
|
});
|
|
|
|
testUsingContext('Included shaders are compiled for the web', () async {
|
|
fileSystem.file('.packages').createSync();
|
|
fileSystem.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
shaders:
|
|
- assets/shader.frag
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript), 0);
|
|
|
|
await writeBundle(
|
|
output,
|
|
bundle.entries,
|
|
bundle.entryKinds,
|
|
loggerOverride: testLogger,
|
|
targetPlatform: TargetPlatform.web_javascript,
|
|
);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
Artifacts: () => artifacts,
|
|
FileSystem: () => fileSystem,
|
|
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
|
|
FakeCommand(
|
|
command: <String>[
|
|
impellerc,
|
|
'--sksl',
|
|
'--iplr',
|
|
'--json',
|
|
'--sl=$outputPath',
|
|
'--spirv=$outputPath.spirv',
|
|
'--input=/$shaderPath',
|
|
'--input-type=frag',
|
|
'--include=/$assetsPath',
|
|
'--include=$shaderLibDir',
|
|
],
|
|
onRun: () {
|
|
fileSystem.file(outputPath).createSync(recursive: true);
|
|
fileSystem.file('$outputPath.spirv').createSync(recursive: true);
|
|
},
|
|
),
|
|
]),
|
|
});
|
|
|
|
testUsingContext('Material shaders are compiled for the web', () async {
|
|
fileSystem.file('.packages').createSync();
|
|
|
|
final String materialIconsPath = fileSystem.path.join(
|
|
getFlutterRoot(),
|
|
'bin', 'cache', 'artifacts', 'material_fonts',
|
|
'MaterialIcons-Regular.otf',
|
|
);
|
|
fileSystem.file(materialIconsPath).createSync(recursive: true);
|
|
|
|
final String materialPath = fileSystem.path.join(
|
|
getFlutterRoot(),
|
|
'packages', 'flutter', 'lib', 'src', 'material',
|
|
);
|
|
final Directory materialDir = fileSystem.directory(materialPath)..createSync(recursive: true);
|
|
for (final String shader in kMaterialShaders) {
|
|
materialDir.childFile(shader).createSync(recursive: true);
|
|
}
|
|
|
|
(globals.processManager as FakeProcessManager)
|
|
.addCommand(FakeCommand(
|
|
command: <String>[
|
|
impellerc,
|
|
'--sksl',
|
|
'--iplr',
|
|
'--json',
|
|
'--sl=${fileSystem.path.join(output.path, 'shaders', 'ink_sparkle.frag')}',
|
|
'--spirv=${fileSystem.path.join(output.path, 'shaders', 'ink_sparkle.frag.spirv')}',
|
|
'--input=${fileSystem.path.join(materialDir.path, 'shaders', 'ink_sparkle.frag')}',
|
|
'--input-type=frag',
|
|
'--include=${fileSystem.path.join(materialDir.path, 'shaders')}',
|
|
'--include=$shaderLibDir',
|
|
],
|
|
onRun: () {
|
|
fileSystem.file(outputPath).createSync(recursive: true);
|
|
fileSystem.file('$outputPath.spirv').createSync(recursive: true);
|
|
},
|
|
));
|
|
|
|
fileSystem.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
uses-material-design: true
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript), 0);
|
|
|
|
await writeBundle(
|
|
output,
|
|
bundle.entries,
|
|
bundle.entryKinds,
|
|
loggerOverride: testLogger,
|
|
targetPlatform: TargetPlatform.web_javascript,
|
|
);
|
|
expect((globals.processManager as FakeProcessManager).hasRemainingExpectations, false);
|
|
}, overrides: <Type, Generator>{
|
|
Artifacts: () => artifacts,
|
|
FileSystem: () => fileSystem,
|
|
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[]),
|
|
});
|
|
});
|
|
|
|
testUsingContext('Does not insert dummy file into additionalDependencies '
|
|
'when wildcards are used by dependencies', () async {
|
|
globals.fs.file('.packages').writeAsStringSync(r'''
|
|
example:lib/
|
|
foo:foo/lib/
|
|
''');
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt'))
|
|
.createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
dependencies:
|
|
foo: any
|
|
''');
|
|
globals.fs.file('foo/pubspec.yaml')
|
|
..createSync(recursive: true)
|
|
..writeAsStringSync(r'''
|
|
name: foo
|
|
|
|
flutter:
|
|
assets:
|
|
- bar/
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
globals.fs.file('foo/bar/fizz.txt').createSync(recursive: true);
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 0);
|
|
expect(bundle.additionalDependencies, isEmpty);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
Platform: () => FakePlatform(),
|
|
});
|
|
|
|
testUsingContext('does not track wildcard directories from dependencies', () async {
|
|
globals.fs.file('.packages').writeAsStringSync(r'''
|
|
example:lib/
|
|
foo:foo/lib/
|
|
''');
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt'))
|
|
.createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
dependencies:
|
|
foo: any
|
|
''');
|
|
globals.fs.file('foo/pubspec.yaml')
|
|
..createSync(recursive: true)
|
|
..writeAsStringSync(r'''
|
|
name: foo
|
|
|
|
flutter:
|
|
assets:
|
|
- bar/
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
globals.fs.file('foo/bar/fizz.txt').createSync(recursive: true);
|
|
|
|
await bundle.build(packagesPath: '.packages');
|
|
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['packages/foo/bar/fizz.txt',
|
|
'AssetManifest.json', 'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z']));
|
|
expect(bundle.needsBuild(), false);
|
|
|
|
// Does not track dependency's wildcard directories.
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt'))
|
|
.deleteSync();
|
|
|
|
expect(bundle.needsBuild(), false);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
Platform: () => FakePlatform(),
|
|
});
|
|
|
|
testUsingContext('reports package that causes asset bundle error when it is '
|
|
'a dependency', () async {
|
|
globals.fs.file('.packages').writeAsStringSync(r'''
|
|
example:lib/
|
|
foo:foo/lib/
|
|
''');
|
|
globals.fs.file(globals.fs.path.join('assets', 'foo', 'bar.txt'))
|
|
.createSync(recursive: true);
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
dependencies:
|
|
foo: any
|
|
''');
|
|
globals.fs.file('foo/pubspec.yaml')
|
|
..createSync(recursive: true)
|
|
..writeAsStringSync(r'''
|
|
name: foo
|
|
|
|
flutter:
|
|
assets:
|
|
- bar.txt
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 1);
|
|
expect(testLogger.errorText, contains('This asset was included from package foo'));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
Platform: () => FakePlatform(),
|
|
});
|
|
|
|
testUsingContext('does not report package that causes asset bundle error '
|
|
'when it is from own pubspec', () async {
|
|
globals.fs.file('.packages').writeAsStringSync(r'''
|
|
example:lib/
|
|
''');
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
flutter:
|
|
assets:
|
|
- bar.txt
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 1);
|
|
expect(testLogger.errorText, isNot(contains('This asset was included from')));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
Platform: () => FakePlatform(),
|
|
});
|
|
|
|
testUsingContext('does not include Material Design assets if uses-material-design: true is '
|
|
'specified only by a dependency', () async {
|
|
globals.fs.file('.packages').writeAsStringSync(r'''
|
|
example:lib/
|
|
foo:foo/lib/
|
|
''');
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
dependencies:
|
|
foo: any
|
|
|
|
flutter:
|
|
uses-material-design: false
|
|
''');
|
|
globals.fs.file('foo/pubspec.yaml')
|
|
..createSync(recursive: true)
|
|
..writeAsStringSync(r'''
|
|
name: foo
|
|
|
|
flutter:
|
|
uses-material-design: true
|
|
''');
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 0);
|
|
expect((bundle.entries['FontManifest.json']! as DevFSStringContent).string, '[]');
|
|
expect((bundle.entries['AssetManifest.json']! as DevFSStringContent).string, '{}');
|
|
expect(testLogger.errorText, contains(
|
|
'package:foo has `uses-material-design: true` set'
|
|
));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
Platform: () => FakePlatform(),
|
|
});
|
|
|
|
testUsingContext('does not include assets in project directories as asset variants', () async {
|
|
globals.fs.file('.packages').writeAsStringSync(r'''
|
|
example:lib/
|
|
''');
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
|
|
flutter:
|
|
assets:
|
|
- assets/foo.txt
|
|
''');
|
|
globals.fs.file('assets/foo.txt').createSync(recursive: true);
|
|
|
|
// Potential build artifacts outside of build directory.
|
|
globals.fs.file('linux/flutter/foo.txt').createSync(recursive: true);
|
|
globals.fs.file('windows/flutter/foo.txt').createSync(recursive: true);
|
|
globals.fs.file('windows/CMakeLists.txt').createSync();
|
|
globals.fs.file('macos/Flutter/foo.txt').createSync(recursive: true);
|
|
globals.fs.file('ios/foo.txt').createSync(recursive: true);
|
|
globals.fs.file('build/foo.txt').createSync(recursive: true);
|
|
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 0);
|
|
expect(bundle.entries.keys, unorderedEquals(<String>['assets/foo.txt',
|
|
'AssetManifest.json', 'AssetManifest.bin', 'FontManifest.json', 'NOTICES.Z']));
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
Platform: () => FakePlatform(),
|
|
});
|
|
|
|
testUsingContext('deferred and regular assets are included in manifest alphabetically', () async {
|
|
globals.fs.file('.packages').writeAsStringSync(r'''
|
|
example:lib/
|
|
''');
|
|
globals.fs.file('pubspec.yaml')
|
|
..createSync()
|
|
..writeAsStringSync(r'''
|
|
name: example
|
|
|
|
flutter:
|
|
assets:
|
|
- assets/zebra.jpg
|
|
- assets/foo.jpg
|
|
|
|
deferred-components:
|
|
- name: component1
|
|
assets:
|
|
- assets/bar.jpg
|
|
- assets/apple.jpg
|
|
''');
|
|
globals.fs.file('assets/foo.jpg').createSync(recursive: true);
|
|
globals.fs.file('assets/bar.jpg').createSync();
|
|
globals.fs.file('assets/apple.jpg').createSync();
|
|
globals.fs.file('assets/zebra.jpg').createSync();
|
|
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
|
|
|
|
expect(await bundle.build(packagesPath: '.packages'), 0);
|
|
expect((bundle.entries['FontManifest.json']! as DevFSStringContent).string, '[]');
|
|
// The assets from deferred components and regular assets
|
|
// are both included in alphabetical order
|
|
expect((bundle.entries['AssetManifest.json']! as DevFSStringContent).string, '{"assets/apple.jpg":["assets/apple.jpg"],"assets/bar.jpg":["assets/bar.jpg"],"assets/foo.jpg":["assets/foo.jpg"],"assets/zebra.jpg":["assets/zebra.jpg"]}');
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => MemoryFileSystem.test(),
|
|
ProcessManager: () => FakeProcessManager.any(),
|
|
Platform: () => FakePlatform(),
|
|
});
|
|
}
|