testBuildHook function
Implementation
@isTest
Future<void> testBuildHook({
required String description,
required void Function(BuildConfigBuilder) extraConfigSetup,
required FutureOr<void> Function(List<String> arguments) mainMethod,
required FutureOr<void> Function(BuildConfig config, BuildOutput output)
check,
BuildMode? buildMode,
OS? targetOS,
List<String>? supportedAssetTypes,
bool? linkingEnabled,
}) async {
test(
description,
() async {
final tempDir = await _tempDirForTest();
final outputDirectory = tempDir.resolve('output/');
final outputDirectoryShared = tempDir.resolve('output_shared/');
await Directory.fromUri(outputDirectory).create();
await Directory.fromUri(outputDirectoryShared).create();
final configBuilder = BuildConfigBuilder();
configBuilder
..setupHookConfig(
packageRoot: Directory.current.uri,
packageName: _readPackageNameFromPubspec(),
targetOS: targetOS ?? OS.current,
supportedAssetTypes: supportedAssetTypes ?? [],
buildMode: buildMode ?? BuildMode.release,
)
..setupBuildConfig(
dryRun: false,
linkingEnabled: true,
)
..setupBuildRunConfig(
outputDirectory: outputDirectory,
outputDirectoryShared: outputDirectoryShared,
);
extraConfigSetup(configBuilder);
final config = BuildConfig(configBuilder.json);
final configUri = tempDir.resolve(Hook.build.outputName);
_writeJsonTo(configUri, config.json);
await mainMethod(['--config=${configUri.toFilePath()}']);
final output = BuildOutput(
_readJsonFrom(config.outputDirectory.resolve(Hook.build.outputName)));
// Test conformance of protocol invariants.
expect(await validateBuildOutput(config, output), isEmpty);
// Run user-defined tests.
check(config, output);
},
);
}