diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-08-06 17:18:47 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-08-06 17:18:47 +0100 |
| commit | 31c83d816e104ecd27e2aed0bb3cd11ad16759f1 (patch) | |
| tree | 58d45060210d2efa17eab78ce3c8e96f968d1d61 /src/test/java/org/zwobble/couscous/tests/backends | |
| parent | 54cd15fbeb33ddcbed0b6635d2069a0380156e16 (diff) | |
Cache execution of C# as well as compilation
Diffstat (limited to 'src/test/java/org/zwobble/couscous/tests/backends')
3 files changed, 54 insertions, 57 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/backends/Processes.java b/src/test/java/org/zwobble/couscous/tests/backends/Processes.java deleted file mode 100644 index 7b139df..0000000 --- a/src/test/java/org/zwobble/couscous/tests/backends/Processes.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.zwobble.couscous.tests.backends; - -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 String run(List<String> arguments) throws IOException, InterruptedException { - return run(arguments, null); - } - - public static String 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()); - if (exitCode != 0) { - throw new RuntimeException("stderr was: " + stderrOutput); - } else { - return output; - } - } - - private static String readString(final InputStream stream) throws IOException { - return CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8)); - } -} 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 +} |
