Skip to content

Commit acc6385

Browse files
committed
Allow publishing files to an existing directory
Prior to this commit, both `TestReporter` and `ExtensionContext` threw an exception when `publishDirectory` was called with an existing directory. Now, they only attempt to create the directory if it doesn't already exist.
1 parent bbfbdcb commit acc6385

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.13.0.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ to start reporting discovery issues.
188188
failures.
189189
* Add support for Kotlin `Sequence` to `@MethodSource`, `@FieldSource`, and
190190
`@TestFactory`.
191+
* Allow publishing files to an existing directory via `TestReporter` and
192+
`ExtensionContext`, for example, when re-running a test class.
191193

192194

193195
[[release-notes-5.13.0-junit-vintage]]

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExtensionContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ default void publishReportEntry(String value) {
419419
* and attach it to the current test or container.
420420
*
421421
* <p>The directory will be resolved and created in the report output directory
422-
* prior to invoking the supplied action.
422+
* prior to invoking the supplied action, if it doesn't already exist.
423423
*
424424
* @param name the name of the directory to be attached; never {@code null}
425425
* or blank and must not contain any path separators

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/AbstractExtensionContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ public void publishDirectory(String name, ThrowingConsumer<Path> action) {
161161
Preconditions.notNull(action, "action must not be null");
162162

163163
ThrowingConsumer<Path> enhancedAction = path -> {
164-
Files.createDirectory(path);
164+
if (!Files.isDirectory(path)) {
165+
Files.createDirectory(path);
166+
}
165167
action.accept(path);
166168
};
167169
publishFileEntry(name, enhancedAction, file -> {

jupiter-tests/src/test/java/org/junit/jupiter/engine/descriptor/ExtensionContextTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ void failsWhenAttemptingToPublishRegularFilesAsDirectories(@TempDir Path tempDir
382382
"Published path must be a directory: " + tempDir.resolve("OuterClass").resolve("test"));
383383
}
384384

385+
@Test
386+
void allowsPublishingToTheSameDirectoryTwice(@TempDir Path tempDir) {
387+
var extensionContext = createExtensionContextForFilePublishing(tempDir);
388+
389+
extensionContext.publishDirectory("test",
390+
dir -> Files.writeString(dir.resolve("nested1.txt"), "Nested content 1"));
391+
extensionContext.publishDirectory("test",
392+
dir -> Files.writeString(dir.resolve("nested2.txt"), "Nested content 2"));
393+
394+
assertThat(tempDir.resolve("OuterClass/test/nested1.txt")).hasContent("Nested content 1");
395+
assertThat(tempDir.resolve("OuterClass/test/nested2.txt")).hasContent("Nested content 2");
396+
}
397+
385398
private ExtensionContext createExtensionContextForFilePublishing(Path tempDir) {
386399
return createExtensionContextForFilePublishing(tempDir, mock(EngineExecutionListener.class),
387400
outerClassDescriptor(null));

0 commit comments

Comments
 (0)