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 EventSink, Future, Stream, StreamTransformer, Timer; |
8 EventSink, | |
9 Future, | |
10 Stream, | |
11 StreamTransformer, | |
12 Timer; | |
13 | 8 |
14 import 'dart:convert' show | 9 import 'dart:convert' show UTF8; |
15 UTF8; | |
16 | 10 |
17 import 'dart:io' show | 11 import 'dart:io' show Process, ProcessSignal, Stdout; |
18 Process, | |
19 ProcessSignal, | |
20 Stdout; | |
21 | 12 |
22 import 'dart:io' as io show | 13 import 'dart:io' as io show stderr, stdout; |
23 stderr, | |
24 stdout; | |
25 | 14 |
26 import 'chain.dart' show | 15 import 'chain.dart' show Result; |
27 Result; | |
28 | 16 |
29 class StdioProcess { | 17 class StdioProcess { |
30 final int exitCode; | 18 final int exitCode; |
31 | 19 |
32 final String output; | 20 final String output; |
33 | 21 |
34 StdioProcess(this.exitCode, this.output); | 22 StdioProcess(this.exitCode, this.output); |
35 | 23 |
36 Result<int> toResult({int expected: 0}) { | 24 Result<int> toResult({int expected: 0}) { |
37 if (exitCode == expected) { | 25 if (exitCode == expected) { |
38 return new Result<int>.pass(exitCode); | 26 return new Result<int>.pass(exitCode); |
39 } else { | 27 } else { |
40 return new Result<int>.fail(exitCode, output); | 28 return new Result<int>.fail(exitCode, output); |
41 } | 29 } |
42 } | 30 } |
43 | 31 |
44 static StreamTransformer<String, String> transformToStdio(Stdout stdio) { | 32 static StreamTransformer<String, String> transformToStdio(Stdout stdio) { |
45 return new StreamTransformer<String, String>.fromHandlers( | 33 return new StreamTransformer<String, String>.fromHandlers( |
46 handleData: (String data, EventSink<String> sink) { | 34 handleData: (String data, EventSink<String> sink) { |
47 sink.add(data); | 35 sink.add(data); |
48 stdio.write(data); | 36 stdio.write(data); |
49 }); | 37 }); |
50 } | 38 } |
51 | 39 |
52 static Future<StdioProcess> run( | 40 static Future<StdioProcess> run(String executable, List<String> arguments, |
53 String executable, List<String> arguments, | 41 {String input, |
54 {String input, Duration timeout: const Duration(seconds: 60), | 42 Duration timeout: const Duration(seconds: 60), |
55 bool suppressOutput: true}) async { | 43 bool suppressOutput: true}) async { |
56 Process process = await Process.start(executable, arguments); | 44 Process process = await Process.start(executable, arguments); |
57 Timer timer; | 45 Timer timer; |
58 StringBuffer sb = new StringBuffer(); | 46 StringBuffer sb = new StringBuffer(); |
59 if (timeout != null) { | 47 if (timeout != null) { |
60 timer = new Timer(timeout, () { | 48 timer = new Timer(timeout, () { |
61 sb.write("Process timed out: "); | 49 sb.write("Process timed out: "); |
62 sb.write(executable); | 50 sb.write(executable); |
63 sb.write(" "); | 51 sb.write(" "); |
64 sb.writeAll(arguments, " "); | 52 sb.writeAll(arguments, " "); |
65 sb.writeln(); | 53 sb.writeln(); |
(...skipping 19 matching lines...) Expand all Loading... |
85 Future<List<String>> stdoutFuture = stdoutStream.toList(); | 73 Future<List<String>> stdoutFuture = stdoutStream.toList(); |
86 Future<List<String>> stderrFuture = stderrStream.toList(); | 74 Future<List<String>> stderrFuture = stderrStream.toList(); |
87 int exitCode = await process.exitCode; | 75 int exitCode = await process.exitCode; |
88 timer?.cancel(); | 76 timer?.cancel(); |
89 sb.writeAll(await stdoutFuture); | 77 sb.writeAll(await stdoutFuture); |
90 sb.writeAll(await stderrFuture); | 78 sb.writeAll(await stderrFuture); |
91 await closeFuture; | 79 await closeFuture; |
92 return new StdioProcess(exitCode, "$sb"); | 80 return new StdioProcess(exitCode, "$sb"); |
93 } | 81 } |
94 } | 82 } |
OLD | NEW |