Skip to content

Commit

Permalink
Fix ant compatible xml reporting for scala tests (pantsbuild#8674)
Browse files Browse the repository at this point in the history
### Problem

Recent changes to ScalaTestUtil (specifically the ScalaTestJunitRunnerWrapper facade) broke the xml reporting (AntJUnitXmlReporter) because the description of the runner had changed and didn't describe children of the suite correctly.

### Solution

The solution implemented here, calls the getDescription method on the delegate as should be.

### Result

This fixes the regression introduced by replacing the original JUnitRunner of scalatests by ScalaTestJunitRunnerWrapper
  • Loading branch information
SergeKireev authored and Eric-Arellano committed Nov 21, 2019
1 parent a26dd9c commit faba1d8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ class TestFilter extends Filter {

@Override
public boolean shouldRun(Description desc) {
if (desc.isSuite()) {
if (desc.isSuite() && !ScalaTestUtil.isScalaTestTest(desc.getTestClass())) {
return true;
}
String descString = Util.getPantsFriendlyDisplayName(desc);
Expand Down
10 changes: 4 additions & 6 deletions src/java/org/pantsbuild/tools/junit/impl/ScalaTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ private ScalaTestUtil() {}
* TODO: Remove this when https://github.com/scalatest/scalatestplus-junit/pull/8 is merged
*/
private static class ScalaTestJunitRunnerWrapper extends Runner implements Filterable {
private Runner delegate;
private Class<?> suite;
private final Runner delegate;

private ScalaTestJunitRunnerWrapper(Runner delegate, Class<?> suite) {
private ScalaTestJunitRunnerWrapper(Runner delegate) {
this.delegate = delegate;
this.suite = suite;
}

@Override
public Description getDescription() {
return Description.createSuiteDescription(suite);
return delegate.getDescription();
}

@Override
Expand All @@ -61,7 +59,7 @@ public void filter(Filter filter) throws NoTestsRemainException {
*/
public static Runner getJUnitRunner(Class<?> clazz) throws Exception {
return new ScalaTestJunitRunnerWrapper(
(Runner) junitRunnerClass.getConstructor(Class.class).newInstance(clazz), clazz);
(Runner) junitRunnerClass.getConstructor(Class.class).newInstance(clazz));
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/java/org/pantsbuild/tools/junit/impl/XmlReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.pantsbuild.tools.junit.lib.XmlReportIgnoredTestSuiteTest;
import org.pantsbuild.tools.junit.lib.XmlReportMockitoStubbingTest;
import org.pantsbuild.tools.junit.lib.XmlReportTestSuite;
import org.pantsbuild.tools.junit.lib.MockScalaTest;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
Expand Down Expand Up @@ -337,6 +338,27 @@ public void testXmlErrorInTestRunnerInitialization() throws Exception {
containsString(FailingTestRunner.class.getCanonicalName() + ".getTestRules("));
}

@Test
public void testXmlReportOnScalaSuite() throws Exception {
String testClassName = MockScalaTest.class.getCanonicalName();
AntJunitXmlReportListener.TestSuite testSuite = runTestAndParseXml(testClassName, false);

assertNotNull(testSuite);
assertEquals(1, testSuite.getTests());
assertEquals(0, testSuite.getFailures());
assertEquals(0, testSuite.getErrors());
assertEquals(0, testSuite.getSkipped());
assertEquals(testClassName, testSuite.getName());

List<AntJunitXmlReportListener.TestCase> testCases = testSuite.getTestCases();
assertEquals(1, testCases.size());

AntJunitXmlReportListener.TestCase testCase = testCases.get(0);
assertEquals(testClassName, testCase.getClassname());
assertNull(testCase.getFailure());
assertNull(testCase.getError());
}

protected File runTestAndReturnXmlFile(String testClassName, boolean shouldFail)
throws IOException, JAXBException {
String outdirPath = temporary.newFolder("testOutputDir").getAbsolutePath();
Expand Down

0 comments on commit faba1d8

Please sign in to comment.