-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Git info in Open Test Reporting XML output
- Loading branch information
1 parent
6eea108
commit 6294364
Showing
3 changed files
with
85 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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()) // | ||
|
@@ -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> | ||
|
@@ -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)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters