Skip to content

Commit

Permalink
Merge branch 'JENKINS-29391' of https://github.com/nfalco79/mstestrun…
Browse files Browse the repository at this point in the history
…ner-plugin into nfalco79-JENKINS-29391
  • Loading branch information
Ido Ran committed Sep 12, 2015
2 parents f2edb36 + 8b6692c commit 5ac4d7e
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions src/main/java/org/jenkinsci/plugins/MsTestBuilder.java
@@ -1,20 +1,28 @@
package org.jenkinsci.plugins;

import hudson.*;
import hudson.model.AbstractBuild;
import hudson.CopyOnWrite;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.tasks.Builder;
import hudson.tools.ToolInstallation;
import hudson.util.ArgumentListBuilder;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import org.kohsuke.stapler.DataBoundConstructor;

/**
* @author Ido Ran
Expand Down Expand Up @@ -93,6 +101,8 @@ public MsTestInstallation getMsTest() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
List<String> args = new ArrayList<String>();

FilePath workspace = build.getWorkspace();

// Build MSTest.exe path.
String execName = "mstest.exe";
MsTestInstallation installation = getMsTest();
Expand Down Expand Up @@ -128,7 +138,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
}

// Delete old result file
FilePath resultFilePath = build.getWorkspace().child(resultFile);
FilePath resultFilePath = workspace.child(resultFile);
if (!resultFilePath.exists()) {
listener.getLogger().println("Result file was not found so no action has been taken. " + resultFilePath.toURI());
} else {
Expand All @@ -148,7 +158,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
args.add("/resultsfile:" + resultFile);

// Checks to use noisolation flag
if (!installation.getOmitNoIsolation()){
if (installation != null && !installation.getOmitNoIsolation()){
args.add("/noisolation");
}

Expand All @@ -157,35 +167,64 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
String normalizedArgs = cmdLineArgs.replaceAll("[\t\r\n]+", " ");
normalizedArgs = Util.replaceMacro(normalizedArgs, env);
normalizedArgs = Util.replaceMacro(normalizedArgs, build.getBuildVariables());
if (normalizedArgs.trim().length() > 0) {
normalizedArgs = Util.fixEmptyAndTrim(normalizedArgs);
if (normalizedArgs != null) {
args.addAll(Arrays.asList(Util.tokenize(normalizedArgs)));
}

if (categories != null && categories.trim().length() > 0) {
args.add("/category:\"" + categories.trim() + "\"");
// check categories
String categories = Util.fixEmptyAndTrim(this.categories);
if (categories != null) {
// If filter consists of a single category such as /category:group1, do not have to enclose the filter in quotation marks.
// However, if filter references more than one category such as /category:"group1&group2" then the filter has to be enclosed in quotation marks.
boolean quotationMarks = categories.contains("&") || categories.contains("!") || categories.contains("|");
args.add("/category:" + (quotationMarks ? "\"" : "") + categories + (quotationMarks ? "\"" : ""));
}

String testFiles = Util.fixEmptyAndTrim(this.testFiles);
// if no test files are specified fail the build.
if (testFiles == null || testFiles.trim().length() == 0) {
if (testFiles == null) {
listener.fatalError("No test files are specified");
return false;
}

// Add test containers to command line
String macroReplacedTestFiles = Util.replaceMacro(testFiles, env);// Required to handle newlines properly
StringTokenizer testFilesToknzr = new StringTokenizer(macroReplacedTestFiles, "\r\n");
Set<String> testContainers = new LinkedHashSet<String>(7);

while (testFilesToknzr.hasMoreTokens()) {
String testFile = testFilesToknzr.nextToken();
testFile = Util.replaceMacro(testFile, env);
testFile = Util.replaceMacro(testFile, build.getBuildVariables());
testFile = Util.fixEmptyAndTrim(testFile);

if (testFile.length() > 0) {
args.add("/testcontainer:" + testFile);
if (testFile != null) {
File tcFile = new File(testFile);
if (tcFile.isAbsolute()) {
if (tcFile.isFile() && tcFile.exists()) {
testContainers.add(testFile);
}
} else {
for (FilePath tcFilePath : workspace.list(testFile)) {
// TODO make path relative to workspace to reduce length of command line (see windows max size)
testContainers.add(tcFilePath.getRemote());
}
}
}
}
// nothing of include rule has match files in workspace folder
if (testContainers.isEmpty()) {
listener.fatalError("No test files was found");
return false;
}

for (String testContainer : testContainers) {
args.add("/testcontainer:" + testContainer);
}

try {
int r = launcher.launch().cmds(args).envs(env).stdout(listener).pwd(build.getWorkspace()).join();
int r = launcher.launch().cmds(args).envs(env).stdout(listener).pwd(workspace).join();

// If continueOnFail is set we always report success.
// If not we report success if MSTest return 0 and exit value.
Expand Down

0 comments on commit 5ac4d7e

Please sign in to comment.