| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 import 'dart:mirrors'; | |
| 8 | 7 |
| 9 import 'package:path/path.dart' as p; | 8 import 'package:path/path.dart' as p; |
| 10 import 'package:pub_semver/pub_semver.dart'; | 9 import 'package:pub_semver/pub_semver.dart'; |
| 11 | 10 |
| 12 import '../backend/operating_system.dart'; | 11 import '../backend/operating_system.dart'; |
| 13 import '../runner/application_exception.dart'; | |
| 14 import '../util/stream_queue.dart'; | 12 import '../util/stream_queue.dart'; |
| 15 import '../utils.dart'; | 13 import '../utils.dart'; |
| 16 | 14 |
| 17 /// The ASCII code for a newline character. | 15 /// The ASCII code for a newline character. |
| 18 const _newline = 0xA; | 16 const _newline = 0xA; |
| 19 | 17 |
| 20 /// The ASCII code for a carriage return character. | 18 /// The ASCII code for a carriage return character. |
| 21 const _carriageReturn = 0xD; | 19 const _carriageReturn = 0xD; |
| 22 | 20 |
| 23 /// The root directory of the Dart SDK. | 21 /// The root directory of the Dart SDK. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 45 | 43 |
| 46 /// The root directory below which to nest temporary directories created by the | 44 /// The root directory below which to nest temporary directories created by the |
| 47 /// test runner. | 45 /// test runner. |
| 48 /// | 46 /// |
| 49 /// This is configurable so that the test code can validate that the runner | 47 /// This is configurable so that the test code can validate that the runner |
| 50 /// cleans up after itself fully. | 48 /// cleans up after itself fully. |
| 51 final _tempDir = Platform.environment.containsKey("_UNITTEST_TEMP_DIR") | 49 final _tempDir = Platform.environment.containsKey("_UNITTEST_TEMP_DIR") |
| 52 ? Platform.environment["_UNITTEST_TEMP_DIR"] | 50 ? Platform.environment["_UNITTEST_TEMP_DIR"] |
| 53 : Directory.systemTemp.path; | 51 : Directory.systemTemp.path; |
| 54 | 52 |
| 55 /// The path to the `lib` directory of the `test` package. | |
| 56 String libDir({String packageRoot}) { | |
| 57 var pathToIo = libraryPath(#test.util.io, packageRoot: packageRoot); | |
| 58 return p.dirname(p.dirname(p.dirname(pathToIo))); | |
| 59 } | |
| 60 | |
| 61 // TODO(nweiz): Make this check [stdioType] once that works within "pub run". | 53 // TODO(nweiz): Make this check [stdioType] once that works within "pub run". |
| 62 /// Whether "special" strings such as Unicode characters or color escapes are | 54 /// Whether "special" strings such as Unicode characters or color escapes are |
| 63 /// safe to use. | 55 /// safe to use. |
| 64 /// | 56 /// |
| 65 /// On Windows or when not printing to a terminal, only printable ASCII | 57 /// On Windows or when not printing to a terminal, only printable ASCII |
| 66 /// characters should be used. | 58 /// characters should be used. |
| 67 bool get canUseSpecialChars => | 59 bool get canUseSpecialChars => |
| 68 Platform.operatingSystem != 'windows' && !inTestTests; | 60 Platform.operatingSystem != 'windows' && !inTestTests; |
| 69 | 61 |
| 70 /// Creates a temporary directory and returns its path. | 62 /// Creates a temporary directory and returns its path. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 /// it controls whether the warning header is color; otherwise, it defaults to | 109 /// it controls whether the warning header is color; otherwise, it defaults to |
| 118 /// [canUseSpecialChars]. | 110 /// [canUseSpecialChars]. |
| 119 void warn(String message, {bool color}) { | 111 void warn(String message, {bool color}) { |
| 120 if (color == null) color = canUseSpecialChars; | 112 if (color == null) color = canUseSpecialChars; |
| 121 var header = color | 113 var header = color |
| 122 ? "\u001b[33mWarning:\u001b[0m" | 114 ? "\u001b[33mWarning:\u001b[0m" |
| 123 : "Warning:"; | 115 : "Warning:"; |
| 124 stderr.writeln(wordWrap("$header $message\n")); | 116 stderr.writeln(wordWrap("$header $message\n")); |
| 125 } | 117 } |
| 126 | 118 |
| 127 /// Returns the package root at [root]. | |
| 128 /// | |
| 129 /// If [override] is passed, that's used. If the package root doesn't exist, an | |
| 130 /// [ApplicationException] is thrown. | |
| 131 String packageRootFor(String root, [String override]) { | |
| 132 if (root == null) root = p.current; | |
| 133 var packageRoot = override == null ? p.join(root, 'packages') : override; | |
| 134 | |
| 135 if (!new Directory(packageRoot).existsSync()) { | |
| 136 throw new ApplicationException( | |
| 137 "Directory ${p.prettyUri(p.toUri(packageRoot))} does not exist."); | |
| 138 } | |
| 139 | |
| 140 return packageRoot; | |
| 141 } | |
| 142 | |
| 143 /// The library name must be globally unique, or the wrong library path may be | |
| 144 /// returned. | |
| 145 String libraryPath(Symbol libraryName, {String packageRoot}) { | |
| 146 var lib = currentMirrorSystem().findLibrary(libraryName); | |
| 147 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri); | |
| 148 | |
| 149 // TODO(nweiz): is there a way to avoid assuming this is being run next to a | |
| 150 // packages directory?. | |
| 151 if (packageRoot == null) packageRoot = p.absolute('packages'); | |
| 152 return p.join(packageRoot, p.fromUri(lib.uri.path)); | |
| 153 } | |
| 154 | |
| 155 /// Repeatedly finds a probably-unused port on localhost and passes it to | 119 /// Repeatedly finds a probably-unused port on localhost and passes it to |
| 156 /// [tryPort] until it binds successfully. | 120 /// [tryPort] until it binds successfully. |
| 157 /// | 121 /// |
| 158 /// [tryPort] should return a non-`null` value or a Future completing to a | 122 /// [tryPort] should return a non-`null` value or a Future completing to a |
| 159 /// non-`null` value once it binds successfully. This value will be returned | 123 /// non-`null` value once it binds successfully. This value will be returned |
| 160 /// by [getUnusedPort] in turn. | 124 /// by [getUnusedPort] in turn. |
| 161 /// | 125 /// |
| 162 /// This is necessary for ensuring that our port binding isn't flaky for | 126 /// This is necessary for ensuring that our port binding isn't flaky for |
| 163 /// applications that don't print out the bound port. | 127 /// applications that don't print out the bound port. |
| 164 Future/*<T>*/ getUnusedPort/*<T>*/(/*=T*/ tryPort(int port)) { | 128 Future/*<T>*/ getUnusedPort/*<T>*/(/*=T*/ tryPort(int port)) { |
| 165 var/*=T*/ value; | 129 var/*=T*/ value; |
| 166 return Future.doWhile(() async { | 130 return Future.doWhile(() async { |
| 167 value = await tryPort(await getUnsafeUnusedPort()); | 131 value = await tryPort(await getUnsafeUnusedPort()); |
| 168 return value == null; | 132 return value == null; |
| 169 }).then((_) => value); | 133 }).then((_) => value); |
| 170 } | 134 } |
| 171 | 135 |
| 172 /// Returns a port that is probably, but not definitely, not in use. | 136 /// Returns a port that is probably, but not definitely, not in use. |
| 173 /// | 137 /// |
| 174 /// This has a built-in race condition: another process may bind this port at | 138 /// This has a built-in race condition: another process may bind this port at |
| 175 /// any time after this call has returned. If at all possible, callers should | 139 /// any time after this call has returned. If at all possible, callers should |
| 176 /// use [getUnusedPort] instead. | 140 /// use [getUnusedPort] instead. |
| 177 Future<int> getUnsafeUnusedPort() async { | 141 Future<int> getUnsafeUnusedPort() async { |
| 178 var socket = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); | 142 var socket = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); |
| 179 var port = socket.port; | 143 var port = socket.port; |
| 180 await socket.close(); | 144 await socket.close(); |
| 181 return port; | 145 return port; |
| 182 } | 146 } |
| OLD | NEW |