OLD | NEW |
(Empty) | |
| 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 library test.runner.load_suite; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 |
| 11 import '../../test.dart'; |
| 12 import '../backend/invoker.dart'; |
| 13 import '../backend/metadata.dart'; |
| 14 import '../backend/test_platform.dart'; |
| 15 import '../utils.dart'; |
| 16 import 'load_exception.dart'; |
| 17 import 'runner_suite.dart'; |
| 18 import 'vm/environment.dart'; |
| 19 |
| 20 /// A [Suite] emitted by a [Loader] that provides a test-like interface for |
| 21 /// loading a test file. |
| 22 /// |
| 23 /// This is used to expose the current status of test loading to the user. It's |
| 24 /// important to provide users visibility into what's taking a long time and |
| 25 /// where failures occur. And since some tests may be loaded at the same time as |
| 26 /// others are run, it's useful to provide that visibility in the form of a test |
| 27 /// suite so that it can integrate well into the existing reporting interface |
| 28 /// without too much extra logic. |
| 29 /// |
| 30 /// A suite is constructed with logic necessary to produce a test suite. As with |
| 31 /// a normal test body, this logic isn't run until [LiveTest.run] is called. The |
| 32 /// suite itself is returned by [suite] once it's avaialble, but any errors or |
| 33 /// prints will be emitted through the running [LiveTest]. |
| 34 class LoadSuite extends RunnerSuite { |
| 35 /// A future that completes to the loaded suite once the suite's test has been |
| 36 /// run and completed successfully. |
| 37 /// |
| 38 /// This will return `null` if the suite is unavailable for some reason (for |
| 39 /// example if an error occurred while loading it). |
| 40 final Future<RunnerSuite> suite; |
| 41 |
| 42 /// Creates a load suite named [name] on [platform]. |
| 43 /// |
| 44 /// [body] may return either a [RunnerSuite] or a [Future] that completes to a |
| 45 /// [RunnerSuite]. Its return value is forwarded through [suite], although if |
| 46 /// it throws an error that will be forwarded through the suite's test. |
| 47 /// |
| 48 /// If the the load test is closed before [body] is complete, it will close |
| 49 /// the suite returned by [body] once it completes. |
| 50 factory LoadSuite(String name, body(), {TestPlatform platform}) { |
| 51 var completer = new Completer.sync(); |
| 52 return new LoadSuite._(name, () { |
| 53 var invoker = Invoker.current; |
| 54 invoker.addOutstandingCallback(); |
| 55 |
| 56 invoke(() async { |
| 57 try { |
| 58 var suite = await body(); |
| 59 if (completer.isCompleted) { |
| 60 // If the load test has already been closed, close the suite it |
| 61 // generated. |
| 62 suite.close(); |
| 63 return; |
| 64 } |
| 65 |
| 66 completer.complete(suite); |
| 67 invoker.removeOutstandingCallback(); |
| 68 } catch (error, stackTrace) { |
| 69 registerException(error, stackTrace); |
| 70 if (!completer.isCompleted) completer.complete(); |
| 71 } |
| 72 }); |
| 73 |
| 74 // If the test is forcibly closed, exit immediately. It doesn't have any |
| 75 // cleanup to do that won't be handled by Loader.close. |
| 76 invoker.onClose.then((_) { |
| 77 if (completer.isCompleted) return; |
| 78 completer.complete(); |
| 79 invoker.removeOutstandingCallback(); |
| 80 }); |
| 81 }, completer.future, platform: platform); |
| 82 } |
| 83 |
| 84 /// A utility constructor for a load suite that just throws [exception]. |
| 85 /// |
| 86 /// The suite's name will be based on [exception]'s path. |
| 87 factory LoadSuite.forLoadException(LoadException exception, |
| 88 {StackTrace stackTrace, TestPlatform platform}) { |
| 89 if (stackTrace == null) stackTrace = new Trace.current(); |
| 90 |
| 91 return new LoadSuite("loading ${exception.path}", () { |
| 92 return new Future.error(exception, stackTrace); |
| 93 }, platform: platform); |
| 94 } |
| 95 |
| 96 /// A utility constructor for a load suite that just emits [suite]. |
| 97 factory LoadSuite.forSuite(RunnerSuite suite) { |
| 98 return new LoadSuite("loading ${suite.path}", () => suite, |
| 99 platform: suite.platform); |
| 100 } |
| 101 |
| 102 LoadSuite._(String name, void body(), this.suite, {TestPlatform platform}) |
| 103 : super(const VMEnvironment(), [ |
| 104 new LocalTest(name, |
| 105 new Metadata(timeout: new Timeout(new Duration(minutes: 5))), |
| 106 body) |
| 107 ], platform: platform); |
| 108 |
| 109 /// A constructor used by [changeSuite]. |
| 110 LoadSuite._changeSuite(LoadSuite old, Future<RunnerSuite> this.suite) |
| 111 : super(const VMEnvironment(), old.tests, platform: old.platform); |
| 112 |
| 113 /// Creates a new [LoadSuite] that's identical to this one, but that |
| 114 /// transforms [suite] once it's loaded. |
| 115 /// |
| 116 /// If [suite] completes to `null`, [change] won't be run. |
| 117 LoadSuite changeSuite(RunnerSuite change(RunnerSuite suite)) { |
| 118 return new LoadSuite._changeSuite(this, suite.then((loadedSuite) { |
| 119 if (loadedSuite == null) return null; |
| 120 return change(loadedSuite); |
| 121 })); |
| 122 } |
| 123 |
| 124 /// Runs the test and returns the suite. |
| 125 /// |
| 126 /// Rather than emitting errors through a [LiveTest], this just pipes them |
| 127 /// through the return value. |
| 128 Future<RunnerSuite> getSuite() async { |
| 129 var liveTest = await tests.single.load(this); |
| 130 liveTest.onPrint.listen(print); |
| 131 await liveTest.run(); |
| 132 |
| 133 if (liveTest.errors.isEmpty) return await suite; |
| 134 |
| 135 var error = liveTest.errors.first; |
| 136 await new Future.error(error.error, error.stackTrace); |
| 137 throw 'unreachable'; |
| 138 } |
| 139 } |
OLD | NEW |