Skip to content

Commit

Permalink
[JENKINS-28166] Added checkbox to ignore missing TRX files
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Bush committed May 1, 2015
1 parent 74404cc commit 2e42acb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
43 changes: 38 additions & 5 deletions src/main/java/hudson/plugins/mstest/MSTestPublisher.java
Expand Up @@ -33,6 +33,8 @@
import net.sf.json.JSONObject;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;

/**
Expand All @@ -45,10 +47,18 @@ public class MSTestPublisher extends Recorder implements Serializable {
private final String testResultsFile;
private String resolvedFilePath;
private long buildTime;
private boolean failOnError;
//private EmmaPublisher emmaPublisher;

public MSTestPublisher(String testResultsFile) {

public MSTestPublisher(String testResultsFile){
this(testResultsFile, true);
}

@DataBoundConstructor
public MSTestPublisher(String testResultsFile, boolean failOnError) {
this.testResultsFile = testResultsFile;
this.failOnError = failOnError;
}

public String getTestResultsTrxFile() {
Expand All @@ -58,6 +68,15 @@ public String getTestResultsTrxFile() {
public String getResolvedFilePath() {
return resolvedFilePath;
}

public boolean getFailOnError(){
return failOnError;
}

@DataBoundSetter
public final void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}

@Override
public Action getProjectAction(AbstractProject<?, ?> project) {
Expand Down Expand Up @@ -95,7 +114,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
resolveFilePath(build, listener);

listener.getLogger().println("MSTest: Processing tests results in file(s) " + resolvedFilePath);
MSTestTransformer transformer = new MSTestTransformer(resolvedFilePath, new MSTestReportConverter(), listener);
MSTestTransformer transformer = new MSTestTransformer(resolvedFilePath, new MSTestReportConverter(), listener, failOnError);
result = build.getWorkspace().act(transformer);

if (result) {
Expand Down Expand Up @@ -158,6 +177,11 @@ private boolean recordTestResult(String junitFilePattern, AbstractBuild<?, ?> bu
}
TestResult result = getTestResult(junitFilePattern, build, existingTestResults);

//There was no result because there was no TRX file
if(result == null) {
return true;
}

if (existingAction == null) {
action = new TestResultAction(build, result, listener);
} else {
Expand Down Expand Up @@ -207,9 +231,14 @@ public TestResult invoke(File ws, VirtualChannel channel) throws IOException {
FileSet fs = Util.createFileSet(ws, junitFilePattern);
DirectoryScanner ds = fs.getDirectoryScanner();
String[] files = ds.getIncludedFiles();

if (files.length == 0) {
// no test result. Most likely a configuration error or fatal problem
throw new AbortException("No test report files were found. Configuration error?");
if(failOnError) {
// no test result. Most likely a configuration error or fatal problem
throw new AbortException("No test report files were found. Configuration error?");
} else {
return null;
}
}
if (existingTestResults == null) {
return new TestResult(buildTime, ds, false);
Expand Down Expand Up @@ -251,7 +280,11 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {

@Override
public Publisher newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return new MSTestPublisher(req.getParameter("mstest_reports.pattern"));
boolean failOnError = true;
if(req.getParameter("failOnError") == null || !req.getParameter("failOnError").equals("on")) {
failOnError = false;
}
return new MSTestPublisher(req.getParameter("mstest_reports.pattern"), failOnError);
}
}
}
12 changes: 11 additions & 1 deletion src/main/java/hudson/plugins/mstest/MSTestTransformer.java
Expand Up @@ -5,7 +5,6 @@
import hudson.remoting.VirtualChannel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -27,16 +26,23 @@ public class MSTestTransformer implements FilePath.FileCallable<Boolean>, Serial

public static final String JUNIT_REPORTS_PATH = "temporary-junit-reports";
private final BuildListener listener;
private final boolean failOnError;

// Build related objects
private final String testResultsFile;

private final MSTestReportConverter unitReportTransformer;


public MSTestTransformer(String testResults, MSTestReportConverter unitReportTransformer, BuildListener listener) throws TransformerException {
this(testResults, unitReportTransformer, listener, true);
}

public MSTestTransformer(String testResults, MSTestReportConverter unitReportTransformer, BuildListener listener, boolean failOnError) throws TransformerException {
this.testResultsFile = testResults;
this.unitReportTransformer = unitReportTransformer;
this.listener = listener;
this.failOnError = failOnError;
}

/**
Expand All @@ -50,6 +56,10 @@ public Boolean invoke(File ws, VirtualChannel channel) throws IOException {
String[] mstestFiles = findMSTestReports(ws);

if (mstestFiles.length == 0) {
if(!failOnError){
listener.getLogger().println("MSTest: No MSTest TRX test report files were found. Ignoring.");
return Boolean.TRUE;
}
listener.fatalError("MSTest: No MSTest TRX test report files were found. Configuration error?");
return Boolean.FALSE;
}
Expand Down
Expand Up @@ -3,6 +3,9 @@
This jelly script is used for per-project configuration.
-->
<f:entry title="${%Test report TRX file}" description="${%description.pattern}">
<f:textbox name="mstest_reports.pattern" value="${instance.testResultsTrxFile}" />
<f:textbox name="mstest_reports.pattern" value="${instance.testResultsTrxFile}" />
</f:entry>
<f:entry title="${%Fail build if no files are found}" field="failOnError" >
<f:checkbox name="failOnError" />
</f:entry>
</j:jelly>

0 comments on commit 2e42acb

Please sign in to comment.