From f0ca1140a7606384b05e196f5e50f389fc576a69 Mon Sep 17 00:00:00 2001 From: Eric Vergnaud Date: Sat, 13 Feb 2021 03:31:12 +0800 Subject: [PATCH] don't delete symlinks content --- .../test/runtime/BaseRuntimeTestSupport.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTestSupport.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTestSupport.java index 77e86f919c..49233e2638 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTestSupport.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTestSupport.java @@ -14,11 +14,14 @@ import org.junit.runner.Description; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.Locale; import java.util.logging.Logger; import static org.junit.Assert.assertEquals; +@SuppressWarnings("ResultOfMethodCallIgnored") public abstract class BaseRuntimeTestSupport implements RuntimeTestSupport { // -J-Dorg.antlr.v4.test.BaseTest.level=FINE @@ -102,8 +105,7 @@ private void createTempDir() { String prop = System.getProperty(propName); if(prop!=null && prop.length()>0) { tempTestDir = new File(prop); - } - else { + } else { String dirName = getClass().getSimpleName() + "-" + Thread.currentThread().getName() + "-" + System.currentTimeMillis(); tempTestDir = new File(System.getProperty("java.io.tmpdir"), dirName); } @@ -149,26 +151,36 @@ public static void eraseDirectory(File dir) { public static void eraseFilesInDir(File dir) { String[] files = dir.list(); for(int i = 0; files!=null && i < files.length; i++) { - File file = new File(dir,files[i]); - if(file.isDirectory()) - eraseDirectory(file); - else - file.delete(); + try { + eraseFile(dir, files[i]); + } catch(IOException e) { + logger.info(e.getMessage()); + } } } + private static void eraseFile(File dir, String name) throws IOException { + File file = new File(dir,name); + if(Files.isSymbolicLink(file.toPath())) + Files.delete(file.toPath()); + else if(file.isDirectory()) + eraseDirectory(file); + else + file.delete(); + } + private static String detectedOS; public static String getOS() { if (detectedOS == null) { String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH); - if ((os.indexOf("mac") >= 0) || (os.indexOf("darwin") >= 0)) { + if (os.contains("mac") || os.contains("darwin")) { detectedOS = "mac"; } - else if (os.indexOf("win") >= 0) { + else if (os.contains("win")) { detectedOS = "windows"; } - else if (os.indexOf("nux") >= 0) { + else if (os.contains("nux")) { detectedOS = "linux"; } else {