Skip to content

Commit 31aadda

Browse files
authored
Ensure TempDir cleanup is executed when validation fails (junit-team#3911)
1 parent c546fe8 commit 31aadda

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.junit.jupiter.engine.config.EnumConfigurationParameterConverter;
5858
import org.junit.jupiter.engine.config.JupiterConfiguration;
5959
import org.junit.platform.commons.JUnitException;
60+
import org.junit.platform.commons.PreconditionViolationException;
6061
import org.junit.platform.commons.logging.Logger;
6162
import org.junit.platform.commons.logging.LoggerFactory;
6263
import org.junit.platform.commons.util.ExceptionUtils;
@@ -286,15 +287,15 @@ static class CloseablePath implements CloseableResource {
286287

287288
private CloseablePath(TempDirFactory factory, CleanupMode cleanupMode, AnnotatedElementContext elementContext,
288289
ExtensionContext extensionContext) throws Exception {
289-
this.dir = validateTempDirectory(factory.createTempDirectory(elementContext, extensionContext));
290+
this.dir = factory.createTempDirectory(elementContext, extensionContext);
290291
this.factory = factory;
291292
this.cleanupMode = cleanupMode;
292293
this.extensionContext = extensionContext;
293-
}
294294

295-
private static Path validateTempDirectory(Path dir) {
296-
Preconditions.condition(dir != null && Files.isDirectory(dir), "temp directory must be a directory");
297-
return dir;
295+
if (dir == null || !Files.isDirectory(dir)) {
296+
close();
297+
throw new PreconditionViolationException("temp directory must be a directory");
298+
}
298299
}
299300

300301
Path get() {
@@ -324,7 +325,7 @@ public void close() throws IOException {
324325

325326
private SortedMap<Path, IOException> deleteAllFilesAndDirectories(FileOperations fileOperations)
326327
throws IOException {
327-
if (Files.notExists(dir)) {
328+
if (dir == null || Files.notExists(dir)) {
328329
return Collections.emptySortedMap();
329330
}
330331

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/CloseablePathTests.java

+31-13
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void cleanupRoot() throws IOException {
9898
@Test
9999
@DisplayName("succeeds if the factory returns a directory")
100100
void factoryReturnsDirectory() throws Exception {
101-
TempDirFactory factory = (elementContext, extensionContext) -> createDirectory(root.resolve("directory"));
101+
TempDirFactory factory = spy(new Factory(createDirectory(root.resolve("directory"))));
102102

103103
closeablePath = TempDirectory.createTempDir(factory, DEFAULT, elementContext, extensionContext);
104104
assertThat(closeablePath.get()).isDirectory();
@@ -111,8 +111,7 @@ void factoryReturnsDirectory() throws Exception {
111111
@DisabledOnOs(OS.WINDOWS)
112112
void factoryReturnsSymbolicLinkToDirectory() throws Exception {
113113
Path directory = createDirectory(root.resolve("directory"));
114-
TempDirFactory factory = (elementContext,
115-
extensionContext) -> createSymbolicLink(root.resolve("symbolicLink"), directory);
114+
TempDirFactory factory = spy(new Factory(createSymbolicLink(root.resolve("symbolicLink"), directory)));
116115

117116
closeablePath = TempDirectory.createTempDir(factory, DEFAULT, elementContext, extensionContext);
118117
assertThat(closeablePath.get()).isDirectory();
@@ -123,23 +122,26 @@ void factoryReturnsSymbolicLinkToDirectory() throws Exception {
123122

124123
@Test
125124
@DisplayName("fails if the factory returns null")
126-
void factoryReturnsNull() {
127-
TempDirFactory factory = (elementContext, extensionContext) -> null;
125+
void factoryReturnsNull() throws IOException {
126+
TempDirFactory factory = spy(new Factory(null));
128127

129128
assertThatExtensionConfigurationExceptionIsThrownBy(
130129
() -> TempDirectory.createTempDir(factory, DEFAULT, elementContext, extensionContext));
130+
131+
verify(factory).close();
131132
}
132133

133134
@Test
134135
@DisplayName("fails if the factory returns a file")
135136
void factoryReturnsFile() throws IOException {
136137
Path file = createFile(root.resolve("file"));
137-
TempDirFactory factory = (elementContext, extensionContext) -> file;
138+
TempDirFactory factory = spy(new Factory(file));
138139

139140
assertThatExtensionConfigurationExceptionIsThrownBy(
140141
() -> TempDirectory.createTempDir(factory, DEFAULT, elementContext, extensionContext));
141142

142-
delete(file);
143+
verify(factory).close();
144+
assertThat(file).doesNotExist();
143145
}
144146

145147
@Test
@@ -148,15 +150,27 @@ void factoryReturnsFile() throws IOException {
148150
void factoryReturnsSymbolicLinkToFile() throws IOException {
149151
Path file = createFile(root.resolve("file"));
150152
Path symbolicLink = createSymbolicLink(root.resolve("symbolicLink"), file);
151-
TempDirFactory factory = (elementContext, extensionContext) -> symbolicLink;
153+
TempDirFactory factory = spy(new Factory(symbolicLink));
152154

153155
assertThatExtensionConfigurationExceptionIsThrownBy(
154156
() -> TempDirectory.createTempDir(factory, DEFAULT, elementContext, extensionContext));
155157

156-
delete(symbolicLink);
158+
verify(factory).close();
159+
assertThat(symbolicLink).doesNotExist();
160+
157161
delete(file);
158162
}
159163

164+
// Mockito spying a lambda fails with: VM does not support modification of given type
165+
private record Factory(Path path) implements TempDirFactory {
166+
167+
@Override
168+
public Path createTempDirectory(AnnotatedElementContext elementContext, ExtensionContext extensionContext) {
169+
return path;
170+
}
171+
172+
}
173+
160174
private static void assertThatExtensionConfigurationExceptionIsThrownBy(ThrowingCallable callable) {
161175
assertThatExceptionOfType(ExtensionConfigurationException.class)//
162176
.isThrownBy(callable)//
@@ -194,8 +208,9 @@ void always() throws IOException {
194208
assertThat(closeablePath.get()).isDirectory();
195209

196210
closeablePath.close();
197-
assertThat(closeablePath.get()).doesNotExist();
211+
198212
verify(factory).close();
213+
assertThat(closeablePath.get()).doesNotExist();
199214
}
200215

201216
@Test
@@ -205,8 +220,9 @@ void never() throws IOException {
205220
assertThat(closeablePath.get()).isDirectory();
206221

207222
closeablePath.close();
208-
assertThat(closeablePath.get()).exists();
223+
209224
verify(factory).close();
225+
assertThat(closeablePath.get()).exists();
210226
}
211227

212228
@Test
@@ -218,8 +234,9 @@ void onSuccessWithException() throws IOException {
218234
assertThat(closeablePath.get()).isDirectory();
219235

220236
closeablePath.close();
221-
assertThat(closeablePath.get()).exists();
237+
222238
verify(factory).close();
239+
assertThat(closeablePath.get()).exists();
223240
}
224241

225242
@Test
@@ -231,8 +248,9 @@ void onSuccessWithNoException() throws IOException {
231248
assertThat(closeablePath.get()).isDirectory();
232249

233250
closeablePath.close();
234-
assertThat(closeablePath.get()).doesNotExist();
251+
235252
verify(factory).close();
253+
assertThat(closeablePath.get()).doesNotExist();
236254
}
237255

238256
}

0 commit comments

Comments
 (0)