Search in sources :

Example 16 with StreamVacuum

use of org.antlr.v4.test.runtime.StreamVacuum in project antlr4 by antlr.

the class BasePythonTest method execModule.

public String execModule(String fileName) {
    String pythonPath = locatePython();
    String runtimePath = locateRuntime();
    File tmpdirFile = new File(getTempDirPath());
    String modulePath = new File(tmpdirFile, fileName).getAbsolutePath();
    String inputPath = new File(tmpdirFile, "input").getAbsolutePath();
    Path outputPath = tmpdirFile.toPath().resolve("output").toAbsolutePath();
    try {
        ProcessBuilder builder = new ProcessBuilder(pythonPath, modulePath, inputPath, outputPath.toString());
        builder.environment().put("PYTHONPATH", runtimePath);
        builder.environment().put("PYTHONIOENCODING", "utf-8");
        builder.directory(tmpdirFile);
        Process process = builder.start();
        StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
        stderrVacuum.start();
        process.waitFor();
        stderrVacuum.join();
        String output = TestOutputReading.read(outputPath);
        if (stderrVacuum.toString().length() > 0) {
            setParseErrors(stderrVacuum.toString());
        }
        return output;
    } catch (Exception e) {
        System.err.println("can't exec recognizer");
        e.printStackTrace(System.err);
    }
    return null;
}
Also used : Path(java.nio.file.Path) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Aggregations

BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)12 File (java.io.File)11 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)11 IOException (java.io.IOException)7 StreamVacuum (org.antlr.v4.test.runtime.StreamVacuum)5 Path (java.nio.file.Path)3 STGroupString (org.stringtemplate.v4.STGroupString)3 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 PrintStream (java.io.PrintStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 URLClassLoader (java.net.URLClassLoader)1 Pair (org.antlr.v4.runtime.misc.Pair)1 BaseRuntimeTest.readFile (org.antlr.v4.test.runtime.BaseRuntimeTest.readFile)1