-
Notifications
You must be signed in to change notification settings - Fork 93
[native_assets_cli] Make package:test dev dep #1799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
0ad8e81
9b1073c
2074230
a157dbe
e4f94f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,18 +6,25 @@ import 'dart:async'; | |
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:meta/meta.dart' show isTest; | ||
| import 'package:test/test.dart'; | ||
| import 'package:yaml/yaml.dart'; | ||
|
|
||
| import 'native_assets_cli_builder.dart'; | ||
| import 'native_assets_cli_internal.dart' show Hook; | ||
|
|
||
| export 'native_assets_cli_builder.dart'; | ||
|
|
||
| @isTest | ||
| /// Validate a build hook; this will throw an exception on validation errors. | ||
| /// | ||
| /// This is intended to be used from tests, e.g.: | ||
| /// | ||
| /// ``` | ||
| /// test('test my build hook', () async { | ||
| /// await testCodeBuildHook( | ||
| /// ... | ||
| /// ); | ||
| /// }); | ||
| /// ``` | ||
| 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) | ||
|
|
@@ -27,50 +34,72 @@ Future<void> testBuildHook({ | |
| List<String>? buildAssetTypes, | ||
| 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, | ||
| buildAssetTypes: buildAssetTypes ?? [], | ||
| 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); | ||
| }, | ||
| ); | ||
| const keepTempKey = 'KEEP_TEMPORARY_DIRECTORIES'; | ||
|
|
||
| final tempDir = await Directory.systemTemp.createTemp(); | ||
|
|
||
| try { | ||
| // Deal with Windows temp folder aliases. | ||
| final tempUri = | ||
| Directory(await tempDir.resolveSymbolicLinks()).uri.normalizePath(); | ||
| final outputDirectory = tempUri.resolve('output/'); | ||
| final outputDirectoryShared = tempUri.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, | ||
| buildAssetTypes: buildAssetTypes ?? [], | ||
| buildMode: buildMode ?? BuildMode.release, | ||
| ) | ||
| ..setupBuildConfig( | ||
| dryRun: false, | ||
| linkingEnabled: true, | ||
| ) | ||
| ..setupBuildRunConfig( | ||
| outputDirectory: outputDirectory, | ||
| outputDirectoryShared: outputDirectoryShared, | ||
| ); | ||
| extraConfigSetup(configBuilder); | ||
|
|
||
| final config = BuildConfig(configBuilder.json); | ||
|
|
||
| final configUri = tempUri.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. | ||
| final validationErrors = await validateBuildOutput(config, output); | ||
| if (validationErrors.isNotEmpty) { | ||
| throw VerificationException( | ||
| 'encountered build output validation issues: $validationErrors'); | ||
| } | ||
|
|
||
| // Run user-defined tests. | ||
| await check(config, output); | ||
| } finally { | ||
| final keepTempDir = (Platform.environment[keepTempKey] ?? '').isNotEmpty; | ||
| if (!keepTempDir) { | ||
| tempDir.deleteSync(recursive: true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// An exception thrown when build hook verification fails. | ||
| class VerificationException implements Exception { | ||
|
||
| final String? message; | ||
|
|
||
| VerificationException(this.message); | ||
|
|
||
| @override | ||
| String toString() => message.toString(); | ||
| } | ||
|
|
||
| void _writeJsonTo(Uri uri, Map<String, Object?> json) { | ||
|
|
@@ -90,18 +119,3 @@ String _readPackageNameFromPubspec() { | |
| final yaml = loadYaml(readAsString) as YamlMap; | ||
| return yaml['name'] as String; | ||
| } | ||
|
|
||
| const keepTempKey = 'KEEP_TEMPORARY_DIRECTORIES'; | ||
devoncarew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Future<Uri> _tempDirForTest({String? prefix, bool keepTemp = false}) async { | ||
| final tempDir = await Directory.systemTemp.createTemp(prefix); | ||
| // Deal with Windows temp folder aliases. | ||
| final tempUri = | ||
| Directory(await tempDir.resolveSymbolicLinks()).uri.normalizePath(); | ||
| if ((!Platform.environment.containsKey(keepTempKey) || | ||
| Platform.environment[keepTempKey]!.isEmpty) && | ||
| !keepTemp) { | ||
| addTearDown(() => tempDir.delete(recursive: true)); | ||
| } | ||
| return tempUri; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.