OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library unittest.io; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 | |
10 import 'package:path/path.dart' as p; | |
11 | |
12 /// Returns whether the current Dart version supports [Isolate.kill]. | |
13 final bool supportsIsolateKill = _supportsIsolateKill; | |
14 bool get _supportsIsolateKill { | |
15 // This isn't 100% accurate, since early 1.9 dev releases didn't support | |
16 // Isolate.kill(), but it's very unlikely anyone will be using them. | |
17 // TODO(nweiz): remove this when we no longer support older Dart versions. | |
18 var path = p.join(p.dirname(p.dirname(Platform.executable)), 'version'); | |
19 return new File(path).readAsStringSync().startsWith('1.9'); | |
20 } | |
21 | |
22 // TODO(nweiz): Make this check [stdioType] once that works within "pub run". | |
23 /// Whether "special" strings such as Unicode characters or color escapes are | |
24 /// safe to use. | |
25 /// | |
26 /// On Windows or when not printing to a terminal, only printable ASCII | |
27 /// characters should be used. | |
28 bool get canUseSpecialChars => | |
29 Platform.operatingSystem != 'windows' && | |
30 Platform.environment["_UNITTEST_USE_COLOR"] != "false"; | |
31 | |
32 /// Gets a "special" string (ANSI escape or Unicode). | |
33 /// | |
34 /// On Windows or when not printing to a terminal, returns something else since | |
35 /// those aren't supported. | |
36 String getSpecial(String special, [String onWindows = '']) => | |
37 canUseSpecialChars ? special : onWindows; | |
38 | |
39 /// Creates a temporary directory and passes its path to [fn]. | |
40 /// | |
41 /// Once the [Future] returned by [fn] completes, the temporary directory and | |
42 /// all its contents are deleted. [fn] can also return `null`, in which case | |
43 /// the temporary directory is deleted immediately afterwards. | |
44 /// | |
45 /// Returns a future that completes to the value that the future returned from | |
46 /// [fn] completes to. | |
47 Future withTempDir(Future fn(String path)) { | |
48 return new Future.sync(() { | |
49 // TODO(nweiz): Empirically test whether sync or async functions perform | |
50 // better here when starting a bunch of isolates. | |
51 var tempDir = Directory.systemTemp.createTempSync('unittest_'); | |
52 return new Future.sync(() => fn(tempDir.path)) | |
53 .whenComplete(() => tempDir.deleteSync(recursive: true)); | |
54 }); | |
55 } | |
OLD | NEW |