From 54ffb7157dd0f62997bc5b7c8315770badf7ad37 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 21 Sep 2015 14:49:28 +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 | 14 +++++- .../testFramework/UsefulTestCase.java | 3 +- .../intellij/openapi/util/io/FileUtilRt.java | 47 +++++++------------ 4 files changed, 33 insertions(+), 33 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 ab2d6ce3cc49..0b4798d70354 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java @@ -53,6 +53,7 @@ import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.impl.local.LocalFileSystemImpl; +import com.intellij.openapi.vfs.newvfs.NewVirtualFile; import com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl; import com.intellij.openapi.vfs.newvfs.persistent.PersistentFS; import com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl; @@ -185,6 +186,7 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro @Override protected void setUp() throws Exception { super.setUp(); + myFilesToDelete.add(new File(FileUtilRt.getTempDirectory())); if (ourTestCase != null) { String message = "Previous test " + ourTestCase + " hasn't called tearDown(). Probably overridden without super call."; ourTestCase = null; @@ -250,7 +252,15 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro } @NotNull - public static Project createProject(File projectFile, String creationPlace) { + public static Project createProject(@NotNull File projectFile, @NotNull String creationPlace) { + VirtualFile projectBase = LocalFileSystem.getInstance().findFileByIoFile(projectFile.getName().endsWith( + ProjectFileType.DOT_DEFAULT_EXTENSION) ? projectFile.getParentFile() : projectFile); + if (projectBase != null) { + // must be leftovers from the previous test run + for (VirtualFile file : ((NewVirtualFile)projectBase).iterInDbChildren()) { + delete(file); + } + } try { Project project = ProjectManagerEx.getInstanceEx().newProject(FileUtil.getNameWithoutExtension(projectFile), projectFile.getPath(), false, false); assert project != null; @@ -293,7 +303,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 aad28bc65d90..97adee22ac48 100644 --- a/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java @@ -139,7 +139,8 @@ public abstract class UsefulTestCase extends TestCase { String testName = 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)); + File tempDirectory = FileUtil.createTempDirectory(new File(ORIGINAL_TEMP_DIR), TEMP_DIR_MARKER + testName, ""); + myTempDir = tempDirectory.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 8a48ba176218..fe7fdf648c90 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 @@ -385,12 +385,14 @@ public class FileUtilRt { if (suffix == null) { suffix = ".tmp"; } + // 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); + final File temp = callCreate(dir, prefix, suffix, isDirectory, i); return normalizeFile(temp); } catch (IOException e) { // Win32 createFileExclusively access denied @@ -398,41 +400,28 @@ public class FileUtilRt { 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, + private static File callCreate(@NotNull File directory, + @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; + 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(directory, 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) {