|
| 1 | +// Copyright (c) 2023, 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:ffi'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:logging/logging.dart'; |
| 10 | +import 'package:test/test.dart'; |
| 11 | + |
| 12 | +export 'package:native_assets_cli/code_assets_builder.dart'; |
| 13 | + |
| 14 | +const keepTempKey = 'KEEP_TEMPORARY_DIRECTORIES'; |
| 15 | + |
| 16 | +Future<Uri> tempDirForTest({String? prefix, bool keepTemp = false}) async { |
| 17 | + final tempDir = await Directory.systemTemp.createTemp(prefix); |
| 18 | + // Deal with Windows temp folder aliases. |
| 19 | + final tempUri = Directory(await tempDir.resolveSymbolicLinks()).uri.normalizePath(); |
| 20 | + if ((!Platform.environment.containsKey(keepTempKey) || Platform.environment[keepTempKey]!.isEmpty) && |
| 21 | + !keepTemp) { |
| 22 | + addTearDown(() => tempDir.delete(recursive: true)); |
| 23 | + } |
| 24 | + return tempUri; |
| 25 | +} |
| 26 | + |
| 27 | +/// Logger that outputs the full trace when a test fails. |
| 28 | +Logger get logger => _logger ??= () { |
| 29 | + // A new logger is lazily created for each test so that the messages |
| 30 | + // captured by printOnFailure are scoped to the correct test. |
| 31 | + addTearDown(() => _logger = null); |
| 32 | + return _createTestLogger(); |
| 33 | + }(); |
| 34 | + |
| 35 | +Logger? _logger; |
| 36 | + |
| 37 | +Logger createCapturingLogger(List<String> capturedMessages) => |
| 38 | + _createTestLogger(capturedMessages: capturedMessages); |
| 39 | + |
| 40 | +Logger _createTestLogger({List<String>? capturedMessages}) => Logger.detached('') |
| 41 | + ..level = Level.ALL |
| 42 | + ..onRecord.listen((record) { |
| 43 | + printOnFailure('${record.level.name}: ${record.time}: ${record.message}'); |
| 44 | + capturedMessages?.add(record.message); |
| 45 | + }); |
| 46 | + |
| 47 | +/// Test files are run in a variety of ways, find this package root in all. |
| 48 | +/// |
| 49 | +/// Test files can be run from source from any working directory. The Dart SDK |
| 50 | +/// `tools/test.py` runs them from the root of the SDK for example. |
| 51 | +/// |
| 52 | +/// Test files can be run from dill from the root of package. `package:test` |
| 53 | +/// does this. |
| 54 | +/// |
| 55 | +/// https://github.com/dart-lang/test/issues/110 |
| 56 | +Uri findPackageRoot(String packageName) { |
| 57 | + final script = Platform.script; |
| 58 | + final fileName = script.name; |
| 59 | + if (fileName.endsWith('_test.dart')) { |
| 60 | + // We're likely running from source. |
| 61 | + var directory = script.resolve('.'); |
| 62 | + while (true) { |
| 63 | + final dirName = directory.name; |
| 64 | + if (dirName == packageName) { |
| 65 | + return directory; |
| 66 | + } |
| 67 | + final parent = directory.resolve('..'); |
| 68 | + if (parent == directory) break; |
| 69 | + directory = parent; |
| 70 | + } |
| 71 | + } else if (fileName.endsWith('.dill')) { |
| 72 | + final cwd = Directory.current.uri; |
| 73 | + final dirName = cwd.name; |
| 74 | + if (dirName == packageName) { |
| 75 | + return cwd; |
| 76 | + } |
| 77 | + } |
| 78 | + throw StateError("Could not find package root for package '$packageName'. " |
| 79 | + 'Tried finding the package root via Platform.script ' |
| 80 | + "'${Platform.script.toFilePath()}' and Directory.current " |
| 81 | + "'${Directory.current.uri.toFilePath()}'."); |
| 82 | +} |
| 83 | + |
| 84 | +Uri packageUri = findPackageRoot('native_toolchain_c'); |
| 85 | + |
| 86 | +extension on Uri { |
| 87 | + String get name => pathSegments.where((e) => e != '').last; |
| 88 | +} |
| 89 | + |
| 90 | +/// Opens the [DynamicLibrary] at [path] and register a tear down hook to close |
| 91 | +/// it when the current test is done. |
| 92 | +DynamicLibrary openDynamicLibraryForTest(String path) { |
| 93 | + final library = DynamicLibrary.open(path); |
| 94 | + addTearDown(library.close); |
| 95 | + return library; |
| 96 | +} |
0 commit comments