Skip to content
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

Render relative links to file attachments #207

Merged
merged 6 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion html-report/src/components/Block.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ defineProps<{ block: BlockData, depth: number }>()
'border-b': index < Object.entries(block.content).length - 1
}" class="border-neutral-200 dark:border-neutral-700 w-full">
<td class="w-44 pr-4 text-sm">{{ pair[0] }}</td>
<td class="break-all"><code class="text-sm">{{ pair[1] }}</code>
<td class="break-all">
<a v-if="(pair[1] as string).startsWith('link:')" :href="(pair[1] as string).substring(5)" class="text-blue-600 dark:text-blue-500 hover:underline">
<code class="text-sm">{{ (pair[1] as string).substring(5) }}</code>
</a>
<code v-else class="text-sm">{{ pair[1] }}</code>
</td>
</tr>
</tbody>
Expand Down
18 changes: 9 additions & 9 deletions sample-project/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ tasks {
options.release = 8
}

val eventXmlFiles =
files(test.map { it.reports.junitXml.outputLocation.get().asFileTree.matching { include("open-test-report.xml") } }).builtBy(test)
val eventXmlFile =
test.map { it.reports.junitXml.outputLocation.get().file("open-test-report.xml") }

val convertTestResultXmlToHierarchicalFormat by registering(JavaExec::class) {
mainClass.set("org.opentest4j.reporting.cli.ReportingCli")
args("convert")
classpath(cliClasspath)
inputs.files(eventXmlFiles).withPathSensitivity(NONE).skipWhenEmpty()
inputs.file(eventXmlFile).withPathSensitivity(NONE).skipWhenEmpty()
argumentProviders += CommandLineArgumentProvider {
listOf(eventXmlFiles.singleFile.absolutePath)
listOf(eventXmlFile.get().asFile.absolutePath)
}
outputs.files(provider { eventXmlFiles.firstOrNull()?.resolveSibling("hierarchy.xml") })
outputs.files(eventXmlFile.map { it.asFile.resolveSibling("hierarchy.xml") })
outputs.cacheIf { true }
}

Expand All @@ -51,19 +51,19 @@ tasks {
args("html-report")
classpath(cliClasspath)
outputs.file(htmlReportFile)
inputs.files(eventXmlFiles).withPathSensitivity(NONE).skipWhenEmpty()
inputs.files(eventXmlFile).withPathSensitivity(NONE).skipWhenEmpty()
argumentProviders += CommandLineArgumentProvider {
listOf(
"--output",
htmlReportFile.get().asFile.absolutePath
) + eventXmlFiles.files.map { it.absolutePath }.toList()
) + eventXmlFile.get().asFile.absolutePath
}
outputs.cacheIf { true }
}

