-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode-harness.js
174 lines (167 loc) · 4.53 KB
/
node-harness.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var Tyrtle = require('./Tyrtle'),
renderer,
fs = require('fs'),
path = require('path'),
glob,
tests,
args,
color,
loadFiles,
inp, inptext,
runTests,
oldCwd
;
args = require('optimist')
.boolean("monochrome")
.describe("monochrome", "Output without any colors")
.boolean("only-errors")
.describe("only-errors", "Only show tests which fail")
.string("list")
.describe("list", "If this is set, then the files specified should contain a JSON object specifying the paths "
+ "of other files to include"
)
.boolean('dry')
.alias('dry', 'dry-run')
.describe('dry', "Perform a dry run. In this mode, none of the test modules are loaded. Instead, a list of the "
+ "files which *would* have been loaded is displayed and the program exits. This is useful for "
+ "testing file-matching patterns")
.string('renderer')
.alias('renderer', 'r')['default']('renderer', 'node')
.usage("[1] : $0 [options] [--] file [file [file ...]]\n"
+ "[2] : $0 [options] --list fileList.json\n"
+ "\n"
+ "In method [1], wildcards can be used, eg: $0 *.test.js test.*.js\n"
+ "To use recursive matching (through subdirectories) you will need to quote the strings,\n"
+ "eg: $0 \"tests/**.js\" \"vendors/tests/**\"\n"
+ "\n"
+ "In method [2], specify a file containing an array of paths in JSON format. Paths in the\n"
+ "file list are relative to the list itself. Wildcard patterns can also be used inside a\n"
+ "file list, however no extra quoting is required."
)
.wrap(80)
.check(function (args) {
// this function throws so that optimist doesn't print out the body of this function
if (args._.length === 0) {
if (!args.list) {
throw "";
}
} else if (args.list) {
throw "";
}
if (args.renderer) {
try {
if (/^\.{0,2}\//.test(args.renderer)) { // relative or absolute path given
// load the path given
renderer = require(args.renderer);
} else {
try {
// load from the renderers folder
renderer = require('./renderers/' + args.renderer);
} catch (ee) {
// try loading from node_modules or whatever
renderer = require(args.renderer);
}
}
} catch (e) {
throw "Renderer [" + args.renderer + "] not found";
}
}
})
.argv
;
//console.log(require('optimist'));
//console.log(args);
//process.exit(0);
if (renderer.setMonochrome) {
renderer.setMonochrome(args.monochrome);
}
renderer.onlyErrors = args['only-errors'];
Tyrtle.setRenderer(renderer);
color = args.monochrome
? function (col, s) {
return s;
}
: (function () {
var c = {
red : '31',
white : '1;37'
};
return function (col, s) {
return '\u001b[' + c[col] + 'm' + s + '\u001b[0m';
};
}())
;
runTests = function () {
var i, l, val,
re_glob = /[?*]/,
newFiles,
run = false,
t,
options = {}
;
Object.keys(args).forEach(function (key) {
if (!(/^(\$0|_)$/.test(key))) {
options[key] = args[key];
}
});
options.callback = function () {
process.exit(t.fails);
};
t = new Tyrtle(options);
for (i = 0, l = loadFiles.length; i < l; ++i) {
val = loadFiles[i];
if (re_glob.test(val)) {
glob = glob || require('glob');
newFiles = glob.sync(val);
loadFiles.push.apply(loadFiles, newFiles);
l += newFiles.length;
} else {
if (!(/^\.{0,2}\//.test(val))) {
val = process.cwd() + '/' + val;
}
if (args.dry) {
console.log(fs.realpathSync(val));
} else {
try {
tests = require(val);
} catch (e) {
console.error(
color('red', "Could not load module ")
+ color('white', "%s"),
val.replace(/\.js$/, '') + '.js'
);
continue;
}
run = true;
if (Tyrtle.util.isArray(tests)) {
for (i = 0, l = tests.length; i < l; ++i) {
t.module(tests[i]);
}
} else {
t.module(tests);
}
}
}
}
if (run) {
t.run();
}
};
if (args.list) {
oldCwd = process.cwd();
inp = fs.createReadStream(args.list); // TODO: handle the cwd
process.chdir(path.dirname(fs.realpathSync(args.list)));
inptext = "";
inp.setEncoding('utf8');
inp.on('data', function (data) {
inptext += data;
});
inp.on('end', function (close) {
loadFiles = JSON.parse(inptext);
runTests();
process.chdir(oldCwd);
});
} else {
loadFiles = args._;
runTests();
}