diff options
Diffstat (limited to 'src/test/java')
3 files changed, 64 insertions, 1 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java index e892cd9..8e68c09 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java @@ -2,6 +2,7 @@ package org.zwobble.hobgoblin.compiler; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; +import org.zwobble.hobgoblin.compiler.testing.TestProcessRunner; import java.io.IOException; import java.util.List; @@ -17,7 +18,13 @@ public class ExampleTests { )); } - private void runTest(ExampleSet exampleSet) throws IOException { + private void runTest(ExampleSet exampleSet) throws IOException, InterruptedException { HobgoblinCompiler.compile(exampleSet.path(), List.of()); + + var javaProjectPath = exampleSet.path().resolve("output/java"); + TestProcessRunner.command(List.of("mvn", "compile")) + .cwd(javaProjectPath) + .runCaptureOutput() + .throwOnError(); } } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/testing/ExecutionResult.java b/src/test/java/org/zwobble/hobgoblin/compiler/testing/ExecutionResult.java new file mode 100644 index 0000000..553e41b --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/testing/ExecutionResult.java @@ -0,0 +1,11 @@ +package org.zwobble.hobgoblin.compiler.testing; + +public record ExecutionResult(int exitCode, String stdout, String stderr) { + public ExecutionResult throwOnError() { + if (exitCode == 0) { + return this; + } else { + throw new AssertionError("exit code was " + exitCode + "\nstdout:\n" + stdout + "\nstderr:\n" + stderr); + } + } +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/testing/TestProcessRunner.java b/src/test/java/org/zwobble/hobgoblin/compiler/testing/TestProcessRunner.java new file mode 100644 index 0000000..f4074f8 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/testing/TestProcessRunner.java @@ -0,0 +1,45 @@ +package org.zwobble.hobgoblin.compiler.testing; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; + +public class TestProcessRunner { + public static TestProcessRunner command(List<String> command) { + return new TestProcessRunner( + new ProcessBuilder().command(command) + ); + } + + private final ProcessBuilder processBuilder; + + private TestProcessRunner(ProcessBuilder processBuilder) { + this.processBuilder = processBuilder; + } + + public TestProcessRunner cwd(Path cwd) { + this.processBuilder.directory(cwd.toFile()); + return this; + } + + /** + * Run a process until it exits, capturing its stdout and stderr. + */ + public ExecutionResult runCaptureOutput() throws IOException, InterruptedException { + var process = this.processBuilder.start(); + + var exitCode = process.waitFor(); + + try ( + var stdoutReader = process.inputReader(StandardCharsets.UTF_8); + var stderrReader = process.errorReader(StandardCharsets.UTF_8) + ) { + var stdout = stdoutReader.lines().collect(Collectors.joining("\n")); + var stderr = stderrReader.lines().collect(Collectors.joining("\n")); + + return new ExecutionResult(exitCode, stdout, stderr); + } + } +} |
