Line data Source code
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 :
7 : import 'package:stack_trace/stack_trace.dart';
8 :
9 : import '../../test.dart';
10 : import '../backend/group.dart';
11 : import '../backend/invoker.dart';
12 : import '../backend/metadata.dart';
13 : import '../backend/suite.dart';
14 : import '../backend/test.dart';
15 : import '../backend/test_platform.dart';
16 : import '../utils.dart';
17 : import 'configuration/suite.dart';
18 : import 'load_exception.dart';
19 : import 'plugin/environment.dart';
20 : import 'runner_suite.dart';
21 :
22 : /// A [Suite] emitted by a [Loader] that provides a test-like interface for
23 : /// loading a test file.
24 : ///
25 : /// This is used to expose the current status of test loading to the user. It's
26 : /// important to provide users visibility into what's taking a long time and
27 : /// where failures occur. And since some tests may be loaded at the same time as
28 : /// others are run, it's useful to provide that visibility in the form of a test
29 : /// suite so that it can integrate well into the existing reporting interface
30 : /// without too much extra logic.
31 : ///
32 : /// A suite is constructed with logic necessary to produce a test suite. As with
33 : /// a normal test body, this logic isn't run until [LiveTest.run] is called. The
34 : /// suite itself is returned by [suite] once it's avaialble, but any errors or
35 : /// prints will be emitted through the running [LiveTest].
36 : class LoadSuite extends Suite implements RunnerSuite {
37 : final environment = const PluginEnvironment();
38 : final SuiteConfiguration config;
39 : final isDebugging = false;
40 : final onDebugging = new StreamController<bool>().stream;
41 :
42 : /// A future that completes to the loaded suite once the suite's test has been
43 : /// run and completed successfully.
44 : ///
45 : /// This will return `null` if the suite is unavailable for some reason (for
46 : /// example if an error occurred while loading it).
47 0 : Future<RunnerSuite> get suite async => (await _suiteAndZone)?.first;
48 :
49 : /// A future that completes to a pair of [suite] and the load test's [Zone].
50 : ///
51 : /// This will return `null` if the suite is unavailable for some reason (for
52 : /// example if an error occurred while loading it).
53 : final Future<Pair<RunnerSuite, Zone>> _suiteAndZone;
54 :
55 : /// Returns the test that loads the suite.
56 : ///
57 : /// Load suites are guaranteed to only contain one test. This is a utility
58 : /// method for accessing it directly.
59 0 : Test get test => this.group.entries.single as Test;
60 :
61 : /// Creates a load suite named [name] on [platform].
62 : ///
63 : /// [body] may return either a [RunnerSuite] or a [Future] that completes to a
64 : /// [RunnerSuite]. Its return value is forwarded through [suite], although if
65 : /// it throws an error that will be forwarded through the suite's test.
66 : ///
67 : /// If the the load test is closed before [body] is complete, it will close
68 : /// the suite returned by [body] once it completes.
69 : factory LoadSuite(
70 : String name, SuiteConfiguration config, FutureOr<RunnerSuite> body(),
71 : {String path, TestPlatform platform}) {
72 0 : var completer = new Completer<Pair<RunnerSuite, Zone>>.sync();
73 0 : return new LoadSuite._(name, config, () {
74 0 : var invoker = Invoker.current;
75 0 : invoker.addOutstandingCallback();
76 :
77 0 : invoke(() async {
78 : try {
79 0 : var suite = await body();
80 0 : if (completer.isCompleted) {
81 : // If the load test has already been closed, close the suite it
82 : // generated.
83 0 : suite?.close();
84 : return;
85 : }
86 :
87 : completer
88 0 : .complete(suite == null ? null : new Pair(suite, Zone.current));
89 0 : invoker.removeOutstandingCallback();
90 : } catch (error, stackTrace) {
91 0 : registerException(error, stackTrace);
92 0 : if (!completer.isCompleted) completer.complete();
93 : }
94 0 : });
95 :
96 : // If the test is forcibly closed, exit immediately. It doesn't have any
97 : // cleanup to do that won't be handled by Loader.close.
98 0 : invoker.onClose.then((_) {
99 0 : if (completer.isCompleted) return;
100 0 : completer.complete();
101 0 : invoker.removeOutstandingCallback();
102 : });
103 0 : }, completer.future, path: path, platform: platform);
104 : }
105 :
106 : /// A utility constructor for a load suite that just throws [exception].
107 : ///
108 : /// The suite's name will be based on [exception]'s path.
109 : factory LoadSuite.forLoadException(
110 : LoadException exception, SuiteConfiguration config,
111 : {StackTrace stackTrace, TestPlatform platform}) {
112 0 : if (stackTrace == null) stackTrace = new Trace.current();
113 :
114 0 : return new LoadSuite(
115 0 : "loading ${exception.path}",
116 0 : config ?? SuiteConfiguration.empty,
117 0 : () => new Future.error(exception, stackTrace),
118 0 : path: exception.path,
119 : platform: platform);
120 : }
121 :
122 : /// A utility constructor for a load suite that just emits [suite].
123 : factory LoadSuite.forSuite(RunnerSuite suite) {
124 0 : return new LoadSuite("loading ${suite.path}", suite.config, () => suite,
125 0 : path: suite.path, platform: suite.platform);
126 : }
127 :
128 : LoadSuite._(String name, this.config, void body(), this._suiteAndZone,
129 : {String path, TestPlatform platform})
130 0 : : super(
131 0 : new Group.root([
132 0 : new LocalTest(
133 : name,
134 0 : new Metadata(timeout: new Timeout(new Duration(minutes: 5))),
135 : body)
136 : ]),
137 : path: path,
138 : platform: platform);
139 :
140 : /// A constructor used by [changeSuite].
141 : LoadSuite._changeSuite(LoadSuite old, this._suiteAndZone)
142 0 : : config = old.config,
143 0 : super(old.group, path: old.path, platform: old.platform);
144 :
145 : /// A constructor used by [filter].
146 : LoadSuite._filtered(LoadSuite old, Group filtered)
147 0 : : config = old.config,
148 0 : _suiteAndZone = old._suiteAndZone,
149 0 : super(old.group, path: old.path, platform: old.platform);
150 :
151 : /// Creates a new [LoadSuite] that's identical to this one, but that
152 : /// transforms [suite] once it's loaded.
153 : ///
154 : /// If [suite] completes to `null`, [change] won't be run. [change] is run
155 : /// within the load test's zone, so any errors or prints it emits will be
156 : /// associated with that test.
157 : LoadSuite changeSuite(RunnerSuite change(RunnerSuite suite)) {
158 0 : return new LoadSuite._changeSuite(this, _suiteAndZone.then((pair) {
159 : if (pair == null) return null;
160 :
161 0 : var zone = pair.last;
162 : var newSuite;
163 0 : zone.runGuarded(() {
164 0 : newSuite = change(pair.first);
165 : });
166 0 : return newSuite == null ? null : new Pair(newSuite, zone);
167 : }));
168 : }
169 :
170 : /// Runs the test and returns the suite.
171 : ///
172 : /// Rather than emitting errors through a [LiveTest], this just pipes them
173 : /// through the return value.
174 : Future<RunnerSuite> getSuite() async {
175 0 : var liveTest = test.load(this);
176 0 : liveTest.onMessage.listen((message) => print(message.text));
177 0 : await liveTest.run();
178 :
179 0 : if (liveTest.errors.isEmpty) return await suite;
180 :
181 0 : var error = liveTest.errors.first;
182 0 : await new Future.error(error.error, error.stackTrace);
183 : throw 'unreachable';
184 0 : }
185 :
186 : LoadSuite filter(bool callback(Test test)) {
187 0 : var filtered = this.group.filter(callback);
188 0 : if (filtered == null) filtered = new Group.root([], metadata: metadata);
189 0 : return new LoadSuite._filtered(this, filtered);
190 : }
191 :
192 0 : Future close() async {}
193 : }
|