Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: lib/src/util/io.dart

Issue 1056733002: Run test tearDowns and clean up temporary directories when a signal is caught. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Code review changes Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/util/dart.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 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 root directory below which to nest temporary directories created by the
30 /// test runner.
31 ///
32 /// This is configurable so that the test code can validate that the runner
33 /// cleans up after itself fully.
34 final _tempDir = Platform.environment.containsKey("_UNITTEST_TEMP_DIR")
35 ? Platform.environment["_UNITTEST_TEMP_DIR"]
36 : Directory.systemTemp.path;
37
29 /// The path to the `lib` directory of the `test` package. 38 /// The path to the `lib` directory of the `test` package.
30 String libDir({String packageRoot}) { 39 String libDir({String packageRoot}) {
31 var pathToIo = libraryPath(#test.util.io, packageRoot: packageRoot); 40 var pathToIo = libraryPath(#test.util.io, packageRoot: packageRoot);
32 return p.dirname(p.dirname(p.dirname(pathToIo))); 41 return p.dirname(p.dirname(p.dirname(pathToIo)));
33 } 42 }
34 43
35 /// Returns whether the current Dart version supports [Isolate.kill]. 44 /// Returns whether the current Dart version supports [Isolate.kill].
36 final bool supportsIsolateKill = _supportsIsolateKill; 45 final bool supportsIsolateKill = _supportsIsolateKill;
37 bool get _supportsIsolateKill { 46 bool get _supportsIsolateKill {
38 // This isn't 100% accurate, since early 1.9 dev releases didn't support 47 // 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. 48 // 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. 49 // TODO(nweiz): remove this when we no longer support older Dart versions.
41 var path = p.join(p.dirname(p.dirname(Platform.executable)), 'version'); 50 var path = p.join(p.dirname(p.dirname(Platform.executable)), 'version');
42 return !new File(path).readAsStringSync().startsWith('1.8'); 51 return !new File(path).readAsStringSync().startsWith('1.8');
43 } 52 }
44 53
45 // TODO(nweiz): Make this check [stdioType] once that works within "pub run". 54 // TODO(nweiz): Make this check [stdioType] once that works within "pub run".
46 /// Whether "special" strings such as Unicode characters or color escapes are 55 /// Whether "special" strings such as Unicode characters or color escapes are
47 /// safe to use. 56 /// safe to use.
48 /// 57 ///
49 /// On Windows or when not printing to a terminal, only printable ASCII 58 /// On Windows or when not printing to a terminal, only printable ASCII
50 /// characters should be used. 59 /// characters should be used.
51 bool get canUseSpecialChars => 60 bool get canUseSpecialChars =>
52 Platform.operatingSystem != 'windows' && 61 Platform.operatingSystem != 'windows' &&
53 Platform.environment["_UNITTEST_USE_COLOR"] != "false"; 62 Platform.environment["_UNITTEST_USE_COLOR"] != "false";
54 63
64 /// Creates a temporary directory and returns its path.
65 String createTempDir() =>
66 new Directory(_tempDir).createTempSync('dart_test_').path;
67
55 /// Creates a temporary directory and passes its path to [fn]. 68 /// Creates a temporary directory and passes its path to [fn].
56 /// 69 ///
57 /// Once the [Future] returned by [fn] completes, the temporary directory and 70 /// 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 71 /// all its contents are deleted. [fn] can also return `null`, in which case
59 /// the temporary directory is deleted immediately afterwards. 72 /// the temporary directory is deleted immediately afterwards.
60 /// 73 ///
61 /// Returns a future that completes to the value that the future returned from 74 /// Returns a future that completes to the value that the future returned from
62 /// [fn] completes to. 75 /// [fn] completes to.
63 Future withTempDir(Future fn(String path)) { 76 Future withTempDir(Future fn(String path)) {
64 return new Future.sync(() { 77 return new Future.sync(() {
65 // TODO(nweiz): Empirically test whether sync or async functions perform 78 var tempDir = createTempDir();
66 // better here when starting a bunch of isolates. 79 return new Future.sync(() => fn(tempDir))
67 var tempDir = Directory.systemTemp.createTempSync('test_'); 80 .whenComplete(() => new Directory(tempDir).deleteSync(recursive: true));
68 return new Future.sync(() => fn(tempDir.path))
69 .whenComplete(() => tempDir.deleteSync(recursive: true));
70 }); 81 });
71 } 82 }
72 83
73 /// Creates a URL string for [address]:[port]. 84 /// Creates a URL string for [address]:[port].
74 /// 85 ///
75 /// Handles properly formatting IPv6 addresses. 86 /// Handles properly formatting IPv6 addresses.
76 Uri baseUrlForAddress(InternetAddress address, int port) { 87 Uri baseUrlForAddress(InternetAddress address, int port) {
77 if (address.isLoopback) { 88 if (address.isLoopback) {
78 return new Uri(scheme: "http", host: "localhost", port: port); 89 return new Uri(scheme: "http", host: "localhost", port: port);
79 } 90 }
(...skipping 27 matching lines...) Expand all
107 /// returned. 118 /// returned.
108 String libraryPath(Symbol libraryName, {String packageRoot}) { 119 String libraryPath(Symbol libraryName, {String packageRoot}) {
109 var lib = currentMirrorSystem().findLibrary(libraryName); 120 var lib = currentMirrorSystem().findLibrary(libraryName);
110 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri); 121 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri);
111 122
112 // TODO(nweiz): is there a way to avoid assuming this is being run next to a 123 // TODO(nweiz): is there a way to avoid assuming this is being run next to a
113 // packages directory?. 124 // packages directory?.
114 if (packageRoot == null) packageRoot = p.absolute('packages'); 125 if (packageRoot == null) packageRoot = p.absolute('packages');
115 return p.join(packageRoot, p.fromUri(lib.uri.path)); 126 return p.join(packageRoot, p.fromUri(lib.uri.path));
116 } 127 }
OLDNEW
« no previous file with comments | « lib/src/util/dart.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698