| 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: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 import 'package:pub_semver/pub_semver.dart'; | 12 import 'package:pub_semver/pub_semver.dart'; |
| 13 | 13 |
| 14 import '../backend/operating_system.dart'; | 14 import '../backend/operating_system.dart'; |
| 15 import '../runner/application_exception.dart'; | 15 import '../runner/application_exception.dart'; |
| 16 | 16 |
| 17 /// The ASCII code for a newline character. |
| 18 const _newline = 0xA; |
| 19 |
| 20 /// The ASCII code for a carriage return character. |
| 21 const _carriageReturn = 0xD; |
| 22 |
| 17 /// The root directory of the Dart SDK. | 23 /// The root directory of the Dart SDK. |
| 18 final String sdkDir = | 24 final String sdkDir = |
| 19 p.dirname(p.dirname(Platform.executable)); | 25 p.dirname(p.dirname(Platform.executable)); |
| 20 | 26 |
| 21 /// The version of the Dart SDK currently in use. | 27 /// The version of the Dart SDK currently in use. |
| 22 final Version _sdkVersion = new Version.parse( | 28 final Version _sdkVersion = new Version.parse( |
| 23 new File(p.join(sdkDir, 'version')) | 29 new File(p.join(sdkDir, 'version')) |
| 24 .readAsStringSync().trim()); | 30 .readAsStringSync().trim()); |
| 25 | 31 |
| 26 /// Returns the current operating system. | 32 /// Returns the current operating system. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 /// Returns a future that completes to the value that the future returned from | 94 /// Returns a future that completes to the value that the future returned from |
| 89 /// [fn] completes to. | 95 /// [fn] completes to. |
| 90 Future withTempDir(Future fn(String path)) { | 96 Future withTempDir(Future fn(String path)) { |
| 91 return new Future.sync(() { | 97 return new Future.sync(() { |
| 92 var tempDir = createTempDir(); | 98 var tempDir = createTempDir(); |
| 93 return new Future.sync(() => fn(tempDir)) | 99 return new Future.sync(() => fn(tempDir)) |
| 94 .whenComplete(() => new Directory(tempDir).deleteSync(recursive: true)); | 100 .whenComplete(() => new Directory(tempDir).deleteSync(recursive: true)); |
| 95 }); | 101 }); |
| 96 } | 102 } |
| 97 | 103 |
| 104 /// Return a transformation of [input] with all null bytes removed. |
| 105 /// |
| 106 /// This works around the combination of issue 23295 and 22667 by removing null |
| 107 /// bytes. This workaround can be removed when either of those are fixed in the |
| 108 /// oldest supported SDK. |
| 109 /// |
| 110 /// It also somewhat works around issue 23303 by removing any carriage returns |
| 111 /// that are followed by newlines, to ensure that carriage returns aren't |
| 112 /// doubled up in the output. This can be removed when the issue is fixed in the |
| 113 /// oldest supported SDk. |
| 114 Stream<List<int>> santizeForWindows(Stream<List<int>> input) { |
| 115 if (!Platform.isWindows) return input; |
| 116 |
| 117 return input.map((list) { |
| 118 var previous; |
| 119 return list.reversed.where((byte) { |
| 120 if (byte == 0) return false; |
| 121 if (byte == _carriageReturn && previous == _newline) return false; |
| 122 previous = byte; |
| 123 return true; |
| 124 }).toList().reversed.toList(); |
| 125 }); |
| 126 } |
| 127 |
| 98 /// Creates a URL string for [address]:[port]. | 128 /// Creates a URL string for [address]:[port]. |
| 99 /// | 129 /// |
| 100 /// Handles properly formatting IPv6 addresses. | 130 /// Handles properly formatting IPv6 addresses. |
| 101 Uri baseUrlForAddress(InternetAddress address, int port) { | 131 Uri baseUrlForAddress(InternetAddress address, int port) { |
| 102 if (address.isLoopback) { | 132 if (address.isLoopback) { |
| 103 return new Uri(scheme: "http", host: "localhost", port: port); | 133 return new Uri(scheme: "http", host: "localhost", port: port); |
| 104 } | 134 } |
| 105 | 135 |
| 106 // IPv6 addresses in URLs need to be enclosed in square brackets to avoid | 136 // IPv6 addresses in URLs need to be enclosed in square brackets to avoid |
| 107 // URL ambiguity with the ":" in the address. | 137 // URL ambiguity with the ":" in the address. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 132 /// returned. | 162 /// returned. |
| 133 String libraryPath(Symbol libraryName, {String packageRoot}) { | 163 String libraryPath(Symbol libraryName, {String packageRoot}) { |
| 134 var lib = currentMirrorSystem().findLibrary(libraryName); | 164 var lib = currentMirrorSystem().findLibrary(libraryName); |
| 135 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri); | 165 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri); |
| 136 | 166 |
| 137 // TODO(nweiz): is there a way to avoid assuming this is being run next to a | 167 // TODO(nweiz): is there a way to avoid assuming this is being run next to a |
| 138 // packages directory?. | 168 // packages directory?. |
| 139 if (packageRoot == null) packageRoot = p.absolute('packages'); | 169 if (packageRoot == null) packageRoot = p.absolute('packages'); |
| 140 return p.join(packageRoot, p.fromUri(lib.uri.path)); | 170 return p.join(packageRoot, p.fromUri(lib.uri.path)); |
| 141 } | 171 } |
| OLD | NEW |