OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
Brian Wilkerson
2017/04/24 20:26:47
We generally keep the copyright date from the old
| |
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 import 'dart:async'; | |
6 import 'dart:convert'; | |
7 import 'dart:io' show exitCode, stdin; | |
8 | |
9 import 'package:analyzer/error/error.dart'; | |
10 | |
11 typedef Future<ErrorSeverity> BatchRunnerHandler(List<String> args); | |
12 | |
13 /// Provides a framework to read command line options from stdin and feed them | |
14 /// to a callback. | |
15 class BatchRunner { | |
16 final outSink; | |
17 final errorSink; | |
18 | |
19 BatchRunner(this.outSink, this.errorSink); | |
20 | |
21 /// Run the tool in 'batch' mode, receiving command lines through stdin and | |
22 /// returning pass/fail status through stdout. This feature is intended for | |
23 /// use in unit testing. | |
24 void runAsBatch(List<String> sharedArgs, BatchRunnerHandler handler) { | |
25 outSink.writeln('>>> BATCH START'); | |
26 Stopwatch stopwatch = new Stopwatch(); | |
27 stopwatch.start(); | |
28 int testsFailed = 0; | |
29 int totalTests = 0; | |
30 ErrorSeverity batchResult = ErrorSeverity.NONE; | |
31 // Read line from stdin. | |
32 Stream cmdLine = | |
33 stdin.transform(UTF8.decoder).transform(new LineSplitter()); | |
34 cmdLine.listen((String line) async { | |
35 // Maybe finish. | |
36 if (line.isEmpty) { | |
37 var time = stopwatch.elapsedMilliseconds; | |
38 outSink.writeln( | |
39 '>>> BATCH END (${totalTests - testsFailed}/$totalTests) ${time}ms') ; | |
40 exitCode = batchResult.ordinal; | |
41 } | |
42 // Prepare arguments. | |
43 var lineArgs = line.split(new RegExp('\\s+')); | |
44 var args = new List<String>(); | |
45 args.addAll(sharedArgs); | |
46 args.addAll(lineArgs); | |
47 args.remove('-b'); | |
48 args.remove('--batch'); | |
49 // Analyze single set of arguments. | |
50 try { | |
51 totalTests++; | |
52 ErrorSeverity result = await handler(args); | |
53 bool resultPass = result != ErrorSeverity.ERROR; | |
54 if (!resultPass) { | |
55 testsFailed++; | |
56 } | |
57 batchResult = batchResult.max(result); | |
58 // Write stderr end token and flush. | |
59 errorSink.writeln('>>> EOF STDERR'); | |
60 String resultPassString = resultPass ? 'PASS' : 'FAIL'; | |
61 outSink.writeln( | |
62 '>>> TEST $resultPassString ${stopwatch.elapsedMilliseconds}ms'); | |
63 } catch (e, stackTrace) { | |
64 errorSink.writeln(e); | |
65 errorSink.writeln(stackTrace); | |
66 errorSink.writeln('>>> EOF STDERR'); | |
67 outSink.writeln('>>> TEST CRASH'); | |
68 } | |
69 }); | |
70 } | |
71 } | |
OLD | NEW |