OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 utils; | 5 library analyzer.test.utils; |
6 | 6 |
7 import 'dart:io'; | 7 import 'package:analyzer/src/generated/java_io.dart'; |
| 8 import 'package:path/path.dart' as path; |
| 9 import 'package:unittest/unittest.dart'; |
8 | 10 |
9 import 'package:analyzer/analyzer.dart'; | 11 void initializeTestEnvironment() { |
10 import 'package:path/path.dart' as pathos; | 12 groupSep = ' | '; |
11 | 13 JavaFile.pathContext = path.posix; |
12 /// Returns the string representation of the [AnalyzerErrorGroup] thrown when | |
13 /// parsing [contents] as a Dart file. If [contents] doesn't throw any errors, | |
14 /// this will return null. | |
15 /// | |
16 /// This replaces the filename in the error string with its basename, since the | |
17 /// full path will vary from machine to machine. It also replaces the exception | |
18 /// message with "..." to decouple these tests from the specific exception | |
19 /// messages. | |
20 String errorsForFile(String contents) { | |
21 return withTempDir((temp) { | |
22 var path = pathos.join(temp, 'test.dart'); | |
23 new File(path).writeAsStringSync(contents); | |
24 try { | |
25 parseDartFile(path); | |
26 } on AnalyzerErrorGroup catch (e) { | |
27 return e.toString().replaceAllMapped(new RegExp( | |
28 r"^(Error on line \d+ of )((?:[A-Z]+:)?[^:]+): .*$", | |
29 multiLine: true), | |
30 (match) => match[1] + pathos.basename(match[2]) + ': ...'); | |
31 } | |
32 return null; | |
33 }); | |
34 } | 14 } |
35 | |
36 /// Creates a temporary directory and passes its path to [fn]. Once [fn] | |
37 /// completes, the temporary directory and all its contents will be deleted. | |
38 /// | |
39 /// Returns the return value of [fn]. | |
40 dynamic withTempDir(fn(String path)) { | |
41 var tempDir = Directory.systemTemp.createTempSync('analyzer_').path; | |
42 try { | |
43 return fn(tempDir); | |
44 } finally { | |
45 new Directory(tempDir).deleteSync(recursive: true); | |
46 } | |
47 } | |
OLD | NEW |