forked from tests-always-included/mo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-spec.js
241 lines (208 loc) · 6.53 KB
/
run-spec.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env node
var async, exec, fs, summary;
function makeShellString(value) {
if (typeof value === 'string') {
// Newlines are tricky
return value.split(/\n/).map(function (chunk) {
return JSON.stringify(chunk);
}).join('"\n"');
}
if (typeof value === 'number') {
return value;
}
return 'ERR_CONVERTING';
}
function addToEnvironment(name, value) {
var result;
if (Array.isArray(value)) {
result = [
'('
];
value.forEach(function (subValue) {
result.push(makeShellString(subValue));
});
result.push(')');
return name + '=' + result.join(' ');
}
// Sometimes the __tag__ property of the code in the lambdas may
// be missing. :-(
if (typeof value === 'object') {
if (value.__tag__ === 'code' || (value.ruby && value.php && value.perl)) {
if (value.bash) {
return name + '() { ' + value.bash + '; }';
}
return name + '() { perl -e \'print ((' + value.perl + ')->("\'"$1"\'"))\'; }';
}
}
if (typeof value === 'object') {
return '# ' + name + ' is an object and will not work in bash';
}
if (typeof value === 'boolean') {
if (value) {
return name + '="true"';
}
return name + '=""';
}
return name + '=' + makeShellString(value);
}
function runTest(test, done) {
var output, partials, script;
script = [
'#!/bin/bash'
];
partials = test.partials || {};
Object.keys(test.data).forEach(function (name) {
script.push(addToEnvironment(name, test.data[name]));
});
script.push('. mo');
script.push('mo spec-runner/spec-template');
test.script = script.join('\n');
async.series([
function (taskDone) {
fs.mkdir("spec-runner/", function (err) {
if (err && err.code !== 'EEXIST') {
return taskDone(err);
}
taskDone();
});
},
function (taskDone) {
fs.writeFile('spec-runner/spec-script', test.script, taskDone);
},
function (taskDone) {
fs.writeFile('spec-runner/spec-template', test.template, taskDone);
},
function (taskDone) {
async.eachSeries(Object.keys(partials), function (partialName, partialDone) {
fs.writeFile('spec-runner/' + partialName, test.partials[partialName], partialDone);
}, taskDone);
},
function (taskDone) {
exec('bash spec-runner/spec-script', function (err, stdout) {
if (err) {
return taskDone(err);
}
output = stdout;
taskDone();
});
},
function (taskDone) {
async.eachSeries(Object.keys(partials), function (partialName, partialDone) {
fs.unlink('spec-runner/' + partialName, partialDone);
}, taskDone);
},
function (taskDone) {
fs.unlink('spec-runner/spec-script', taskDone);
},
function (taskDone) {
fs.unlink('spec-runner/spec-template', taskDone);
},
function (taskDone) {
fs.rmdir('spec-runner/', taskDone);
}
], function (err) {
if (err) {
return done(err);
}
done(null, output);
});
return '';
}
function prepareAndRunTest(test, done) {
async.waterfall([
function (taskDone) {
console.log('### ' + test.name);
console.log('');
console.log(test.desc);
console.log('');
runTest(test, taskDone);
},
function (actual, taskDone) {
test.actual = actual;
test.pass = (test.actual === test.expected);
if (test.pass) {
console.log('Passed.');
} else {
console.log('Failed.');
console.log('');
console.log(test);
}
console.log('');
taskDone();
}
], done);
}
function specFileToName(file) {
return file.replace(/.*\//, '').replace('.json', '').replace('~', '').replace(/(^|-)[a-z]/g, function (match) {
return match.toUpperCase();
});
}
function processSpecFile(specFile, done) {
fs.readFile(specFile, 'utf8', function (err, data) {
var name;
if (err) {
return done(err);
}
name = specFileToName(specFile);
data = JSON.parse(data);
console.log(name);
console.log('====================');
console.log('');
console.log(data.overview);
console.log('');
console.log('Tests');
console.log('-----');
console.log('');
async.series([
function (taskDone) {
async.eachSeries(data.tests, prepareAndRunTest, taskDone);
},
function (taskDone) {
summary[name] = {};
data.tests.forEach(function (test) {
summary[name][test.name] = test.pass;
});
taskDone();
}
], done);
});
}
// 0 = node, 1 = script, 2 = file
if (process.argv.length < 3) {
console.log('Specify one or more JSON spec files on the command line');
process.exit();
}
async = require('async');
fs = require('fs');
exec = require('child_process').exec;
summary = {};
async.eachSeries(process.argv.slice(2), processSpecFile, function () {
var fail, pass;
console.log('');
console.log('Summary');
console.log('=======');
console.log('');
pass = 0;
fail = 0;
Object.keys(summary).forEach(function (name) {
var groupPass, groupFail, testResults;
testResults = [];
groupPass = 0;
groupFail = 0;
Object.keys(summary[name]).forEach(function (testName) {
if (summary[name][testName]) {
testResults.push(' * pass - ' + testName);
groupPass += 1;
pass += 1;
} else {
testResults.push(' * FAIL - ' + testName);
groupFail += 1;
fail += 1;
}
});
testResults.unshift('* ' + name + ' (failed ' + groupFail + ' out of ' + (groupPass + groupFail) + ' tests)');
console.log(testResults.join('\n'));
});
console.log('');
console.log('Failed ' + fail + ' out of ' + (pass + fail) + ' tests');
});