Permalink
Browse files

Prelyminary code coverage support using runforcover and bunker.

  • Loading branch information...
1 parent 62c5653 commit 68f6a3553336b3ce1bdbba71b69b44914ce5dc25 @kusor kusor committed with isaacs Nov 28, 2011
View
@@ -1 +1,2 @@
node_modules/*
+coverage-example/coverage/*
View
@@ -74,5 +74,9 @@ this is very useful for CI systems and such.
* tap-global-harness: A default harness that provides the top-level
support for running TAP tests.
+Code Coverage with runforcover & bunker:
+
+`TAP_COV=1 tap ./tests [--cover=./lib,foo.js] [--cover-dir=./coverage]`
+
More docs coming soon, hopefully.
@@ -0,0 +1,15 @@
+var Bar = module.exports = function(str) {
+ this.bar = str;
+ this.str = str;
+};
+
+Bar.prototype.foo = function() {
+ var self = this;
+ return self.bar;
+};
+
+Bar.prototype.baz = function() {
+ var self = this;
+ return self.str;
+};
+
@@ -0,0 +1,15 @@
+var Foo = module.exports = function(str) {
+ this.foo = str;
+ this.str = str;
+};
+
+Foo.prototype.bar = function() {
+ var self = this;
+ return self.foo;
+};
+
+Foo.prototype.baz = function() {
+ var self = this;
+ return self.str;
+};
+

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,20 @@
+var test = require('tap').test,
+ Bar = require('../lib/bar'),
+ bar;
+
+test('setup', function(t) {
+ bar = new Bar('baz');
+ t.ok(bar);
+ t.end();
+});
+
+test('bar', function(t) {
+ t.equal('baz', bar.foo());
+ t.end();
+});
+
+test('teardown', function(t) {
+ t.ok(true);
+ t.end();
+});
+
@@ -0,0 +1,29 @@
+var test = require('tap').test,
+ Foo = require('../lib/foo'),
+ Bar = require('../lib/bar'),
+ foo, bar;
+
+test('setup', function(t) {
+ foo = new Foo('baz');
+ t.ok(foo);
+ bar = new Bar('baz');
+ t.ok(bar);
+ t.end();
+});
+
+test('baz from Foo', function(t) {
+ t.equal('baz', foo.baz());
+ t.end();
+});
+
+test('baz from Bar', function(t) {
+ t.equal('baz', bar.baz());
+ t.end();
+});
+
+
+test('teardown', function(t) {
+ t.ok(true);
+ t.end();
+});
+
@@ -0,0 +1,20 @@
+var test = require('tap').test,
+ Foo = require('../lib/foo'),
+ foo;
+
+test('setup', function(t) {
+ foo = new Foo('baz');
+ t.ok(foo);
+ t.end();
+});
+
+test('bar', function(t) {
+ t.equal('baz', foo.bar());
+ t.end();
+});
+
+test('teardown', function(t) {
+ t.ok(true);
+ t.end();
+});
+
View
@@ -0,0 +1,76 @@
+var fs = require('fs'),
+ path = require('path'),
+ asyncMap = require("slide").asyncMap,
+ util = require('util');
+
+var CovHtml = module.exports = function(cov_stats, cov_dir, cb) {
+ var index = [];
+
+ asyncMap(
+ Object.keys(cov_stats),
+ function(f, cb) {
+ var st = cov_stats[f],
+ missing_lines = st.missing.map(function(l) {
+ return l.number;
+ }),
+ out = '<!doctype html>\n<html lang="en">\n<head>\n ' +
+ '<meta charset="utf-8">\n <title>' +
+
+ f + ' (' + st.loc + ')</title>\n' +
+ '<style type="text/css">\n' +
+ 'li {\n' +
+ ' font-family: monospace;\n' +
+ ' white-space: pre;\n' +
+ '}\n' +
+ '</style>\n' +
+ '</head>\n<body>\n' +
+ '<h1>' + f + ' (' + st.loc + ')' + '</h1>' +
+ '<h2>Run: ' + (st.missing.length ? st.loc - st.missing.length : st.loc) + ', Missing: ' +
+ st.missing.length + ', Percentage: ' + st.percentage + '</h2>' +
+ '<h2>Source:</h2>\n' +
+ '<ol>\n' +
+ st.lines.map(function(line) {
+ var number = line.number,
+ color = (missing_lines.indexOf(number) !== -1) ? 'red' : 'green';
+ return '<li id="L' + line.number + '" style="background-color: ' + color +
+ ';">' + line.source + '</li>';
+ }).join('\n') +
+ '</ol>\n</body>\n</html>';
+
+ fs.writeFile(
+ cov_dir + '/' +
+ f.replace(process.cwd() + '/', '').replace('/', '+') + '.html',
+ out,
+ 'utf8',
+ function(err) {
+ if (err) {
+ throw err;
+ }
+ index.push(f);
+ cb();
+ });
+ },
+ function(err) {
+ if (err) {
+ throw err;
+ }
+ var out = '<!doctype html>\n<html lang="en">\n<head>\n ' +
+ '<meta charset="utf-8">\n <title>Coverage Index</title>\n</head>\n' +
+ '<body>\n<h1>Code Coverage Information</h1>\n<ul>' +
+ index.map(function(fname) {
+ return '<li><a href="' +
+ fname.replace(process.cwd() + '/', '').replace('/', '+') + '.html' +
+ '">' + fname + '</a></li>';
+ }).join('\n') + '</ul>\n</body>\n</html>';
+
+ fs.writeFile(cov_dir + '/index.html', out, 'utf8', function(err) {
+ if (err) {
+ throw err;
+ }
+ cb();
+ });
+ }
+ );
+};
+
+
Oops, something went wrong.

0 comments on commit 68f6a35

Please sign in to comment.