aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/couscous/tests/util/processes
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-08-06 17:18:47 +0100
committerMichael Williamson <mike@zwobble.org>2016-08-06 17:18:47 +0100
commit31c83d816e104ecd27e2aed0bb3cd11ad16759f1 (patch)
tree58d45060210d2efa17eab78ce3c8e96f968d1d61 /src/test/java/org/zwobble/couscous/tests/util/processes
parent54cd15fbeb33ddcbed0b6635d2069a0380156e16 (diff)
Cache execution of C# as well as compilation
Diffstat (limited to 'src/test/java/org/zwobble/couscous/tests/util/processes')
-rw-r--r--src/test/java/org/zwobble/couscous/tests/util/processes/ExecutionResult.java31
-rw-r--r--src/test/java/org/zwobble/couscous/tests/util/processes/Processes.java31
2 files changed, 62 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/util/processes/ExecutionResult.java b/src/test/java/org/zwobble/couscous/tests/util/processes/ExecutionResult.java
new file mode 100644
index 0000000..c662055
--- /dev/null
+++ b/src/test/java/org/zwobble/couscous/tests/util/processes/ExecutionResult.java
@@ -0,0 +1,31 @@
+package org.zwobble.couscous.tests.util.processes;
+
+public class ExecutionResult {
+ private final int exitCode;
+ private final String stdout;
+ private final String stderr;
+
+ public ExecutionResult(int exitCode, String stdout, String stderr) {
+ this.exitCode = exitCode;
+ this.stdout = stdout;
+ this.stderr = stderr;
+ }
+
+ public int getExitCode() {
+ return exitCode;
+ }
+
+ public String getStdout() {
+ return stdout;
+ }
+
+ public String getStderr() {
+ return stderr;
+ }
+
+ public void assertSuccess() {
+ if (exitCode != 0) {
+ throw new RuntimeException("stderr was: " + stderr);
+ }
+ }
+}
diff --git a/src/test/java/org/zwobble/couscous/tests/util/processes/Processes.java b/src/test/java/org/zwobble/couscous/tests/util/processes/Processes.java
new file mode 100644
index 0000000..6229947
--- /dev/null
+++ b/src/test/java/org/zwobble/couscous/tests/util/processes/Processes.java
@@ -0,0 +1,31 @@
+package org.zwobble.couscous.tests.util.processes;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.CharStreams;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.file.Path;
+import java.util.List;
+
+public class Processes {
+ public static ExecutionResult run(List<String> arguments) throws IOException, InterruptedException {
+ return run(arguments, null);
+ }
+
+ public static ExecutionResult run(List<String> arguments, Path directoryPath) throws IOException, InterruptedException {
+ Process process = new ProcessBuilder(arguments.toArray(new String[arguments.size()]))
+ .directory(directoryPath == null ? null : directoryPath.toFile())
+ .start();
+
+ int exitCode = process.waitFor();
+ String output = readString(process.getInputStream()).trim();
+ String stderrOutput = readString(process.getErrorStream());
+ return new ExecutionResult(exitCode, output, stderrOutput);
+ }
+
+ private static String readString(final InputStream stream) throws IOException {
+ return CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
+ }
+}