From 2f496439f1771d9ec454f638569d6b320cf33005 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Wed, 25 Mar 2009 02:32:12 +0000 Subject: [PATCH] "mvn -Pcobertura install" will now run unit tests with Cobertura. The coverage data file will be published to the Maven repository for further aggregation. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@16508 71c3de6d-444a-0410-be80-ed276b4c234a --- core/pom.xml | 17 ++---- core/src/build-script/Cobertura.groovy | 77 ++++++++++++++++++++++++++ core/src/build-script/unitTest.groovy | 30 +++------- pom.xml | 24 +++++++- test/pom.xml | 40 +++++++++++++ test/src/build-script/unitTest.groovy | 15 +++++ 6 files changed, 166 insertions(+), 37 deletions(-) create mode 100644 core/src/build-script/Cobertura.groovy create mode 100644 test/src/build-script/unitTest.groovy diff --git a/core/pom.xml b/core/pom.xml index e08f079010..7ae9fa2b50 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -272,7 +272,7 @@ THE SOFTWARE. - org.codehaus.groovy.maven + org.kohsuke.gmaven gmaven-plugin @@ -282,22 +282,13 @@ THE SOFTWARE. execute + + ${project.basedir}/src/build-script + ${project.basedir}/src/build-script/unitTest.groovy - - - org.apache.ant - ant-junit - 1.7.0 - - - net.sourceforge.cobertura - cobertura - 1.9 - - diff --git a/core/src/build-script/Cobertura.groovy b/core/src/build-script/Cobertura.groovy new file mode 100644 index 0000000000..cc8966c2f0 --- /dev/null +++ b/core/src/build-script/Cobertura.groovy @@ -0,0 +1,77 @@ +import org.apache.maven.project.MavenProject; + +/** + * Cobertura invoker. + */ +public class Cobertura { + private final MavenProject project; + // maven helper + private def maven; + // ant builder + private def ant; + /** + * Cobertura data file. + */ + private final File ser; + + def Cobertura(project, maven, ant, ser) { + this.project = maven.project; + this.maven = maven; + this.ant = ant; + this.ser =ser; + + // define cobertura tasks + ant.taskdef(resource:"tasks.properties") + } + + // function that ensures that the given directory exists + private String dir(String dir) { + new File(project.basedir,dir).mkdirs(); + return dir; + } + + /** + * Instruments the given class dirs/jars by cobertura + * + * @param files + * List of jar files and class dirs to instrument. + */ + def instrument(files) { + ant."cobertura-instrument"(todir:dir("target/cobertura-classes"),datafile:ser) { + fileset(dir:"target/classes"); + files.each{ fileset(file:it) } + } + } + + def runTests() { + ant.junit(fork:true, forkMode:"once", failureproperty:"failed", printsummary:true) { + classpath { + junitClasspath() + } + batchtest(todir:dir("target/surefire-reports")) { + fileset(dir:"src/test/java") { + include(name:"**/*Test.java") + } + formatter(type:"xml") + } + sysproperty(key:"net.sourceforge.cobertura.datafile",value:ser) + } + } + + def junitClasspath() { + ant.pathelement(path: "target/cobertura-classes") // put the instrumented classes first + ant.fileset(dir:"target/cobertura-classes",includes:"*.jar") // instrumented jar files + ant.pathelement(path: maven.resolveArtifact("net.sourceforge.cobertura:cobertura:1.9")) // cobertura runtime + project.getTestClasspathElements().each { ant.pathelement(path: it) } // the rest of the dependencies + } + + def report() { + maven.attachArtifact(ser,"ser","cobertura") + ant."cobertura-report"(format:"html",datafile:ser,destdir:dir("target/cobertura-reports"),srcdir:"src/main/java") + } + + def makeBuildFailIfTestFail() { + if(ant.project.getProperty("failed")!=null) + fail("Some unit tests failed"); + } +} \ No newline at end of file diff --git a/core/src/build-script/unitTest.groovy b/core/src/build-script/unitTest.groovy index 4d8bb0513e..20832b0ec0 100644 --- a/core/src/build-script/unitTest.groovy +++ b/core/src/build-script/unitTest.groovy @@ -1,27 +1,11 @@ // run unit tests -org.apache.maven.project.MavenProject p = project; -ant.project.setBaseDir(p.basedir) +ant.project.setBaseDir(project.basedir) +ser=new File(project.basedir,"target/cobertura.ser"); // store cobertura data file in a module-specific location -ant.taskdef(resource:"tasks.properties") +cob = new Cobertura(project,maven,ant,ser); -new File("target/cobertura-classes").mkdirs(); -ant."cobertura-instrument"(todir:"target/cobertura-classes") { - fileset(dir:"target/classes"); -} - -ant.junit(fork:true, forkMode:"once", failureproperty:"failed", printsummary:true) { - classpath { - p.getTestClasspathElements().each{ pathelement(path:it) } - } - def reportDir = "target/surefire-reports"; - new File(p.basedir,reportDir).mkdirs(); - batchtest(todir:reportDir) { - fileset(dir:"src/test/java") { - include(name:"**/*Test.java") - } - formatter(type:"xml") - } - if(ant.project.getProperty("failed")!=null) - throw new Exception("tests failed"); -} +cob.instrument([]) +cob.runTests() +cob.report() +cob.makeBuildFailIfTestFail(); diff --git a/pom.xml b/pom.xml index a2fab900a8..4d19852b34 100644 --- a/pom.xml +++ b/pom.xml @@ -53,9 +53,31 @@ THE SOFTWARE. scm:svn:https://svn.dev.java.net/svn/hudson/trunk/hudson/main/ https://hudson.dev.java.net/source/browse/hudson/trunk/hudson/main - + install + + + + org.kohsuke.gmaven + gmaven-plugin + 1.0-rc-5-patch-2 + + + org.apache.ant + ant-junit + 1.7.0 + + + net.sourceforge.cobertura + cobertura + 1.9 + + + + + + maven-release-plugin diff --git a/test/pom.xml b/test/pom.xml index 641aa04896..24967da794 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -129,4 +129,44 @@ THE SOFTWARE. 2.4 + + + + + cobertura + + + + org.kohsuke.gmaven + gmaven-plugin + + + + test + + execute + + + + + ${project.basedir}/../core/src/build-script + + ${project.basedir}/src/build-script/unitTest.groovy + + + + + + + maven-surefire-plugin + + true + + + + + + diff --git a/test/src/build-script/unitTest.groovy b/test/src/build-script/unitTest.groovy new file mode 100644 index 0000000000..366701f4b7 --- /dev/null +++ b/test/src/build-script/unitTest.groovy @@ -0,0 +1,15 @@ +// run unit tests +import org.apache.commons.io.FileUtils + +ant.project.setBaseDir(project.basedir) + +// start from where the core left off, and build from there +ser=new File(project.basedir,"target/cobertura.ser"); +FileUtils.copyFile(maven.resolveArtifact("${project.groupId}:hudson-core:${project.version}:cobertura:ser"),ser) + +cob = new Cobertura(project,maven,ant,ser); + +cob.instrument(["remoting","hudson-core"].collect{ m -> maven.resolveArtifact("${project.groupId}:${m}:${project.version}") }) +cob.runTests() +cob.report() +cob.makeBuildFailIfTestFail(); -- GitLab