Search in sources :

Example 11 with StreamVacuum

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

the class BaseSwiftTest method execTest.

private String execTest() {
    try {
        String exec = tmpdir + "/" + EXEC_NAME;
        String[] args = //new File(tmpdir, "input").getAbsolutePath()
        new String[] { exec, "input" };
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.directory(new File(tmpdir));
        Process p = pb.start();
        StreamVacuum stdoutVacuum = new StreamVacuum(p.getInputStream());
        StreamVacuum stderrVacuum = new StreamVacuum(p.getErrorStream());
        stdoutVacuum.start();
        stderrVacuum.start();
        p.waitFor();
        stdoutVacuum.join();
        stderrVacuum.join();
        String output = stdoutVacuum.toString();
        if (output.length() == 0) {
            output = null;
        }
        if (stderrVacuum.toString().length() > 0) {
            this.stderrDuringParse = stderrVacuum.toString();
        }
        return output;
    } catch (Exception e) {
        System.err.println("can't exec recognizer");
        e.printStackTrace(System.err);
    }
    return null;
}
Also used : StreamVacuum(org.antlr.v4.test.runtime.StreamVacuum) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) IOException(java.io.IOException)

Example 12 with StreamVacuum

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

the class BaseCSharpTest method runProcess.

private boolean runProcess(String[] args, String path, int retries) throws Exception {
    ProcessBuilder pb = new ProcessBuilder(args);
    pb.directory(new File(path));
    Process process = pb.start();
    StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
    StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
    stdoutVacuum.start();
    stderrVacuum.start();
    process.waitFor();
    stdoutVacuum.join();
    stderrVacuum.join();
    int exitValue = process.exitValue();
    boolean success = (exitValue == 0);
    if (!success) {
        setParseErrors(stderrVacuum.toString());
        System.err.println("runProcess command: " + Utils.join(args, " "));
        System.err.println("runProcess exitValue: " + exitValue);
        System.err.println("runProcess stdoutVacuum: " + stdoutVacuum.toString());
        System.err.println("runProcess stderrVacuum: " + getParseErrors());
    }
    if (exitValue == 132) {
        // macOS (issue #2078).
        if (retries < 3) {
            System.err.println("runProcess retrying; " + retries + " retries so far");
            return runProcess(args, path, retries + 1);
        } else {
            System.err.println("runProcess giving up after " + retries + " retries");
            return false;
        }
    }
    return success;
}
Also used : File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 13 with StreamVacuum

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

the class BaseJavaTest method execClass.

public String execClass(String className) {
    if (TEST_IN_SAME_PROCESS) {
        try {
            ClassLoader loader = new URLClassLoader(new URL[] { getTempTestDir().toURI().toURL() }, ClassLoader.getSystemClassLoader());
            final Class<?> mainClass = (Class<?>) loader.loadClass(className);
            final Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
            PipedInputStream stdoutIn = new PipedInputStream();
            PipedInputStream stderrIn = new PipedInputStream();
            PipedOutputStream stdoutOut = new PipedOutputStream(stdoutIn);
            PipedOutputStream stderrOut = new PipedOutputStream(stderrIn);
            StreamVacuum stdoutVacuum = new StreamVacuum(stdoutIn);
            StreamVacuum stderrVacuum = new StreamVacuum(stderrIn);
            PrintStream originalOut = System.out;
            System.setOut(new PrintStream(stdoutOut));
            try {
                PrintStream originalErr = System.err;
                try {
                    System.setErr(new PrintStream(stderrOut));
                    stdoutVacuum.start();
                    stderrVacuum.start();
                    mainMethod.invoke(null, (Object) new String[] { new File(getTempTestDir(), "input").getAbsolutePath() });
                } finally {
                    System.setErr(originalErr);
                }
            } finally {
                System.setOut(originalOut);
            }
            stdoutOut.close();
            stderrOut.close();
            stdoutVacuum.join();
            stderrVacuum.join();
            String output = stdoutVacuum.toString();
            if (output.length() == 0) {
                output = null;
            }
            if (stderrVacuum.toString().length() > 0) {
                setParseErrors(stderrVacuum.toString());
            }
            return output;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    try {
        String[] args = new String[] { "java", "-classpath", getTempDirPath() + PATH_SEP + CLASSPATH, "-Dfile.encoding=UTF-8", className, new File(getTempTestDir(), "input").getAbsolutePath() };
        // String cmdLine = Utils.join(args, " ");
        // System.err.println("execParser: "+cmdLine);
        Process process = Runtime.getRuntime().exec(args, null, getTempTestDir());
        StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
        StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
        stdoutVacuum.start();
        stderrVacuum.start();
        process.waitFor();
        stdoutVacuum.join();
        stderrVacuum.join();
        String output = stdoutVacuum.toString();
        if (output.length() == 0) {
            output = null;
        }
        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 : PrintStream(java.io.PrintStream) PipedOutputStream(java.io.PipedOutputStream) Method(java.lang.reflect.Method) STGroupString(org.stringtemplate.v4.STGroupString) PipedInputStream(java.io.PipedInputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 14 with StreamVacuum

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

the class BasePHPTest method execModule.

public String execModule(String fileName) {
    String phpPath = locatePhp();
    String runtimePath = locateRuntime();
    String modulePath = new File(getTempTestDir(), fileName).getAbsolutePath();
    String inputPath = new File(getTempTestDir(), "input").getAbsolutePath();
    Path outputPath = getTempTestDir().toPath().resolve("output").toAbsolutePath();
    try {
        ProcessBuilder builder = new ProcessBuilder(phpPath, modulePath, inputPath, outputPath.toString());
        builder.environment().put("RUNTIME", runtimePath);
        builder.directory(getTempTestDir());
        Process process = builder.start();
        StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
        StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
        stdoutVacuum.start();
        stderrVacuum.start();
        process.waitFor();
        stdoutVacuum.join();
        stderrVacuum.join();
        String output = stdoutVacuum.toString();
        if (output.length() == 0) {
            output = null;
        }
        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) IOException(java.io.IOException)

Example 15 with StreamVacuum

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

the class BaseSwiftTest method runProcess.

private static Pair<String, String> runProcess(String execPath, String... args) throws IOException, InterruptedException {
    List<String> argsWithArch = new ArrayList<>();
    if (USE_ARCH_ARM64 && isMacOSArm64())
        argsWithArch.addAll(Arrays.asList("arch", "-arm64"));
    argsWithArch.addAll(Arrays.asList(args));
    if (VERBOSE)
        System.err.println("Executing " + argsWithArch.toString() + " " + execPath);
    final Process process = Runtime.getRuntime().exec(argsWithArch.toArray(new String[0]), null, new File(execPath));
    StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
    StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
    stdoutVacuum.start();
    stderrVacuum.start();
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            try {
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace(System.err);
            }
        }
    }, 120_000);
    int status = process.waitFor();
    timer.cancel();
    stdoutVacuum.join();
    stderrVacuum.join();
    if (VERBOSE)
        System.err.println("Done executing " + argsWithArch.toString() + " " + execPath);
    if (status != 0) {
        System.err.println("Process exited with status " + status);
        throw new IOException("Process exited with status " + status + ":\n" + stdoutVacuum.toString() + "\n" + stderrVacuum.toString());
    }
    return new Pair<>(stdoutVacuum.toString(), stderrVacuum.toString());
}
Also used : BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) IOException(java.io.IOException) IOException(java.io.IOException) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) Pair(org.antlr.v4.runtime.misc.Pair)

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