Skip to content

Commit

Permalink
switching to instanbul for test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanDonovan committed Aug 16, 2013
1 parent f2e77cd commit faba5e1
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,4 +2,5 @@ node_modules
test/settings.js
doc
lib-cov
coverage
coverage.html
3 changes: 3 additions & 0 deletions History.md
Expand Up @@ -33,3 +33,6 @@

- 1.0.2
Making logging work when using EasyMySQL.connect_with_pool().

- 1.0.3
Switching to Istanbul for test coverage.
29 changes: 18 additions & 11 deletions Makefile
@@ -1,22 +1,29 @@
PACKAGE = easy_mysql
NODEJS = $(if $(shell test -f /usr/bin/nodejs && echo "true"),nodejs,node)
BASE = .

all: test lint doc

test:
./node_modules/.bin/mocha
ISTANBUL = ./node_modules/.bin/istanbul
TEST_COMMAND = NODE_ENV=test ./node_modules/.bin/mocha
COVERAGE_OPTS = --lines 87 --statements 87 --branches 77 --functions 95

test-cov: lib-cov
EASY_MYSQL_JSCOV=1 ./node_modules/.bin/mocha --reporter html-cov > coverage.html && echo 'coverage saved to coverage.html'
main: lint test

lib-cov:
@rm -fr ./$@
@jscoverage lib $@
cover:
$(ISTANBUL) cover test/run.js -- -T unit,functional

check-coverage:
$(ISTANBUL) check-coverage $(COVERAGE_OPTS)

test: cover check-coverage


test-cov: cover check-coverage
open coverage/lcov-report/index.html

lint:
./node_modules/.bin/jshint lib test --config $(BASE)/.jshintrc && echo "Lint Done"

./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \
./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc

doc:
./node_modules/jsdoc-toolkit/app/run.js -t=./node_modules/jsdoc-toolkit/templates/jsdoc -d=./doc ./lib

Expand Down
6 changes: 1 addition & 5 deletions index.js
@@ -1,5 +1 @@
if (process.env.EASY_MYSQL_JSCOV) {
module.exports = require("./lib-cov/easy_mysql");
} else {
module.exports = require("./lib/easy_mysql");
}
module.exports = require("./lib/easy_mysql");
10 changes: 6 additions & 4 deletions package.json
Expand Up @@ -10,17 +10,19 @@
"repository": {
"type": "git",
"url": "git://github.com:Mog-Inc/easy-mysql.git"
},
},
"dependencies": {
"generic-pool": "2.0.2",
"mysql": ">=0.9.1"
},
"devDependencies": {
"async": "0.1.22",
"mocha": "1.12.0",
"sinon": "1.6.0",
"istanbul": "0.1.43",
"jsdoc-toolkit": ">=0.0.2",
"jshint": "2.0.1",
"jsdoc-toolkit": ">=0.0.2"
"mocha": "1.12.0",
"optimist": "0.6.0",
"sinon": "1.6.0"
},
"keywords": ["mysql", "nodejs"],
"main": "index",
Expand Down
88 changes: 88 additions & 0 deletions test/run.js
@@ -0,0 +1,88 @@
#!/usr/bin/env node
process.env.NODE_ENV = 'test';
require('../index');

var Mocha = require('mocha');
var optimist = require('optimist');
var walk_dir = require('./support/walk_dir');

var argv = optimist
.usage("Usage: $0 -T [types] --reporter [reporter] --timeout [timeout]")
.default({types: 'unit,component', reporter: 'spec', timeout: 6000})
.describe('types', 'The types of tests to run, separated by commas. E.g., unit,functional,acceptance')
.describe('reporter', 'The mocha test reporter to use.')
.describe('timeout', 'The mocha timeout to use per test (ms).')
.boolean('bail')
.describe('bail', 'Bail on first failure')
.boolean('verbose')
.describe('verbose', 'Verbose output')
.boolean('help')
.alias('types', 'T')
.alias('timeout', 't')
.alias('reporter', 'R')
.alias('bail', 'b')
.alias('help', 'h')
.argv;

var mocha = new Mocha({timeout: argv.timeout, reporter: argv.reporter, ui: 'bdd'});
mocha.checkLeaks();
if (argv.bail) {
mocha.bail();
}

var valid_test_types = ['unit', 'functional', 'acceptance', 'integration'];
var requested_types = argv.types.split(',');
var types_to_use = [];

valid_test_types.forEach(function (valid_test_type) {
if (requested_types.indexOf(valid_test_type) !== -1) {
types_to_use.push(valid_test_type);
}
});

if (argv.help || types_to_use.length === 0) {
console.log('\n' + optimist.help());
process.exit();
}

var is_valid_file = function (file) {
for (var i = 0; i < types_to_use.length; i++) {
var test_type = types_to_use[i];
var ext = test_type + ".js";

if (file.indexOf(ext) !== -1) {
return true;
}
}

return false;
};

var test_files = [];

function run(cb) {
walk_dir.walk('test', is_valid_file, function (err, files) {
if (err) { return cb(err); }

files.forEach(function (file) {
test_files.push(file);
mocha.addFile(file);
});

cb();
});
}

run(function (err) {
if (err) { throw err; }

if (argv.verbose) {
console.log("\nTest files:");
test_files.forEach(function (file) {
console.log(file + " \\");
});
}
mocha.run(function (failures) {
process.exit(failures);
});
});
40 changes: 40 additions & 0 deletions test/support/index.js
@@ -0,0 +1,40 @@
var util = require('util');
require('../../index');

module.exports = {
fake_logger: require('./fake_logger'),

random: require('./random'),

walk_dir: require('./walk_dir'),

shallow_clone: function (object) {
var ret = {};
if (object) {
Object.keys(object).forEach(function (val) {
ret[val] = object[val];
});
}
return ret;
},

fake_error: function () {
return new Error(this.random.string());
},

check_err: function (err) {
if (err) {
var msg;

if (err instanceof Error) {
msg = err;
} else if (err.msg) {
msg = err.msg;
} else {
msg = util.inspect(err);
}

throw new Error(msg);
}
}
};
43 changes: 43 additions & 0 deletions test/support/walk_dir.js
@@ -0,0 +1,43 @@
var fs = require('fs');

var methods = {
walk: function (dir, validation_function, cb) {
if (arguments.length === 2) {
cb = validation_function;
validation_function = null;
}

var results = [];
fs.readdir(dir, function (err, list) {
if (err) { return cb(err); }

var pending = list.length;

if (!pending) { return cb(null, results); }

list.forEach(function (file) {
file = dir + '/' + file;
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
methods.walk(file, validation_function, function (err, res) {
results = results.concat(res);
if (!--pending) { cb(null, results); }
});
} else {
if (typeof validation_function === 'function') {
if (validation_function(file)) {
results.push(file);
}
} else {
results.push(file);
}

if (!--pending) { cb(null, results); }
}
});
});
});
}
};

module.exports = methods;

0 comments on commit faba5e1

Please sign in to comment.