summaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/hobgoblin/compiler/testing/TestProcessRunner.java
blob: 73117a1b9191501dea60aca565d9da8c9238ff55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package org.zwobble.hobgoblin.compiler.testing;

import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;

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;
    }

    public TestProcessRunner addEnvVar(String name, String value) {
        this.processBuilder.environment().put(name, value);
        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 stdout = ThreadReader.start(process.inputReader(StandardCharsets.UTF_8));
        var stderr = ThreadReader.start(process.errorReader(StandardCharsets.UTF_8));
        var exitCode = process.waitFor();

        return new ExecutionResult(exitCode, stdout.waitForString(), stderr.waitForString());
    }

    private static class ThreadReader {
        private static class ReadResult {
            String output;
            IOException exception;
        }

        public static ThreadReader start(Reader reader) {
            var lock = new Object();
            var readResult = new ReadResult();
            Thread.startVirtualThread(() -> {
                synchronized (lock) {
                    try (reader) {
                        readResult.output = reader.readAllAsString();
                    } catch (IOException exception) {
                        readResult.exception = exception;
                    }
                }
            });

            return new ThreadReader(lock, readResult);
        }

        private final Object lock;
        private final ReadResult readResult;

        public ThreadReader(Object lock, ReadResult readResult) {
            this.lock = lock;
            this.readResult = readResult;
        }

        public String waitForString() throws IOException {
            synchronized (this.lock) {
                if (this.readResult.exception != null) {
                    throw this.readResult.exception;
                } else {
                    return this.readResult.output;
                }
            }
        }
    }
}