Skip to content

Commit

Permalink
Merge pull request antlr#3386 from KvanTTT/csharp-go-runtime-tests-fixes
Browse files Browse the repository at this point in the history
C# and Go runtime tests fixes on non-Windows OS
  • Loading branch information
parrt authored Dec 6, 2021
2 parents 261769f + 5a23e37 commit 3801358
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class BaseCSharpTest extends BaseRuntimeTestSupport implements RuntimeTes
private final static String testProjectFileName = "Antlr4.Test.csproj";
private final static boolean isDebug = false;
private static String cSharpTestProjectContent;
private static final String cSharpCachingDirectory = Paths.get(cachingDirectory, "CSharp").toString();

@Override
protected String getPropertyPrefix() {
Expand Down Expand Up @@ -208,7 +209,7 @@ public boolean buildProject() {
return true;
}

private boolean initializeRuntime() {
private synchronized boolean initializeRuntime() {
// Compile runtime project once per tests session
if (isRuntimeInitialized)
return true;
Expand All @@ -222,20 +223,19 @@ private boolean initializeRuntime() {
File runtimeProjFile = new File(runtimeProj.getFile());
String runtimeProjPath = runtimeProjFile.getPath();

RuntimeTestUtils.mkdir(cachingDirectory);
RuntimeTestUtils.mkdir(cSharpCachingDirectory);
String[] args = new String[]{
"dotnet",
"build",
runtimeProjPath,
"-c",
"Release",
"-o",
cachingDirectory
cSharpCachingDirectory
};

boolean success;
try
{
try {
String cSharpTestProjectResourceName = BaseCSharpTest.class.getPackage().getName().replace(".", "/") + "/";
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(cSharpTestProjectResourceName + testProjectFileName);
int bufferSize = 1024;
Expand All @@ -245,11 +245,10 @@ private boolean initializeRuntime() {
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
out.append(buffer, 0, numRead);
}
cSharpTestProjectContent = out.toString().replace(cSharpAntlrRuntimeDllName, Paths.get(cachingDirectory, cSharpAntlrRuntimeDllName).toString());
cSharpTestProjectContent = out.toString().replace(cSharpAntlrRuntimeDllName, Paths.get(cSharpCachingDirectory, cSharpAntlrRuntimeDllName).toString());

success = runProcess(args, cachingDirectory);
}
catch (Exception e) {
success = runProcess(args, cSharpCachingDirectory);
} catch (Exception e) {
e.printStackTrace(System.err);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

import static junit.framework.TestCase.*;
import static org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString;
Expand Down Expand Up @@ -188,30 +189,32 @@ private String execModule(String fileName) {
stdoutVacuum.join();
stderrVacuum.join();
String output = stdoutVacuum.toString();
if ( output.length()==0 ) {
if (output.length() == 0) {
output = null;
}
if (stderrVacuum.toString().length() > 0) {
setParseErrors(stderrVacuum.toString());
}
return output;
}
catch (Exception e) {
} catch (Exception e) {
System.err.println("can't exec recognizer");
e.printStackTrace(System.err);
}
return null;
}

private boolean initializeRuntime() {
private synchronized boolean initializeRuntime() {
if (isRuntimeInitialized)
return true;

String goRoot = getGoRootValue();
Path newGoRoot = Paths.get(cachingDirectory, "Go");
newGoRootString = newGoRoot.toString();
try {
copyDirectory(Paths.get(goRoot), newGoRoot, StandardCopyOption.REPLACE_EXISTING);
File newGoRootDirectory = newGoRoot.toFile();
if (newGoRootDirectory.exists())
deleteDirectory(newGoRootDirectory);
copyDirectory(Paths.get(goRoot), newGoRoot);
} catch (IOException e) {
e.printStackTrace();
Assert.fail("Unable to copy go system files");
Expand Down Expand Up @@ -240,7 +243,7 @@ private boolean initializeRuntime() {

private void copyDirectory(final Path source, final Path target, final CopyOption... options)
throws IOException {
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), 2147483647, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
Expand All @@ -257,6 +260,15 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
});
}

private void deleteDirectory(File f) throws IOException {
if (f.isDirectory()) {
for (File c : f.listFiles())
deleteDirectory(c);
}
if (!f.delete())
throw new FileNotFoundException("Failed to delete file: " + f);
}

private static String getGoRootValue() {
try {
ProcessBuilder pb = new ProcessBuilder("go", "env", "GOROOT");
Expand Down
3 changes: 2 additions & 1 deletion runtime/CSharp/src/Antlr4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<Company>The ANTLR Organization</Company>
<Version>4.9.3</Version>
<NeutralLanguage>en-US</NeutralLanguage>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<TargetFramework Condition="'$(OS)' != 'Windows_NT'">netstandard2.0</TargetFramework>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">net45;netstandard2.0</TargetFrameworks>
<NoWarn>$(NoWarn);CS1591;CS1574;CS1580</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Antlr4.Runtime.Standard</AssemblyName>
Expand Down

0 comments on commit 3801358

Please sign in to comment.