Skip to content

Commit 5ce7933

Browse files
committed
Makefile, test/run.js
1 parent 8118829 commit 5ce7933

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
coverage

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BASE = .
2+
3+
ISTANBUL = ./node_modules/.bin/istanbul
4+
TEST_COMMAND = NODE_ENV=test ./node_modules/.bin/mocha
5+
COVERAGE_OPTS = --lines 95 --statements 90 --branches 80 --functions 90
6+
7+
main: lint test
8+
9+
cover:
10+
$(ISTANBUL) cover test/run.js
11+
12+
check-coverage:
13+
$(ISTANBUL) check-coverage $(COVERAGE_OPTS)
14+
15+
test: cover check-coverage
16+
17+
18+
test-cov: cover check-coverage
19+
open coverage/lcov-report/index.html
20+
21+
test-acceptance:
22+
test/run.js -T acceptance
23+
24+
lint:
25+
./node_modules/jshint/bin/hint ./lib --config $(BASE)/.jshintrc && cd test && ../node_modules/jshint/bin/hint . && cd ..
26+
27+
28+
.PHONY: test docs

test/run.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env node
2+
process.env.NODE_ENV = 'test';
3+
require('../lib');
4+
5+
var fs = require('fs');
6+
var Mocha = require('mocha');
7+
var optimist = require('optimist');
8+
var walk_dir = require('./support/walk_dir');
9+
10+
var argv = optimist
11+
.usage("Usage: $0 -t [types] --reporter [reporter] --timeout [timeout]")
12+
.default({types: 'unit,component', reporter: 'spec', timeout: 6000})
13+
.describe('types', 'The types of tests to run, separated by commas. E.g., unit,component,acceptance')
14+
.describe('reporter', 'The mocha test reporter to use.')
15+
.describe('timeout', 'The mocha timeout to use per test (ms).')
16+
.boolean('help')
17+
.alias('types', 'T')
18+
.alias('timeout', 't')
19+
.alias('reporter', 'R')
20+
.alias('help', 'h')
21+
.argv;
22+
23+
var mocha = new Mocha({timeout: argv.timeout, reporter: argv.reporter, ui: 'bdd'});
24+
25+
var valid_test_types = ['unit', 'functional', 'acceptance', 'integration'];
26+
var requested_types = argv.types.split(',');
27+
var types_to_use = [];
28+
29+
valid_test_types.forEach(function (valid_test_type) {
30+
if (requested_types.indexOf(valid_test_type) !== -1) {
31+
types_to_use.push(valid_test_type);
32+
}
33+
});
34+
35+
if (argv.help || types_to_use.length === 0) {
36+
console.log('\n' + optimist.help());
37+
process.exit();
38+
}
39+
40+
var is_valid_file = function (file) {
41+
for (var i = 0; i < types_to_use.length; i++) {
42+
var test_type = types_to_use[i];
43+
var ext = test_type + ".js";
44+
45+
if (file.indexOf(ext) !== -1) {
46+
return true;
47+
}
48+
}
49+
50+
return false;
51+
};
52+
53+
function run(cb) {
54+
walk_dir.walk('test', is_valid_file, function (err, files) {
55+
if (err) { return cb(err); }
56+
57+
files.forEach(function (file) {
58+
mocha.addFile(file);
59+
});
60+
61+
cb();
62+
});
63+
}
64+
65+
run(function (err) {
66+
mocha.run(function (failures) {
67+
process.exit(failures);
68+
});
69+
});

test/support/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
http: require('./http'),
3-
random: require('./random')
3+
random: require('./random'),
4+
walk_dir: require('./walk_dir')
45
};

test/support/walk_dir.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var fs = require('fs');
2+
3+
var methods = {
4+
walk: function (dir, validation_function, cb) {
5+
if (arguments.length === 2) {
6+
cb = validation_function;
7+
validation_function = null;
8+
}
9+
10+
var results = [];
11+
fs.readdir(dir, function (err, list) {
12+
if (err) { return cb(err); }
13+
14+
var pending = list.length;
15+
16+
if (!pending) { return cb(null, results); }
17+
18+
list.forEach(function (file) {
19+
file = dir + '/' + file;
20+
fs.stat(file, function (err, stat) {
21+
if (stat && stat.isDirectory()) {
22+
methods.walk(file, validation_function, function (err, res) {
23+
results = results.concat(res);
24+
if (!--pending) { cb(null, results); }
25+
});
26+
} else {
27+
if (typeof validation_function === 'function') {
28+
if (validation_function(file)) {
29+
results.push(file);
30+
}
31+
} else {
32+
results.push(file);
33+
}
34+
35+
if (!--pending) { cb(null, results); }
36+
}
37+
});
38+
});
39+
});
40+
}
41+
};
42+
43+
module.exports = methods;

0 commit comments

Comments
 (0)