提交 452a02ec 编写于 作者: K kohsuke

added MavenReporter that record Surefire test reports.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@2061 71c3de6d-444a-0410-be80-ed276b4c234a
上级 3afaecaf
package hudson.maven;
import hudson.FilePath;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.FilePath.FileCallable;
import hudson.maven.PluginManagerInterceptor.AbortException;
import hudson.model.AbstractBuild;
......@@ -84,6 +85,11 @@ public class MavenBuild extends AbstractBuild<MavenModule,MavenBuild> {
return true;
}
@Override
public AbstractTestResultAction getTestResultAction() {
return getAction(AbstractTestResultAction.class);
}
@Override
public void run() {
run(new RunnerImpl());
......
......@@ -8,6 +8,7 @@ import hudson.model.Hudson;
import hudson.model.Result;
import hudson.remoting.VirtualChannel;
import hudson.util.IOException2;
import hudson.tasks.test.AbstractTestResultAction;
import org.apache.maven.embedder.MavenEmbedderException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
......@@ -46,6 +47,12 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
super(project, buildDir);
}
@Override
public AbstractTestResultAction getTestResultAction() {
// TODO
return null;
}
public void run() {
run(new RunnerImpl());
}
......
......@@ -4,6 +4,7 @@ import hudson.model.Descriptor;
import hudson.maven.reporters.MavenArtifactArchiver;
import hudson.maven.reporters.MavenFingerprinter;
import hudson.maven.reporters.MavenJavadocArchiver;
import hudson.maven.reporters.SurefireArchiver;
import java.util.List;
import java.util.ArrayList;
......@@ -19,7 +20,8 @@ public final class MavenReporters {
public static final List<MavenReporterDescriptor> LIST = Descriptor.toList(
MavenArtifactArchiver.DescriptorImpl.DESCRIPTOR,
MavenFingerprinter.DescriptorImpl.DESCRIPTOR,
MavenJavadocArchiver.DescriptorImpl.DESCRIPTOR
MavenJavadocArchiver.DescriptorImpl.DESCRIPTOR,
SurefireArchiver.DescriptorImpl.DESCRIPTOR
);
/**
......
package hudson.maven.reporters;
import hudson.maven.MavenBuild;
import hudson.maven.MavenBuildProxy;
import hudson.maven.MavenBuildProxy.BuildCallable;
import hudson.maven.MavenModule;
import hudson.maven.MavenReporter;
import hudson.maven.MavenReporterDescriptor;
import hudson.maven.MojoInfo;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.junit.TestResult;
import hudson.tasks.junit.TestResultAction;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* Records the surefire test result.
* @author Kohsuke Kawaguchi
*/
public class SurefireArchiver extends MavenReporter {
private transient Date started;
public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException {
if (isSurefireTest(mojo))
started = new Date();
return true;
}
public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, final BuildListener listener) throws InterruptedException, IOException {
if (!isSurefireTest(mojo)) return true;
File reportsDir;
try {
reportsDir = mojo.getConfigurationValue("reportsDirectory", File.class);
} catch (ComponentConfigurationException e) {
e.printStackTrace(listener.fatalError("Unable to obtain the reportsDirectory from surefire:test mojo"));
build.setResult(Result.FAILURE);
return true;
}
if(reportsDir.exists()) {
// surefire:test just skips itself when the current project is not a java project
FileSet fs = new FileSet();
Project p = new Project();
fs.setProject(p);
fs.setDir(reportsDir);
fs.setIncludes("*.xml");
DirectoryScanner ds = fs.getDirectoryScanner(p);
if(ds.getIncludedFiles().length==0)
// no test in this module
return true;
final TestResult tr = new TestResult(started.getTime() - 1000/*error margin*/, ds);
build.execute(new BuildCallable<Void, IOException>() {
public Void call(MavenBuild build) throws IOException, InterruptedException {
TestResultAction action = new TestResultAction(build, tr, listener);
build.getActions().add(action);
if(tr.getFailCount()>0)
build.setResult(Result.UNSTABLE);
return null;
}
});
}
return true;
}
private boolean isSurefireTest(MojoInfo mojo) {
return mojo.pluginName.matches("org.apache.maven.plugins", "maven-surefire-plugin") && mojo.getGoal().equals("test");
}
public DescriptorImpl getDescriptor() {
return DescriptorImpl.DESCRIPTOR;
}
public static final class DescriptorImpl extends MavenReporterDescriptor {
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
private DescriptorImpl() {
super(SurefireArchiver.class);
}
public String getDisplayName() {
return "Publish surefire reports";
}
public SurefireArchiver newAutoInstance(MavenModule module) {
return new SurefireArchiver();
}
}
}
......@@ -4,6 +4,7 @@ import hudson.Launcher;
import hudson.Proc.LocalProc;
import hudson.Util;
import hudson.tasks.Fingerprinter.FingerprintAction;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.maven.MavenBuild;
import static hudson.model.Hudson.isWindows;
import hudson.model.listeners.SCMListener;
......@@ -211,6 +212,16 @@ public abstract class AbstractBuild<P extends AbstractProject<P,R>,R extends Abs
return env;
}
public Calendar due() {
return timestamp;
}
/**
* Gets {@link AbstractTestResultAction} associated with this build if any.
* <p>
*/
public abstract AbstractTestResultAction getTestResultAction();
/**
* Invoked by {@link Executor} to performs a build.
*/
......
......@@ -11,7 +11,6 @@ import hudson.triggers.SCMTrigger;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
......@@ -36,17 +35,11 @@ public final class Build extends AbstractBuild<Project,Build> {
super(project,buildDir);
}
public Calendar due() {
return timestamp;
}
/**
* Gets {@link AbstractTestResultAction} associated with this build if any.
*/
@Override
public AbstractTestResultAction getTestResultAction() {
return getAction(AbstractTestResultAction.class);
}
/**
* During the build this field remembers {@link Environment}s created by
* {@link BuildWrapper}. This design is bit ugly but forced due to compatibility.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册