mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
make temporary names more nice-looking, remove project files cached in VFS from the previous tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>testSimple_0.iml</file>
|
||||
<file>testSimple.iml</file>
|
||||
<problem_class>Unused library</problem_class>
|
||||
<description>Unused library 'JUnit'</description>
|
||||
</problem>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user