-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spike API for attaching files to test executions #3336
base: main
Are you sure you want to change the base?
Changes from all commits
66c5dc0
6256b39
170bfa6
40e585b
fc073e3
64c22de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2015-2023 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example.vintage; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.vintage.reporting.TestReporting; | ||
|
||
public class VintageTestReportingDemo { | ||
|
||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
@Rule | ||
public TestReporting testReporting = new TestReporting(); | ||
|
||
@Test | ||
public void reportFiles() throws Exception { | ||
Path existingFile = Files.write(temporaryFolder.getRoot().toPath().resolve("test.txt"), singletonList("Test")); | ||
testReporting.publishFile(existingFile); | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,17 @@ | |
|
||
package org.junit.jupiter.api; | ||
|
||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | ||
import static org.apiguardian.api.API.Status.EXPERIMENTAL; | ||
import static org.apiguardian.api.API.Status.STABLE; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.apiguardian.api.API; | ||
import org.junit.jupiter.api.function.ThrowingConsumer; | ||
|
||
/** | ||
* Parameters of type {@code TestReporter} can be injected into | ||
|
@@ -77,4 +82,36 @@ default void publishEntry(String value) { | |
this.publishEntry("value", value); | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a way to "add" a remote file, e.g., via a URI, would be useful. |
||
* Publish the supplied file and attach it to the current test or container. | ||
* <p> | ||
* The file will be copied to the report output directory replacing any | ||
* potentially existing file with the same name. | ||
* | ||
* @param file the file to be attached; never {@code null} or blank | ||
* @since 5.11 | ||
*/ | ||
@API(status = EXPERIMENTAL, since = "5.11") | ||
default void publishFile(Path file) { | ||
publishFile(file.getFileName().toString(), path -> Files.copy(file, path, REPLACE_EXISTING)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would make sense to move the file by default or at least provide an option to do so. |
||
} | ||
|
||
/** | ||
* Publish a file with the supplied name written by the supplied action and | ||
* attach it to the current test or container. | ||
* <p> | ||
* The {@link Path} passed to the supplied action will be relative to the | ||
* report output directory, but it's up to the action to write the file or | ||
* directory. | ||
* | ||
* @param fileName the name of the file to be attached; never {@code null} or blank | ||
* and must not contain any path separators | ||
* @param action the action to be executed to write the file; never {@code null} | ||
* @since 5.11 | ||
*/ | ||
@API(status = EXPERIMENTAL, since = "5.11") | ||
default void publishFile(String fileName, ThrowingConsumer<Path> action) { | ||
throw new UnsupportedOperationException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This interface shouldn't be implemented by clients but is annotated with |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JUnit 4 example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really clean!