提交 2f496439 编写于 作者: K kohsuke

"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
上级 3924a16d
...@@ -272,7 +272,7 @@ THE SOFTWARE. ...@@ -272,7 +272,7 @@ THE SOFTWARE.
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.codehaus.groovy.maven</groupId> <groupId>org.kohsuke.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId> <artifactId>gmaven-plugin</artifactId>
<executions> <executions>
<!-- run unit test --> <!-- run unit test -->
...@@ -282,22 +282,13 @@ THE SOFTWARE. ...@@ -282,22 +282,13 @@ THE SOFTWARE.
<goal>execute</goal> <goal>execute</goal>
</goals> </goals>
<configuration> <configuration>
<scriptpath>
<element>${project.basedir}/src/build-script</element>
</scriptpath>
<source>${project.basedir}/src/build-script/unitTest.groovy</source> <source>${project.basedir}/src/build-script/unitTest.groovy</source>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>
<dependencies>
<dependency><!-- this needs to be visible to Ant inside GMaven, so has to be a plugin dependency -->
<groupId>org.apache.ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
</plugin> </plugin>
<plugin> <plugin>
<!-- unit tests are run by GMaven through Ant. --> <!-- unit tests are run by GMaven through Ant. -->
......
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
// run unit tests // 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(); cob.instrument([])
ant."cobertura-instrument"(todir:"target/cobertura-classes") { cob.runTests()
fileset(dir:"target/classes"); cob.report()
} cob.makeBuildFailIfTestFail();
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");
}
...@@ -53,9 +53,31 @@ THE SOFTWARE. ...@@ -53,9 +53,31 @@ THE SOFTWARE.
<developerConnection>scm:svn:https://svn.dev.java.net/svn/hudson/trunk/hudson/main/</developerConnection> <developerConnection>scm:svn:https://svn.dev.java.net/svn/hudson/trunk/hudson/main/</developerConnection>
<url>https://hudson.dev.java.net/source/browse/hudson/trunk/hudson/main</url> <url>https://hudson.dev.java.net/source/browse/hudson/trunk/hudson/main</url>
</scm> </scm>
<build> <build>
<defaultGoal>install</defaultGoal> <defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.kohsuke.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0-rc-5-patch-2</version>
<dependencies>
<dependency><!-- this needs to be visible to Ant inside GMaven, so has to be a plugin dependency -->
<groupId>org.apache.ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-release-plugin</artifactId> <artifactId>maven-release-plugin</artifactId>
......
...@@ -129,4 +129,44 @@ THE SOFTWARE. ...@@ -129,4 +129,44 @@ THE SOFTWARE.
<version>2.4</version> <version>2.4</version>
</dependency> </dependency>
</dependencies> </dependencies>
<profiles>
<profile>
<!--
Obtain code coverage report. This is done by running Unit tests on our own and suppressing surefire.
-->
<id>cobertura</id>
<build>
<plugins>
<plugin>
<groupId>org.kohsuke.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<!-- run unit test -->
<execution>
<phase>test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scriptpath>
<!-- load helpers from core -->
<element>${project.basedir}/../core/src/build-script</element>
</scriptpath>
<source>${project.basedir}/src/build-script/unitTest.groovy</source>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- unit tests are run by GMaven through Ant. -->
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project> </project>
// 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();
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册