Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #794 from roadrunner2/scalatest-maven-plugin-suppo…
…rt-v3

[FIXED JENKINS-18086] Add support for scalatest-maven-plugin test results.
Originally-Committed-As: 12807ba8e917ccd25e44fb669df6e5f057301fe0
  • Loading branch information
kutzi committed May 28, 2013
2 parents efc6518 + b71a768 commit 06c3213
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main/java/hudson/maven/reporters/TestMojo.java
Expand Up @@ -69,7 +69,38 @@ public Collection<File> getReportFiles(MavenProject pom,MojoInfo mojo)
return null;
}
},
TOOLKIT_RESOLVER_PLUGIN("org.terracotta.maven.plugins", "toolkit-resolver-plugin", "toolkit-resolve-test","reportsDirectory");
TOOLKIT_RESOLVER_PLUGIN("org.terracotta.maven.plugins", "toolkit-resolver-plugin", "toolkit-resolve-test","reportsDirectory"),
SCALATEST_MAVEN_PLUGIN("org.scalatest", "scalatest-maven-plugin", "test", null) {
@Override
public Iterable<File> getReportFiles(MavenProject pom, MojoInfo mojo)
throws ComponentConfigurationException {
/* scalatest-maven-plugin has a configuration entry 'junitxml' which is a
* comma-separated list of directories; commas may be escaped with a backslash
* (\,). Each directory is taken relative to the reportsDirectory.
*/
File reportsDir = mojo.getConfigurationValue("reportsDirectory", File.class);
String junitDirs = mojo.getConfigurationValue("junitxml", String.class);

if (junitDirs == null || junitDirs.trim().length() == 0) {
return null;
}

// split along non-escaped commas
String[] junitDirsList = junitDirs.trim().split("(?<!\\\\),");
for (String dir : junitDirsList) {
if (dir.trim().length() > 0) {
// unescape escaped commas
String junitDirName = dir.trim().replaceAll("\\\\,", ",");
File junitDir = new File(reportsDir, junitDirName);
if (junitDir.exists()) {
return super.getReportFiles(junitDir, super.getFileSet(junitDir));
}
}
}

return null;
}
};

private String reportDirectoryConfigKey;
private Key key;
Expand Down
138 changes: 138 additions & 0 deletions src/test/java/hudson/maven/reporters/TestMojoTest.java
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -81,4 +83,140 @@ public void testGetReportFilesAndroidMavenPlugin() throws ComponentConfiguration
testResults.delete();
}
}

@Test
public void testScalatestMavenPluginNoJunitxml() throws Exception {
runScalatestPluginTestNoEntries(null);
}

@Test
public void testScalatestMavenPluginEmptyJunitxml() throws Exception {
runScalatestPluginTestNoEntries("");
}

@Test
public void testScalatestMavenPluginJunitxmlHasListOfEmptyEntries() throws Exception {
runScalatestPluginTestNoEntries(", ");
}

@Test
public void testScalatestMavenPluginJunitxmlEntryDoesntExist() throws Exception {
runScalatestPluginTestNoEntries("foo");
}

@Test
public void testScalatestMavenPluginJunitxmlHasOneEntry() throws Exception {
runScalatestPluginTestOneEntry("ut-xml", "ut-xml");
}

@Test
public void testScalatestMavenPluginJunitxmlHasOneEntryAndTrailingDummy() throws Exception {
runScalatestPluginTestOneEntry("ut-xml,", "ut-xml");
}

@Test
public void testScalatestMavenPluginJunitxmlHasOneEntryAndLeadingDummy() throws Exception {
runScalatestPluginTestOneEntry(",ut-xml", "ut-xml");
}

@Test
public void testScalatestMavenPluginJunitxmlHasOneEntryAndDummiesOnBothEnds() throws Exception {
runScalatestPluginTestOneEntry(",ut-xml,", "ut-xml");
}

@Test
public void testScalatestMavenPluginJunitxmlHasMultipleEntries() throws Exception {
runScalatestPluginTestOneEntry("ut-xml, foo, baz", "ut-xml");
}

@Test
public void testScalatestMavenPluginJunitxmlHasMultipleEntriesAndFirstOneInvalid()
throws Exception {
runScalatestPluginTestOneEntry("foo, ut-xml, baz", "ut-xml");
}

@Test
public void testScalatestMavenPluginJunitxmlHasMultipleEntriesWithEscapedCommas()
throws Exception {
runScalatestPluginTestOneEntry("ut\\,xml, foo, baz", "ut,xml");
}

private void runScalatestPluginTestNoEntries(final String junitxml) throws Exception {
runScalatestPluginTest("", new ScalatestPluginTest() {
public void run(MojoInfoBuilder mojoBuilder, MavenProject pom,
String testResultsName) throws Exception {
if (junitxml != null)
mojoBuilder = mojoBuilder.configValue("junitxml", junitxml);
MojoInfo mojoInfo = mojoBuilder.build();

Iterable<File> files =
TestMojo.SCALATEST_MAVEN_PLUGIN.getReportFiles(pom, mojoInfo);
assertNull("unexpected report files returned", files);
}
});
}

private void runScalatestPluginTestOneEntry(final String junitxml, String expectedDir)
throws Exception {
runScalatestPluginTest(expectedDir, new ScalatestPluginTest() {
public void run(MojoInfoBuilder mojoBuilder, MavenProject pom,
String testResultsName) throws Exception {
MojoInfo mojoInfo = mojoBuilder.configValue("junitxml", junitxml).build();

Iterable<File> files =
TestMojo.SCALATEST_MAVEN_PLUGIN.getReportFiles(pom, mojoInfo);
assertHasOneFile(files, testResultsName);
}
});
}

private void assertHasOneFile(Iterable<File> files, String expectedFile) {
assertNotNull("no report files returned", files);

boolean found = false;
for (File file : files) {
assertFalse("unexpected report files returned: " + file, found);
assertEquals(expectedFile, file.getName());
found = true;
}
assertTrue("report file not found", found);
}

private void runScalatestPluginTest(String junitXmlDirName, ScalatestPluginTest test)
throws Exception {
final String testResultsName = "TEST-are-we-foobared.xml";

File testDir = hudson.Util.createTempDir();
File targetDir = new File(testDir, "target");
File reportsDir = new File(targetDir, "scalatest-reports");
File junitXmlDir = new File(reportsDir, junitXmlDirName);
assertTrue(junitXmlDir.mkdirs());

MojoInfoBuilder mojoBuilder =
MojoInfoBuilder.mojoBuilder("org.scalatest", "scalatest-maven-plugin", "test")
.configValue("reportsDirectory", reportsDir.toString());

File testResults = new File(junitXmlDir, testResultsName);
try {
FileWriter fw = new FileWriter(testResults, false);
fw.write("this is a fake junit reports file");
fw.close();

MavenProject pom = mock(MavenProject.class);
when(pom.getBasedir()).thenReturn(testDir);

Build build = mock(Build.class);
when(build.getDirectory()).thenReturn(targetDir.getAbsolutePath());
when(pom.getBuild()).thenReturn(build);

test.run(mojoBuilder, pom, testResultsName);
} finally {
testResults.delete();
}
}

private static interface ScalatestPluginTest {
public void run(MojoInfoBuilder mojoBuilder, MavenProject pom,
String testResultsName) throws Exception;
}
}

0 comments on commit 06c3213

Please sign in to comment.