Skip to content

Commit 3b84604

Browse files
committed
Add tests for Git info in Open Test Reporting XML output
1 parent 33679e7 commit 3b84604

File tree

3 files changed

+85
-16
lines changed

3 files changed

+85
-16
lines changed

platform-tests/src/processStarter/java/org/junit/platform/tests/process/ProcessStarter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public ProcessResult startAndWait() throws InterruptedException {
6767
}
6868

6969
public WatchedProcess start() {
70-
var command = Stream.concat(Stream.of(executable.toAbsolutePath().toString()), arguments.stream()).toList();
70+
var command = Stream.concat(Stream.of(executable.toString()), arguments.stream()).toList();
7171
try {
7272
var builder = new ProcessBuilder().command(command);
7373
if (workingDir != null) {

platform-tests/src/test/java/org/junit/platform/reporting/open/xml/OpenTestReportGeneratingListenerTests.java

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
import static java.util.Objects.requireNonNull;
1414
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
1516
import static org.junit.jupiter.api.Assertions.fail;
16-
import static org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS;
17+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
1718
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
1819
import static org.junit.platform.launcher.LauncherConstants.OUTPUT_DIR_PROPERTY_NAME;
1920
import static org.junit.platform.launcher.LauncherConstants.OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER;
@@ -23,15 +24,21 @@
2324
import static org.junit.platform.reporting.testutil.FileUtils.findPath;
2425

2526
import java.net.URISyntaxException;
27+
import java.nio.file.Files;
2628
import java.nio.file.Path;
27-
import java.util.regex.Pattern;
29+
import java.util.Map;
2830

2931
import org.junit.jupiter.api.Test;
3032
import org.junit.jupiter.api.io.TempDir;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.ValueSource;
3135
import org.junit.platform.engine.TestEngine;
3236
import org.junit.platform.engine.UniqueId;
3337
import org.junit.platform.engine.reporting.ReportEntry;
3438
import org.junit.platform.engine.support.hierarchical.DemoHierarchicalTestEngine;
39+
import org.junit.platform.tests.process.ProcessResult;
40+
import org.junit.platform.tests.process.ProcessStarter;
41+
import org.opentest4j.reporting.schema.Namespace;
3542
import org.opentest4j.reporting.tooling.core.validator.DefaultValidator;
3643
import org.opentest4j.reporting.tooling.core.validator.ValidationResult;
3744
import org.xmlunit.assertj3.XmlAssert;
@@ -44,19 +51,16 @@
4451
*/
4552
public class OpenTestReportGeneratingListenerTests {
4653

47-
@TempDir(cleanup = ON_SUCCESS)
48-
Path tempDirectory;
49-
5054
@Test
51-
void writesValidXmlReport() throws Exception {
55+
void writesValidXmlReport(@TempDir Path tempDirectory) throws Exception {
5256
var engine = new DemoHierarchicalTestEngine("dummy");
5357
engine.addTest("failingTest", "display<-->Name 😎", (context, descriptor) -> {
5458
var listener = context.request.getEngineExecutionListener();
5559
listener.reportingEntryPublished(descriptor, ReportEntry.from("key", "value"));
5660
fail("failure message");
5761
});
5862

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

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

117121
XmlAssert.assertThat(xmlFile).and(expected) //
118-
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator(Pattern.quote("${"), Pattern.quote("}"),
119-
Pattern.quote("#"), Pattern.quote("#"), ",")) //
122+
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()) //
120123
.ignoreWhitespace() //
121124
.areIdentical();
122125
}
123126

127+
@ParameterizedTest
128+
@ValueSource(strings = { "https://github.com/junit-team/junit5.git", "[email protected]:junit-team/junit5.git" })
129+
void includesGitInfo(String originUrl, @TempDir Path tempDirectory) throws Exception {
130+
131+
assumeTrue(tryExecGit(tempDirectory, "--version").exitCode() == 0, "git not installed");
132+
execGit(tempDirectory, "init", "--initial-branch=my_branch");
133+
execGit(tempDirectory, "remote", "add", "origin", originUrl);
134+
135+
Files.writeString(tempDirectory.resolve("README.md"), "Hello, world!");
136+
execGit(tempDirectory, "add", ".");
137+
138+
execGit(tempDirectory, "config", "user.name", "Alice");
139+
execGit(tempDirectory, "config", "user.email", "[email protected]");
140+
execGit(tempDirectory, "commit", "-m", "Initial commit");
141+
142+
var engine = new DemoHierarchicalTestEngine("dummy");
143+
144+
executeTests(tempDirectory, engine, tempDirectory.resolve("junit-reports"));
145+
146+
var xmlFile = findPath(tempDirectory, "glob:**/open-test-report.xml");
147+
assertThat(validate(xmlFile)).isEmpty();
148+
149+
var namespaceContext = Map.of("core", Namespace.REPORTING_CORE.getUri(), "e",
150+
Namespace.REPORTING_EVENTS.getUri(), "git", Namespace.REPORTING_GIT.getUri());
151+
152+
XmlAssert.assertThat(xmlFile) //
153+
.withNamespaceContext(namespaceContext) //
154+
.valueByXPath("/e:events/core:infrastructure/git:repository/@originUrl") //
155+
.isEqualTo(originUrl);
156+
157+
XmlAssert.assertThat(xmlFile) //
158+
.withNamespaceContext(namespaceContext) //
159+
.valueByXPath("/e:events/core:infrastructure/git:branch") //
160+
.isEqualTo("my_branch");
161+
162+
var commitHash = execGit(tempDirectory, "rev-parse", "--verify", "HEAD").stdOut().trim();
163+
XmlAssert.assertThat(xmlFile) //
164+
.withNamespaceContext(namespaceContext) //
165+
.valueByXPath("/e:events/core:infrastructure/git:commit") //
166+
.isEqualTo(commitHash);
167+
168+
XmlAssert.assertThat(xmlFile) //
169+
.withNamespaceContext(namespaceContext) //
170+
.valueByXPath("/e:events/core:infrastructure/git:status/@clean") //
171+
.isEqualTo(false);
172+
173+
XmlAssert.assertThat(xmlFile) //
174+
.withNamespaceContext(namespaceContext) //
175+
.valueByXPath("/e:events/core:infrastructure/git:status") //
176+
.startsWith("?? junit-reports");
177+
}
178+
179+
private static ProcessResult execGit(Path workingDir, String... arguments) throws InterruptedException {
180+
var result = tryExecGit(workingDir, arguments);
181+
assertEquals(0, result.exitCode(), "git " + String.join(" ", arguments) + " failed");
182+
return result;
183+
}
184+
185+
private static ProcessResult tryExecGit(Path workingDir, String... arguments) throws InterruptedException {
186+
System.out.println("$ git " + String.join(" ", arguments));
187+
return new ProcessStarter() //
188+
.executable(Path.of("git")) //
189+
.workingDir(workingDir) //
190+
.addArguments(arguments) //
191+
.startAndWait();
192+
}
193+
124194
private ValidationResult validate(Path xmlFile) throws URISyntaxException {
125195
var catalogUri = requireNonNull(getClass().getResource("catalog.xml")).toURI();
126196
return new DefaultValidator(catalogUri).validate(xmlFile);
127197
}
128198

129-
private void executeTests(TestEngine engine) {
199+
private void executeTests(Path tempDirectory, TestEngine engine, Path outputDir) {
130200
var build = request() //
131201
.selectors(selectUniqueId(UniqueId.forEngine(engine.getId()))) //
132202
.configurationParameter(ENABLED_PROPERTY_NAME, String.valueOf(true)) //
133-
.configurationParameter(OUTPUT_DIR_PROPERTY_NAME,
134-
tempDirectory.resolve("junit-" + OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER).toString()) //
203+
.configurationParameter(OUTPUT_DIR_PROPERTY_NAME, outputDir.toString()) //
135204
.build();
136205
createLauncher(engine).execute(build, new OpenTestReportGeneratingListener(tempDirectory));
137206
}

platform-tooling-support-tests/src/main/java/platform/tooling/support/ProcessStarters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static ProcessStarter javaCommand(Path javaHome, String commandName) {
4545

4646
public static ProcessStarter gradlew() {
4747
return new ProcessStarter() //
48-
.executable(Path.of("..").resolve(windowsOrOtherExecutable("gradlew.bat", "gradlew"))) //
48+
.executable(Path.of("..").resolve(windowsOrOtherExecutable("gradlew.bat", "gradlew")).toAbsolutePath()) //
4949
.putEnvironment("JAVA_HOME", getGradleJavaHome().orElseThrow(TestAbortedException::new)) //
5050
.addArguments("-PjupiterVersion=" + Helper.version("junit-jupiter")) //
5151
.addArguments("-PvintageVersion=" + Helper.version("junit-vintage")) //
@@ -55,7 +55,7 @@ public static ProcessStarter gradlew() {
5555
public static ProcessStarter maven() {
5656
return new ProcessStarter() //
5757
.executable(Path.of(System.getProperty("mavenDistribution")).resolve("bin").resolve(
58-
windowsOrOtherExecutable("mvn.cmd", "mvn"))) //
58+
windowsOrOtherExecutable("mvn.cmd", "mvn")).toAbsolutePath()) //
5959
.addArguments("-Djunit.jupiter.version=" + Helper.version("junit-jupiter")) //
6060
.addArguments("-Djunit.bom.version=" + Helper.version("junit-jupiter")) //
6161
.addArguments("-Djunit.vintage.version=" + Helper.version("junit-vintage")) //

0 commit comments

Comments
 (0)