Skip to content

Commit

Permalink
Include root cause when failing to delete file
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp authored and yhkuo41 committed Apr 19, 2023
1 parent 0f8ed39 commit 00f964b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ will exclude all methods called `methodName` under package `org.example`.
`@MethodSource("myFactory([I)"` (which was already supported) and
`@MethodSource("myFactory(java.lang.String[])` instead of
`@MethodSource("myFactory([Ljava.lang.String;)`.
* Exceptions thrown for undeletable files when cleaning up a temporary directory created
via `@TempDir` now include the root cause.

==== Deprecations and Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ private void resetPermissionsAndTryToDeleteAgain(Path path, IOException exceptio
}
catch (Exception suppressed) {
exception.addSuppressed(suppressed);
failures.put(path, exception);
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
Expand All @@ -55,7 +56,6 @@
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -200,7 +200,19 @@ void onlyAttemptsToDeleteUndeletableDirectoriesOnce() {
assertSingleFailedTest(results, //
instanceOf(IOException.class), //
message(it -> it.startsWith("Failed to delete temp directory")), //
suppressed(0, instanceOf(IOException.class), message("Simulated failure")), //
suppressed(0, instanceOf(DirectoryNotEmptyException.class)), //
suppressed(1, instanceOf(IOException.class), message("Simulated failure")));
}

@Test
@DisplayName("only attempts to delete undeletable files once")
void onlyAttemptsToDeleteUndeletableFilesOnce() {
var results = executeTestsForClass(UndeletableFileTestCase.class);

assertSingleFailedTest(results, //
instanceOf(IOException.class), //
message(it -> it.startsWith("Failed to delete temp directory")), //
suppressed(0, instanceOf(DirectoryNotEmptyException.class)), //
suppressed(1, instanceOf(IOException.class), message("Simulated failure")));
}

Expand Down Expand Up @@ -1014,16 +1026,45 @@ private static Map<String, Path> getTempDirs(TestInfo testInfo) {

static class UndeletableDirectoryTestCase {

static final Path UNDELETABLE_SUB_DIR = Path.of("undeletable");

@RegisterExtension
Extension injector = (BeforeEachCallback) context -> context //
BeforeEachCallback injector = context -> context //
.getStore(TempDirectory.NAMESPACE) //
.put(TempDirectory.FILE_OPERATIONS_KEY, (FileOperations) path -> {
throw new IOException("Simulated failure");
if (path.endsWith(UNDELETABLE_SUB_DIR)) {
throw new IOException("Simulated failure");
}
else {
Files.delete(path);
}
});

@Test
void test(@TempDir Path tempDir) throws Exception {
Files.createDirectory(tempDir.resolve("test-sub-dir"));
Files.createDirectory(tempDir.resolve(UNDELETABLE_SUB_DIR));
}
}

static class UndeletableFileTestCase {

static final Path UNDELETABLE_FILE = Path.of("undeletable");

@RegisterExtension
BeforeEachCallback injector = context -> context //
.getStore(TempDirectory.NAMESPACE) //
.put(TempDirectory.FILE_OPERATIONS_KEY, (FileOperations) path -> {
if (path.endsWith(UNDELETABLE_FILE)) {
throw new IOException("Simulated failure");
}
else {
Files.delete(path);
}
});

@Test
void test(@TempDir Path tempDir) throws Exception {
Files.createFile(tempDir.resolve(UNDELETABLE_FILE));
}
}
}

0 comments on commit 00f964b

Please sign in to comment.