| 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 unittest.util.io; | 5 library test.util.io; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:mirrors'; | 9 import 'dart:mirrors'; |
| 10 | 10 |
| 11 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 12 | 12 |
| 13 import '../backend/operating_system.dart'; | 13 import '../backend/operating_system.dart'; |
| 14 import '../runner/load_exception.dart'; | 14 import '../runner/load_exception.dart'; |
| 15 | 15 |
| 16 /// The root directory of the Dart SDK. | 16 /// The root directory of the Dart SDK. |
| 17 final String sdkDir = | 17 final String sdkDir = |
| 18 p.dirname(p.dirname(Platform.executable)); | 18 p.dirname(p.dirname(Platform.executable)); |
| 19 | 19 |
| 20 /// Returns the current operating system. | 20 /// Returns the current operating system. |
| 21 final OperatingSystem currentOS = (() { | 21 final OperatingSystem currentOS = (() { |
| 22 var name = Platform.operatingSystem; | 22 var name = Platform.operatingSystem; |
| 23 var os = OperatingSystem.findByIoName(name); | 23 var os = OperatingSystem.findByIoName(name); |
| 24 if (os != null) return os; | 24 if (os != null) return os; |
| 25 | 25 |
| 26 throw new UnsupportedError('Unsupported operating system "$name".'); | 26 throw new UnsupportedError('Unsupported operating system "$name".'); |
| 27 })(); | 27 })(); |
| 28 | 28 |
| 29 /// The path to the `lib` directory of the `unittest` package. | 29 /// The path to the `lib` directory of the `test` package. |
| 30 String libDir({String packageRoot}) { | 30 String libDir({String packageRoot}) { |
| 31 var pathToIo = libraryPath(#unittest.util.io, packageRoot: packageRoot); | 31 var pathToIo = libraryPath(#test.util.io, packageRoot: packageRoot); |
| 32 return p.dirname(p.dirname(p.dirname(pathToIo))); | 32 return p.dirname(p.dirname(p.dirname(pathToIo))); |
| 33 } | 33 } |
| 34 | 34 |
| 35 /// Returns whether the current Dart version supports [Isolate.kill]. | 35 /// Returns whether the current Dart version supports [Isolate.kill]. |
| 36 final bool supportsIsolateKill = _supportsIsolateKill; | 36 final bool supportsIsolateKill = _supportsIsolateKill; |
| 37 bool get _supportsIsolateKill { | 37 bool get _supportsIsolateKill { |
| 38 // This isn't 100% accurate, since early 1.9 dev releases didn't support | 38 // This isn't 100% accurate, since early 1.9 dev releases didn't support |
| 39 // Isolate.kill(), but it's very unlikely anyone will be using them. | 39 // Isolate.kill(), but it's very unlikely anyone will be using them. |
| 40 // TODO(nweiz): remove this when we no longer support older Dart versions. | 40 // TODO(nweiz): remove this when we no longer support older Dart versions. |
| 41 var path = p.join(p.dirname(p.dirname(Platform.executable)), 'version'); | 41 var path = p.join(p.dirname(p.dirname(Platform.executable)), 'version'); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 57 /// Once the [Future] returned by [fn] completes, the temporary directory and | 57 /// Once the [Future] returned by [fn] completes, the temporary directory and |
| 58 /// all its contents are deleted. [fn] can also return `null`, in which case | 58 /// all its contents are deleted. [fn] can also return `null`, in which case |
| 59 /// the temporary directory is deleted immediately afterwards. | 59 /// the temporary directory is deleted immediately afterwards. |
| 60 /// | 60 /// |
| 61 /// Returns a future that completes to the value that the future returned from | 61 /// Returns a future that completes to the value that the future returned from |
| 62 /// [fn] completes to. | 62 /// [fn] completes to. |
| 63 Future withTempDir(Future fn(String path)) { | 63 Future withTempDir(Future fn(String path)) { |
| 64 return new Future.sync(() { | 64 return new Future.sync(() { |
| 65 // TODO(nweiz): Empirically test whether sync or async functions perform | 65 // TODO(nweiz): Empirically test whether sync or async functions perform |
| 66 // better here when starting a bunch of isolates. | 66 // better here when starting a bunch of isolates. |
| 67 var tempDir = Directory.systemTemp.createTempSync('unittest_'); | 67 var tempDir = Directory.systemTemp.createTempSync('test_'); |
| 68 return new Future.sync(() => fn(tempDir.path)) | 68 return new Future.sync(() => fn(tempDir.path)) |
| 69 .whenComplete(() => tempDir.deleteSync(recursive: true)); | 69 .whenComplete(() => tempDir.deleteSync(recursive: true)); |
| 70 }); | 70 }); |
| 71 } | 71 } |
| 72 | 72 |
| 73 /// Creates a URL string for [address]:[port]. | 73 /// Creates a URL string for [address]:[port]. |
| 74 /// | 74 /// |
| 75 /// Handles properly formatting IPv6 addresses. | 75 /// Handles properly formatting IPv6 addresses. |
| 76 Uri baseUrlForAddress(InternetAddress address, int port) { | 76 Uri baseUrlForAddress(InternetAddress address, int port) { |
| 77 if (address.isLoopback) { | 77 if (address.isLoopback) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 107 /// returned. | 107 /// returned. |
| 108 String libraryPath(Symbol libraryName, {String packageRoot}) { | 108 String libraryPath(Symbol libraryName, {String packageRoot}) { |
| 109 var lib = currentMirrorSystem().findLibrary(libraryName); | 109 var lib = currentMirrorSystem().findLibrary(libraryName); |
| 110 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri); | 110 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri); |
| 111 | 111 |
| 112 // TODO(nweiz): is there a way to avoid assuming this is being run next to a | 112 // TODO(nweiz): is there a way to avoid assuming this is being run next to a |
| 113 // packages directory?. | 113 // packages directory?. |
| 114 if (packageRoot == null) packageRoot = p.absolute('packages'); | 114 if (packageRoot == null) packageRoot = p.absolute('packages'); |
| 115 return p.join(packageRoot, p.fromUri(lib.uri.path)); | 115 return p.join(packageRoot, p.fromUri(lib.uri.path)); |
| 116 } | 116 } |
| OLD | NEW |