Skip to content
Permalink
Browse files

[JENKINS-20706] Support matrix builds

by copying and parsing coverage.xml files from each MatrixRun.

This is my second attempt at addressing
https://issues.jenkins-ci.org/browse/JENKINS-20706

This works by creating a MatrixAggregator that on endRun, copies the
coverage.xml from each MatrixRun into the root build's directory. Then
in endBuild, those files are parsed and aggregated.

Refs:
* https://issues.jenkins-ci.org/browse/JENKINS-20706
* jenkinsci#26 (this PR)
* jenkinsci#22 (older PR)
  • Loading branch information...
msabramo committed Nov 24, 2013
1 parent bac4f17 commit d7ac03edbe95b0ca573b784dd58da997e2d23119
@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.424</version>
<version>1.540</version>
</parent>

<artifactId>cobertura</artifactId>
@@ -46,6 +46,7 @@
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
<version>2.0</version>
<scope>compile</scope>
<!-- Exclude a dependency that conflicts with easymock dependencies -->
<exclusions>
@@ -0,0 +1,125 @@
package hudson.plugins.cobertura;

import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;
import hudson.model.BuildListener;
import hudson.model.Result;
import static hudson.plugins.cobertura.CoberturaPublisher.getCoberturaReports;
import hudson.plugins.cobertura.targets.CoverageResult;
import hudson.plugins.cobertura.targets.CoverageTarget;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CoberturaMatrixAggregator extends MatrixAggregator {

private final CoberturaPublisher publisher;

/**
* {@inheritDoc}
*
* @param build the MatrixBuild to aggregate coverage for.
* @param launcher the launcher.
* @param listener the listener.
* @param publisher the CoberturaPublisher
*/
public CoberturaMatrixAggregator(
MatrixBuild build,
Launcher launcher,
BuildListener listener,
CoberturaPublisher publisher) {
super(build, launcher, listener);
this.publisher = publisher;
}

@Override
public boolean endRun(MatrixRun run) {
FilePath rootBuildDir = new FilePath(build.getRootBuild().getRootDir());

for (File coberturaXmlReport : getCoberturaReports(run)) {
FilePath report = new FilePath(coberturaXmlReport.getAbsoluteFile());
final FilePath rootTargetPath = new FilePath(
rootBuildDir,
"coverage_" + run.getWorkspace().getBaseName() + ".xml"
);

try {
report.copyTo(rootTargetPath);
} catch (IOException ex) {
Logger.getLogger(CoberturaMatrixAggregator.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(CoberturaMatrixAggregator.class.getName()).log(Level.SEVERE, null, ex);
}
}

return true;
}

@Override
public boolean endBuild() {
CoverageResult result = null;

for (File report : getCoberturaReports(build)) {
try {
result = CoberturaCoverageParser.parse(report, result);
} catch (IOException e) {
Util.displayIOException(e, listener);
e.printStackTrace(
listener.fatalError("Unable to parse " + report)
);
build.setResult(Result.FAILURE);
}
}

if (result != null) {
build.addAction(
CoberturaBuildAction.load(
build,
result,
this.getHealthyTarget(), this.getUnhealthyTarget(),
this.getOnlyStable(),
this.getFailUnhealthy(), this.getFailUnstable(),
this.getAutoUpdateHealth(), this.getAutoUpdateStability()
)
);
}

return true;
}

private CoverageTarget getHealthyTarget() {
return this.publisher.getHealthyTarget();
}

private CoverageTarget getUnhealthyTarget() {
return this.publisher.getUnhealthyTarget();
}

private boolean getOnlyStable() {
return this.publisher.getOnlyStable();
}

private boolean getFailUnhealthy() {
return this.publisher.getFailUnhealthy();
}

private boolean getFailUnstable() {
return this.publisher.getFailUnstable();
}

private boolean getAutoUpdateHealth() {
return this.publisher.getAutoUpdateHealth();
}

private boolean getAutoUpdateStability() {
return this.publisher.getAutoUpdateStability();
}

}
@@ -4,6 +4,9 @@
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.matrix.MatrixAggregatable;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Result;
@@ -50,7 +53,7 @@
*
* @author Stephen Connolly
*/
public class CoberturaPublisher extends Recorder {
public class CoberturaPublisher extends Recorder implements MatrixAggregatable {

private final String coberturaReportFile;

@@ -318,6 +321,13 @@ static File getCoberturaReportDir(AbstractItem project) {
return build.getRootDir().listFiles(COBERTURA_FILENAME_FILTER);
}

public MatrixAggregator createAggregator(
final MatrixBuild build,
final Launcher launcher,
final BuildListener listener) {
return new CoberturaMatrixAggregator(build, launcher, listener, this);
}

/**
* {@inheritDoc}
*/

0 comments on commit d7ac03e

Please sign in to comment.
You can’t perform that action at this time.