Permalink
Browse files

Added code coverage to srunner

* Switched to colors for my colour needs
* Improved help for srunner
* Fixed a few input validation bugs in srunner
  • Loading branch information...
1 parent 251288e commit f4dc331f41ac5ab296b7b9ee9b1a33adf13129d1 Siddharth Mahendraker committed Dec 14, 2011
Showing with 76 additions and 19 deletions.
  1. +2 −1 TODO.md
  2. +60 −9 bin/srunner.js
  3. +9 −6 lib/stest.js
  4. +3 −2 package.json
  5. +2 −1 test/test-stest.js
View
@@ -1,3 +1,4 @@
# TODO
-* Add code coverage
+* Output code coverage results
+to file
View
@@ -3,34 +3,60 @@
var path = require("path"),
fs = require("fs"),
cp = require("child_process"),
- color = require("ansi-color").set,
- argv = require("optimist")
- .usage("Usage: $0 [-s] -r [regex]")
- .demand(["r"])
+ color = require("colors"),
+ optimist = require("optimist"),
+ argv = optimist
+ .usage("Usage: $0 [-s] [-c] [modules] -r [regexp]")
+ .options("s",{
+ description: "supress output",
+ alias: "silent",
+ boolean: true
+ })
+ .options("c",{
+ description: "files to output code coverage for",
+ alias: "cover",
+ string: true
+ })
+ .options("r",{
+ description: "regexp of files to test",
+ alias: "regexp",
+ string: true,
+ demand: true
+ })
.argv;
/* srunner args
*
* -r: regexp of files to test
* -s: silent
+ * -c: code coverage
*
*/
-var silent = argv.s ? argv.s : false;
+var silent = argv.s;
+var coverage = argv.c ? argv.c.split(",") : [];
var r = path.join(process.cwd(), argv.r);
var info = function(buffer, colr){
if(!silent){
var string = buffer.toString();
- process.stdout.write(color(string, colr));
+ if(colr)
+ process.stdout.write(color[colr](string));
+ else
+ process.stdout.write(string);
}
}
+if(typeof(argv.r) !== "string" || !argv.r.length){
+ info(optimist.help());
+ process.exit();
+}
+
var dir = path.dirname(r);
var filexp = new RegExp(path.basename(r));
if(!path.existsSync(dir)){
- info("This directory does not exist\n", "red");
+ info("Invalid directory: This directory does not exist\n", "red");
process.exit();
}
@@ -39,13 +65,38 @@ var matchExp = function(file){ return filexp.test(file) ? true : false; };
var filtered = files.filter(matchExp);
if(!filtered.length){
- info("No tests found to run\n", "red");
+ info("Invalid path: No tests found to run\n", "red");
process.exit();
}
filtered.forEach(function(file){
cp.exec("node "+dir+"/"+file, function(err, stdout, stderr){
if(err) throw err;
- info(stdout);
+ if(!coverage) info(stdout);
});
});
+
+if(coverage){
+ var codecover = require("runforcover").cover(/.*/g);
+
+ coverage = coverage.map(function(file){ return path.join(process.cwd(), file); });
+ coverage.forEach(function(file){ require(file); });
+
+ filtered = filtered.map(function(file){ return dir+"/"+file; });
+ filtered.forEach(function(file){ require(file); });
+
+ coverage.forEach(function(obj, i){
+ codecover(function(cd){
+ var stats = cd[obj].stats();
+
+ var col = "magenta";
+ info("Coverage for: "+path.basename(obj)+"\n", col);
+ info("Lines seen: "+stats.seen+"\n", col);
+ info("Lines missing: "+stats.missing+"\n", col);
+ info("Percentage seen: "+Math.floor(stats.percentage*100)+"%\n", col);
+ info("\n");
+ });
+ });
+
+ codecover.release();
+}
View
@@ -1,5 +1,6 @@
var events = require("events"),
- color = require("ansi-color").set;
+ color = require("colors");
+
/*
* @class stest
*
@@ -10,10 +11,12 @@ var events = require("events"),
function stest(){
+ var self = this;
+
this.ctx = {};
this.promise = new events.EventEmitter();
- this._queue = [];
-
+ this._queue = [];
+
this._addTime = function(func){
var now = Date.now();
func();
@@ -35,12 +38,12 @@ function stest(){
}
// write results
- process.stdout.write(color(result+" "+name+" - "+time+"ms\n", c));
+ process.stdout.write(color[c](result+" "+name+" - "+time+"ms\n"));
// print errs
errors.forEach(function(err){
- process.stdout.write(color(err.message+"\n", "red"));
+ process.stdout.write(color.red(err.message+"\n"));
if(err.type !== "stest")
- process.stdout.write(color(err.stack+"\n", "red"));
+ process.stdout.write(color.red(err.stack+"\n"));
});
}
}
View
@@ -16,7 +16,7 @@
},
"main": "lib/stest.js",
"scripts": {
- "test": "srunner -r test/test-.*\\.js"
+ "test": "srunner -c lib/stest.js -r test/test-.*\\.js"
},
"bin": {
"srunner": "bin/srunner.js"
@@ -26,7 +26,8 @@
},
"dependencies": {
"optimist": ">=0.2.8",
- "ansi-color": "0.2.1"
+ "colors": ">=0.6.0",
+ "runforcover": ">=0.0.1"
},
"devDependencies": {}
}
View
@@ -4,7 +4,8 @@ var assert = require("assert"),
// defaults to 250 ms
var opts = { timeout: 0 };
-stest.addCase("stest - basic tests", opts, {
+stest
+.addCase("stest - basic tests", opts, {
setup: function(promise){
this.one = "one";
promise.emit("ctx_1");

0 comments on commit f4dc331

Please sign in to comment.