|
| 1 | +// Copyright (c) 2015, 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:io'; |
| 7 | +import 'dart:isolate'; |
| 8 | + |
| 9 | +import 'package:path/path.dart' as p; |
| 10 | +import 'package:test/test.dart'; |
| 11 | +import 'package:test_descriptor/test_descriptor.dart' as d; |
| 12 | +import 'package:test_process/test_process.dart'; |
| 13 | + |
| 14 | +/// The path to the root directory of the `test` package. |
| 15 | +final Future<String> packageDir = |
| 16 | + Isolate.resolvePackageUri(Uri(scheme: 'package', path: 'test/')) |
| 17 | + .then((uri) { |
| 18 | + var dir = p.dirname(uri!.path); |
| 19 | + // If it starts with a `/C:` or other drive letter, remove the leading `/`. |
| 20 | + if (dir[0] == '/' && dir[2] == ':') dir = dir.substring(1); |
| 21 | + return dir; |
| 22 | +}); |
| 23 | + |
| 24 | +/// The path to the `pub` executable in the current Dart SDK. |
| 25 | +final _pubPath = p.absolute(p.join(p.dirname(Platform.resolvedExecutable), |
| 26 | + Platform.isWindows ? 'pub.bat' : 'pub')); |
| 27 | + |
| 28 | +/// The platform-specific message emitted when a nonexistent file is loaded. |
| 29 | +final String noSuchFileMessage = Platform.isWindows |
| 30 | + ? 'The system cannot find the file specified.' |
| 31 | + : 'No such file or directory'; |
| 32 | + |
| 33 | +/// A regular expression that matches the output of "pub serve". |
| 34 | +final _servingRegExp = |
| 35 | + RegExp(r'^Serving myapp [a-z]+ on http://localhost:(\d+)$'); |
| 36 | + |
| 37 | +/// An operating system name that's different than the current operating system. |
| 38 | +final otherOS = Platform.isWindows ? 'mac-os' : 'windows'; |
| 39 | + |
| 40 | +/// The port of a pub serve instance run via [runPubServe]. |
| 41 | +/// |
| 42 | +/// This is only set after [runPubServe] is called. |
| 43 | +int get pubServePort => _pubServePort!; |
| 44 | +int? _pubServePort; |
| 45 | + |
| 46 | +/// Expects that the entire stdout stream of [test] equals [expected]. |
| 47 | +void expectStdoutEquals(TestProcess test, String expected) => |
| 48 | + _expectStreamEquals(test.stdoutStream(), expected); |
| 49 | + |
| 50 | +/// Expects that the entire stderr stream of [test] equals [expected]. |
| 51 | +void expectStderrEquals(TestProcess test, String expected) => |
| 52 | + _expectStreamEquals(test.stderrStream(), expected); |
| 53 | + |
| 54 | +/// Expects that the entirety of the line stream [stream] equals [expected]. |
| 55 | +void _expectStreamEquals(Stream<String> stream, String expected) { |
| 56 | + expect((() async { |
| 57 | + var lines = await stream.toList(); |
| 58 | + expect(lines.join('\n').trim(), equals(expected.trim())); |
| 59 | + })(), completes); |
| 60 | +} |
| 61 | + |
| 62 | +/// Returns a [StreamMatcher] that asserts that the stream emits strings |
| 63 | +/// containing each string in [strings] in order. |
| 64 | +/// |
| 65 | +/// This expects each string in [strings] to match a different string in the |
| 66 | +/// stream. |
| 67 | +StreamMatcher containsInOrder(Iterable<String> strings) => |
| 68 | + emitsInOrder(strings.map((string) => emitsThrough(contains(string)))); |
| 69 | + |
| 70 | +/// Lazily compile the test package to kernel and re-use that, initialized with |
| 71 | +/// [precompileTestExecutable]. |
| 72 | +String? _testExecutablePath; |
| 73 | + |
| 74 | +/// Must be invoked before any call to [runTests], should be invoked in a top |
| 75 | +/// level `setUpAll` for best caching results. |
| 76 | +Future<void> precompileTestExecutable() async { |
| 77 | + if (_testExecutablePath != null) { |
| 78 | + throw StateError('Test executable already precompiled'); |
| 79 | + } |
| 80 | + var tmpDirectory = await Directory.systemTemp.createTemp('test'); |
| 81 | + var precompiledPath = p.join(tmpDirectory.path, 'test_runner.dill'); |
| 82 | + var result = await Process.run(Platform.executable, [ |
| 83 | + 'compile', |
| 84 | + 'kernel', |
| 85 | + p.url.join(await packageDir, 'bin', 'test.dart'), |
| 86 | + '-o', |
| 87 | + precompiledPath, |
| 88 | + ]); |
| 89 | + if (result.exitCode != 0) { |
| 90 | + throw StateError( |
| 91 | + 'Failed to compile test runner:\n${result.stdout}\n${result.stderr}'); |
| 92 | + } |
| 93 | + |
| 94 | + addTearDown(() async { |
| 95 | + await tmpDirectory.delete(recursive: true); |
| 96 | + }); |
| 97 | + _testExecutablePath = precompiledPath; |
| 98 | +} |
| 99 | + |
| 100 | +/// Runs the test executable with the package root set properly. |
| 101 | +/// |
| 102 | +/// You must invoke [precompileTestExecutable] before invoking this function. |
| 103 | +Future<TestProcess> runTest(Iterable<String> args, |
| 104 | + {String? reporter, |
| 105 | + String? fileReporter, |
| 106 | + int? concurrency, |
| 107 | + Map<String, String>? environment, |
| 108 | + bool forwardStdio = false, |
| 109 | + String? packageConfig, |
| 110 | + Iterable<String>? vmArgs}) async { |
| 111 | + concurrency ??= 1; |
| 112 | + var testExecutablePath = _testExecutablePath; |
| 113 | + if (testExecutablePath == null) { |
| 114 | + throw StateError( |
| 115 | + 'You must call `precompileTestExecutable` before calling `runTest`'); |
| 116 | + } |
| 117 | + |
| 118 | + var allArgs = [ |
| 119 | + ...?vmArgs, |
| 120 | + testExecutablePath, |
| 121 | + '--concurrency=$concurrency', |
| 122 | + if (reporter != null) '--reporter=$reporter', |
| 123 | + if (fileReporter != null) '--file-reporter=$fileReporter', |
| 124 | + ...args, |
| 125 | + ]; |
| 126 | + |
| 127 | + environment ??= {}; |
| 128 | + environment.putIfAbsent('_DART_TEST_TESTING', () => 'true'); |
| 129 | + |
| 130 | + return await runDart(allArgs, |
| 131 | + environment: environment, |
| 132 | + description: 'dart bin/test.dart', |
| 133 | + forwardStdio: forwardStdio, |
| 134 | + packageConfig: packageConfig); |
| 135 | +} |
| 136 | + |
| 137 | +/// Runs Dart. |
| 138 | +/// |
| 139 | +/// If [packageConfig] is provided then that is passed for the `--packages` |
| 140 | +/// arg, otherwise the current isolate config is passed. |
| 141 | +Future<TestProcess> runDart(Iterable<String> args, |
| 142 | + {Map<String, String>? environment, |
| 143 | + String? description, |
| 144 | + bool forwardStdio = false, |
| 145 | + String? packageConfig}) async { |
| 146 | + var allArgs = <String>[ |
| 147 | + ...Platform.executableArguments.where((arg) => |
| 148 | + !arg.startsWith('--package-root=') && !arg.startsWith('--packages=')), |
| 149 | + '--packages=${packageConfig ?? await Isolate.packageConfig}', |
| 150 | + ...args |
| 151 | + ]; |
| 152 | + |
| 153 | + return await TestProcess.start( |
| 154 | + p.absolute(Platform.resolvedExecutable), allArgs, |
| 155 | + workingDirectory: d.sandbox, |
| 156 | + environment: environment, |
| 157 | + description: description, |
| 158 | + forwardStdio: forwardStdio); |
| 159 | +} |
| 160 | + |
| 161 | +/// Runs Pub. |
| 162 | +Future<TestProcess> runPub(Iterable<String> args, |
| 163 | + {Map<String, String>? environment}) { |
| 164 | + return TestProcess.start(_pubPath, args, |
| 165 | + workingDirectory: d.sandbox, |
| 166 | + environment: environment, |
| 167 | + description: 'pub ${args.first}'); |
| 168 | +} |
| 169 | + |
| 170 | +/// Runs "pub serve". |
| 171 | +/// |
| 172 | +/// This returns assigns [_pubServePort] to a future that will complete to the |
| 173 | +/// port of the "pub serve" instance. |
| 174 | +Future<TestProcess> runPubServe( |
| 175 | + {Iterable<String>? args, |
| 176 | + String? workingDirectory, |
| 177 | + Map<String, String>? environment}) async { |
| 178 | + var allArgs = ['serve', '--port', '0']; |
| 179 | + if (args != null) allArgs.addAll(args); |
| 180 | + |
| 181 | + var pub = await runPub(allArgs, environment: environment); |
| 182 | + |
| 183 | + Match? match; |
| 184 | + while (match == null) { |
| 185 | + match = _servingRegExp.firstMatch(await pub.stdout.next); |
| 186 | + } |
| 187 | + _pubServePort = int.parse(match[1]!); |
| 188 | + |
| 189 | + return pub; |
| 190 | +} |
0 commit comments