Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Side by Side Diff: pkg/analyzer_experimental/test/utils.dart

Issue 14998006: Add some user-friendly utility functions to pkg/analyzer_experimental. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library utils;
6
7 import 'dart:io';
8
9 import 'package:analyzer_experimental/analyzer.dart';
10 import 'package:pathos/path.dart' as pathos;
11
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(
28 new RegExp(r"^(Error on line \d+ of )([^:]+): .*$", multiLine: true),
29 (match) => match[1] + pathos.basename(match[2]) + ': ...');
30 }
31 return null;
32 });
33 }
34
35 /// Creates a temporary directory and passes its path to [fn]. Once [fn]
36 /// completes, the temporary directory and all its contents will be deleted.
37 ///
38 /// Returns the return value of [fn].
39 dynamic withTempDir(fn(String path)) {
40 var tempDir = new Directory('').createTempSync().path;
scheglov 2013/05/09 01:26:46 Just curious - why not store Directory instance in
41 try {
42 return fn(tempDir);
43 } finally {
44 new Directory(tempDir).deleteSync(recursive: true);
45 }
46 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698