| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 library test.runner.load_suite; | 5 library test.runner.load_suite; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 | 10 |
| 11 import '../../test.dart'; | 11 import '../../test.dart'; |
| 12 import '../backend/invoker.dart'; | 12 import '../backend/invoker.dart'; |
| 13 import '../backend/metadata.dart'; | 13 import '../backend/metadata.dart'; |
| 14 import '../backend/suite.dart'; | 14 import '../backend/suite.dart'; |
| 15 import '../backend/test_platform.dart'; |
| 15 import '../utils.dart'; | 16 import '../utils.dart'; |
| 16 import 'load_exception.dart'; | 17 import 'load_exception.dart'; |
| 17 | 18 |
| 18 /// A [Suite] emitted by a [Loader] that provides a test-like interface for | 19 /// A [Suite] emitted by a [Loader] that provides a test-like interface for |
| 19 /// loading a test file. | 20 /// loading a test file. |
| 20 /// | 21 /// |
| 21 /// This is used to expose the current status of test loading to the user. It's | 22 /// This is used to expose the current status of test loading to the user. It's |
| 22 /// important to provide users visibility into what's taking a long time and | 23 /// important to provide users visibility into what's taking a long time and |
| 23 /// where failures occur. And since some tests may be loaded at the same time as | 24 /// where failures occur. And since some tests may be loaded at the same time as |
| 24 /// others are run, it's useful to provide that visibility in the form of a test | 25 /// others are run, it's useful to provide that visibility in the form of a test |
| (...skipping 13 matching lines...) Expand all Loading... |
| 38 final Future<Suite> suite; | 39 final Future<Suite> suite; |
| 39 | 40 |
| 40 /// Creates a load suite named [name] on [platform]. | 41 /// Creates a load suite named [name] on [platform]. |
| 41 /// | 42 /// |
| 42 /// [body] may return either a [Suite] or a [Future] that completes to a | 43 /// [body] may return either a [Suite] or a [Future] that completes to a |
| 43 /// [Suite]. Its return value is forwarded through [suite], although if it | 44 /// [Suite]. Its return value is forwarded through [suite], although if it |
| 44 /// throws an error that will be forwarded through the suite's test. | 45 /// throws an error that will be forwarded through the suite's test. |
| 45 /// | 46 /// |
| 46 /// If the the load test is closed before [body] is complete, it will close | 47 /// If the the load test is closed before [body] is complete, it will close |
| 47 /// the suite returned by [body] once it completes. | 48 /// the suite returned by [body] once it completes. |
| 48 factory LoadSuite(String name, body(), {String platform}) { | 49 factory LoadSuite(String name, body(), {TestPlatform platform}) { |
| 49 var completer = new Completer.sync(); | 50 var completer = new Completer.sync(); |
| 50 return new LoadSuite._(name, () { | 51 return new LoadSuite._(name, () { |
| 51 var invoker = Invoker.current; | 52 var invoker = Invoker.current; |
| 52 invoker.addOutstandingCallback(); | 53 invoker.addOutstandingCallback(); |
| 53 | 54 |
| 54 invoke(() async { | 55 invoke(() async { |
| 55 try { | 56 try { |
| 56 var suite = await body(); | 57 var suite = await body(); |
| 57 if (completer.isCompleted) { | 58 if (completer.isCompleted) { |
| 58 // If the load test has already been closed, close the suite it | 59 // If the load test has already been closed, close the suite it |
| (...skipping 17 matching lines...) Expand all Loading... |
| 76 completer.complete(); | 77 completer.complete(); |
| 77 invoker.removeOutstandingCallback(); | 78 invoker.removeOutstandingCallback(); |
| 78 }); | 79 }); |
| 79 }, completer.future, platform: platform); | 80 }, completer.future, platform: platform); |
| 80 } | 81 } |
| 81 | 82 |
| 82 /// A utility constructor for a load suite that just throws [exception]. | 83 /// A utility constructor for a load suite that just throws [exception]. |
| 83 /// | 84 /// |
| 84 /// The suite's name will be based on [exception]'s path. | 85 /// The suite's name will be based on [exception]'s path. |
| 85 factory LoadSuite.forLoadException(LoadException exception, | 86 factory LoadSuite.forLoadException(LoadException exception, |
| 86 {StackTrace stackTrace, String platform}) { | 87 {StackTrace stackTrace, TestPlatform platform}) { |
| 87 if (stackTrace == null) stackTrace = new Trace.current(); | 88 if (stackTrace == null) stackTrace = new Trace.current(); |
| 88 | 89 |
| 89 return new LoadSuite("loading ${exception.path}", () { | 90 return new LoadSuite("loading ${exception.path}", () { |
| 90 return new Future.error(exception, stackTrace); | 91 return new Future.error(exception, stackTrace); |
| 91 }, platform: platform); | 92 }, platform: platform); |
| 92 } | 93 } |
| 93 | 94 |
| 94 /// A utility constructor for a load suite that just emits [suite]. | 95 /// A utility constructor for a load suite that just emits [suite]. |
| 95 factory LoadSuite.forSuite(Suite suite) { | 96 factory LoadSuite.forSuite(Suite suite) { |
| 96 return new LoadSuite("loading ${suite.path}", () => suite, | 97 return new LoadSuite("loading ${suite.path}", () => suite, |
| 97 platform: suite.platform); | 98 platform: suite.platform); |
| 98 } | 99 } |
| 99 | 100 |
| 100 LoadSuite._(String name, void body(), this.suite, {String platform}) | 101 LoadSuite._(String name, void body(), this.suite, {TestPlatform platform}) |
| 101 : super([ | 102 : super([ |
| 102 new LocalTest(name, | 103 new LocalTest(name, |
| 103 new Metadata(timeout: new Timeout(new Duration(minutes: 5))), | 104 new Metadata(timeout: new Timeout(new Duration(minutes: 5))), |
| 104 body) | 105 body) |
| 105 ], platform: platform); | 106 ], platform: platform); |
| 106 | 107 |
| 107 /// A constructor used by [changeSuite]. | 108 /// A constructor used by [changeSuite]. |
| 108 LoadSuite._changeSuite(LoadSuite old, Future<Suite> this.suite) | 109 LoadSuite._changeSuite(LoadSuite old, Future<Suite> this.suite) |
| 109 : super(old.tests, platform: old.platform); | 110 : super(old.tests, platform: old.platform); |
| 110 | 111 |
| 111 /// Creates a new [LoadSuite] that's identical to this one, but that | 112 /// Creates a new [LoadSuite] that's identical to this one, but that |
| 112 /// transforms [suite] once it's loaded. | 113 /// transforms [suite] once it's loaded. |
| 113 /// | 114 /// |
| 114 /// If [suite] completes to `null`, [change] won't be run. | 115 /// If [suite] completes to `null`, [change] won't be run. |
| 115 Suite changeSuite(Suite change(Suite suite)) { | 116 LoadSuite changeSuite(Suite change(Suite suite)) { |
| 116 return new LoadSuite._changeSuite(this, suite.then((loadedSuite) { | 117 return new LoadSuite._changeSuite(this, suite.then((loadedSuite) { |
| 117 if (loadedSuite == null) return null; | 118 if (loadedSuite == null) return null; |
| 118 return change(loadedSuite); | 119 return change(loadedSuite); |
| 119 })); | 120 })); |
| 120 } | 121 } |
| 121 | 122 |
| 122 /// Runs the test and returns the suite. | 123 /// Runs the test and returns the suite. |
| 123 /// | 124 /// |
| 124 /// Rather than emitting errors through a [LiveTest], this just pipes them | 125 /// Rather than emitting errors through a [LiveTest], this just pipes them |
| 125 /// through the return value. | 126 /// through the return value. |
| 126 Future<Suite> getSuite() async { | 127 Future<Suite> getSuite() async { |
| 127 var liveTest = await tests.single.load(this); | 128 var liveTest = await tests.single.load(this); |
| 128 liveTest.onPrint.listen(print); | 129 liveTest.onPrint.listen(print); |
| 129 await liveTest.run(); | 130 await liveTest.run(); |
| 130 | 131 |
| 131 if (liveTest.errors.isEmpty) return await suite; | 132 if (liveTest.errors.isEmpty) return await suite; |
| 132 | 133 |
| 133 var error = liveTest.errors.first; | 134 var error = liveTest.errors.first; |
| 134 await new Future.error(error.error, error.stackTrace); | 135 await new Future.error(error.error, error.stackTrace); |
| 135 throw 'unreachable'; | 136 throw 'unreachable'; |
| 136 } | 137 } |
| 137 } | 138 } |
| OLD | NEW |