Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit - Tools for enabling code coverage for Java tasks #2430

Merged
merged 6 commits into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 41 additions & 39 deletions Tasks/ANT/anttask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import tl = require('vsts-task-lib/task');
import path = require('path');
import fs = require('fs');
import os = require('os');
import * as Q from "q";
import {CodeCoverageEnablerFactory} from 'codecoverage-tools/codecoveragefactory';

var isWindows = os.type().match(/^Win/);

Expand Down Expand Up @@ -78,25 +80,45 @@ function processAntOutputLine(line) {

async function doWork() {

function enableCodeCoverage() {
function execEnableCodeCoverage(): Q.Promise<string> {
return enableCodeCoverage()
.then(function (resp) {
tl.debug("Enabled code coverage successfully");
return "CodeCoverage_9064e1d0";
}).catch(function (err) {
tl.warning("Failed to enable code coverage: " + err);
return "";
});
};

function enableCodeCoverage(): Q.Promise<any> {
if (!isCodeCoverageOpted) {
return Q.resolve(true);
}

var classFilter: string = tl.getInput('classFilter');
var classFilesDirectories: string = tl.getInput('classFilesDirectories', true);
var sourceDirectories: string = tl.getInput('srcDirectories');
// appending with small guid to keep it unique. Avoiding full guid to ensure no long path issues.
var reportDirectoryName = "CCReport43F6D5EF";
reportDirectory = path.join(buildRootPath, reportDirectoryName);
ccReportTask = "CodeCoverage_9064e1d0";
var reportBuildFileName = "CCReportBuildA4D283EG.xml";
reportBuildFile = path.join(buildRootPath, reportBuildFileName);
var summaryFileName = "coverage.xml";
summaryFile = path.join(buildRootPath, reportDirectoryName);
summaryFile = path.join(summaryFile, summaryFileName);
var coberturaCCFile = path.join(buildRootPath, "cobertura.ser");
var instrumentedClassesDirectory = path.join(buildRootPath, "InstrumentedClasses");

// clean any previous reports.
tl.rmRF(coberturaCCFile, true);
tl.rmRF(reportDirectory, true);
tl.rmRF(reportBuildFile, true);
try {
tl.rmRF(coberturaCCFile, true);
tl.rmRF(reportDirectory, true);
tl.rmRF(reportBuildFile, true);
tl.rmRF(instrumentedClassesDirectory, true);
} catch (err) {
tl.debug("Error removing previous cc files: " + err);
}

var buildProps: { [key: string]: string } = {};
buildProps['buildfile'] = antBuildFile;
Expand All @@ -105,20 +127,15 @@ async function doWork() {
buildProps['sourcedirectories'] = sourceDirectories;
buildProps['summaryfile'] = summaryFileName;
buildProps['reportdirectory'] = reportDirectory;
buildProps['ccreporttask'] = ccReportTask
buildProps['ccreporttask'] = "CodeCoverage_9064e1d0"
buildProps['reportbuildfile'] = reportBuildFile;
try {
var codeCoverageEnabler = new tl.CodeCoverageEnabler('Ant', ccTool);
codeCoverageEnabler.enableCodeCoverage(buildProps);
tl.debug("Code coverage is successfully enabled.");
}
catch (Error) {
tl.warning("Enabling code coverage failed. Check the build logs for errors.");
}

let ccEnabler = new CodeCoverageEnablerFactory().getTool("ant", ccTool.toLowerCase());
return ccEnabler.enableCodeCoverage(buildProps);
}

function publishCodeCoverage(codeCoverageOpted: boolean) {
if (codeCoverageOpted) {
function publishCodeCoverage(codeCoverageOpted: boolean, ccReportTask: string) {
if (codeCoverageOpted && ccReportTask) {
tl.debug("Collecting code coverage reports");
var antRunner = tl.tool(anttool);
antRunner.arg('-buildfile');
Expand Down Expand Up @@ -208,39 +225,26 @@ async function doWork() {

var ccTool = tl.getInput('codeCoverageTool');
var isCodeCoverageOpted = (typeof ccTool != "undefined" && ccTool && ccTool.toLowerCase() != 'none');

var buildRootPath = path.dirname(antBuildFile);
var instrumentedClassesDirectory = path.join(buildRootPath, "InstrumentedClasses");
//delete any previous cobertura instrumented classes as they might interfere with ant execution.
tl.rmRF(instrumentedClassesDirectory, true);

if (isCodeCoverageOpted) {
var summaryFile: string = null;
var reportDirectory: string = null;
var ccReportTask: string = null;
var reportBuildFile: string = null;
enableCodeCoverage();
}
else {
tl.debug("Option to enable code coverage was not selected and is being skipped.");
}


var summaryFile: string = null;
var reportDirectory: string = null;
var ccReportTask: string = null;
var reportBuildFile: string = null;
var publishJUnitResults = tl.getInput('publishJUnitResults');
var testResultsFiles = tl.getInput('testResultsFiles', true);

await antv.exec();
ccReportTask = await execEnableCodeCoverage();

await antv.exec();
var buffer;
antb.on('stdout', (data) => {
if (data) {
buffer += data.toString();

let idx = buffer.indexOf(os.EOL);
while (idx > -1) {
let line = buffer.substring(0, idx);

processAntOutputLine(line);

buffer = buffer.substring(idx + os.EOL.length);
idx = buffer.indexOf(os.EOL);
}
Expand All @@ -250,11 +254,10 @@ async function doWork() {
antb.exec()
.then(function (code) {
publishTestResults(publishJUnitResults, testResultsFiles);
publishCodeCoverage(isCodeCoverageOpted);
publishCodeCoverage(isCodeCoverageOpted, ccReportTask);
tl.setResult(tl.TaskResult.Succeeded, "Task succeeded");
})
.fail(function (err) {
publishTestResults(publishJUnitResults, testResultsFiles);
console.error(err.message);
tl.debug('taskRunner fail');
tl.setResult(tl.TaskResult.Failed, err);
Expand All @@ -264,7 +267,6 @@ async function doWork() {
tl._writeError(e);
tl.setResult(tl.TaskResult.Failed, e.message);
}

}

doWork();
Expand Down
1 change: 1 addition & 0 deletions Tasks/ANT/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"url": "https://github.com/Microsoft/vso-agent-tasks/issues"
},
"dependencies": {
"xml2js": "^0.4.16",
"vsts-task-lib": "0.9.7"
}
}
2 changes: 1 addition & 1 deletion Tasks/ANT/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 51
"Patch": 54
},
"demands": [
"ant"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ANT/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 51
"Patch": 54
},
"demands": [
"ant"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"loc.messages.InvalidBuildFile": "Invalid or unsupported build file",
"loc.messages.FileNotFound": "File or folder doesn't exist: %s",
"loc.messages.FailedToAppendCC": "Unable to append code coverage data: %s"
}
190 changes: 190 additions & 0 deletions Tasks/Common/codecoverage-tools/cobertura/cobertura.ant.ccenabler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/// <reference path="../../../../definitions/Q.d.ts" />
/// <reference path="../../../../definitions/string.d.ts" />
/// <reference path="../../../../definitions/vsts-task-lib.d.ts" />
/// <reference path="../../../../definitions/node.d.ts" />

import * as Q from "q";
import * as util from "../utilities";
import * as tl from "vsts-task-lib/task";
import * as ccc from "../codecoverageconstants";
import * as cc from "../codecoverageenabler";
import * as str from "string";
import * as path from "path";

export class CoberturaAntCodeCoverageEnabler extends cc.CoberturaCodeCoverageEnabler {

reportDir: string;
reportbuildfile: string;
classDirs: string;
includeFilter: string;
excludeFilter: string;
sourceDirs: string;

// -----------------------------------------------------
// Enable code coverage for Cobertura Ant Builds
// - enableCodeCoverage: CodeCoverageProperties - ccProps
// -----------------------------------------------------
public enableCodeCoverage(ccProps: { [name: string]: string }): Q.Promise<boolean> {
let _this = this;

tl.debug("Input parameters: " + JSON.stringify(ccProps));

_this.reportDir = "CCReport43F6D5EF";
_this.reportbuildfile = "CCReportBuildA4D283EG.xml";
_this.buildFile = ccProps["buildfile"];
let classFilter = ccProps["classfilter"];
let srcDirs = ccProps["sourcedirectories"];
if (str(srcDirs).isEmpty()) {
srcDirs = ".";
}
_this.sourceDirs = srcDirs;
_this.classDirs = ccProps["classfilesdirectories"];

let filter = _this.extractFilters(classFilter);
_this.excludeFilter = _this.applyFilterPattern(filter.excludeFilter).join(",");
_this.includeFilter = _this.applyFilterPattern(filter.includeFilter).join(",");

tl.debug("Reading the build file: " + _this.buildFile);

return util.readXmlFileAsJson(_this.buildFile)
.then(function (resp) {
return _this.addCodeCoverageData(resp);
})
.thenResolve(true);
}

protected applyFilterPattern(filter: string): string[] {
let ccfilter = [];

if (!util.isNullOrWhitespace(filter)) {
str(util.trimToEmptyString(filter)).replaceAll(".", "/").s.split(":").forEach(exFilter => {
if (exFilter) {
ccfilter.push(str(exFilter).endsWith("*") ? ("**/" + exFilter + "/**") : ("**/" + exFilter + ".class"));
}
});
}

tl.debug("Applying the filter pattern: " + filter + " op: " + ccfilter);
return ccfilter;
}

protected getClassData(): any {
let _this = this;
let fileset = [];
let classDirs = _this.classDirs;

if (str(classDirs).isEmpty()) {
classDirs = ".";
}
classDirs.split(",").forEach(cdir => {
let filter = {
$: {
dir: cdir,
includes: _this.includeFilter,
excludes: _this.excludeFilter
}
};
fileset.push(filter);
});
return fileset;
}

protected createReportFile(reportContent: string): Q.Promise<void> {
let _this = this;
tl.debug("Creating the report file: " + _this.reportbuildfile);

let reportFile = path.join(path.dirname(_this.buildFile), _this.reportbuildfile);
return util.writeFile(reportFile, reportContent);
}

protected addCodeCoverageData(pomJson: any): Q.Promise<any[]> {
let _this = this;

if (!pomJson || !pomJson.project) {
return Q.reject<any>(tl.loc("InvalidBuildFile"));
}

let reportPluginData = ccc.coberturaAntReport(_this.sourceDirs, path.join(path.dirname(_this.buildFile), _this.reportDir));
return Q.all([_this.addCodeCoverageNodes(pomJson), _this.createReportFile(reportPluginData)]);
}

protected addCodeCoverageNodes(buildJsonContent: any): Q.Promise<any> {
let _this = this;

if (!buildJsonContent.project.target) {
tl.debug("Build tag is not present");
return Q.reject(tl.loc("InvalidBuildFile"));
}

ccc.coberturaAntCoverageEnable(buildJsonContent.project);

if (!buildJsonContent.project.target || typeof buildJsonContent.project.target === "string") {
buildJsonContent.project.target = {};
}

if (buildJsonContent.project.target instanceof Array) {
buildJsonContent.project.target.forEach(element => {
_this.enableForking(element);
});
} else {
_this.enableForking(buildJsonContent.project.target);
}
return util.writeJsonAsXmlFile(_this.buildFile, buildJsonContent);
}

protected enableForking(targetNode: any) {
let _this = this;
let coberturaNode = ccc.coberturaAntInstrumentedClasses(path.dirname(_this.buildFile), _this.reportDir);
coberturaNode.fileset = _this.getClassData();
let testNodes = ["junit", "java", "testng", "batchtest"];

if (targetNode.javac) {
if (targetNode.javac instanceof Array) {
targetNode.javac.forEach(jn => {
jn.$.debug = "true";
});
}
}

testNodes.forEach(tn => {
if (!targetNode[tn]) {
return;
}

let node = targetNode[tn];
_this.enableForkOnTestNodes(node, true);
if (node instanceof Array) {
node.forEach(n => {
ccc.coberturaAntProperties(n, _this.reportDir, path.dirname(_this.buildFile));
});
} else {
ccc.coberturaAntProperties(node, _this.reportDir, path.dirname(_this.buildFile));
}

targetNode["cobertura-instrument"] = coberturaNode;
});
}

protected enableForkOnTestNodes(testNode: any, enableForkMode: boolean) {
if (testNode instanceof Array) {
testNode.forEach(element => {
if (!element.$) {
element.$ = {};
}
if (enableForkMode) {
element.$.forkmode = "once";
}
element.$.fork = "true";

});
} else {
if (!testNode.$) {
testNode.$ = {};
}
if (enableForkMode) {
testNode.$.forkmode = "once";
}
testNode.$.fork = "true";
}
}
}
Loading