|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:yaml/yaml.dart'; |
| 10 | + |
| 11 | +import 'config.dart'; |
| 12 | +import 'validation.dart'; |
| 13 | + |
| 14 | +/// Validate a build hook; this will throw an exception on validation errors. |
| 15 | +/// |
| 16 | +/// This is intended to be used from tests, e.g.: |
| 17 | +/// |
| 18 | +/// ``` |
| 19 | +/// test('test my build hook', () async { |
| 20 | +/// await testCodeBuildHook( |
| 21 | +/// ... |
| 22 | +/// ); |
| 23 | +/// }); |
| 24 | +/// ``` |
| 25 | +Future<void> testBuildHook({ |
| 26 | + required void Function(BuildInputBuilder) extraInputSetup, |
| 27 | + required FutureOr<void> Function(List<String> arguments) mainMethod, |
| 28 | + required FutureOr<void> Function(BuildInput input, BuildOutput output) check, |
| 29 | + bool? linkingEnabled, |
| 30 | +}) async { |
| 31 | + const keepTempKey = 'KEEP_TEMPORARY_DIRECTORIES'; |
| 32 | + |
| 33 | + final tempDir = await Directory.systemTemp.createTemp(); |
| 34 | + |
| 35 | + try { |
| 36 | + // Deal with Windows temp folder aliases. |
| 37 | + final tempUri = Directory( |
| 38 | + await tempDir.resolveSymbolicLinks(), |
| 39 | + ).uri.normalizePath(); |
| 40 | + final outputDirectory = tempUri.resolve('output/'); |
| 41 | + final outputDirectoryShared = tempUri.resolve('output_shared/'); |
| 42 | + final outputFile = tempUri.resolve('output.json'); |
| 43 | + |
| 44 | + await Directory.fromUri(outputDirectory).create(); |
| 45 | + await Directory.fromUri(outputDirectoryShared).create(); |
| 46 | + |
| 47 | + final inputBuilder = BuildInputBuilder(); |
| 48 | + inputBuilder |
| 49 | + ..setupShared( |
| 50 | + packageRoot: Directory.current.uri, |
| 51 | + packageName: _readPackageNameFromPubspec(), |
| 52 | + outputFile: outputFile, |
| 53 | + outputDirectoryShared: outputDirectoryShared, |
| 54 | + ) |
| 55 | + ..config.setupBuild(linkingEnabled: true); |
| 56 | + extraInputSetup(inputBuilder); |
| 57 | + |
| 58 | + final input = BuildInput(inputBuilder.json); |
| 59 | + |
| 60 | + final inputUri = tempUri.resolve('input.json'); |
| 61 | + _writeJsonTo(inputUri, input.json); |
| 62 | + await mainMethod(['--config=${inputUri.toFilePath()}']); |
| 63 | + final output = BuildOutput(_readJsonFrom(input.outputFile)); |
| 64 | + |
| 65 | + // Test conformance of protocol invariants. |
| 66 | + final validationErrors = await validateBuildOutput(input, output); |
| 67 | + if (validationErrors.isNotEmpty) { |
| 68 | + throw ValidationFailure( |
| 69 | + 'encountered build output validation issues: $validationErrors', |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + // Run user-defined tests. |
| 74 | + await check(input, output); |
| 75 | + } finally { |
| 76 | + final keepTempDir = (Platform.environment[keepTempKey] ?? '').isNotEmpty; |
| 77 | + if (!keepTempDir) { |
| 78 | + tempDir.deleteSync(recursive: true); |
| 79 | + } else { |
| 80 | + print('$keepTempKey ${tempDir.uri}'); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void _writeJsonTo(Uri uri, Map<String, Object?> json) { |
| 86 | + final encoder = const JsonEncoder().fuse(const Utf8Encoder()); |
| 87 | + File.fromUri(uri).writeAsBytesSync(encoder.convert(json)); |
| 88 | +} |
| 89 | + |
| 90 | +Map<String, Object?> _readJsonFrom(Uri uri) { |
| 91 | + final decoder = const Utf8Decoder().fuse(const JsonDecoder()); |
| 92 | + final bytes = File.fromUri(uri).readAsBytesSync(); |
| 93 | + return decoder.convert(bytes) as Map<String, Object?>; |
| 94 | +} |
| 95 | + |
| 96 | +String _readPackageNameFromPubspec() { |
| 97 | + final uri = Directory.current.uri.resolve('pubspec.yaml'); |
| 98 | + final readAsString = File.fromUri(uri).readAsStringSync(); |
| 99 | + final yaml = loadYaml(readAsString) as YamlMap; |
| 100 | + return yaml['name'] as String; |
| 101 | +} |
0 commit comments