| 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.util.io; | 5 library test.util.io; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 import 'dart:mirrors'; | 10 import 'dart:mirrors'; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 /// it controls whether the warning header is color; otherwise, it defaults to | 119 /// it controls whether the warning header is color; otherwise, it defaults to |
| 120 /// [canUseSpecialChars]. | 120 /// [canUseSpecialChars]. |
| 121 void warn(String message, {bool color}) { | 121 void warn(String message, {bool color}) { |
| 122 if (color == null) color = canUseSpecialChars; | 122 if (color == null) color = canUseSpecialChars; |
| 123 var header = color | 123 var header = color |
| 124 ? "\u001b[33mWarning:\u001b[0m" | 124 ? "\u001b[33mWarning:\u001b[0m" |
| 125 : "Warning:"; | 125 : "Warning:"; |
| 126 stderr.writeln(wordWrap("$header $message\n")); | 126 stderr.writeln(wordWrap("$header $message\n")); |
| 127 } | 127 } |
| 128 | 128 |
| 129 /// Creates a URL string for [address]:[port]. | |
| 130 /// | |
| 131 /// Handles properly formatting IPv6 addresses. | |
| 132 Uri baseUrlForAddress(InternetAddress address, int port) { | |
| 133 if (address.isLoopback) { | |
| 134 return new Uri(scheme: "http", host: "localhost", port: port); | |
| 135 } | |
| 136 | |
| 137 // IPv6 addresses in URLs need to be enclosed in square brackets to avoid | |
| 138 // URL ambiguity with the ":" in the address. | |
| 139 if (address.type == InternetAddressType.IP_V6) { | |
| 140 return new Uri(scheme: "http", host: "[${address.address}]", port: port); | |
| 141 } | |
| 142 | |
| 143 return new Uri(scheme: "http", host: address.address, port: port); | |
| 144 } | |
| 145 | |
| 146 /// Returns the package root at [root]. | 129 /// Returns the package root at [root]. |
| 147 /// | 130 /// |
| 148 /// If [override] is passed, that's used. If the package root doesn't exist, an | 131 /// If [override] is passed, that's used. If the package root doesn't exist, an |
| 149 /// [ApplicationException] is thrown. | 132 /// [ApplicationException] is thrown. |
| 150 String packageRootFor(String root, [String override]) { | 133 String packageRootFor(String root, [String override]) { |
| 151 if (root == null) root = p.current; | 134 if (root == null) root = p.current; |
| 152 var packageRoot = override == null ? p.join(root, 'packages') : override; | 135 var packageRoot = override == null ? p.join(root, 'packages') : override; |
| 153 | 136 |
| 154 if (!new Directory(packageRoot).existsSync()) { | 137 if (!new Directory(packageRoot).existsSync()) { |
| 155 throw new ApplicationException( | 138 throw new ApplicationException( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 /// | 175 /// |
| 193 /// This has a built-in race condition: another process may bind this port at | 176 /// This has a built-in race condition: another process may bind this port at |
| 194 /// any time after this call has returned. If at all possible, callers should | 177 /// any time after this call has returned. If at all possible, callers should |
| 195 /// use [getUnusedPort] instead. | 178 /// use [getUnusedPort] instead. |
| 196 Future<int> getUnsafeUnusedPort() async { | 179 Future<int> getUnsafeUnusedPort() async { |
| 197 var socket = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); | 180 var socket = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); |
| 198 var port = socket.port; | 181 var port = socket.port; |
| 199 await socket.close(); | 182 await socket.close(); |
| 200 return port; | 183 return port; |
| 201 } | 184 } |
| OLD | NEW |