diff options
6 files changed, 112 insertions, 1 deletions
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 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.zwobble.hobgoblin.examples</groupId> + <artifactId>example</artifactId> + <version>1.0-SNAPSHOT</version> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.source>25</maven.compiler.source> + <maven.compiler.target>25</maven.compiler.target> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <version>3.6.1</version> + <executions> + <execution> + <id>add-source</id> + <phase>generate-sources</phase> + <goals> + <goal>add-source</goal> + </goals> + <configuration> + <sources> + <source>src/gen/java</source> + </sources> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> 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<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); + } + } +} |
