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 analyzer_cli.test.utils; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'dart:mirrors'; |
| 9 |
| 10 import 'package:analyzer/analyzer.dart'; |
| 11 import 'package:path/path.dart' as pathos; |
| 12 |
| 13 /// Returns the string representation of the [AnalyzerErrorGroup] thrown when |
| 14 /// parsing [contents] as a Dart file. If [contents] doesn't throw any errors, |
| 15 /// this will return null. |
| 16 /// |
| 17 /// This replaces the filename in the error string with its basename, since the |
| 18 /// full path will vary from machine to machine. It also replaces the exception |
| 19 /// message with "..." to decouple these tests from the specific exception |
| 20 /// messages. |
| 21 String errorsForFile(String contents) { |
| 22 return withTempDir((temp) { |
| 23 var path = pathos.join(temp, 'test.dart'); |
| 24 new File(path).writeAsStringSync(contents); |
| 25 try { |
| 26 parseDartFile(path); |
| 27 } on AnalyzerErrorGroup catch (e) { |
| 28 return e.toString().replaceAllMapped( |
| 29 new RegExp(r"^(Error on line \d+ of )((?:[A-Z]+:)?[^:]+): .*$", |
| 30 multiLine: true), |
| 31 (match) => match[1] + pathos.basename(match[2]) + ': ...'); |
| 32 } |
| 33 return null; |
| 34 }); |
| 35 } |
| 36 |
| 37 /// Creates a temporary directory and passes its path to [fn]. Once [fn] |
| 38 /// completes, the temporary directory and all its contents will be deleted. |
| 39 /// |
| 40 /// Returns the return value of [fn]. |
| 41 dynamic withTempDir(fn(String path)) { |
| 42 var tempDir = Directory.systemTemp.createTempSync('analyzer_').path; |
| 43 try { |
| 44 return fn(tempDir); |
| 45 } finally { |
| 46 new Directory(tempDir).deleteSync(recursive: true); |
| 47 } |
| 48 } |
| 49 |
| 50 /// Gets the test directory in a way that works with |
| 51 /// package:test and package:unittest. |
| 52 /// See <https://github.com/dart-lang/test/issues/110> for more info. |
| 53 final String testDirectory = pathos.dirname( |
| 54 pathos.fromUri((reflectClass(_TestUtils).owner as LibraryMirror).uri)); |
| 55 |
| 56 class _TestUtils {} |
OLD | NEW |