Permalink
Browse files

Add emma support

  • Loading branch information...
1 parent 398adde commit 74da6e6ea596cd5d55dd88340ff104cd48b48dd5 @hpychan committed Sep 7, 2011
Showing with 152 additions and 0 deletions.
  1. +5 −0 bin/vows
  2. +147 −0 lib/vows/coverage/report-emma.js
View
5 bin/vows
@@ -60,6 +60,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-emma Write unified coverage map to \"coverage.emma\"",
//" --no-color Don't use terminal colors",
" --version Show version",
" -h, --help You're staring at it"
@@ -141,6 +142,10 @@ while (arg = argv.shift()) {
options.coverage = true;
_coverage = require('../lib/vows/coverage/report-json');
break;
+ case 'cover-emma':
+ options.coverage = true;
+ _coverage = require('../lib/vows/coverage/report-emma');
+ break;
case 'verbose':
case 'v':
options.verbose = true;
View
147 lib/vows/coverage/report-emma.js
@@ -0,0 +1,147 @@
+var sys = require('sys'),
+ fs = require('fs'),
+ file = require('./file');
+
+this.name = 'coverage-report-emma';
+
+function xmlEnc(value) {
+ return !value ? value : String(value).replace(/&/g, "&")
+ .replace(/>/g, ">")
+ .replace(/</g, "&lt;")
+ .replace(/"/g, "&quot;")
+ .replace(/\u001b\[\d{1,2}m/g, '');
+}
+
+function tag(name, attribs, single, content) {
+ var strAttr = [], t, end = '>';
+ for (var attr in attribs) {
+ if (attribs.hasOwnProperty(attr)) {
+ strAttr.push(attr + '="' + xmlEnc(attribs[attr]) + '"');
+ }
+ }
+ if (single) {
+ end = ' />';
+ }
+ if (strAttr.length) {
+ t = '<' + name + ' ' + strAttr.join(' ') + end;
+ } else {
+ t = '<' + name + end;
+ }
+ if (typeof content !== 'undefined') {
+ return t + content + '</' + name + end;
+ }
+ return t;
+}
+
+function end(name) {
+ return '</' + name + '>';
+}
+
+function cdata(data) {
+ return '<![CDATA[' + xmlEnc(data) + ']]>';
+}
+
+/**
+ * coverageMap coverage info for each file
+ */
+this.report = function (coverageMap) {
+ var output = {
+ meta: {
+ "generator": "vowsjs",
+ "generated": new Date().toString(),
+ "instrumentation": "node-jscoverage",
+ "file-version": "1.0"
+ },
+ files: [ ],
+ coverage: [ ]
+ };
+
+ for (var filename in coverageMap) {
+ if (coverageMap.hasOwnProperty(filename)) {
+ // get data for the file
+ var data = file.coverage(filename, coverageMap[filename]);
+
+ var coverage = {
+ file: filename,
+ coverage: data.coverage.toFixed(2),
+ hits: data.hits,
+ misses: data.misses,
+ sloc: data.sloc,
+ source: { }
+ };
+
+ for (var i = 0; i < data.source.length; i++) {
+ coverage.source[i + 1] = {
+ line: data.source[i].line,
+ coverage: data.source[i].coverage
+ };
+ }
+
+ output.coverage.push(coverage);
+
+ output.files.push(filename);
+ }
+ }
+ var totalsloc = 0;
+ var totalhits = 0;
+ for ( var i = 0; i < output.coverage.length; i++) {
+ totalsloc += output.coverage[i].sloc;
+ totalhits += output.coverage[i].hits;
+ }
+
+ var buffer = [];
+ buffer.push(tag('report', {}, false));
+ buffer.push(tag('stats', {}, false));
+ buffer.push(tag('packages', { value: 1 }, false));
+ buffer.push(tag('classes', { value: 1 }, false));
+ buffer.push(tag('srcfiles', { value: output.files.length }, false));
+ buffer.push(tag('srclines', { value: totalsloc }, false));
+ buffer.push(end('stats'));
+
+ buffer.push(tag('data', {}, false));
+ // all
+ buffer.push(tag('all', { name : 'all classes'}, false));
+ buffer.push(tag('coverage', { type : 'class, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'method, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'block, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'line, %', value : (totalhits*1.0/totalsloc * 100.0) + '% ('+totalhits+'/'+totalsloc+')'}, true) );
+
+ // package
+ buffer.push(tag('package', {name: '{root}'}, false));
+ buffer.push(tag('coverage', { type : 'class, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'method, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'block, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'line, %', value : (totalhits*1.0/totalsloc * 100.0) + '% ('+totalhits+'/'+totalsloc+')'}, true) );
+
+ for ( var i = 0; i < output.coverage.length; i++) {
+ var sloc = output.coverage[i].sloc;
+ var hits = output.coverage[i].hits;
+ var file = output.coverage[i].file;
+
+ buffer.push(tag('srcfile', { name : file }, false) );
+ buffer.push(tag('coverage', { type : 'class, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'method, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'block, %', value : '100% (1/1)'}, true) );
+ buffer.push(tag('coverage', { type : 'line, %', value : (hits*1.0/sloc * 100.0) + '% ('+hits+'/'+sloc+')'}, true) );
+ buffer.push(end('srcfile'));
+ }
+
+ buffer.push(end('package'));
+
+ buffer.push(end('all'));
+ buffer.push(end('data'));
+
+
+ buffer.push(end('report'));
+
+ buffer.unshift('<!-- EMMA v2.0.4015 (stable) report, generated '+ output.meta.generated + ' by ' + output.meta.generator + ' -->');
+
+ try {
+ out = fs.openSync("coverage.emma", "w");
+ fs.writeSync(out, buffer.join('\n'));
+ fs.close(out);
+ } catch (error) {
+ sys.print("Error: Unable to write to file coverage.emma\n");
+ return;
+ }
+};

0 comments on commit 74da6e6

Please sign in to comment.