Skip to content

Commit

Permalink
Add coverage report in xml format.
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffano committed Nov 27, 2011
1 parent fbf7f69 commit b2ca904
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bin/vows
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var help = [
" --cover-plain Print plain coverage map if detected",
" --cover-html Write coverage map to \"coverage.html\"",
" --cover-json Write unified coverage map to \"coverage.json\"",
" --cover-xml Write coverage map to \"coverage.xml\" in Emma xml",
" --no-color Don't use terminal colors",
" --version Show version",
" -h, --help You're staring at it"
Expand Down Expand Up @@ -134,6 +135,10 @@ while (arg = argv.shift()) {
options.coverage = true;
_coverage = require('../lib/vows/coverage/report-json');
break;
case 'cover-xml':
options.coverage = true;
_coverage = require('../lib/vows/coverage/report-xml');
break;
case 'verbose':
case 'v':
options.verbose = true;
Expand Down
81 changes: 81 additions & 0 deletions lib/vows/coverage/report-xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
var util = require('util'),
file = require('./file');

this.name = 'coverage-report-xml';

this.report = function (coverageMap) {
var all = {
xml: '',
packages: 0,
files: 0,
lines: 0,
hits: 0
},
data = {};

// group data by path
for (var filename in coverageMap) {
if (coverageMap.hasOwnProperty(filename)) {
var pkg = (filename.indexOf('/') > 0)
? filename.substr(0, filename.lastIndexOf('/'))
: filename;
if (!data[pkg]) {
data[pkg] = {};
}
data[pkg][ (filename.indexOf('/'))
? filename.substr(filename.lastIndexOf('/') + 1, filename.length)
: filename ]
= file.coverage(filename, coverageMap[filename]);
}
}

// generate groups xml-fragment
for (var pkg in data) {
if (data.hasOwnProperty(pkg)) {
var pkgStat = {
xml: '',
files: 0,
lines: 0,
hits: 0
};

all.xml += '\t<package name="' + pkg + '">\n';

for (var filename in data[pkg]) {
if (data[pkg].hasOwnProperty(filename)) {
pkgStat.files += 1;
pkgStat.lines += data[pkg][filename].sloc;
pkgStat.hits += data[pkg][filename].hits;

pkgStat.xml += '\t\t<srcfile name="' + filename + '">\n'
+ '\t\t\t<coverage type="line, %" value="' + data[pkg][filename].coverage.toFixed(2) + '% (' + data[pkg][filename].hits + '/' + data[pkg][filename].sloc + ')"/>\n'
+ '\t\t</srcfile>\n';
}
}

all.packages += 1;
all.files += pkgStat.files;
all.lines += pkgStat.lines;
all.hits += pkgStat.hits;

all.xml += '\t\t<coverage type="line, %" value="' + ((pkgStat.hits * 100) / pkgStat.lines).toFixed(2) + '% (' + pkgStat.hits + '/' + pkgStat.lines + ')"/>\n'
+ pkgStat.xml
+ '\t</package>\n';
}
}

all.xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
+ '<report>\n<stats>\n'
+ '\t<packages value="' + all.packages + '"/>\n'
+ '\t<classes value="0"/>\n'
+ '\t<methods value="0"/>\n'
+ '\t<srcfiles value="' + all.files + '"/>\n'
+ '\t<srclines value="' + all.lines + '"/>\n'
+ '</stats>\n<data>\n'
+ '<all name="all classes">\n'
+ '\t<coverage type="line, %" value="' + ((all.hits * 100) / all.lines).toFixed(2) + '% (' + all.hits + '/' + all.lines + ')"/>\n'
+ all.xml
+ '</all>\n</data>\n</report>\n';

util.print(all.xml);
};

0 comments on commit b2ca904

Please sign in to comment.