testBuildHook function

  1. @isTest
Future<void> testBuildHook({
  1. required String description,
  2. required void extraConfigSetup(
    1. BuildConfigBuilder
    ),
  3. required FutureOr<void> mainMethod(
    1. List<String> arguments
    ),
  4. required FutureOr<void> check(
    1. BuildConfig config,
    2. BuildOutput output
    ),
  5. BuildMode? buildMode,
  6. OS? targetOS,
  7. List<String>? supportedAssetTypes,
  8. bool? linkingEnabled,
})

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);
    },
  );
}