OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 library testing.stdio_process; | 5 library testing.stdio_process; |
6 | 6 |
7 import 'dart:async' show | 7 import 'dart:async' show |
| 8 EventSink, |
8 Future, | 9 Future, |
| 10 Stream, |
| 11 StreamTransformer, |
9 Timer; | 12 Timer; |
10 | 13 |
11 import 'dart:convert' show | 14 import 'dart:convert' show |
12 UTF8; | 15 UTF8; |
13 | 16 |
14 import 'dart:io' show | 17 import 'dart:io' show |
15 Process, | 18 Process, |
16 ProcessSignal; | 19 ProcessSignal, |
| 20 Stdout; |
| 21 |
| 22 import 'dart:io' as io show |
| 23 stderr, |
| 24 stdout; |
17 | 25 |
18 import 'chain.dart' show | 26 import 'chain.dart' show |
19 Result; | 27 Result; |
20 | 28 |
21 class StdioProcess { | 29 class StdioProcess { |
22 final int exitCode; | 30 final int exitCode; |
23 | 31 |
24 final String output; | 32 final String output; |
25 | 33 |
26 StdioProcess(this.exitCode, this.output); | 34 StdioProcess(this.exitCode, this.output); |
27 | 35 |
28 Result<int> toResult({int expected: 0}) { | 36 Result<int> toResult({int expected: 0}) { |
29 if (exitCode == expected) { | 37 if (exitCode == expected) { |
30 return new Result<int>.pass(exitCode); | 38 return new Result<int>.pass(exitCode); |
31 } else { | 39 } else { |
32 return new Result<int>.fail(exitCode, output); | 40 return new Result<int>.fail(exitCode, output); |
33 } | 41 } |
34 } | 42 } |
35 | 43 |
| 44 static StreamTransformer<String, String> transformToStdio(Stdout stdio) { |
| 45 return new StreamTransformer<String, String>.fromHandlers( |
| 46 handleData: (String data, EventSink<String> sink) { |
| 47 sink.add(data); |
| 48 stdio.write(data); |
| 49 }); |
| 50 } |
| 51 |
36 static Future<StdioProcess> run( | 52 static Future<StdioProcess> run( |
37 String executable, List<String> arguments, | 53 String executable, List<String> arguments, |
38 {String input, Duration timeout: const Duration(seconds: 60)}) async { | 54 {String input, Duration timeout: const Duration(seconds: 60), |
| 55 bool suppressOutput: true}) async { |
39 Process process = await Process.start(executable, arguments); | 56 Process process = await Process.start(executable, arguments); |
40 Timer timer; | 57 Timer timer; |
41 StringBuffer sb = new StringBuffer(); | 58 StringBuffer sb = new StringBuffer(); |
42 timer = new Timer(timeout, () { | 59 if (timeout != null) { |
43 sb.write("Process timed out: "); | 60 timer = new Timer(timeout, () { |
44 sb.write(executable); | 61 sb.write("Process timed out: "); |
45 sb.write(" "); | 62 sb.write(executable); |
46 sb.writeAll(arguments, " "); | 63 sb.write(" "); |
47 sb.writeln(); | 64 sb.writeAll(arguments, " "); |
48 sb.writeln("Sending SIGTERM to process"); | 65 sb.writeln(); |
49 process.kill(); | 66 sb.writeln("Sending SIGTERM to process"); |
50 timer = new Timer(const Duration(seconds: 10), () { | 67 process.kill(); |
51 sb.writeln("Sending SIGKILL to process"); | 68 timer = new Timer(const Duration(seconds: 10), () { |
52 process.kill(ProcessSignal.SIGKILL); | 69 sb.writeln("Sending SIGKILL to process"); |
| 70 process.kill(ProcessSignal.SIGKILL); |
| 71 }); |
53 }); | 72 }); |
54 }); | 73 } |
55 if (input != null) { | 74 if (input != null) { |
56 process.stdin.write(input); | 75 process.stdin.write(input); |
57 await process.stdin.flush(); | 76 await process.stdin.flush(); |
58 } | 77 } |
59 Future closeFuture = process.stdin.close(); | 78 Future closeFuture = process.stdin.close(); |
60 Future<List<String>> stdoutFuture = | 79 Stream stdoutStream = process.stdout.transform(UTF8.decoder); |
61 process.stdout.transform(UTF8.decoder).toList(); | 80 Stream stderrStream = process.stderr.transform(UTF8.decoder); |
62 Future<List<String>> stderrFuture = | 81 if (!suppressOutput) { |
63 process.stderr.transform(UTF8.decoder).toList(); | 82 stdoutStream = stdoutStream.transform(transformToStdio(io.stdout)); |
| 83 stderrStream = stderrStream.transform(transformToStdio(io.stderr)); |
| 84 } |
| 85 Future<List<String>> stdoutFuture = stdoutStream.toList(); |
| 86 Future<List<String>> stderrFuture = stderrStream.toList(); |
64 int exitCode = await process.exitCode; | 87 int exitCode = await process.exitCode; |
65 timer.cancel(); | 88 timer?.cancel(); |
66 sb.writeAll(await stdoutFuture); | 89 sb.writeAll(await stdoutFuture); |
67 sb.writeAll(await stderrFuture); | 90 sb.writeAll(await stderrFuture); |
68 await closeFuture; | 91 await closeFuture; |
69 return new StdioProcess(exitCode, "$sb"); | 92 return new StdioProcess(exitCode, "$sb"); |
70 } | 93 } |
71 } | 94 } |
OLD | NEW |