Skip to content

Commit 4b27910

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. (cherry picked from commit acc6385)
1 parent b819306 commit 4b27910

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
@@ -420,7 +420,7 @@ default void publishReportEntry(String value) {
420420
* and attach it to the current test or container.
421421
*
422422
* <p>The directory will be resolved and created in the report output directory
423-
* prior to invoking the supplied action.
423+
* prior to invoking the supplied action, if it doesn't already exist.
424424
*
425425
* @param name the name of the directory to be attached; never {@code null}
426426
* 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
@@ -159,7 +159,9 @@ public void publishDirectory(String name, ThrowingConsumer<Path> action) {
159159
Preconditions.notNull(action, "action must not be null");
160160

161161
ThrowingConsumer<Path> enhancedAction = path -> {
162-
Files.createDirectory(path);
162+
if (!Files.isDirectory(path)) {
163+
Files.createDirectory(path);
164+
}
163165
action.accept(path);
164166
};
165167
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
@@ -374,6 +374,19 @@ void failsWhenAttemptingToPublishRegularFilesAsDirectories(@TempDir Path tempDir
374374
"Published path must be a directory: " + tempDir.resolve("OuterClass").resolve("test"));
375375
}
376376

377+
@Test
378+
void allowsPublishingToTheSameDirectoryTwice(@TempDir Path tempDir) {
379+
var extensionContext = createExtensionContextForFilePublishing(tempDir);
380+
381+
extensionContext.publishDirectory("test",
382+
dir -> Files.writeString(dir.resolve("nested1.txt"), "Nested content 1"));
383+
extensionContext.publishDirectory("test",
384+
dir -> Files.writeString(dir.resolve("nested2.txt"), "Nested content 2"));
385+
386+
assertThat(tempDir.resolve("OuterClass/test/nested1.txt")).hasContent("Nested content 1");
387+
assertThat(tempDir.resolve("OuterClass/test/nested2.txt")).hasContent("Nested content 2");
388+
}
389+
377390
private ExtensionContext createExtensionContextForFilePublishing(Path tempDir) {
378391
return createExtensionContextForFilePublishing(tempDir, mock(EngineExecutionListener.class),
379392
outerClassDescriptor(null));

0 commit comments

Comments
 (0)