Skip to content

Commit 0cdb264

Browse files
author
Nick Fisher
committed
Significant improvements to the node test runner
- It now has some decent help text for running it from the command line - Lists of test files are now supported - Wildcards (including recursive `**`) is allowed from the command line and in lists A basic package.json file added, mainly just to track the dependencies for now.
1 parent d27c7a9 commit 0cdb264

File tree

4 files changed

+149
-10
lines changed

4 files changed

+149
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11

2+
node_modules

demo/node-list.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["node-tests"]

demo/node-runner.js

+126-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,134 @@
11
var Tyrtle = require('../Tyrtle'),
22
renderer = require('../renderers/node'),
3-
tests = require('./node-tests'),
4-
t,
5-
i, l
3+
fs = require('fs'),
4+
path = require('path'),
5+
glob,
6+
tests,
7+
args = require('optimist')
8+
.boolean("monochrome")
9+
.describe("monochrome", "Output without any colors")
10+
.boolean("only-errors")
11+
.describe("only-errors", "Only show tests which fail")
12+
.string("list")
13+
.describe("list", "If this is set, then the files specified should contain a JSON object specifying the paths "
14+
+ "of other files to include"
15+
)
16+
.boolean('dry')
17+
.alias('dry', 'dry-run')
18+
.describe('dry', "Perform a dry run. In this mode, none of the test modules are loaded. Instead, a list of the "
19+
+ "files which *would* have been loaded is displayed and the program exits. This is useful for "
20+
+ "testing file-matching patterns")
21+
.usage("[1] : $0 [options] [--] file [file [file ...]]\n"
22+
+ "[2] : $0 [options] --list fileList\n"
23+
+ "\n"
24+
+ "In method [1], wildcards can be used, eg: $0 *.test.js test.*.js\n"
25+
+ "To use recursive matching (through subdirectories) you will need to quote the strings,\n"
26+
+ "eg: $0 \"tests/**.js\" \"vendors/tests/**\""
27+
+ "\n"
28+
+ "In method [2], paths in the file list are relative to the list itself. Wildcard patterns can also be \n"
29+
+ "used inside a file list, however no extra quoting is required."
30+
)
31+
.wrap(80)
32+
.check(function (args) {
33+
// this function throws so that optimist doesn't print out the body of this function
34+
if (args._.length === 0) {
35+
if (!args.list && !args.pattern) {
36+
throw "";
37+
}
38+
} else if (args.list || args.pattern) {
39+
throw "";
40+
}
41+
})
42+
.argv,
43+
color,
44+
t, i, l,
45+
loadFiles,
46+
inp, inptext,
47+
runTests,
48+
oldCwd
649
;
7-
Tyrtle.setRenderer(renderer);
50+
//console.log(args);
51+
//process.exit(0);
852

9-
t = new Tyrtle();
53+
renderer.setMonochrome(args.monochrome);
54+
renderer.onlyErrors = args['only-errors'];
55+
Tyrtle.setRenderer(renderer);
1056

11-
if (Tyrtle.isArray(tests)) {
12-
for (i = 0, l = tests.length; i < l; ++i) {
13-
t.module(tests[i]);
57+
color = args.monochrome
58+
? function (col, s) {
59+
return s;
60+
}
61+
: (function () {
62+
var c = {
63+
red : '31',
64+
white : '1;37'
65+
};
66+
return function (col, s) {
67+
return '\u001b[' + c[col] + 'm' + s + '\u001b[0m';
68+
};
69+
}())
70+
;
71+
runTests = function () {
72+
t = new Tyrtle();
73+
var i, l, val, re_glob, newFiles, run = false;
74+
re_glob = /[?*]/;
75+
for (i = 0, l = loadFiles.length; i < l; ++i) {
76+
val = loadFiles[i];
77+
if (re_glob.test(val)) {
78+
glob = glob || require('glob');
79+
newFiles = glob.globSync(val);
80+
loadFiles.push.apply(loadFiles, newFiles);
81+
l += newFiles.length;
82+
} else {
83+
if (!(/^\.{0,2}\//.test(val))) {
84+
val = process.cwd() + '/' + val;
85+
}
86+
if (args.dry) {
87+
console.log(fs.realpathSync(val));
88+
} else {
89+
try {
90+
tests = require(val);
91+
} catch (e) {
92+
console.error(
93+
color('red', "Could not load module ")
94+
+ color('white', "%s"),
95+
val.replace(/\.js$/, '') + '.js'
96+
);
97+
continue;
98+
}
99+
run = true;
100+
if (Tyrtle.isArray(tests)) {
101+
for (i = 0, l = tests.length; i < l; ++i) {
102+
t.module(tests[i]);
103+
}
104+
} else {
105+
t.module(tests);
106+
}
107+
}
108+
}
14109
}
110+
if (run) {
111+
t.run();
112+
}
113+
};
114+
115+
116+
if (args.list) {
117+
oldCwd = process.cwd();
118+
inp = fs.createReadStream(args.list); // TODO: handle the cwd
119+
process.chdir(path.dirname(fs.realpathSync(args.list)));
120+
inptext = "";
121+
122+
inp.setEncoding('utf8');
123+
inp.on('data', function (data) {
124+
inptext += data;
125+
});
126+
inp.on('end', function (close) {
127+
loadFiles = JSON.parse(inptext);
128+
runTests();
129+
process.chdir(oldCwd);
130+
});
15131
} else {
16-
t.module(tests);
132+
loadFiles = args._;
133+
runTests();
17134
}
18-
t.run();

package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name" : "Myrtle",
3+
"description" : "A mocking and unit test framework for browsers and node",
4+
"version" : '0.1.0',
5+
"homepage" : 'http://github.com/spadgos/myrtle/wiki',
6+
"author" : {
7+
"name" : "Nick Fisher",
8+
"email" : "[email protected]"
9+
},
10+
"repository" : {
11+
"type" : "git",
12+
"url" : "https://github.com/spadgos/myrtle"
13+
},
14+
"bugs" : {
15+
"web" : "http://github.com/spadgos/myrtle/issues"
16+
},
17+
"dependencies" : {
18+
"optimist" : "0.2",
19+
"glob" : "2.0"
20+
}
21+
}

0 commit comments

Comments
 (0)