Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Unified Diff: pkg/testing/lib/src/stdio_process.dart

Issue 2624373003: Move package:testing to SDK. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/testing/lib/src/run_tests.dart ('k') | pkg/testing/lib/src/suite.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/testing/lib/src/stdio_process.dart
diff --git a/pkg/testing/lib/src/stdio_process.dart b/pkg/testing/lib/src/stdio_process.dart
new file mode 100644
index 0000000000000000000000000000000000000000..e45c6e2622561683076315587c1cf30cd1019cf3
--- /dev/null
+++ b/pkg/testing/lib/src/stdio_process.dart
@@ -0,0 +1,70 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE.md file.
+
+library testing.stdio_process;
+
+import 'dart:async' show
+ Future,
+ Timer;
+
+import 'dart:convert' show
+ UTF8;
+
+import 'dart:io' show
+ Process,
+ ProcessSignal;
+
+import 'chain.dart' show
+ Result;
+
+class StdioProcess {
+ final int exitCode;
+
+ final String output;
+
+ StdioProcess(this.exitCode, this.output);
+
+ Result<int> toResult({int expected: 0}) {
+ if (exitCode == expected) {
+ return new Result<int>.pass(exitCode);
+ } else {
+ return new Result<int>.fail(exitCode, output);
+ }
+ }
+
+ static Future<StdioProcess> run(
+ String executable, List<String> arguments,
+ {String input, Duration timeout: const Duration(seconds: 60)}) async {
+ Process process = await Process.start(executable, arguments);
+ Timer timer;
+ StringBuffer sb = new StringBuffer();
+ timer = new Timer(timeout, () {
+ sb.write("Process timed out: ");
+ sb.write(executable);
+ sb.write(" ");
+ sb.writeAll(arguments, " ");
+ sb.writeln();
+ sb.writeln("Sending SIGTERM to process");
+ process.kill();
+ timer = new Timer(const Duration(seconds: 10), () {
+ sb.writeln("Sending SIGKILL to process");
+ process.kill(ProcessSignal.SIGKILL);
+ });
+ });
+ if (input != null) {
+ process.stdin.write(input);
+ }
+ Future closeFuture = process.stdin.close();
+ Future<List<String>> stdoutFuture =
+ process.stdout.transform(UTF8.decoder).toList();
+ Future<List<String>> stderrFuture =
+ process.stderr.transform(UTF8.decoder).toList();
+ int exitCode = await process.exitCode;
+ timer.cancel();
+ sb.writeAll(await stdoutFuture);
+ sb.writeAll(await stderrFuture);
+ await closeFuture;
+ return new StdioProcess(exitCode, "$sb");
+ }
+}
« no previous file with comments | « pkg/testing/lib/src/run_tests.dart ('k') | pkg/testing/lib/src/suite.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698