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