OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:stack_trace/stack_trace.dart'; | |
8 import 'package:stream_channel/stream_channel.dart'; | 7 import 'package:stream_channel/stream_channel.dart'; |
9 | 8 |
10 import '../../backend/group.dart'; | |
11 import '../../backend/metadata.dart'; | 9 import '../../backend/metadata.dart'; |
12 import '../../backend/test.dart'; | |
13 import '../../backend/test_platform.dart'; | 10 import '../../backend/test_platform.dart'; |
14 import '../../util/io.dart'; | |
15 import '../../util/remote_exception.dart'; | |
16 import '../environment.dart'; | 11 import '../environment.dart'; |
17 import '../load_exception.dart'; | |
18 import '../runner_suite.dart'; | 12 import '../runner_suite.dart'; |
19 import '../runner_test.dart'; | |
20 import 'environment.dart'; | 13 import 'environment.dart'; |
| 14 import 'platform_helpers.dart'; |
21 | 15 |
22 /// A class that defines a platform for which test suites can be loaded. | 16 /// A class that defines a platform for which test suites can be loaded. |
23 /// | 17 /// |
24 /// A minimal plugin must define [platforms], which indicates the platforms it | 18 /// A minimal plugin must define [platforms], which indicates the platforms it |
25 /// supports, and [loadChannel], which connects to a client in which the tests | 19 /// supports, and [loadChannel], which connects to a client in which the tests |
26 /// are defined. This is enough to support most of the test runner's | 20 /// are defined. This is enough to support most of the test runner's |
27 /// functionality. | 21 /// functionality. |
28 /// | 22 /// |
29 /// In order to support interactive debugging, a plugin must override [load] as | 23 /// In order to support interactive debugging, a plugin must override [load] as |
30 /// well, which returns a [RunnerSuite] that can contain a custom [Environment] | 24 /// well, which returns a [RunnerSuite] that can contain a custom [Environment] |
31 /// and control debugging metadata such as [RunnerSuite.isDebugging] and | 25 /// and control debugging metadata such as [RunnerSuite.isDebugging] and |
32 /// [RunnerSuite.onDebugging]. To make this easier, implementations can call | 26 /// [RunnerSuite.onDebugging]. To make this easier, implementations can call |
33 /// [deserializeSuite]. | 27 /// [deserializeSuite] in `platform_helpers.dart`. |
34 /// | 28 /// |
35 /// A platform plugin can be registered with [Loader.registerPlatformPlugin]. | 29 /// A platform plugin can be registered with [Loader.registerPlatformPlugin]. |
36 abstract class PlatformPlugin { | 30 abstract class PlatformPlugin { |
37 /// The platforms supported by this plugin. | |
38 /// | |
39 /// A plugin may declare support for existing platform, in which case it | |
40 /// overrides the previous loading functionality for that platform. | |
41 List<TestPlatform> get platforms; | |
42 | |
43 /// Loads and establishes a connection with the test file at [path] using | 31 /// Loads and establishes a connection with the test file at [path] using |
44 /// [platform]. | 32 /// [platform]. |
45 /// | 33 /// |
46 /// This returns a channel that's connected to a remote client. The client | 34 /// This returns a channel that's connected to a remote client. The client |
47 /// must connect it to a channel returned by [serializeGroup]. The default | 35 /// must connect it to a channel returned by [serializeGroup]. The default |
48 /// implementation of [load] will take care of wrapping it up in a | 36 /// implementation of [load] will take care of wrapping it up in a |
49 /// [RunnerSuite] and running the tests when necessary. | 37 /// [RunnerSuite] and running the tests when necessary. |
50 /// | 38 /// |
51 /// The returned channel may emit exceptions, indicating that the suite failed | 39 /// The returned channel may emit exceptions, indicating that the suite failed |
52 /// to load or crashed later on. If the channel is closed by the caller, that | 40 /// to load or crashed later on. If the channel is closed by the caller, that |
53 /// indicates that the suite is no longer needed and its resources may be | 41 /// indicates that the suite is no longer needed and its resources may be |
54 /// released. | 42 /// released. |
55 /// | 43 /// |
56 /// The [platform] is guaranteed to be a member of [platforms]. | 44 /// The [platform] is guaranteed to be a member of [platforms]. |
57 StreamChannel loadChannel(String path, TestPlatform platform); | 45 StreamChannel loadChannel(String path, TestPlatform platform); |
58 | 46 |
59 /// Loads the runner suite for the test file at [path] using [platform], with | 47 /// Loads the runner suite for the test file at [path] using [platform], with |
60 /// [metadata] parsed from the test file's top-level annotations. | 48 /// [metadata] parsed from the test file's top-level annotations. |
61 /// | 49 /// |
62 /// By default, this just calls [loadChannel] and passes its result to | 50 /// By default, this just calls [loadChannel] and passes its result to |
63 /// [deserializeSuite]. However, it can be overridden to provide more | 51 /// [deserializeSuite]. However, it can be overridden to provide more |
64 /// fine-grained control over the [RunnerSuite], including providing a custom | 52 /// fine-grained control over the [RunnerSuite], including providing a custom |
65 /// implementation of [Environment]. | 53 /// implementation of [Environment]. |
66 /// | 54 /// |
67 /// It's recommended that subclasses overriding this method call | 55 /// It's recommended that subclasses overriding this method call |
68 /// [deserializeSuite] to obtain a [RunnerSuiteController]. | 56 /// [deserializeSuite] in `platform_helpers.dart` to obtain a |
| 57 /// [RunnerSuiteController]. |
69 Future<RunnerSuite> load(String path, TestPlatform platform, | 58 Future<RunnerSuite> load(String path, TestPlatform platform, |
70 Metadata metadata) async { | 59 Metadata metadata) async { |
71 // loadChannel may throw an exception. That's fine; it will cause the | 60 // loadChannel may throw an exception. That's fine; it will cause the |
72 // LoadSuite to emit an error, which will be presented to the user. | 61 // LoadSuite to emit an error, which will be presented to the user. |
73 var channel = loadChannel(path, platform); | 62 var channel = loadChannel(path, platform); |
74 var controller = await deserializeSuite( | 63 var controller = await deserializeSuite( |
75 path, platform, metadata, new PluginEnvironment(), channel); | 64 path, platform, metadata, new PluginEnvironment(), channel); |
76 return controller.suite; | 65 return controller.suite; |
77 } | 66 } |
78 | 67 |
79 /// A helper method for creating a [RunnerSuiteController] containing tests | 68 Future closeEphemeral() async {} |
80 /// that communicate over [channel]. | |
81 /// | |
82 /// This is notionally a protected method. It may be called by subclasses, but | |
83 /// it shouldn't be accessed by externally. | |
84 /// | |
85 /// This returns a controller so that the caller has a chance to control the | |
86 /// runner suite's debugging state based on plugin-specific logic. | |
87 Future<RunnerSuiteController> deserializeSuite(String path, | |
88 TestPlatform platform, Metadata metadata, Environment environment, | |
89 StreamChannel channel) async { | |
90 var disconnector = new Disconnector(); | |
91 var suiteChannel = new MultiChannel(channel.transform(disconnector)); | |
92 | 69 |
93 suiteChannel.sink.add({ | 70 Future close() async {} |
94 'platform': platform.identifier, | |
95 'metadata': metadata.serialize(), | |
96 'os': platform == TestPlatform.vm ? currentOS.name : null | |
97 }); | |
98 | |
99 var completer = new Completer(); | |
100 | |
101 handleError(error, stackTrace) { | |
102 disconnector.disconnect(); | |
103 | |
104 if (completer.isCompleted) { | |
105 // If we've already provided a controller, send the error to the | |
106 // LoadSuite. This will cause the virtual load test to fail, which will | |
107 // notify the user of the error. | |
108 Zone.current.handleUncaughtError(error, stackTrace); | |
109 } else { | |
110 completer.completeError(error, stackTrace); | |
111 } | |
112 } | |
113 | |
114 suiteChannel.stream.listen((response) { | |
115 switch (response["type"]) { | |
116 case "print": | |
117 print(response["line"]); | |
118 break; | |
119 | |
120 case "loadException": | |
121 handleError( | |
122 new LoadException(path, response["message"]), | |
123 new Trace.current()); | |
124 break; | |
125 | |
126 case "error": | |
127 var asyncError = RemoteException.deserialize(response["error"]); | |
128 handleError( | |
129 new LoadException(path, asyncError.error), | |
130 asyncError.stackTrace); | |
131 break; | |
132 | |
133 case "success": | |
134 completer.complete( | |
135 _deserializeGroup(suiteChannel, response["root"])); | |
136 break; | |
137 } | |
138 }, onError: handleError, onDone: () { | |
139 if (completer.isCompleted) return; | |
140 completer.completeError( | |
141 new LoadException( | |
142 path, "Connection closed before test suite loaded."), | |
143 new Trace.current()); | |
144 }); | |
145 | |
146 return new RunnerSuiteController( | |
147 environment, | |
148 await completer.future, | |
149 path: path, | |
150 platform: platform, | |
151 os: currentOS, | |
152 onClose: disconnector.disconnect); | |
153 } | |
154 | |
155 /// Deserializes [group] into a concrete [Group]. | |
156 Group _deserializeGroup(MultiChannel suiteChannel, Map group) { | |
157 var metadata = new Metadata.deserialize(group['metadata']); | |
158 return new Group(group['name'], group['entries'].map((entry) { | |
159 if (entry['type'] == 'group') { | |
160 return _deserializeGroup(suiteChannel, entry); | |
161 } | |
162 | |
163 return _deserializeTest(suiteChannel, entry); | |
164 }), | |
165 metadata: metadata, | |
166 setUpAll: _deserializeTest(suiteChannel, group['setUpAll']), | |
167 tearDownAll: _deserializeTest(suiteChannel, group['tearDownAll'])); | |
168 } | |
169 | |
170 /// Deserializes [test] into a concrete [Test] class. | |
171 /// | |
172 /// Returns `null` if [test] is `null`. | |
173 Test _deserializeTest(MultiChannel suiteChannel, Map test) { | |
174 if (test == null) return null; | |
175 | |
176 var metadata = new Metadata.deserialize(test['metadata']); | |
177 var testChannel = suiteChannel.virtualChannel(test['channel']); | |
178 return new RunnerTest(test['name'], metadata, testChannel); | |
179 } | |
180 } | 71 } |
OLD | NEW |