Skip to content

Commit

Permalink
Add tests for Git info in Open Test Reporting XML output
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Nov 28, 2024
1 parent 6eea108 commit 6294364
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ProcessResult startAndWait() throws InterruptedException {
}

public WatchedProcess start() {
var command = Stream.concat(Stream.of(executable.toAbsolutePath().toString()), arguments.stream()).toList();
var command = Stream.concat(Stream.of(executable.toString()), arguments.stream()).toList();
try {
var builder = new ProcessBuilder().command(command);
if (workingDir != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
import static org.junit.platform.launcher.LauncherConstants.OUTPUT_DIR_PROPERTY_NAME;
import static org.junit.platform.launcher.LauncherConstants.OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER;
Expand All @@ -23,15 +24,21 @@
import static org.junit.platform.reporting.testutil.FileUtils.findPath;

import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.platform.engine.TestEngine;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine;
import org.junit.platform.tests.process.ProcessResult;
import org.junit.platform.tests.process.ProcessStarter;
import org.opentest4j.reporting.schema.Namespace;
import org.opentest4j.reporting.tooling.core.validator.DefaultValidator;
import org.opentest4j.reporting.tooling.core.validator.ValidationResult;
import org.xmlunit.assertj3.XmlAssert;
Expand All @@ -44,19 +51,16 @@
*/
public class OpenTestReportGeneratingListenerTests {

@TempDir(cleanup = ON_SUCCESS)
Path tempDirectory;

@Test
void writesValidXmlReport() throws Exception {
void writesValidXmlReport(@TempDir Path tempDirectory) throws Exception {
var engine = new DemoHierarchicalTestEngine("dummy");
engine.addTest("failingTest", "display<-->Name 😎", (context, descriptor) -> {
var listener = context.request.getEngineExecutionListener();
listener.reportingEntryPublished(descriptor, ReportEntry.from("key", "value"));
fail("failure message");
});

executeTests(engine);
executeTests(tempDirectory, engine, tempDirectory.resolve("junit-" + OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER));

var xmlFile = findPath(tempDirectory, "glob:**/open-test-report.xml");
assertThat(tempDirectory.relativize(xmlFile).toString()) //
Expand Down Expand Up @@ -104,7 +108,7 @@ void writesValidXmlReport() throws Exception {
<e:finished id="2" time="${xmlunit.isDateTime}">
<result status="FAILED">
<java:throwable assertionError="true" type="org.opentest4j.AssertionFailedError">
${xmlunit.matchesRegex#org\\.opentest4j\\.AssertionFailedError: failure message#}
${xmlunit.matchesRegex(org\\.opentest4j\\.AssertionFailedError: failure message)}
</java:throwable>
</result>
</e:finished>
Expand All @@ -115,23 +119,88 @@ void writesValidXmlReport() throws Exception {
""";

XmlAssert.assertThat(xmlFile).and(expected) //
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator(Pattern.quote("${"), Pattern.quote("}"),
Pattern.quote("#"), Pattern.quote("#"), ",")) //
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()) //
.ignoreWhitespace() //
.areIdentical();
}

@ParameterizedTest
@ValueSource(strings = { "https://github.com/junit-team/junit5.git", "[email protected]:junit-team/junit5.git" })
void includesGitInfo(String originUrl, @TempDir Path tempDirectory) throws Exception {

assumeTrue(tryExecGit(tempDirectory, "--version").exitCode() == 0, "git not installed");
execGit(tempDirectory, "init", "--initial-branch=my_branch");
execGit(tempDirectory, "remote", "add", "origin", originUrl);

Files.writeString(tempDirectory.resolve("README.md"), "Hello, world!");
execGit(tempDirectory, "add", ".");

execGit(tempDirectory, "config", "user.name", "Alice");
execGit(tempDirectory, "config", "user.email", "[email protected]");
execGit(tempDirectory, "commit", "-m", "Initial commit");

var engine = new DemoHierarchicalTestEngine("dummy");

executeTests(tempDirectory, engine, tempDirectory.resolve("junit-reports"));

var xmlFile = findPath(tempDirectory, "glob:**/open-test-report.xml");
assertThat(validate(xmlFile)).isEmpty();

var namespaceContext = Map.of("core", Namespace.REPORTING_CORE.getUri(), "e",
Namespace.REPORTING_EVENTS.getUri(), "git", Namespace.REPORTING_GIT.getUri());

XmlAssert.assertThat(xmlFile) //
.withNamespaceContext(namespaceContext) //
.valueByXPath("/e:events/core:infrastructure/git:repository/@originUrl") //
.isEqualTo(originUrl);

XmlAssert.assertThat(xmlFile) //
.withNamespaceContext(namespaceContext) //
.valueByXPath("/e:events/core:infrastructure/git:branch") //
.isEqualTo("my_branch");

var commitHash = execGit(tempDirectory, "rev-parse", "--verify", "HEAD").stdOut().trim();
XmlAssert.assertThat(xmlFile) //
.withNamespaceContext(namespaceContext) //
.valueByXPath("/e:events/core:infrastructure/git:commit") //
.isEqualTo(commitHash);

XmlAssert.assertThat(xmlFile) //
.withNamespaceContext(namespaceContext) //
.valueByXPath("/e:events/core:infrastructure/git:status/@clean") //
.isEqualTo(false);

XmlAssert.assertThat(xmlFile) //
.withNamespaceContext(namespaceContext) //
.valueByXPath("/e:events/core:infrastructure/git:status") //
.startsWith("?? junit-reports");
}

private static ProcessResult execGit(Path workingDir, String... arguments) throws InterruptedException {
var result = tryExecGit(workingDir, arguments);
assertEquals(0, result.exitCode(), "git " + String.join(" ", arguments) + " failed");
return result;
}

private static ProcessResult tryExecGit(Path workingDir, String... arguments) throws InterruptedException {
System.out.println("$ git " + String.join(" ", arguments));
return new ProcessStarter() //
.executable(Path.of("git")) //
.workingDir(workingDir) //
.addArguments(arguments) //
.startAndWait();
}

private ValidationResult validate(Path xmlFile) throws URISyntaxException {
var catalogUri = requireNonNull(getClass().getResource("catalog.xml")).toURI();
return new DefaultValidator(catalogUri).validate(xmlFile);
}

private void executeTests(TestEngine engine) {
private void executeTests(Path tempDirectory, TestEngine engine, Path outputDir) {
var build = request() //
.selectors(selectUniqueId(UniqueId.forEngine(engine.getId()))) //
.configurationParameter(ENABLED_PROPERTY_NAME, String.valueOf(true)) //
.configurationParameter(OUTPUT_DIR_PROPERTY_NAME,
tempDirectory.resolve("junit-" + OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER).toString()) //
.configurationParameter(OUTPUT_DIR_PROPERTY_NAME, outputDir.toString()) //
.build();
createLauncher(engine).execute(build, new OpenTestReportGeneratingListener(tempDirectory));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static ProcessStarter javaCommand(Path javaHome, String commandName) {

public static ProcessStarter gradlew() {
return new ProcessStarter() //
.executable(Path.of("..").resolve(windowsOrOtherExecutable("gradlew.bat", "gradlew"))) //
.executable(Path.of("..").resolve(windowsOrOtherExecutable("gradlew.bat", "gradlew")).toAbsolutePath()) //
.putEnvironment("JAVA_HOME", getGradleJavaHome().orElseThrow(TestAbortedException::new)) //
.addArguments("-PjupiterVersion=" + Helper.version("junit-jupiter")) //
.addArguments("-PvintageVersion=" + Helper.version("junit-vintage")) //
Expand All @@ -55,7 +55,7 @@ public static ProcessStarter gradlew() {
public static ProcessStarter maven() {
return new ProcessStarter() //
.executable(Path.of(System.getProperty("mavenDistribution")).resolve("bin").resolve(
windowsOrOtherExecutable("mvn.cmd", "mvn"))) //
windowsOrOtherExecutable("mvn.cmd", "mvn")).toAbsolutePath()) //
.addArguments("-Djunit.jupiter.version=" + Helper.version("junit-jupiter")) //
.addArguments("-Djunit.bom.version=" + Helper.version("junit-jupiter")) //
.addArguments("-Djunit.vintage.version=" + Helper.version("junit-vintage")) //
Expand Down

0 comments on commit 6294364

Please sign in to comment.