mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[test framework] don't extend junit's TemporaryFolder in our TempDirectory
It doesn't bring any value but complicates API. GitOrigin-RevId: e98b12630a136f1e1be13c2ef3d996b7460eabe5
This commit is contained in:
committed by
intellij-monorepo-bot
parent
cbe5cf3d67
commit
32f220ab50
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user