OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 |
| 5 library testing.stdio_process; |
| 6 |
| 7 import 'dart:async' show |
| 8 Future, |
| 9 Timer; |
| 10 |
| 11 import 'dart:convert' show |
| 12 UTF8; |
| 13 |
| 14 import 'dart:io' show |
| 15 Process, |
| 16 ProcessSignal; |
| 17 |
| 18 import 'chain.dart' show |
| 19 Result; |
| 20 |
| 21 class StdioProcess { |
| 22 final int exitCode; |
| 23 |
| 24 final String output; |
| 25 |
| 26 StdioProcess(this.exitCode, this.output); |
| 27 |
| 28 Result<int> toResult({int expected: 0}) { |
| 29 if (exitCode == expected) { |
| 30 return new Result<int>.pass(exitCode); |
| 31 } else { |
| 32 return new Result<int>.fail(exitCode, output); |
| 33 } |
| 34 } |
| 35 |
| 36 static Future<StdioProcess> run( |
| 37 String executable, List<String> arguments, |
| 38 {String input, Duration timeout: const Duration(seconds: 60)}) async { |
| 39 Process process = await Process.start(executable, arguments); |
| 40 Timer timer; |
| 41 StringBuffer sb = new StringBuffer(); |
| 42 timer = new Timer(timeout, () { |
| 43 sb.write("Process timed out: "); |
| 44 sb.write(executable); |
| 45 sb.write(" "); |
| 46 sb.writeAll(arguments, " "); |
| 47 sb.writeln(); |
| 48 sb.writeln("Sending SIGTERM to process"); |
| 49 process.kill(); |
| 50 timer = new Timer(const Duration(seconds: 10), () { |
| 51 sb.writeln("Sending SIGKILL to process"); |
| 52 process.kill(ProcessSignal.SIGKILL); |
| 53 }); |
| 54 }); |
| 55 if (input != null) { |
| 56 process.stdin.write(input); |
| 57 } |
| 58 Future closeFuture = process.stdin.close(); |
| 59 Future<List<String>> stdoutFuture = |
| 60 process.stdout.transform(UTF8.decoder).toList(); |
| 61 Future<List<String>> stderrFuture = |
| 62 process.stderr.transform(UTF8.decoder).toList(); |
| 63 int exitCode = await process.exitCode; |
| 64 timer.cancel(); |
| 65 sb.writeAll(await stdoutFuture); |
| 66 sb.writeAll(await stderrFuture); |
| 67 await closeFuture; |
| 68 return new StdioProcess(exitCode, "$sb"); |
| 69 } |
| 70 } |
OLD | NEW |