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