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 analyzer_cli.test.utils; | 5 library analyzer_cli.test.utils; |
6 | 6 |
| 7 import 'dart:async'; |
7 import 'dart:io'; | 8 import 'dart:io'; |
8 import 'dart:mirrors'; | 9 import 'dart:mirrors'; |
9 | 10 |
10 import 'package:analyzer/analyzer.dart'; | 11 import 'package:analyzer/analyzer.dart'; |
11 import 'package:path/path.dart' as pathos; | 12 import 'package:path/path.dart' as pathos; |
12 | 13 |
13 /// Gets the test directory in a way that works with package:test | 14 /// Gets the test directory in a way that works with package:test |
14 /// See <https://github.com/dart-lang/test/issues/110> for more info. | 15 /// See <https://github.com/dart-lang/test/issues/110> for more info. |
15 final String testDirectory = pathos.dirname( | 16 final String testDirectory = pathos.dirname( |
16 pathos.fromUri((reflectClass(_TestUtils).owner as LibraryMirror).uri)); | 17 pathos.fromUri((reflectClass(_TestUtils).owner as LibraryMirror).uri)); |
(...skipping 28 matching lines...) Expand all Loading... |
45 /// Returns the return value of [fn]. | 46 /// Returns the return value of [fn]. |
46 dynamic withTempDir(fn(String path)) { | 47 dynamic withTempDir(fn(String path)) { |
47 var tempDir = Directory.systemTemp.createTempSync('analyzer_').path; | 48 var tempDir = Directory.systemTemp.createTempSync('analyzer_').path; |
48 try { | 49 try { |
49 return fn(tempDir); | 50 return fn(tempDir); |
50 } finally { | 51 } finally { |
51 new Directory(tempDir).deleteSync(recursive: true); | 52 new Directory(tempDir).deleteSync(recursive: true); |
52 } | 53 } |
53 } | 54 } |
54 | 55 |
| 56 /// Creates a temporary directory and passes its path to [fn]. Once [fn] |
| 57 /// completes, the temporary directory and all its contents will be deleted. |
| 58 /// |
| 59 /// Returns the return value of [fn]. |
| 60 Future<dynamic> withTempDirAsync(Future<dynamic> fn(String path)) async { |
| 61 var tempDir = (await Directory.systemTemp.createTemp('analyzer_')).path; |
| 62 try { |
| 63 return await fn(tempDir); |
| 64 } finally { |
| 65 await new Directory(tempDir).delete(recursive: true); |
| 66 } |
| 67 } |
| 68 |
| 69 /// Recursively copy the specified [src] directory (or file) |
| 70 /// to the specified destination path. |
| 71 Future<Null> recursiveCopy(FileSystemEntity src, String dstPath) async { |
| 72 if (src is Directory) { |
| 73 await (new Directory(dstPath)).create(recursive: true); |
| 74 for (FileSystemEntity entity in src.listSync()) { |
| 75 await recursiveCopy( |
| 76 entity, pathos.join(dstPath, pathos.basename(entity.path))); |
| 77 } |
| 78 } else if (src is File) { |
| 79 await src.copy(dstPath); |
| 80 } |
| 81 } |
| 82 |
55 class _TestUtils {} | 83 class _TestUtils {} |
OLD | NEW |