Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-14102] mark maven build with test failures as unstable
Originally-Committed-As: 5f4f68be0081cd9b9554c7d0ce8529c573764056
  • Loading branch information
imod committed Dec 21, 2012
1 parent cc2707f commit adf9fc6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/main/java/hudson/maven/Maven3Builder.java
Expand Up @@ -24,6 +24,7 @@
package hudson.maven;

import hudson.maven.MavenBuild.ProxyImpl2;
import hudson.maven.reporters.TestFailureDetector;
import hudson.maven.util.ExecutionEventLogger;
import hudson.model.BuildListener;
import hudson.model.Result;
Expand Down Expand Up @@ -53,6 +54,7 @@
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

import static hudson.Util.*;
Expand Down Expand Up @@ -120,7 +122,12 @@ public Result call() throws IOException {

PrintStream logger = listener.getLogger();

if(r==0 && mavenExecutionResult.getThrowables().isEmpty()) return Result.SUCCESS;
if(r==0 && mavenExecutionResult.getThrowables().isEmpty()) {
if(mavenExecutionListener.hasTestFailures()){
return Result.UNSTABLE;
}
return Result.SUCCESS;
}

if (!mavenExecutionResult.getThrowables().isEmpty()) {
logger.println( "mavenExecutionResult exceptions not empty");
Expand All @@ -137,6 +144,9 @@ public Result call() throws IOException {

if(markAsSuccess) {
listener.getLogger().println(Messages.MavenBuilder_Failed());
if(mavenExecutionListener.hasTestFailures()){
return Result.UNSTABLE;
}
return Result.SUCCESS;
}
return Result.FAILURE;
Expand All @@ -158,6 +168,8 @@ private static final class MavenExecutionListener extends AbstractExecutionListe
private static final long serialVersionUID = 4942789836756366116L;

private final Maven3Builder maven3Builder;

private AtomicBoolean hasTestFailures = new AtomicBoolean();

/**
* Number of total nanoseconds {@link Maven3Builder} spent.
Expand Down Expand Up @@ -185,6 +197,10 @@ public MavenExecutionListener(Maven3Builder maven3Builder) {
this.eventLogger = new ExecutionEventLogger( new PrintStreamLogger( maven3Builder.listener.getLogger() ) );
}

public boolean hasTestFailures(){
return hasTestFailures.get();
}

private MavenBuildProxy2 getMavenBuildProxy2(MavenProject mavenProject) {
for (Entry<ModuleName,FilterImpl> entry : proxies.entrySet()) {
if (entry.getKey().compareTo( new ModuleName( mavenProject ) ) == 0) {
Expand Down Expand Up @@ -419,6 +435,15 @@ private void recordMojoEnded(ExecutionEvent event, Exception problem) {
for (MavenReporter mavenReporter : fixNull(mavenReporters)) {
try {
mavenReporter.postExecute( mavenBuildProxy2, mavenProject, mojoInfo, maven3Builder.listener, problem);
if (mavenReporter instanceof TestFailureDetector) {
TestFailureDetector detector = (TestFailureDetector) mavenReporter;
if(detector.hasTestFailures()) {
System.out.println("+++++++>>>>"+mavenReporter.getClass());
hasTestFailures.compareAndSet(false, true);
}else{
System.out.println("------->>>>"+mavenReporter.getClass());
}
}
} catch ( InterruptedException e ) {
e.printStackTrace();
} catch ( IOException e ) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/hudson/maven/MavenModuleSet.java
Expand Up @@ -398,6 +398,10 @@ public DescribableList<Builder,Descriptor<Builder>> getPostbuilders() {
return postbuilders;
}

void addPostBuilder(Builder builder) throws IOException{
postbuilders.add(builder);
}

/**
* {@link #postbuilders} are run if the result is better or equal to this threshold.
*
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/hudson/maven/reporters/SurefireArchiver.java
Expand Up @@ -51,6 +51,7 @@
import java.util.ListIterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
Expand All @@ -64,15 +65,21 @@
* Records the surefire test result.
* @author Kohsuke Kawaguchi
*/
public class SurefireArchiver extends MavenReporter {
public class SurefireArchiver extends TestFailureDetector {
private transient TestResult result;
private final AtomicBoolean hasTestFailures = new AtomicBoolean();

/**
* Store the filesets here as we want to track ignores between multiple runs of this class<br/>
* Note: Because this class can be run with different mojo goals with different path settings,
* we track multiple {@link FileSet}s for each encountered <tt>reportsDir</tt>
*/
private transient ConcurrentMap<File, FileSet> fileSets = new ConcurrentHashMap<File,FileSet>();

@Override
public boolean hasTestFailures() {
return hasTestFailures.get();
}

public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException {
if (isTestMojo(mojo)) {
Expand Down Expand Up @@ -161,6 +168,7 @@ public Integer call(MavenBuild build) throws IOException, InterruptedException {
// intercept that (or otherwise build will be marked as failure)
if(failCount>0) {
markBuildAsSuccess(error,build.getMavenBuildInformation());
hasTestFailures.compareAndSet(false, true);
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/hudson/maven/reporters/TestFailureDetector.java
@@ -0,0 +1,21 @@
package hudson.maven.reporters;

import hudson.maven.MavenReporter;

/**
* A maven reporter expressing whether he found test failures and the build should be marked as UNSTABLE.
*
* @author Dominik Bartholdi (imod)
*/
public abstract class TestFailureDetector extends MavenReporter {

private static final long serialVersionUID = 1L;

/**
* Have any test failures been detected?
*
* @return <code>true</code> if there ar test failures
*/
public abstract boolean hasTestFailures();

}

0 comments on commit adf9fc6

Please sign in to comment.