Skip to content
Permalink
Browse files

Merge pull request #2 from a-filatov/master

JENKINS-22450 - Make trx and coverage files paths available to further build steps in VSTestRunner plugin
  • Loading branch information...
yasu-s committed Aug 31, 2014
2 parents 7e93f95 + c7450ae commit caf3b15ec8f31b82e8d1598f0b4af434da9797c8
@@ -15,6 +15,7 @@
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Computer;
import hudson.model.EnvironmentContributingAction;
import hudson.model.Result;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
@@ -462,7 +463,10 @@ private boolean execVsTest(List<String> args, AbstractBuild<?, ?> build, Launche
listener.getLogger().println("Executing VSTest: " + cmdExecArgs.toStringWithQuote());

try {
int r = launcher.launch().cmds(cmdExecArgs).envs(env).stdout(listener).pwd(pwd).join();
VsTestListenerDecorator parserListener = new VsTestListenerDecorator(listener);
int r = launcher.launch().cmds(cmdExecArgs).envs(env).stdout(parserListener).pwd(pwd).join();

build.addAction(new AddVsTestEnvVarsAction(parserListener.getTrxFile(), parserListener.getCoverageFile()));

if (failBuild)
return (r == 0);
@@ -527,4 +531,42 @@ private String concatString(List<String> args) {
}
return buf.toString();
}

private static class AddVsTestEnvVarsAction implements EnvironmentContributingAction {

private final static String TRX_ENV = "VSTEST_RESULT_TRX";
private final static String COVERAGE_ENV = "VSTEST_RESULT_COVERAGE";

private final String trxEnv;
private final String coverageEnv;

public AddVsTestEnvVarsAction(String trxEnv, String coverageEnv) {
this.trxEnv = trxEnv;
this.coverageEnv = coverageEnv;
}

public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
if (trxEnv != null)
{
env.put(TRX_ENV, trxEnv);
}

if (coverageEnv != null)
{
env.put(COVERAGE_ENV, coverageEnv);
}
}

public String getDisplayName() {
return "Add VSTestRunner Environment Variables to Build Environment";
}

public String getIconFileName() {
return null;
}

public String getUrlName() {
return null;
}
}
}
@@ -0,0 +1,89 @@
package org.jenkinsci.plugins.vstest_runner;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import hudson.EnvVars;
import hudson.console.LineTransformationOutputStream;
import hudson.model.TaskListener;

/**
* @author a.filatov
* 31.03.2014.
*/
public class VsTestListenerDecorator extends LineTransformationOutputStream {

private final static String TRX_PATTERN = "^Results File: (.*\\.trx)$";
private final static int TRX_GROUP = 1;

private final static String ATTACHMENTS_PATTERN = "^Attachments:\\s*$";

private final static String COVERAGE_PATTERN = "^\\s*(.*\\.coverage)$";
private final static int COVERAGE_GROUP = 1;

private final OutputStream listener;

private final Pattern trxPattern;
private final Pattern attachmentsPattern;
private final Pattern coveragePattern;

private boolean attachmentsSection;

private String trxFile;
private String coverageFile;

public VsTestListenerDecorator(TaskListener listener) throws FileNotFoundException {
this.listener = listener != null ? listener.getLogger() : null;

trxFile = null;
coverageFile = null;

this.attachmentsSection = false;
this.trxPattern = Pattern.compile(TRX_PATTERN);
this.attachmentsPattern = Pattern.compile(ATTACHMENTS_PATTERN);
this.coveragePattern = Pattern.compile(COVERAGE_PATTERN);
}

public String getTrxFile()
{
return this.trxFile;
}

public String getCoverageFile()
{
return this.coverageFile;
}

@Override
protected void eol(byte[] bytes, int len) throws IOException {

if (this.listener == null) {
return;
}

String line = new String(bytes, 0, len);

Matcher trxMatcher = this.trxPattern.matcher(line);
if (trxMatcher.find()) {
this.trxFile = trxMatcher.group(TRX_GROUP);
}

if (!this.attachmentsSection) {
Matcher attachmentsMatcher = this.attachmentsPattern.matcher(line);

if (attachmentsMatcher.matches()) {
this.attachmentsSection = true;
}
} else {
Matcher coverageMatcher = this.coveragePattern.matcher(line);
if (coverageMatcher.find()) {
this.coverageFile = coverageMatcher.group(COVERAGE_GROUP);
}
}

this.listener.write(line.getBytes());
}
}

0 comments on commit caf3b15

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