|
| 1 | +// Copyright (c) 2020, 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:collection'; |
| 7 | + |
| 8 | +import 'package:path/path.dart' as p; |
| 9 | +import 'package:test_api/backend.dart'; //ignore: deprecated_member_use |
| 10 | +import 'package:test_api/src/backend/declarer.dart'; //ignore: implementation_imports |
| 11 | +import 'package:test_api/src/backend/group.dart'; //ignore: implementation_imports |
| 12 | +import 'package:test_api/src/backend/group_entry.dart'; //ignore: implementation_imports |
| 13 | +import 'package:test_api/src/backend/invoker.dart'; // ignore: implementation_imports |
| 14 | +import 'package:test_api/src/backend/test.dart'; //ignore: implementation_imports |
| 15 | +import 'package:test_api/src/utils.dart'; // ignore: implementation_imports |
| 16 | + |
| 17 | +import 'runner/configuration.dart'; |
| 18 | +import 'runner/engine.dart'; |
| 19 | +import 'runner/plugin/environment.dart'; |
| 20 | +import 'runner/reporter.dart'; |
| 21 | +import 'runner/reporter/expanded.dart'; |
| 22 | +import 'runner/runner_suite.dart'; |
| 23 | +import 'runner/suite.dart'; |
| 24 | +import 'util/print_sink.dart'; |
| 25 | + |
| 26 | +/// Runs all unskipped test cases declared in [testMain]. |
| 27 | +/// |
| 28 | +/// Test suite level metadata defined in annotations is not read. No filtering |
| 29 | +/// is applied except for the filtering defined by `solo` or `skip` arguments to |
| 30 | +/// `group` and `test`. Returns [true] if all tests passed. |
| 31 | +Future<bool> directRunTests(FutureOr<void> Function() testMain, |
| 32 | + {Reporter Function(Engine)? reporterFactory}) => |
| 33 | + _directRunTests(testMain, reporterFactory: reporterFactory); |
| 34 | + |
| 35 | +/// Runs a single test declared in [testMain] matched by it's full test name. |
| 36 | +/// |
| 37 | +/// There must be exactly one test defined with the name [fullTestName]. Note |
| 38 | +/// that not all tests and groups are checked, so a test case that is not be |
| 39 | +/// intended to be run (due to a `solo` on a different test) may still be run |
| 40 | +/// with this API. Only the test names returned by [enumerateTestCases] should |
| 41 | +/// be used to prevent running skipped tests. |
| 42 | +/// |
| 43 | +/// Return [true] if the test passes. |
| 44 | +/// |
| 45 | +/// If there are no tests matching [fullTestName] a [MissingTestException] is |
| 46 | +/// thrown. If there is more than one test with the name [fullTestName] they |
| 47 | +/// will both be run, then a [DuplicateTestnameException] will be thrown. |
| 48 | +Future<bool> directRunSingleTest( |
| 49 | + FutureOr<void> Function() testMain, String fullTestName, |
| 50 | + {Reporter Function(Engine)? reporterFactory}) => |
| 51 | + _directRunTests(testMain, |
| 52 | + reporterFactory: reporterFactory, fullTestName: fullTestName); |
| 53 | + |
| 54 | +Future<bool> _directRunTests(FutureOr<void> Function() testMain, |
| 55 | + {Reporter Function(Engine)? reporterFactory, String? fullTestName}) async { |
| 56 | + reporterFactory ??= (engine) => ExpandedReporter.watch(engine, PrintSink(), |
| 57 | + color: Configuration.empty.color, printPath: false, printPlatform: false); |
| 58 | + final declarer = Declarer(fullTestName: fullTestName); |
| 59 | + await declarer.declare(testMain); |
| 60 | + |
| 61 | + final suite = RunnerSuite(const PluginEnvironment(), SuiteConfiguration.empty, |
| 62 | + declarer.build(), SuitePlatform(Runtime.vm, os: currentOSGuess), |
| 63 | + path: p.prettyUri(Uri.base)); |
| 64 | + |
| 65 | + final engine = Engine() |
| 66 | + ..suiteSink.add(suite) |
| 67 | + ..suiteSink.close(); |
| 68 | + |
| 69 | + reporterFactory(engine); |
| 70 | + |
| 71 | + final success = await runZoned(() => Invoker.guard(engine.run), |
| 72 | + zoneValues: {#test.declarer: declarer}); |
| 73 | + |
| 74 | + if (fullTestName != null) { |
| 75 | + final testCount = engine.liveTests.length; |
| 76 | + if (testCount > 1) { |
| 77 | + throw DuplicateTestNameException(fullTestName); |
| 78 | + } |
| 79 | + if (testCount == 0) { |
| 80 | + throw MissingTestException(fullTestName); |
| 81 | + } |
| 82 | + } |
| 83 | + return success!; |
| 84 | +} |
| 85 | + |
| 86 | +/// Runs [testMain] and returns the names of all declared tests. |
| 87 | +/// |
| 88 | +/// Test names declared must be unique. If any test repeats the full name, |
| 89 | +/// including group prefixes, of a prior test a [DuplicateTestNameException] |
| 90 | +/// will be thrown. |
| 91 | +/// |
| 92 | +/// Skipped tests are ignored. |
| 93 | +Future<Set<String>> enumerateTestCases( |
| 94 | + FutureOr<void> Function() testMain) async { |
| 95 | + final declarer = Declarer(); |
| 96 | + await declarer.declare(testMain); |
| 97 | + |
| 98 | + final toVisit = Queue<GroupEntry>.of([declarer.build()]); |
| 99 | + final allTestNames = <String>{}; |
| 100 | + final unskippedTestNames = <String>{}; |
| 101 | + while (toVisit.isNotEmpty) { |
| 102 | + final current = toVisit.removeLast(); |
| 103 | + if (current is Group) { |
| 104 | + toVisit.addAll(current.entries.reversed); |
| 105 | + } else if (current is Test) { |
| 106 | + if (!allTestNames.add(current.name)) { |
| 107 | + throw DuplicateTestNameException(current.name); |
| 108 | + } |
| 109 | + if (current.metadata.skip) continue; |
| 110 | + unskippedTestNames.add(current.name); |
| 111 | + } else { |
| 112 | + throw StateError('Unandled Group Entry: ${current.runtimeType}'); |
| 113 | + } |
| 114 | + } |
| 115 | + return unskippedTestNames; |
| 116 | +} |
| 117 | + |
| 118 | +/// An exception thrown when two test cases in the same test suite (same `main`) |
| 119 | +/// have an identical name. |
| 120 | +class DuplicateTestNameException implements Exception { |
| 121 | + final String name; |
| 122 | + DuplicateTestNameException(this.name); |
| 123 | + |
| 124 | + @override |
| 125 | + String toString() => 'A test with the name "$name" was already declared. ' |
| 126 | + 'Test cases must have unique names.'; |
| 127 | +} |
| 128 | + |
| 129 | +/// An exception thrown when a specific test was requested by name that does not |
| 130 | +/// exist. |
| 131 | +class MissingTestException implements Exception { |
| 132 | + final String name; |
| 133 | + MissingTestException(this.name); |
| 134 | + |
| 135 | + @override |
| 136 | + String toString() => |
| 137 | + 'A test with the name "$name" was not declared in the test suite.'; |
| 138 | +} |
0 commit comments