1212
1313import static java .util .Objects .requireNonNull ;
1414import static org .assertj .core .api .Assertions .assertThat ;
15+ import static org .junit .jupiter .api .Assertions .assertEquals ;
1516import 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 ;
1718import static org .junit .platform .engine .discovery .DiscoverySelectors .selectUniqueId ;
1819import static org .junit .platform .launcher .LauncherConstants .OUTPUT_DIR_PROPERTY_NAME ;
1920import static org .junit .platform .launcher .LauncherConstants .OUTPUT_DIR_UNIQUE_NUMBER_PLACEHOLDER ;
2324import static org .junit .platform .reporting .testutil .FileUtils .findPath ;
2425
2526import java .net .URISyntaxException ;
27+ import java .nio .file .Files ;
2628import java .nio .file .Path ;
27- import java .util .regex . Pattern ;
29+ import java .util .Map ;
2830
2931import org .junit .jupiter .api .Test ;
3032import org .junit .jupiter .api .io .TempDir ;
33+ import org .junit .jupiter .params .ParameterizedTest ;
34+ import org .junit .jupiter .params .provider .ValueSource ;
3135import org .junit .platform .engine .TestEngine ;
3236import org .junit .platform .engine .UniqueId ;
3337import org .junit .platform .engine .reporting .ReportEntry ;
3438import 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 ;
3542import org .opentest4j .reporting .tooling .core .validator .DefaultValidator ;
3643import org .opentest4j .reporting .tooling .core .validator .ValidationResult ;
3744import org .xmlunit .assertj3 .XmlAssert ;
4451 */
4552public 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 }
0 commit comments