configurations.consumable("htmlReport") {
outgoing.artifact(generateHtmlReport.map { it.outputs.files.singleFile })
configurations.consumable("xmlReport") {
attributes {
outgoing.artifact(eventXmlFile)
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.RESOURCES))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ static void beforeAll(TestReporter reporter) {
@Order(1)
void successful(TestReporter reporter) {
reporter.publishEntry("✅");
reporter.publishFile("test.txt", file -> Files.write(file, singletonList("Hello, World!")));
}

@Test
Expand Down Expand Up @@ -63,6 +62,8 @@ void failed(TestReporter reporter) {
map.put("baz", "qux");
reporter.publishEntry(map);

reporter.publishFile("test.txt", file -> Files.write(file, singletonList("Hello, World!")));

assertEquals("foo", "bar");
}
}
12 changes: 6 additions & 6 deletions tooling-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

val htmlReportTemplate = configurations.dependencyScope("htmlReportTemplate")
val sampleHtmlReport = configurations.dependencyScope("sampleHtmlReport")
val sampleXmlReport = configurations.dependencyScope("sampleXmlReport")

dependencies {
api(projects.schema)
Expand All @@ -19,7 +19,7 @@ dependencies {
testCompileOnly(libs.jetbrains.annotations)

htmlReportTemplate(projects.htmlReport)
sampleHtmlReport(project(mapOf("path" to projects.sampleProject.identityPath, "configuration" to "htmlReport")))
sampleXmlReport(project(mapOf("path" to projects.sampleProject.identityPath, "configuration" to "xmlReport")))
}

tasks.compileJava {
Expand All @@ -34,14 +34,14 @@ val installPlaywrightDeps by tasks.registering {
doFirst(playwrightInstallationAction)
}

val sampleHtmlReportFiles = configurations.resolvable("sampleHtmlReportFiles") {
extendsFrom(sampleHtmlReport.get())
val sampleXmlReportFiles = configurations.resolvable("sampleXmlReportFiles") {
extendsFrom(sampleXmlReport.get())
}

tasks.test {
inputs.files(sampleHtmlReportFiles).withPathSensitivity(PathSensitivity.NONE)
inputs.files(sampleXmlReportFiles).withPathSensitivity(PathSensitivity.NONE)
jvmArgumentProviders.add(CommandLineArgumentProvider {
listOf("-DsampleHtmlReport=${sampleHtmlReportFiles.get().singleFile.absolutePath}")
listOf("-DsampleXmlReport=${sampleXmlReportFiles.get().singleFile.absolutePath}")
})
if (System.getenv("CI") != null) {
doFirst(playwrightInstallationAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.opentest4j.reporting.tooling.spi.htmlreport.Subsections;
import org.w3c.dom.Element;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -48,19 +49,19 @@
public class CoreContributor implements Contributor {

@Override
public List<Section> contributeSectionsForExecution(Element executionElement) {
public List<Section> contributeSectionsForExecution(Context context) {
var sections = new ArrayList<Section>();
createInfrastructureSection(executionElement).ifPresent(sections::add);
createInfrastructureSection(context.element()).ifPresent(sections::add);
return sections;
}

@Override
public List<Section> contributeSectionsForTestNode(Element testNodeElement) {
public List<Section> contributeSectionsForTestNode(Context context) {
var sections = new ArrayList<Section>();
createTagsSection(testNodeElement).ifPresent(sections::add);
createSourcesSection(testNodeElement).ifPresent(sections::add);
createReasonSection(testNodeElement).ifPresent(sections::add);
createAttachmentsSection(testNodeElement).ifPresent(sections::add);
createTagsSection(context.element()).ifPresent(sections::add);
createSourcesSection(context.element()).ifPresent(sections::add);
createReasonSection(context.element()).ifPresent(sections::add);
createAttachmentsSection(context).ifPresent(sections::add);
return sections;
}

Expand Down Expand Up @@ -134,16 +135,24 @@ private static Optional<Section> createReasonSection(Element element) {
return Optional.of(Section.builder().title("Reason").order(20).addBlock(paragraph).build());
}

private static Optional<Section> createAttachmentsSection(Element element) {
var children = $(element).children("attachments").children().get();
private static Optional<Section> createAttachmentsSection(Context context) {
var children = $(context.element()).children("attachments").children().get();
if (children.isEmpty()) {
return Optional.empty();
}

var subsections = Subsections.builder();
children.stream().map(child -> {
var attributes = KeyValuePairs.builder();
stream(child.getAttributes()).forEach(it -> attributes.putContent(it.getNodeName(), it.getNodeValue()));
if ("file".equals(child.getLocalName())) {
attributes.putContent("time", $(child).attr("time"));
var originalPath = context.sourceXmlFile().getParent().resolve($(child).attr("path")).toAbsolutePath();
attributes.putContent("path",
"link:" + tryRelativize(context.targetHtmlFile().getParent(), originalPath));
}
else {
stream(child.getAttributes()).forEach(it -> attributes.putContent(it.getNodeName(), it.getNodeValue()));
}
if ("data".equals(child.getLocalName())) {
$(child).children("entry").forEach(entry -> {
var key = entry.getAttribute("key");
Expand All @@ -158,6 +167,16 @@ private static Optional<Section> createAttachmentsSection(Element element) {
return Optional.of(Section.builder().title("Attachments").order(30).addBlock(subsections.build()).build());
}

private static Path tryRelativize(Path parent, Path path) {
try {
return parent.relativize(path);
}
catch (IllegalArgumentException e) {
// This can happen on Windows if the paths are on different drives
return path;
}
}

static void addToTable(Match march, QualifiedName elementName, String label, BiConsumer<String, String> table) {
var value = findChild(march, elementName).text();
if (value != null) {
Expand Down
Loading