OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of testrunner; | 5 part of testrunner; |
6 | 6 |
| 7 /** Create a file [fileName] and populate it with [contents]. */ |
| 8 void writeFile(String fileName, String contents) { |
| 9 var file = new File(fileName); |
| 10 file.writeAsStringSync(contents); |
| 11 } |
| 12 |
7 /** | 13 /** |
8 * Read the contents of a file [fileName] into a [List] of [String]s. | 14 * Read the contents of a file [fileName] into a [List] of [String]s. |
9 * If the file does not exist and [errorIfNoFile] is true, throw an | 15 * If the file does not exist and [errorIfNoFile] is true, throw an |
10 * exception, else return an empty list. | 16 * exception, else return an empty list. |
11 */ | 17 */ |
12 List<String> getFileContents(String filename, bool errorIfNoFile) { | 18 List<String> getFileContents(String filename, bool errorIfNoFile) { |
13 File f = new File(filename); | 19 File f = new File(filename); |
14 if (!f.existsSync()) { | 20 if (!f.existsSync()) { |
15 if (errorIfNoFile) { | 21 if (errorIfNoFile) { |
16 throw new Exception('Config file $filename not found.'); | 22 throw new Exception('Config file $filename not found.'); |
17 } else { | 23 } else { |
18 return new List(); | 24 return new List(); |
19 } | 25 } |
20 } | 26 } |
21 return f.readAsLinesSync(); | 27 return f.readAsLinesSync(); |
22 } | 28 } |
23 | 29 |
24 /** | 30 /** |
25 * Given a file path [path], make it absolute if it is relative, | 31 * Given a file path [path], make it absolute if it is relative, |
26 * and return the result. | 32 * and return the result. |
27 */ | 33 */ |
28 String makePathAbsolute(String path) { | 34 String makePathAbsolute(String path) { |
29 var p = new Path(path).canonicalize(); | 35 var p = new Path(path).canonicalize(); |
30 if (p.isAbsolute) { | 36 if (p.isAbsolute) { |
31 return p.toString(); | 37 return p.toNativePath(); |
32 } else { | 38 } else { |
33 var cwd = new Path((new Directory.current()).path); | 39 var cwd = new Path((new Directory.current()).path); |
34 return cwd.join(p).toString(); | 40 return cwd.join(p).toNativePath(); |
35 } | 41 } |
36 } | 42 } |
37 | 43 |
38 /** | 44 /** |
39 * Create the list of all the files in a set of directories | 45 * Create the list of all the files in a set of directories |
40 * ([dirs]) whose names match [filePat]. If [recurse] is true | 46 * ([dirs]) whose names match [filePat]. If [recurse] is true |
41 * look at subdirectories too. Once they have all been enumerated, | 47 * look at subdirectories too. An optional [excludePat] can be supplied |
42 * call [onComplete]. An optional [excludePat] can be supplied | |
43 * and files or directories that match that will be excluded. | 48 * and files or directories that match that will be excluded. |
| 49 * [includeSymLinks] controls whether or not to include files that |
| 50 * have symlinks in the traversed tree. |
44 */ | 51 */ |
45 // TODO(gram): The key thing here is we want to avoid package | 52 List buildFileList(List dirs, RegExp filePat, bool recurse, |
46 // directories, which have symlinks. excludePat was added for | |
47 // that but can't currently be used because the symlinked files | |
48 // have canonicalized paths. So instead we exploit that fact and | |
49 // assert that every file must have a prefix that matches the | |
50 // directory. If this changes then we will need to switch to using | |
51 // the exclude pattern or some other mechanism. | |
52 void buildFileList(List dirs, RegExp filePat, bool recurse, | |
53 Function onComplete, | |
54 [RegExp excludePat, bool includeSymLinks = false]) { | 53 [RegExp excludePat, bool includeSymLinks = false]) { |
55 var files = new List(); | 54 var files = new List(); |
56 var dirCount = 1; | |
57 for (var i = 0; i < dirs.length; i++) { | 55 for (var i = 0; i < dirs.length; i++) { |
58 var path = dirs[i]; | 56 var path = dirs[i]; |
59 if (excludePat != null && excludePat.hasMatch(path)) { | 57 if (excludePat != null && excludePat.hasMatch(path)) { |
60 continue; | 58 continue; |
61 } | 59 } |
62 // Is this a regular file? | 60 // Is this a regular file? |
63 File f = new File(path); | 61 File f = new File(path); |
64 if (f.existsSync()) { | 62 if (f.existsSync()) { |
65 if (filePat.hasMatch(path)) { | 63 if (filePat.hasMatch(path)) { |
66 files.add(path); | 64 files.add(path); |
67 } | 65 } |
68 } else { // Try treat it as a directory. | 66 } else { // Try treat it as a directory. |
69 path = makePathAbsolute(path); | 67 path = makePathAbsolute(path); |
70 Directory d = new Directory(path); | 68 Directory d = new Directory(path); |
71 if (d.existsSync()) { | 69 if (d.existsSync()) { |
72 ++dirCount; | 70 var contents = d.listSync(recursive: recurse, |
73 d.list(recursive: recurse).listen( | 71 followLinks: includeSymLinks); |
74 (entity) { | 72 for (var entity in contents) { |
75 if (entity is File) { | 73 if (entity is File) { |
76 var file = entity.name; | 74 var file = entity.path; |
77 if (filePat.hasMatch(file)) { | 75 if (filePat.hasMatch(file)) { |
78 if (excludePat == null || !excludePat.hasMatch(file)) { | 76 if (excludePat == null || !excludePat.hasMatch(file)) { |
79 if (includeSymLinks || file.startsWith(path)) { | 77 files.add(file); |
80 files.add(file); | |
81 } | |
82 } | |
83 } | |
84 } | 78 } |
85 }, | 79 } |
86 onDone: () { | 80 } |
87 if (complete && --dirCount == 0) { | 81 } |
88 onComplete(files); | |
89 } | |
90 }); | |
91 } else { // Does not exist. | 82 } else { // Does not exist. |
92 print('$path does not exist.'); | 83 print('$path does not exist.'); |
93 } | 84 } |
94 } | 85 } |
95 } | 86 } |
96 if (--dirCount == 0) { | 87 return files; |
97 onComplete(files); | |
98 } | |
99 } | 88 } |
100 | 89 |
101 /** | 90 /** |
102 * Get the directory that testrunner lives in; we need it to import | 91 * Get the directory that testrunner lives in; we need it to import |
103 * support files into the generated scripts. | 92 * support files into the generated scripts. |
104 */ | 93 */ |
105 | 94 |
106 String get runnerDirectory { | 95 String get runnerDirectory { |
107 var libDirectory = makePathAbsolute(new Options().script); | 96 var libDirectory = makePathAbsolute(new Options().script); |
108 return libDirectory.substring(0, | 97 return libDirectory.substring(0, |
109 libDirectory.lastIndexOf(Platform.pathSeparator)); | 98 libDirectory.lastIndexOf(Platform.pathSeparator)); |
110 } | 99 } |
| 100 |
| 101 /* |
| 102 * Run an external process [cmd] with command line arguments [args]. |
| 103 * Returns a [Future] for when the process terminates. |
| 104 */ |
| 105 Future _processHelper(String command, List<String> args, |
| 106 {String workingDir}) { |
| 107 var options = null; |
| 108 if (workingDir != null) { |
| 109 options = new ProcessOptions(); |
| 110 options.workingDirectory = workingDir; |
| 111 } |
| 112 return Process.run(command, args, options) |
| 113 .then((result) => result.exitCode) |
| 114 .catchError((e) { |
| 115 print("$command ${args.join(' ')}: ${e.toString()}"); |
| 116 }); |
| 117 } |
| 118 |
OLD | NEW |