| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 /// Helper application to run `closed_world2_test` on multiple files or | 5 /// Helper application to run `closed_world2_test` on multiple files or |
| 6 /// directories. | 6 /// directories. |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'package:args/args.dart'; |
| 11 import 'package:compiler/src/filenames.dart'; | 12 import 'package:compiler/src/filenames.dart'; |
| 12 import 'closed_world2_test.dart'; | 13 import 'closed_world2_test.dart'; |
| 13 | 14 |
| 15 const String ERROR_MARKER = '---'; |
| 16 |
| 14 main(List<String> args) async { | 17 main(List<String> args) async { |
| 15 for (String arg in args) { | 18 ArgParser parser = new ArgParser(); |
| 19 parser.addOption('log'); |
| 20 |
| 21 ArgResults argsResults = parser.parse(args); |
| 22 String logName = argsResults['log']; |
| 23 IOSink log; |
| 24 Map<String, Result> results; |
| 25 if (logName != null) { |
| 26 if (FileSystemEntity.isFileSync(logName)) { |
| 27 // Log a previous log file if it exists and use it to only test files that |
| 28 // previously failed. |
| 29 results = readLogResults(logName); |
| 30 } |
| 31 log = new File(logName).openWrite(); |
| 32 } |
| 33 |
| 34 if (results != null) { |
| 35 for (String fileName in results.keys) { |
| 36 Result result = results[fileName]; |
| 37 if (result.kind == ResultKind.failure) { |
| 38 if (FileSystemEntity.isFileSync(fileName)) { |
| 39 await testFile(new File(fileName), log); |
| 40 } else { |
| 41 print("$fileName doesn't exist"); |
| 42 } |
| 43 } else { |
| 44 log?.writeln('${fileName}: ${result.kind}'); |
| 45 } |
| 46 } |
| 47 } |
| 48 |
| 49 for (String arg in argsResults.rest) { |
| 16 String path = nativeToUriPath(arg); | 50 String path = nativeToUriPath(arg); |
| 17 if (FileSystemEntity.isDirectorySync(path)) { | 51 if (FileSystemEntity.isDirectorySync(path)) { |
| 18 Directory dir = new Directory(path); | 52 Directory dir = new Directory(path); |
| 19 for (FileSystemEntity file in dir.listSync(recursive: true)) { | 53 for (FileSystemEntity file in dir.listSync(recursive: true)) { |
| 20 if (file is File && file.path.endsWith('.dart')) { | 54 if (file is File && file.path.endsWith('.dart')) { |
| 21 await testFile(file); | 55 if (results == null || !results.containsKey(file.path)) { |
| 56 await testFile(file, log); |
| 57 } |
| 22 } | 58 } |
| 23 } | 59 } |
| 24 } else if (FileSystemEntity.isFileSync(path)) { | 60 } else if (FileSystemEntity.isFileSync(path)) { |
| 25 await testFile(new File(path)); | 61 if (results == null || !results.containsKey(path)) { |
| 62 await testFile(new File(path), log); |
| 63 } |
| 26 } else { | 64 } else { |
| 27 print("$arg doesn't exist"); | 65 print("$arg doesn't exist"); |
| 28 } | 66 } |
| 29 } | 67 } |
| 68 |
| 69 await log?.close(); |
| 30 } | 70 } |
| 31 | 71 |
| 32 Future testFile(File file) async { | 72 /// Read the log file in [logName] and returns the map from test file name to |
| 73 /// [Result] stored in the log file. |
| 74 Map<String, Result> readLogResults(String logName) { |
| 75 Map<String, Result> results = <String, Result>{}; |
| 76 String text = new File(logName).readAsStringSync(); |
| 77 // Make a backup of the log file. |
| 78 new File('$logName~').writeAsStringSync(text); |
| 79 List<String> lines = text.split('\n'); |
| 80 int index = 0; |
| 81 while (index < lines.length) { |
| 82 String line = lines[index]; |
| 83 int colonPos = line.lastIndexOf(':'); |
| 84 if (colonPos == -1) { |
| 85 if (!line.isEmpty) { |
| 86 print('Invalid log line @ $index: $line'); |
| 87 } |
| 88 } else { |
| 89 String fileName = line.substring(0, colonPos); |
| 90 String kindName = line.substring(colonPos + 1).trim(); |
| 91 ResultKind kind = |
| 92 ResultKind.values.firstWhere((kind) => '$kind' == kindName); |
| 93 String error; |
| 94 if (kind == ResultKind.failure) { |
| 95 assert(lines[index + 1] == ERROR_MARKER); |
| 96 index += 2; |
| 97 StringBuffer sb = new StringBuffer(); |
| 98 while (lines[index] != ERROR_MARKER) { |
| 99 sb.writeln(lines[index]); |
| 100 index++; |
| 101 } |
| 102 error = sb.toString(); |
| 103 } |
| 104 results[fileName] = new Result(kind, error); |
| 105 } |
| 106 index++; |
| 107 } |
| 108 return results; |
| 109 } |
| 110 |
| 111 Future testFile(File file, IOSink log) async { |
| 33 print('===================================================================='); | 112 print('===================================================================='); |
| 34 print('testing ${file.path}'); | 113 print('testing ${file.path}'); |
| 114 ResultKind kind; |
| 115 String error; |
| 35 try { | 116 try { |
| 36 await mainInternal([file.path], skipWarnings: true, skipErrors: true); | 117 kind = |
| 118 await mainInternal([file.path], skipWarnings: true, skipErrors: true); |
| 37 } catch (e, s) { | 119 } catch (e, s) { |
| 38 print('$e:\n$s'); | 120 kind = ResultKind.failure; |
| 121 error = '$e:\n$s'; |
| 122 print(error); |
| 39 } | 123 } |
| 124 log?.writeln('${file.path}: ${kind}'); |
| 125 if (error != null) { |
| 126 log?.writeln(ERROR_MARKER); |
| 127 log?.writeln(error); |
| 128 log?.writeln(ERROR_MARKER); |
| 129 } |
| 130 await log.flush(); |
| 40 } | 131 } |
| 132 |
| 133 class Result { |
| 134 final ResultKind kind; |
| 135 final String error; |
| 136 |
| 137 Result(this.kind, [this.error]); |
| 138 } |
| OLD | NEW |