Skip to content

Commit

Permalink
Polish TempDir cleanup support
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen authored and runningcode committed Feb 15, 2023
1 parent fbb3705 commit ce51f20
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS;
import static org.junit.jupiter.engine.config.JupiterConfiguration.TEMP_DIR_SCOPE_PROPERTY_NAME;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
import static org.junit.platform.commons.util.ReflectionUtils.makeAccessible;

import java.io.File;
Expand Down Expand Up @@ -54,7 +55,6 @@
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.support.AnnotationSupport;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.commons.util.ReflectionUtils;

Expand Down Expand Up @@ -166,12 +166,12 @@ private CleanupMode findCleanupModeForParameter(ParameterContext parameterContex

private CleanupMode findCleanupModeForField(Field field) {
CleanupMode cleanupMode = null;
Optional<TempDir> optional = AnnotationSupport.findAnnotation(field, TempDir.class);
Optional<TempDir> optional = findAnnotation(field, TempDir.class);
if (optional.isPresent()) {
cleanupMode = optional.get().cleanup();
}
if (cleanupMode == null || cleanupMode == DEFAULT) {
cleanupMode = configuration.getDefaultTempDirCleanupMode();
cleanupMode = this.configuration.getDefaultTempDirCleanupMode();
}
return cleanupMode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.engine.AbstractJupiterTestEngineTests;
import org.opentest4j.TestAbortedException;

/**
* Integration tests for cleanup of the {@link TempDirectory} when the {@link CleanupMode} is
Expand Down Expand Up @@ -77,8 +76,8 @@ void never() throws IOException {
* Ensure a closeable path is not cleaned up for a cleanup mode of ON_SUCCESS, if there is a TestAbortedException.
*/
@Test
void onSuccessWithTestAbortedException() throws IOException {
when(extensionContext.getExecutionException()).thenReturn(Optional.of(new TestAbortedException()));
void onSuccessWithException() throws IOException {
when(extensionContext.getExecutionException()).thenReturn(Optional.of(new Exception()));

closeablePath = TempDirectory.createTempDir(ON_SUCCESS, extensionContext);
assertThat(closeablePath.get()).exists();
Expand All @@ -91,7 +90,7 @@ void onSuccessWithTestAbortedException() throws IOException {
* Ensure a closeable path is cleaned up for a cleanup mode of ON_SUCCESS, if there is no exception.
*/
@Test
void onSuccessWithNoTestAbortedException() throws IOException {
void onSuccessWithNoException() throws IOException {
when(extensionContext.getExecutionException()).thenReturn(Optional.empty());

closeablePath = TempDirectory.createTempDir(ON_SUCCESS, extensionContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
package org.junit.jupiter.engine.extension;

import static java.nio.file.Files.deleteIfExists;
import static java.nio.file.Files.exists;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.io.CleanupMode.ALWAYS;
import static org.junit.jupiter.api.io.CleanupMode.NEVER;
Expand All @@ -33,9 +31,9 @@
import org.junit.platform.launcher.LauncherDiscoveryRequest;

/**
* Test that {@link TempDir temporary directories} are not deleted if set for {@link CleanupMode#NEVER},
* deletes any if set for {@link CleanupMode#ON_SUCCESS} only if the test passes,
* and deletes any if set for {@link CleanupMode#ALWAYS}.
* Test that {@linkplain TempDir temporary directories} are not deleted with
* {@link CleanupMode#NEVER}, are deleted with {@link CleanupMode#ON_SUCCESS}
* but only if the test passes, and are always deleted with {@link CleanupMode#ALWAYS}.
*
* @see CleanupMode
* @see TempDir
Expand All @@ -53,17 +51,18 @@ class TempDirFieldTests {
private static Path onSuccessPassingFieldDir;

/**
* Ensure the cleanup modes defaults to ALWAYS for fields.
* Ensure the cleanup mode defaults to ALWAYS for fields.
* <p/>
* Expect the TempDir to be cleaned up.
*/
@Test
void testCleanupModeDefaultField() {
LauncherDiscoveryRequest request = request().selectors(
selectMethod(DefaultFieldCase.class, "testDefaultField")).build();
void cleanupModeDefaultField() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(DefaultFieldCase.class, "testDefaultField"))//
.build();
executeTests(request);

assertFalse(exists(defaultFieldDir));
assertThat(defaultFieldDir).doesNotExist();
}

/**
Expand All @@ -72,12 +71,13 @@ void testCleanupModeDefaultField() {
* Expect the TempDir not to be cleaned up.
*/
@Test
void testCleanupModeNeverField() {
LauncherDiscoveryRequest request = request().selectors(
selectMethod(NeverFieldCase.class, "testNeverField")).build();
void cleanupModeNeverField() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(NeverFieldCase.class, "testNeverField"))//
.build();
executeTests(request);

assertTrue(exists(neverFieldDir));
assertThat(neverFieldDir).exists();
}

/**
Expand All @@ -86,12 +86,13 @@ void testCleanupModeNeverField() {
* Expect the TempDir to be cleaned up.
*/
@Test
void testCleanupModeAlwaysField() {
LauncherDiscoveryRequest request = request().selectors(
selectMethod(AlwaysFieldCase.class, "testAlwaysField")).build();
void cleanupModeAlwaysField() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(AlwaysFieldCase.class, "testAlwaysField"))//
.build();
executeTests(request);

assertFalse(exists(alwaysFieldDir));
assertThat(alwaysFieldDir).doesNotExist();
}

/**
Expand All @@ -100,12 +101,13 @@ void testCleanupModeAlwaysField() {
* Expect the TempDir to be cleaned up.
*/
@Test
void testCleanupModeOnSuccessPassingField() {
LauncherDiscoveryRequest request = request().selectors(
selectMethod(OnSuccessPassingFieldCase.class, "testOnSuccessPassingField")).build();
void cleanupModeOnSuccessPassingField() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(OnSuccessPassingFieldCase.class, "testOnSuccessPassingField"))//
.build();
executeTests(request);

assertFalse(exists(onSuccessPassingFieldDir));
assertThat(onSuccessPassingFieldDir).doesNotExist();
}

/**
Expand All @@ -114,12 +116,13 @@ void testCleanupModeOnSuccessPassingField() {
* Expect the TempDir not to be cleaned up.
*/
@Test
void testCleanupModeOnSuccessFailingField() {
LauncherDiscoveryRequest request = request().selectors(
selectMethod(OnSuccessFailingFieldCase.class, "testOnSuccessFailingField")).build();
void cleanupModeOnSuccessFailingField() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(OnSuccessFailingFieldCase.class, "testOnSuccessFailingField"))//
.build();
executeTests(request);

assertTrue(exists(onSuccessFailingFieldDir));
assertThat(onSuccessFailingFieldDir).exists();
}

@AfterAll
Expand Down Expand Up @@ -201,17 +204,18 @@ class TempDirParameterTests {
private static Path onSuccessPassingParameterDir;

/**
* Ensure the cleanup modes defaults to ALWAYS for parameters.
* Ensure the cleanup mode defaults to ALWAYS for parameters.
* <p/>
* Expect the TempDir to be cleaned up.
*/
@Test
void testCleanupModeDefaultParameter() {
LauncherDiscoveryRequest request = request().selectors(
selectMethod(DefaultParameterCase.class, "testDefaultParameter", "java.nio.file.Path")).build();
void cleanupModeDefaultParameter() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(DefaultParameterCase.class, "testDefaultParameter", "java.nio.file.Path"))//
.build();
executeTests(request);

assertFalse(exists(defaultParameterDir));
assertThat(defaultParameterDir).doesNotExist();
}

/**
Expand All @@ -220,12 +224,13 @@ void testCleanupModeDefaultParameter() {
* Expect the TempDir not to be cleaned up.
*/
@Test
void testCleanupModeNeverParameter() {
LauncherDiscoveryRequest request = request().selectors(
selectMethod(NeverParameterCase.class, "testNeverParameter", "java.nio.file.Path")).build();
void cleanupModeNeverParameter() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(NeverParameterCase.class, "testNeverParameter", "java.nio.file.Path"))//
.build();
executeTests(request);

assertTrue(exists(neverParameterDir));
assertThat(neverParameterDir).exists();
}

/**
Expand All @@ -234,12 +239,13 @@ void testCleanupModeNeverParameter() {
* Expect the TempDir to be cleaned up.
*/
@Test
void testCleanupModeAlwaysParameter() {
LauncherDiscoveryRequest request = request().selectors(
selectMethod(AlwaysParameterCase.class, "testAlwaysParameter", "java.nio.file.Path")).build();
void cleanupModeAlwaysParameter() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(AlwaysParameterCase.class, "testAlwaysParameter", "java.nio.file.Path"))//
.build();
executeTests(request);

assertFalse(exists(alwaysParameterDir));
assertThat(alwaysParameterDir).doesNotExist();
}

/**
Expand All @@ -248,12 +254,14 @@ void testCleanupModeAlwaysParameter() {
* Expect the TempDir to be cleaned up.
*/
@Test
void testCleanupModeOnSuccessPassingParameter() {
LauncherDiscoveryRequest request = request().selectors(selectMethod(OnSuccessPassingParameterCase.class,
"testOnSuccessPassingParameter", "java.nio.file.Path")).build();
void cleanupModeOnSuccessPassingParameter() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(OnSuccessPassingParameterCase.class, "testOnSuccessPassingParameter",
"java.nio.file.Path"))//
.build();
executeTests(request);

assertFalse(exists(onSuccessPassingParameterDir));
assertThat(onSuccessPassingParameterDir).doesNotExist();
}

/**
Expand All @@ -262,12 +270,14 @@ void testCleanupModeOnSuccessPassingParameter() {
* Expect the TempDir not to be cleaned up.
*/
@Test
void testCleanupModeOnSuccessFailingParameter() {
LauncherDiscoveryRequest request = request().selectors(selectMethod(OnSuccessFailingParameterCase.class,
"testOnSuccessFailingParameter", "java.nio.file.Path")).build();
void cleanupModeOnSuccessFailingParameter() {
LauncherDiscoveryRequest request = request()//
.selectors(selectMethod(OnSuccessFailingParameterCase.class, "testOnSuccessFailingParameter",
"java.nio.file.Path"))//
.build();
executeTests(request);

assertTrue(exists(onSuccessFailingParameterDir));
assertThat(onSuccessFailingParameterDir).exists();
}

@AfterAll
Expand Down

0 comments on commit ce51f20

Please sign in to comment.