From 32f220ab5023c9a002c07d6375dccb1eea5836cd Mon Sep 17 00:00:00 2001 From: Nikolay Chashnikov Date: Wed, 29 Apr 2020 11:14:29 +0300 Subject: [PATCH] [test framework] don't extend junit's TemporaryFolder in our TempDirectory It doesn't bring any value but complicates API. GitOrigin-RevId: e98b12630a136f1e1be13c2ef3d996b7460eabe5 --- .../openapi/vfs/local/FileWatcherTest.kt | 2 +- .../testFramework/rules/TempDirectory.java | 41 ++++++++++++------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/FileWatcherTest.kt b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/FileWatcherTest.kt index 618691d6678c..2252832dc69b 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/FileWatcherTest.kt +++ b/platform/platform-tests/testSrc/com/intellij/openapi/vfs/local/FileWatcherTest.kt @@ -607,7 +607,7 @@ class FileWatcherTest : BareTestFixtureTestCase() { val name = IoTestUtil.getUnicodeName() assumeTrue("Unicode names not supported", name != null) - val root = tempDir.newFolder(name) + val root = tempDir.newFolder(name!!) val file = tempDir.newFile("${name}/${name}.txt") refresh(root) watch(root) diff --git a/platform/testFramework/src/com/intellij/testFramework/rules/TempDirectory.java b/platform/testFramework/src/com/intellij/testFramework/rules/TempDirectory.java index 285ed72d521e..a2b221a69b2a 100644 --- a/platform/testFramework/src/com/intellij/testFramework/rules/TempDirectory.java +++ b/platform/testFramework/src/com/intellij/testFramework/rules/TempDirectory.java @@ -6,6 +6,7 @@ import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.UsefulTestCase; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.junit.rules.ExternalResource; import org.junit.rules.TemporaryFolder; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -16,13 +17,15 @@ import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.concurrent.atomic.AtomicInteger; /** - * A clone of {@link TemporaryFolder} with lazy init, no symlinks in a temporary directory path, better directory name, + * An improved variant of {@link TemporaryFolder} with lazy init, no symlinks in a temporary directory path, better directory name, * and more convenient {@linkplain #newFile(String)} / {@linkplain #newFolder(String)} methods. */ -public class TempDirectory extends TemporaryFolder { +public class TempDirectory extends ExternalResource { private String myName; + private final AtomicInteger myNextDirNameSuffix = new AtomicInteger(); private File myRoot; @Override @@ -49,7 +52,6 @@ public class TempDirectory extends TemporaryFolder { } } - @Override public @NotNull File getRoot() { if (myRoot == null) { if (myName == null) { @@ -66,24 +68,35 @@ public class TempDirectory extends TemporaryFolder { return myRoot; } - /** Allows subdirectories in a directory name (i.e. "dir1/dir2/target"); does not fail if these intermediates already exist. */ - @Override - public @NotNull File newFolder(@NotNull String directoryName) throws IOException { - Path dir = Paths.get(getRoot().getPath(), directoryName); + /** + * Creates a new directory with the given relative path from the root temp directory. Throws an exception if such a directory already exists. + */ + public @NotNull File newFolder(@NotNull String relativePath) throws IOException { + Path dir = Paths.get(getRoot().getPath(), relativePath); if (Files.exists(dir)) throw new IOException("Already exists: " + dir); makeDirectories(dir); return dir.toFile(); } - /** Allows subdirectories in a file name (i.e. "dir1/dir2/target"); does not fail if these intermediates already exist. */ - @Override - public @NotNull File newFile(@NotNull String fileName) throws IOException { - return newFile(fileName, null); + /** + * Creates a new directory with random name under the root temp directory. + */ + public @NotNull File newFolder() throws IOException { + return FileUtil.createTempDirectory(getRoot(), "dir" + myNextDirNameSuffix.incrementAndGet(), null); } - /** Allows subdirectories in a file name (i.e. "dir1/dir2/target"); does not fail if these intermediates already exist. */ - public @NotNull File newFile(@NotNull String fileName, byte @Nullable [] content) throws IOException { - Path file = Paths.get(getRoot().getPath(), fileName); + /** + * Creates a new file with the given relative path from the root temp directory. Throws an exception if such a file already exists. + */ + public @NotNull File newFile(@NotNull String relativePath) throws IOException { + return newFile(relativePath, null); + } + + /** + * Creates a new file with the given relative path from the root temp directory. Throws an exception if such a file already exists. + */ + public @NotNull File newFile(@NotNull String relativePath, byte @Nullable [] content) throws IOException { + Path file = Paths.get(getRoot().getPath(), relativePath); if (Files.exists(file)) throw new IOException("Already exists: " + file); makeDirectories(file.getParent()); Files.createFile(file);