From fd339313dcf2503ced7ba33004fe215770418d68 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 21 Oct 2015 20:00:18 +0300 Subject: [PATCH] make temporary names more nice-looking, remove project files cached in VFS from the previous tests --- .../unusedLibrary/simple/expected.xml | 2 +- .../testFramework/PlatformTestCase.java | 7 +- .../testFramework/UsefulTestCase.java | 2 +- .../intellij/openapi/util/io/FileUtilRt.java | 65 +++++++------------ 4 files changed, 32 insertions(+), 44 deletions(-) diff --git a/java/java-tests/testData/inspection/unusedLibrary/simple/expected.xml b/java/java-tests/testData/inspection/unusedLibrary/simple/expected.xml index 8184735d55cf..9cd23b0df42b 100644 --- a/java/java-tests/testData/inspection/unusedLibrary/simple/expected.xml +++ b/java/java-tests/testData/inspection/unusedLibrary/simple/expected.xml @@ -1,7 +1,7 @@ - testSimple_0.iml + testSimple.iml Unused library Unused library 'JUnit' diff --git a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java index d1e5c6663726..afb7063bf09c 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java @@ -110,7 +110,7 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro protected boolean myAssertionsInTestDetected; protected static final Logger LOG = Logger.getInstance("#com.intellij.testFramework.PlatformTestCase"); public static Thread ourTestThread; - private static TestCase ourTestCase = null; + private static TestCase ourTestCase; public static final long DEFAULT_TEST_TIME = 300L; public static long ourTestTime = DEFAULT_TEST_TIME; private EditorListenerTracker myEditorListenerTracker; @@ -191,6 +191,9 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro @Override protected void setUp() throws Exception { super.setUp(); + File tempDir = new File(FileUtilRt.getTempDirectory()); + myFilesToDelete.add(tempDir); + if (ourTestCase != null) { String message = "Previous test " + ourTestCase + " hasn't called tearDown(). Probably overridden without super call."; ourTestCase = null; @@ -307,7 +310,7 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro } protected File getIprFile() throws IOException { - File tempFile = FileUtil.createTempFile(getName() + "_", ProjectFileType.DOT_DEFAULT_EXTENSION); + File tempFile = FileUtil.createTempFile(getName(), ProjectFileType.DOT_DEFAULT_EXTENSION); myFilesToDelete.add(tempFile); return tempFile; } diff --git a/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java b/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java index fcc8bee14fb6..71718080f774 100644 --- a/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java @@ -139,7 +139,7 @@ public abstract class UsefulTestCase extends TestCase { String testName = FileUtil.sanitizeFileName(getTestName(true)); if (StringUtil.isEmptyOrSpaces(testName)) testName = ""; testName = new File(testName).getName(); // in case the test name contains file separators - myTempDir = FileUtil.toSystemDependentName(ORIGINAL_TEMP_DIR + "/" + TEMP_DIR_MARKER + testName + "_"+ RNG.nextInt(1000)); + myTempDir = new File(ORIGINAL_TEMP_DIR, TEMP_DIR_MARKER + testName).getPath(); FileUtil.resetCanonicalTempPathCache(myTempDir); } ApplicationInfoImpl.setInPerformanceTest(isPerformanceTest()); diff --git a/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java b/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java index 3c1cfabb1acb..e9aac8b47c50 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java +++ b/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java @@ -315,7 +315,7 @@ public class FileUtilRt { } private static class FilesToDeleteHolder { - public static final Queue ourFilesToDelete = createFilesToDelete(); + private static final Queue ourFilesToDelete = createFilesToDelete(); private static Queue createFilesToDelete() { final ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); @@ -387,62 +387,47 @@ public class FileUtilRt { prefix = (prefix + "___").substring(0, 3); } if (suffix == null) { - suffix = ".tmp"; + suffix = ""; } + // normalize and use only the file name from the prefix + prefix = new File(prefix).getName(); int exceptionsCount = 0; + int i = 0; while (true) { try { - // If there was an IOException, there's no reason to do sequential search - fallback to random - final File temp = createTemp(prefix, suffix, dir, isDirectory, exceptionsCount > 0); - return normalizeFile(temp); + File f = calcName(dir, prefix, suffix, i); + + boolean success = isDirectory ? f.mkdir() : f.createNewFile(); + if (!success) { + throw new IOException("Unable to create temporary file " + f); + } + + return normalizeFile(f); } catch (IOException e) { // Win32 createFileExclusively access denied if (++exceptionsCount >= 100) { throw e; } } + i++; // for some reason the file1 can't be created (previous file1 was deleted but got locked by anti-virus?). try file2. + if (i > 2) { + i = 2 + (int)(System.nanoTime() % 998); // generate random suffix if too many failures + } } } @NotNull - private static File createTemp(@NotNull String prefix, - @NotNull String suffix, - @NotNull File directory, - boolean isDirectory, - boolean randomName) throws IOException { - // Fallback to the original File.createTempFile - if (randomName) { - @SuppressWarnings("SSBasedInspection") - File res = File.createTempFile(prefix, suffix, directory); - if (isDirectory) { - if (!res.delete() || !res.mkdir()) { - throw new IOException("Cannot create directory: " + res); - } - } - return res; + private static File calcName(@NotNull File dir, @NotNull String prefix, @NotNull String suffix, int i) throws IOException { + prefix += i == 0 ? "" : i; + if (prefix.endsWith(".") && suffix.startsWith(".")) { + prefix = prefix.substring(0, prefix.length() - 1); } - - // normalize and use only the file name from the prefix - prefix = new File(prefix).getName(); - - File f; - int i = 0; - do { - String name = prefix + i + suffix; - f = new File(directory, name); - if (!name.equals(f.getName())) { - throw new IOException("Unable to create temporary file " + f + " for name " + name); - } - i++; + String name = prefix + suffix; + File f = new File(dir, name); + if (!name.equals(f.getName())) { + throw new IOException("Unable to create temporary file " + f + " for name " + name); } - while (f.exists()); - - boolean success = isDirectory ? f.mkdir() : f.createNewFile(); - if (!success) { - throw new IOException("Unable to create temporary file " + f); - } - return f; }