From 8d3923b4371580b48c3c6668c828a7dd63e61255 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 26 Apr 2026 17:24:24 +0100 Subject: Add stubbed java output example --- examples/01-struct/output/java/.gitignore | 2 + examples/01-struct/output/java/pom.xml | 39 +++++++++++++++++++ .../src/main/java/org/zwobble/example/Main.java | 7 ++++ .../zwobble/hobgoblin/compiler/ExampleTests.java | 9 ++++- .../compiler/testing/ExecutionResult.java | 11 ++++++ .../compiler/testing/TestProcessRunner.java | 45 ++++++++++++++++++++++ 6 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 examples/01-struct/output/java/.gitignore create mode 100644 examples/01-struct/output/java/pom.xml create mode 100644 examples/01-struct/output/java/src/main/java/org/zwobble/example/Main.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/testing/ExecutionResult.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/testing/TestProcessRunner.java diff --git a/examples/01-struct/output/java/.gitignore b/examples/01-struct/output/java/.gitignore new file mode 100644 index 0000000..ff511d4 --- /dev/null +++ b/examples/01-struct/output/java/.gitignore @@ -0,0 +1,2 @@ +/src/gen/ +/target/ diff --git a/examples/01-struct/output/java/pom.xml b/examples/01-struct/output/java/pom.xml new file mode 100644 index 0000000..7493667 --- /dev/null +++ b/examples/01-struct/output/java/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + + org.zwobble.hobgoblin.examples + example + 1.0-SNAPSHOT + + + UTF-8 + 25 + 25 + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.6.1 + + + add-source + generate-sources + + add-source + + + + src/gen/java + + + + + + + + diff --git a/examples/01-struct/output/java/src/main/java/org/zwobble/example/Main.java b/examples/01-struct/output/java/src/main/java/org/zwobble/example/Main.java new file mode 100644 index 0000000..5e0e111 --- /dev/null +++ b/examples/01-struct/output/java/src/main/java/org/zwobble/example/Main.java @@ -0,0 +1,7 @@ +package org.zwobble.example; + +public class Main { + public static void main() { + + } +} 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 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); + } + } +} -- cgit v1.2.3