diff --git a/.gitignore b/.gitignore index e3655ac..ddeda2c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ npm-debug.log node_modules test/conf.json -scratch.js \ No newline at end of file +scratch.js +coverage diff --git a/Makefile b/Makefile index 82842d2..668a270 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,21 @@ -DOCS = docs/*.md -REPORTER ?=spec +BASE = . -test: - @NODE_ENV=test ./node_modules/.bin/mocha \ - --reporter $(REPORTER) +ISTANBUL = ./node_modules/.bin/istanbul +TEST_COMMAND = NODE_ENV=test ./node_modules/.bin/mocha +COVERAGE_OPTS = --lines 65 --statements 65 --branches 57 --functions 79 + +main: lint test + +cover: + $(ISTANBUL) cover test/run.js -test-cov: lib-cov - @ELASTICSEARCHCLIENT_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html - @rm -rf ./lib-cov +check-coverage: + $(ISTANBUL) check-coverage $(COVERAGE_OPTS) + +test: + test/run.js -lib-cov: - @jscoverage lib $@ +test-cov: cover check-coverage -.PHONY: test test-cov +.PHONY: test diff --git a/package.json b/package.json index a4d000d..5bae09e 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,11 @@ }, "dependencies": {}, "devDependencies": { - "mocha": "1.7.4" - , "chai": "*" - }, + "chai": "*", + "istanbul": "0.1.29", + "mocha": "1.7.4", + "optimist" : "0.3.5" + }, "repository": { "type": "git", "url": "git://github.com/phillro/node-elasticsearch-client.git" diff --git a/test/run.js b/test/run.js new file mode 100755 index 0000000..966f9d7 --- /dev/null +++ b/test/run.js @@ -0,0 +1,78 @@ +#!/usr/bin/env node +process.env.NODE_ENV = 'test'; + +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] [--bail]") + .default({types: 'test', reporter: 'spec', timeout: 2000}) + .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('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 = ['test']; +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; } + + mocha.run(function(failures) { + process.exit(failures); + }); +}); diff --git a/test/support/index.js b/test/support/index.js new file mode 100644 index 0000000..9d9bb2b --- /dev/null +++ b/test/support/index.js @@ -0,0 +1,16 @@ +require('../../index'); + +module.exports = { + 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; + } +}; diff --git a/test/support/random.js b/test/support/random.js new file mode 100644 index 0000000..195e617 --- /dev/null +++ b/test/support/random.js @@ -0,0 +1,25 @@ +var Faker = require('Faker'); + +var random = { + number: function (max) { + max = max || 10000; + return Faker.Helpers.randomNumber(max); + }, + + string: function (str_len) { + str_len = str_len || 8; + var chars = "abcdefghiklmnopqrstuvwxyz"; + var random_str = ''; + for (var i = 0; i < str_len; i++) { + var rnum = Math.floor(Math.random() * chars.length); + random_str += chars.substring(rnum, rnum + 1); + } + return random_str; + }, + + email: function () { + return this.string() + '+' + this.string() + '@' + 'example.com'; + } +}; + +module.exports = random; diff --git a/test/support/walk_dir.js b/test/support/walk_dir.js new file mode 100644 index 0000000..ab4fef0 --- /dev/null +++ b/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;