Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ public static String getRuntimePath(String language) {
return runtimePath.toString() + FileSeparator + language;
}

// Allows any target to add additional options for the antlr tool such as the location of the output files
// which is useful for the Go target for instance to avoid having to move them before running the test
//
protected List<String> getTargetToolOptions(RunOptions ro) {
return null;
}

public State run(RunOptions runOptions) {
List<String> options = new ArrayList<>();
if (runOptions.useVisitor) {
Expand All @@ -162,6 +169,14 @@ public State run(RunOptions runOptions) {
if (runOptions.superClass != null && runOptions.superClass.length() > 0) {
options.add("-DsuperClass=" + runOptions.superClass);
}

// See if the target wants to add tool options.
//
List<String> targetOpts = getTargetToolOptions(runOptions);
if (targetOpts != null) {
options.addAll(targetOpts);
}

ErrorQueue errorQueue = Generator.antlrOnString(getTempDirPath(), getLanguage(),
runOptions.grammarFileName, runOptions.grammarStr, false, options.toArray(new String[0]));

Expand Down Expand Up @@ -239,7 +254,8 @@ protected String grammarParseRuleToRecognizerName(String startRuleName) {
return startRuleName;
}

protected void addExtraRecognizerParameters(ST template) {}
protected void addExtraRecognizerParameters(ST template) {
}

private boolean initAntlrRuntimeIfRequired(RunOptions runOptions) {
String language = getLanguage();
Expand Down Expand Up @@ -301,8 +317,7 @@ protected ExecutedState execute(RunOptions runOptions, CompiledState compiledSta
ProcessorResult result = Processor.run(args.toArray(new String[0]), getTempDirPath(), getExecEnvironment());
output = result.output;
errors = result.errors;
}
catch (InterruptedException | IOException e) {
} catch (InterruptedException | IOException e) {
exception = e;
}
return new ExecutedState(compiledState, output, errors, exception);
Expand All @@ -316,11 +331,10 @@ protected ProcessorResult runCommand(String[] command, String workPath, String d
String cmd = String.join(" ", command);
try {
return Processor.run(command, workPath);
}
catch (InterruptedException | IOException e) {
String msg = "command \""+cmd+"\"\n in "+workPath+" failed";
if ( description != null ) {
msg += ":\n can't "+description;
} catch (InterruptedException | IOException e) {
String msg = "command \"" + cmd + "\"\n in " + workPath + " failed";
if (description != null) {
msg += ":\n can't " + description;
}
throw new Exception(msg, e);
}
Expand All @@ -332,8 +346,7 @@ private void removeTempTestDirIfRequired() {
if (dirFile.exists()) {
try {
deleteDirectory(dirFile);
}
catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
Expand Down
57 changes: 35 additions & 22 deletions runtime-testsuite/test/org/antlr/v4/test/runtime/go/GoRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public String[] getExtraRunArgs() {
private final static Map<String, String> environment;

private static String cachedGoMod;
private static String cachedGoSum;
private static ArrayList<String> options = new ArrayList<>();

static {
environment = new HashMap<>();
Expand All @@ -88,6 +90,8 @@ protected void initRuntime(RunOptions runOptions) throws Exception {
Processor.run(new String[]{runtimeToolPath, "mod", "init", "test"}, cachePath, environment);
Processor.run(new String[]{runtimeToolPath, "mod", "edit",
"-replace=" + GoRuntimeImportPath + "=" + runtimeFilesPath}, cachePath, environment);
Processor.run(new String[]{runtimeToolPath, "mod", "edit",
"-require=" + GoRuntimeImportPath + "@v4.0.0"}, cachePath, environment);
cachedGoMod = readFile(cachePath + FileSeparator, "go.mod");
}

Expand All @@ -111,36 +115,45 @@ protected String grammarParseRuleToRecognizerName(String startRuleName) {
return rn;
}

@Override
protected List<String> getTargetToolOptions(RunOptions ro) {
// Unfortunately this cannot be cached because all the synchronization is out of whack, and
// we end up return the options before they are populated. I prefer to make this small change
// at the expense of an object rather than try to change teh synchronized initialization, which is
// very fragile.
// Also, the options may need to change in the future according to the test options. This is safe
ArrayList<String> options = new ArrayList<>();
options.add("-o");
options.add(tempTestDir.resolve("parser").toString());
return options;
}

@Override
protected CompiledState compile(RunOptions runOptions, GeneratedState generatedState) {
List<GeneratedFile> generatedFiles = generatedState.generatedFiles;
String tempDirPath = getTempDirPath();
File generatedParserDir = new File(tempDirPath, "parser");
if (!generatedParserDir.mkdir()) {
return new CompiledState(generatedState, new Exception("can't make dir " + generatedParserDir));
}
// We have already created a suitable go.mod file, though it may need to have go mod tidy run on it one time
//
writeFile(getTempDirPath(), "go.mod", cachedGoMod);

// The generated files seem to need to be in the parser subdirectory.
// We have no need to change the import of the runtime because of go mod replace so, we could just generate them
// directly in to the parser subdir. But in case down the line, there is some reason to want to replace things in
// the generated code, then I will leave this here, and we can use replaceInFile()
// We need to run a go mod tidy once, now that we have source code. This will generate a valid go.sum file and
// recognize the indirect requirements in the go.mod file. Then we re-cache the go.mod and cache
// the go.sum and therefore save sparking a new process for all the remaining go tests. This is probably
// a race condition as these tests are run in parallel, but it does not matter as they are all going to
// generate the same go.mod and go.sum file anyway.
//
for (GeneratedFile generatedFile : generatedFiles) {
Exception ex = null;
if (cachedGoSum == null) {
try {
Path originalFile = Paths.get(tempDirPath, generatedFile.name);
Files.move(originalFile, Paths.get(tempDirPath, "parser", generatedFile.name));
} catch (IOException e) {
return new CompiledState(generatedState, e);
Processor.run(new String[]{getRuntimeToolPath(), "mod", "tidy"}, getTempDirPath(), environment);
} catch (InterruptedException | IOException e) {
ex = e;
}
cachedGoMod = readFile(getTempDirPath() + FileSeparator, "go.mod");
cachedGoSum = readFile(getTempDirPath() + FileSeparator, "go.sum");
}

writeFile(tempDirPath, "go.mod", cachedGoMod);
Exception ex = null;
try {
Processor.run(new String[]{getRuntimeToolPath(), "mod", "tidy"}, tempDirPath, environment);
} catch (InterruptedException | IOException e) {
ex = e;
}
// We can now write the go.sum file, which will allow the go compiler to build the module
//
writeFile(getTempDirPath(), "go.sum", cachedGoSum);

return new CompiledState(generatedState, ex);
}
Expand Down