-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.js
140 lines (83 loc) · 2.37 KB
/
runner.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
'use strict';
const EventEmitter = require('events').EventEmitter;
const colo = require('colo');
const forkTest = require('./forkTest');
const runEmitter = new EventEmitter();
class Runner {
static test(message, check) {
Runner.tasks.push({message, check});
if (!Runner.eaten) {
process.nextTick(Runner.eat);
Runner.eaten = true;
}
}
static only(message, check) {
Runner.onlyTask = {message, check};
if (!Runner.eaten) {
process.nextTick(Runner.eat);
Runner.eaten = true;
}
}
static runTest(task) {
const checkPromise = new Promise(task.check);
checkPromise.then(() => {
process.exit(0);
}).catch((e) => {
if (e && e.stack) {
console.error(`${e.stack}`);
} else {
console.error(`${colo.red.bold('Subtest failed message: ')}${e}`);
}
});
}
static nextTest(next) {
const task = Runner.tasks[next];
Runner.report({type: 'testname', testName: task.message});
const child = forkTest(
process.argv[1], {
silent: true,
env: Object.assign(process.env, {EATER_SUBTEST: '' + next})
});
child.on('success', (out) => {
Runner.report({type: 'success', testName: task.message, out});
});
child.on('failure', (out, error) => {
Runner.report({type: 'failure', testName: task.message, out, error});
++Runner.errorTasks;
});
child.on('close', () => {
if (next + 1 < Runner.taskCount) {
runEmitter.emit('next', next + 1);
} else if (Runner.errorTasks) {
console.error(colo.red.bold(`${Runner.errorTasks} Subtest(s) failed`));
}
});
}
static report(message) {
if (process.send) {
process.send(message);
} else {
console.log(`${message.type}: ${message.testName}`);
}
}
static eat() {
if (Runner.onlyTask) {
Runner.tasks = [Runner.onlyTask];
}
Runner.taskCount = Runner.tasks.length;
runEmitter.on('run', Runner.runTest);
runEmitter.on('next', Runner.nextTest);
if (process.env.EATER_SUBTEST) {
const next = +(process.env.EATER_SUBTEST) || 0;
runEmitter.emit('run', Runner.tasks[next]);
} else {
runEmitter.emit('next', 0);
}
}
}
Runner.eaten = false;
Runner.tasks = [];
Runner.onlyTask = null;
Runner.taskCount = 0;
Runner.errorTasks = 0;
module.exports = Runner;