|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
| 5 | +import 'dart:typed_data'; |
| 6 | + |
| 7 | +import 'package:args/args.dart'; |
5 | 8 | import 'package:file/memory.dart'; |
| 9 | +import 'package:file_testing/file_testing.dart'; |
| 10 | +import 'package:flutter_tools/src/artifacts.dart'; |
| 11 | +import 'package:flutter_tools/src/asset.dart'; |
6 | 12 | import 'package:flutter_tools/src/base/config.dart'; |
7 | 13 | import 'package:flutter_tools/src/base/file_system.dart'; |
| 14 | +import 'package:flutter_tools/src/base/logger.dart'; |
8 | 15 | import 'package:flutter_tools/src/build_info.dart'; |
9 | 16 | import 'package:flutter_tools/src/build_system/build_system.dart'; |
10 | | -import 'package:flutter_tools/src/bundle.dart'; |
| 17 | +import 'package:flutter_tools/src/bundle.dart' hide defaultManifestPath; |
11 | 18 | import 'package:flutter_tools/src/bundle_builder.dart'; |
| 19 | +import 'package:flutter_tools/src/devfs.dart'; |
| 20 | +import 'package:flutter_tools/src/device.dart'; |
| 21 | +import 'package:flutter_tools/src/flutter_manifest.dart'; |
12 | 22 | import 'package:flutter_tools/src/globals.dart' as globals; |
13 | 23 | import 'package:flutter_tools/src/project.dart'; |
| 24 | +import 'package:test/fake.dart'; |
14 | 25 |
|
15 | 26 | import '../src/common.dart'; |
16 | 27 | import '../src/context.dart'; |
@@ -46,6 +57,75 @@ void main() { |
46 | 57 | ProcessManager: () => FakeProcessManager.any(), |
47 | 58 | }); |
48 | 59 |
|
| 60 | + testWithoutContext('writeBundle applies transformations to any assets that have them defined', () async { |
| 61 | + final MemoryFileSystem fileSystem = MemoryFileSystem.test(); |
| 62 | + final File asset = fileSystem.file('my-asset.txt') |
| 63 | + ..createSync() |
| 64 | + ..writeAsBytesSync(<int>[1, 2, 3]); |
| 65 | + final Artifacts artifacts = Artifacts.test(); |
| 66 | + |
| 67 | + final FakeProcessManager processManager = FakeProcessManager.list( |
| 68 | + <FakeCommand>[ |
| 69 | + FakeCommand( |
| 70 | + command: <Pattern>[ |
| 71 | + artifacts.getArtifactPath(Artifact.engineDartBinary), |
| 72 | + 'run', |
| 73 | + 'increment', |
| 74 | + '--input=/.tmp_rand0/my-asset.txt-transformOutput0.txt', |
| 75 | + '--output=/.tmp_rand0/my-asset.txt-transformOutput1.txt' |
| 76 | + ], |
| 77 | + onRun: (List<String> command) { |
| 78 | + final ArgResults argParseResults = (ArgParser() |
| 79 | + ..addOption('input', mandatory: true) |
| 80 | + ..addOption('output', mandatory: true)) |
| 81 | + .parse(command); |
| 82 | + |
| 83 | + final File inputFile = fileSystem.file(argParseResults['input']); |
| 84 | + final File outputFile = fileSystem.file(argParseResults['output']); |
| 85 | + |
| 86 | + expect(inputFile, exists); |
| 87 | + outputFile |
| 88 | + ..createSync() |
| 89 | + ..writeAsBytesSync( |
| 90 | + Uint8List.fromList( |
| 91 | + inputFile.readAsBytesSync().map((int b) => b + 1).toList(), |
| 92 | + ), |
| 93 | + ); |
| 94 | + }, |
| 95 | + ), |
| 96 | + ], |
| 97 | + ); |
| 98 | + |
| 99 | + final FakeAssetBundle bundle = FakeAssetBundle() |
| 100 | + ..entries['my-asset.txt'] = AssetBundleEntry( |
| 101 | + DevFSFileContent(asset), |
| 102 | + kind: AssetKind.regular, |
| 103 | + transformers: const <AssetTransformerEntry>[ |
| 104 | + AssetTransformerEntry(package: 'increment', args: <String>[]), |
| 105 | + ], |
| 106 | + ); |
| 107 | + |
| 108 | + final Directory bundleDir = fileSystem.directory( |
| 109 | + getAssetBuildDirectory(Config.test(), fileSystem), |
| 110 | + ); |
| 111 | + |
| 112 | + await writeBundle( |
| 113 | + bundleDir, |
| 114 | + bundle.entries, |
| 115 | + targetPlatform: TargetPlatform.tester, |
| 116 | + impellerStatus: ImpellerStatus.platformDefault, |
| 117 | + processManager: processManager, |
| 118 | + fileSystem: fileSystem, |
| 119 | + artifacts: artifacts, |
| 120 | + logger: BufferLogger.test(), |
| 121 | + projectDir: fileSystem.currentDirectory, |
| 122 | + ); |
| 123 | + |
| 124 | + final File outputAssetFile = fileSystem.file('build/flutter_assets/my-asset.txt'); |
| 125 | + expect(outputAssetFile, exists); |
| 126 | + expect(outputAssetFile.readAsBytesSync(), orderedEquals(<int>[2, 3, 4])); |
| 127 | + }); |
| 128 | + |
49 | 129 | testUsingContext('Handles build system failure', () { |
50 | 130 | expect( |
51 | 131 | () => BundleBuilder().build( |
@@ -157,3 +237,8 @@ void main() { |
157 | 237 | ), 'build/95b595cca01caa5f0ca0a690339dd7f6.cache.dill.track.dill'); |
158 | 238 | }); |
159 | 239 | } |
| 240 | + |
| 241 | +class FakeAssetBundle extends Fake implements AssetBundle { |
| 242 | + @override |
| 243 | + final Map<String, AssetBundleEntry> entries = <String, AssetBundleEntry>{}; |
| 244 | +} |
0 commit comments