diff options
Diffstat (limited to 'src/test/java/org/zwobble')
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java | 66 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java | 10 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/util/processes/ExecutionResult.java | 31 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/util/processes/Processes.java (renamed from src/test/java/org/zwobble/couscous/tests/backends/Processes.java) | 12 |
4 files changed, 89 insertions, 30 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java index 5c26bce..f2749ce 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.tests.backends.csharp; +import com.eclipsesource.json.Json; +import com.eclipsesource.json.JsonObject; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; import com.google.common.collect.Ordering; @@ -11,7 +13,8 @@ import org.zwobble.couscous.ast.TypeNode; import org.zwobble.couscous.backends.csharp.CsharpCodeGenerator; import org.zwobble.couscous.backends.csharp.CsharpSerializer; import org.zwobble.couscous.tests.MethodRunner; -import org.zwobble.couscous.tests.backends.Processes; +import org.zwobble.couscous.tests.util.processes.ExecutionResult; +import org.zwobble.couscous.tests.util.processes.Processes; import org.zwobble.couscous.types.ScalarType; import org.zwobble.couscous.types.Type; import org.zwobble.couscous.types.Types; @@ -19,6 +22,7 @@ import org.zwobble.couscous.util.ExtraLists; import org.zwobble.couscous.values.PrimitiveValue; import org.zwobble.couscous.values.PrimitiveValues; +import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -56,7 +60,7 @@ public class CsharpMethodRunner implements MethodRunner { } public static PrimitiveValue runFunction( - Path directoryPath, + Path workingDirectory, ScalarType className, String methodName, List<PrimitiveValue> arguments, @@ -89,12 +93,11 @@ public class CsharpMethodRunner implements MethodRunner { " " + body + " }" + "}"; - Files.write(directoryPath.resolve("MethodRunnerExample.cs"), list(program)); - String executablePath = compileFiles( - findCsharpFiles(directoryPath), - directoryPath + Files.write(workingDirectory.resolve("MethodRunnerExample.cs"), list(program)); + String output = executeSource( + findCsharpFiles(workingDirectory), + workingDirectory ); - String output = Processes.run(list("mono", executablePath)); if (isVoid) { return PrimitiveValues.UNIT; } else { @@ -102,22 +105,49 @@ public class CsharpMethodRunner implements MethodRunner { } } - private static String compileFiles(List<String> paths, Path directoryPath) throws IOException, InterruptedException { + private static String executeSource(List<String> paths, Path workingDirectory) throws IOException, InterruptedException { // TODO: find cache directory properly Path cacheDirectory = Paths.get(System.getProperty("user.home"), ".cache/couscous/tests/csharp"); String hash = hashFiles(paths); - Path cacheExecutable = cacheDirectory.resolve(hash); - if (!cacheExecutable.toFile().exists()) { + Path cacheResult = cacheDirectory.resolve(hash); + if (!cacheResult.toFile().exists()) { cacheDirectory.toFile().mkdirs(); - Processes.run( - ExtraLists.concat( - list("mcs", "-out:" + cacheExecutable.toString()), - paths - ), - directoryPath - ); + String executablePath = compileFiles(paths, workingDirectory); + ExecutionResult result = Processes.run(list("mono", executablePath)); + writeExecuteResult(cacheResult, result); } - return cacheExecutable.toString(); + ExecutionResult result = readExecutionResult(cacheResult); + result.assertSuccess(); + return result.getStdout(); + } + + private static ExecutionResult readExecutionResult(Path path) throws IOException { + JsonObject result = Json.parse(new FileReader(path.toFile())).asObject(); + return new ExecutionResult( + result.get("exitCode").asInt(), + result.get("stdout").asString(), + result.get("stderr").asString() + ); + } + + private static void writeExecuteResult(Path path, ExecutionResult result) throws IOException { + JsonObject json = Json.object() + .add("exitCode", result.getExitCode()) + .add("stdout", result.getStdout()) + .add("stderr", result.getStderr()); + Files.write(path, list(json.toString())); + } + + private static String compileFiles(List<String> paths, Path workingDirectory) throws IOException, InterruptedException { + Path executablePath = workingDirectory.resolve("Program.cs"); + Processes.run( + ExtraLists.concat( + list("mcs", "-out:" + executablePath), + paths + ), + workingDirectory + ); + return executablePath.toString(); } private static String hashFiles(List<String> paths) throws IOException { diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java index db0e385..87ece2d 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java @@ -3,6 +3,7 @@ package org.zwobble.couscous.tests.backends.python; import com.google.common.base.Joiner; import org.hamcrest.Matchers; import org.zwobble.couscous.ast.MethodSignature; +import org.zwobble.couscous.tests.util.processes.ExecutionResult; import org.zwobble.couscous.types.ScalarType; import org.zwobble.couscous.ast.TypeNode; import org.zwobble.couscous.types.Type; @@ -11,7 +12,7 @@ import org.zwobble.couscous.backends.python.PythonBackend; import org.zwobble.couscous.backends.python.PythonCodeGenerator; import org.zwobble.couscous.backends.python.PythonSerializer; import org.zwobble.couscous.tests.MethodRunner; -import org.zwobble.couscous.tests.backends.Processes; +import org.zwobble.couscous.tests.util.processes.Processes; import org.zwobble.couscous.values.PrimitiveValue; import java.io.IOException; @@ -62,8 +63,9 @@ public class PythonMethodRunner implements MethodRunner { String program = "from couscous." + className.getQualifiedName() + " import " + className.getSimpleName() + ";" + "value = " + className.getSimpleName() + "." + pythonMethodName + "(" + argumentsString + ");" + "print(type(value)); print(value)"; - String output = Processes.run(list("python3.4", "-c", program), directoryPath); - return readPrimitive(output); + ExecutionResult result = Processes.run(list("python3.4", "-c", program), directoryPath); + result.assertSuccess(); + return readPrimitive(result.getStdout()); } private static final String PACKAGE_PREFIX = "couscous."; @@ -89,4 +91,4 @@ public class PythonMethodRunner implements MethodRunner { throw new RuntimeException("Unhandled type: " + type); } } -}
\ No newline at end of file +} 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/backends/Processes.java b/src/test/java/org/zwobble/couscous/tests/util/processes/Processes.java index 7b139df..6229947 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/Processes.java +++ b/src/test/java/org/zwobble/couscous/tests/util/processes/Processes.java @@ -1,4 +1,4 @@ -package org.zwobble.couscous.tests.backends; +package org.zwobble.couscous.tests.util.processes; import com.google.common.base.Charsets; import com.google.common.io.CharStreams; @@ -10,11 +10,11 @@ import java.nio.file.Path; import java.util.List; public class Processes { - public static String run(List<String> arguments) throws IOException, InterruptedException { + public static ExecutionResult run(List<String> arguments) throws IOException, InterruptedException { return run(arguments, null); } - public static String run(List<String> arguments, Path directoryPath) throws IOException, InterruptedException { + 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(); @@ -22,11 +22,7 @@ public class Processes { int exitCode = process.waitFor(); String output = readString(process.getInputStream()).trim(); String stderrOutput = readString(process.getErrorStream()); - if (exitCode != 0) { - throw new RuntimeException("stderr was: " + stderrOutput); - } else { - return output; - } + return new ExecutionResult(exitCode, output, stderrOutput); } private static String readString(final InputStream stream) throws IOException { |
