Search in sources :

Example 6 with StreamVacuum

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

the class BaseGoTest method execModule.

public String execModule(String fileName) {
    String goExecutable = locateGo();
    String modulePath = new File(getTempTestDir(), fileName).getAbsolutePath();
    String inputPath = new File(getTempTestDir(), "input").getAbsolutePath();
    try {
        ProcessBuilder builder = new ProcessBuilder(goExecutable, "run", modulePath, inputPath);
        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 : BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 7 with StreamVacuum

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

the class BaseDartTest method rawGenerateAndBuildRecognizer.

/**
 * Return true if all is well
 */
protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Dart", grammarFileName, grammarStr, defaultListener, extraOptions);
    if (!equeue.errors.isEmpty()) {
        return false;
    }
    List<String> files = new ArrayList<String>();
    if (lexerName != null) {
        files.add(lexerName + ".dart");
    }
    if (parserName != null) {
        files.add(parserName + ".dart");
        Set<String> optionsSet = new HashSet<String>(Arrays.asList(extraOptions));
        String grammarName = grammarFileName.substring(0, grammarFileName.lastIndexOf('.'));
        if (!optionsSet.contains("-no-listener")) {
            files.add(grammarName + "Listener.dart");
            files.add(grammarName + "BaseListener.dart");
        }
        if (optionsSet.contains("-visitor")) {
            files.add(grammarName + "Visitor.dart");
            files.add(grammarName + "BaseVisitor.dart");
        }
    }
    String runtime = locateRuntime();
    writeFile(getTempDirPath(), "pubspec.yaml", "name: \"test\"\n" + "dependencies:\n" + "  antlr4:\n" + "    path: " + runtime + "\n" + "environment:\n" + "  sdk: \">=2.12.0 <3.0.0\"\n");
    final File dartToolDir = new File(getTempDirPath(), ".dart_tool");
    if (cacheDartPackages == null) {
        try {
            final Process process = Runtime.getRuntime().exec(new String[] { locateDart(), "pub", "get" }, null, getTempTestDir());
            StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
            stderrVacuum.start();
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    try {
                        process.destroy();
                    } catch (Exception e) {
                        e.printStackTrace(System.err);
                    }
                }
            }, 30_000);
            process.waitFor();
            timer.cancel();
            stderrVacuum.join();
            String stderrDuringPubGet = stderrVacuum.toString();
            if (!stderrDuringPubGet.isEmpty()) {
                System.out.println("Pub Get error: " + stderrVacuum.toString());
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return false;
        }
        cacheDartPackages = readFile(getTempDirPath(), ".packages");
        cacheDartPackageConfig = readFile(dartToolDir.getAbsolutePath(), "package_config.json");
    } else {
        writeFile(getTempDirPath(), ".packages", cacheDartPackages);
        // noinspection ResultOfMethodCallIgnored
        dartToolDir.mkdir();
        writeFile(dartToolDir.getAbsolutePath(), "package_config.json", cacheDartPackageConfig);
    }
    // allIsWell: no compile
    return true;
}
Also used : BaseRuntimeTest.readFile(org.antlr.v4.test.runtime.BaseRuntimeTest.readFile) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 8 with StreamVacuum

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

the class BaseGoTest method setupGoMod.

private void setupGoMod() throws Exception {
    String goExecutable = locateGo();
    ProcessBuilder pb = new ProcessBuilder(goExecutable, "mod", "init", "antlr.org/test");
    pb.directory(getTempTestDir());
    pb.redirectErrorStream(true);
    Process process = pb.start();
    StreamVacuum sucker = new StreamVacuum(process.getInputStream());
    sucker.start();
    int exit = process.waitFor();
    sucker.join();
    if (exit != 0) {
        throw new Exception("Non-zero exit while setting up go module: " + sucker.toString());
    }
}
Also used : BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 9 with StreamVacuum

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

the class BaseCSharpTest method execTest.

public String execTest() {
    String exec = locateExec();
    try {
        File tmpdirFile = new File(getTempDirPath());
        Path output = tmpdirFile.toPath().resolve("output");
        Path errorOutput = tmpdirFile.toPath().resolve("error-output");
        String[] args = getExecTestArgs(exec, output, errorOutput);
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.directory(tmpdirFile);
        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();
        String writtenOutput = TestOutputReading.read(output);
        setParseErrors(TestOutputReading.read(errorOutput));
        int exitValue = process.exitValue();
        String stdoutString = stdoutVacuum.toString().trim();
        String stderrString = stderrVacuum.toString().trim();
        if (exitValue != 0) {
            System.err.println("execTest command: " + Utils.join(args, " "));
            System.err.println("execTest exitValue: " + exitValue);
        }
        if (!stdoutString.isEmpty()) {
            System.err.println("execTest stdoutVacuum: " + stdoutString);
        }
        if (!stderrString.isEmpty()) {
            System.err.println("execTest stderrVacuum: " + stderrString);
        }
        return writtenOutput;
    } 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 10 with StreamVacuum

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

the class BaseCppTest method runProcess.

private String runProcess(ProcessBuilder builder, String description, boolean showStderr) throws Exception {
    // System.out.println("BUILDER: " + builder.command() + " @ " + builder.directory().toString());
    Process process = builder.start();
    StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
    StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
    stdoutVacuum.start();
    stderrVacuum.start();
    int errcode = process.waitFor();
    stdoutVacuum.join();
    stderrVacuum.join();
    String output = stdoutVacuum.toString();
    if (stderrVacuum.toString().length() > 0) {
        setParseErrors(stderrVacuum.toString());
        if (showStderr)
            System.err.println(getParseErrors());
    }
    if (errcode != 0) {
        String err = "execution of '" + description + "' failed with error code: " + errcode;
        if (getParseErrors() != null) {
            setParseErrors(getParseErrors() + err);
        } else {
            setParseErrors(err);
        }
    }
    return output;
}
Also used : BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

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