-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
271 lines (228 loc) · 7.12 KB
/
index.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var chalk = require('chalk');
var readline = require('readline');
var _ = require('underscore');
var path = require('path');
var sliceAnsi = require('slice-ansi');
var stripAnsi = require('strip-ansi');
var spinners = [
'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏',
'┤┘┴└├┌┬┐'
];
var selectedSpinner = 1;
var index;
var currentPending;
var currentErrors = [];
var log = {
info: chalk.bgWhite.black,
pending: chalk.bgCyan.white,
error: chalk.bgRed.white,
success: chalk.bgGreen.white,
warn: chalk.bgYellow.white
};
var writeStream = process.stdout;
function write (st) {
var maxLength = writeStream.columns - 10;
// TODO Make something that works. Right now it's removing the capped line with the next one.
// Cap the complete string length based on the output width
if (stripAnsi(st).length > maxLength) {
// st = sliceAnsi(st, 0, maxLength - 4) + '... ';
}
writeStream.write(st);
}
function pending (text, tab) {
var spinner = spinners[selectedSpinner];
tab = tab || '';
index = 0;
clearInterval(currentPending);
currentPending = setInterval(function () {
clear();
write(
tab +
log.pending(
chalk.bold(' [ ' + spinner.charAt(index) + ' ] ') + text
)
);
index += 1;
if (index >= spinner.length) {
index = 0;
}
}, 80);
}
function clear () {
readline.clearLine(writeStream);
readline.cursorTo(writeStream, 0);
}
function finishTask () {
currentErrors = [];
}
function buildString (task) {
var st = ' ';
var duration = '';
// The longest a param can be.
var maxLength = 20;
var param;
if (task.duration) {
duration = chalk.bold.magenta(' [' + (task.duration / 1000).toFixed(2) +
's] ');
}
if (task.it) {
st += chalk.black(task.it) + ' ';
return chalk.bgWhite(st) + ' ' + chalk.bold.bgWhite(duration);
} else {
for (var i in task.params) {
if (task.params.hasOwnProperty(i)) {
param = task.params[i];
if (typeof param === 'object') {
param = JSON.stringify(param);
}
// Cap param string length.
if (param.length > maxLength) {
param = param.substring(0, maxLength - 4) + '...';
}
st += chalk.blue.bold(i) + chalk.bold(' : ');
st += chalk.black(param) + ' ';
}
}
return chalk.bold(task.type) + ' ' +
chalk.bgWhite(st) + ' ' +
chalk.bold.bgWhite(duration);
}
}
module.exports = function (runner) {
runner.on('begin', function (taskFiles) {
var st = '\n begin';
var last;
taskFiles = _.map(taskFiles, function (taskFile) {
return taskFile.name;
});
st += ' ' + taskFiles.length + ' suite';
if (taskFiles.length > 1) {
st += 's';
last = taskFiles.pop();
}
st += ' : ';
st += chalk.blue(taskFiles.join(', '));
if (last) {
st += chalk.blue(' and ' + last);
}
st += ' ';
write(log.info(st) + '\n');
});
runner.on('start', function (taskFile) {
var tasks, nbTasks, config;
try {
tasks = require(taskFile);
} catch (e) {
// Can't get the file.
}
var st = '\n ';
st += ' ' + chalk.blue(path.basename(taskFile, '.json'));
if (tasks) {
if (tasks.config.describe) {
st += chalk.blue.bold(': ');
st += chalk.black(tasks.config.describe);
}
nbTasks = tasks.tasks.length;
st += chalk.bold.red(' (' + nbTasks + ' task');
if (nbTasks > 1) {
st += chalk.bold.red('s');
}
st += chalk.bold.red(') ');
config = {
type: ' - configuration',
// Omit the url because we have it in the first task.
// Omit the describe because we have already wrote it.
params: _.omit(tasks.config, ['url', 'describe'])
};
st += '\n ' + buildString(config);
}
st += ' ';
// Write.
clearInterval(currentPending);
clear();
write(log.info(st) + '\n');
});
runner.on('pending', function (task) {
pending(buildString(task), ' ');
});
runner.on('error', function (error) {
currentErrors.push(error);
});
runner.on('pass', function (task) {
clearInterval(currentPending);
clear();
if (currentErrors.length) {
write(
' ' +
log.warn(chalk.bold(' [ ! ] ') + buildString(task)) +
chalk.bgYellow.red(' ' + currentErrors.length +
' error(s) ') +
'\n'
);
} else {
write(
' ' +
log.success(chalk.bold(' [ √ ] ') + buildString(task)) + '\n'
);
}
finishTask();
});
runner.on('fail', function (task, err) {
clearInterval(currentPending);
clear();
var st = chalk.bold(' [ x ] ');
st += buildString(task);
st += ' ';
if (err) {
st += chalk.bgYellow.red(' ' + err + ' ') + ' ';
}
write(' ' + log.error(st) + '\n');
finishTask();
});
runner.on('finish', function (report) {
var t = report.timing;
var e = report.errors;
var i, max, j;
clearInterval(currentPending);
write(log.info(chalk.bold('\n Report ')) + '\n');
write(log.info('- Ran for : ' +
chalk.bold((t.total / 1000).toFixed(2) + 's ')) + '\n');
if (t.slowest && t.slowest.test) {
write(
log.info('- Slowest : ' +
chalk.bold((t.slowest.test.it + ' ') +
chalk.red(
(t.slowest.test.duration / 1000).toFixed(2) + 's '))
) + '\n'
);
}
if (t.above && t.above.length) {
write(log.info('- Above median : ') + '\n');
for (i = 0, max = t.above.length; i < max; i += 1) {
write(log.info(' - ' + t.above[i].test.it + ' ') + '\n');
}
}
if (_.isEmpty(e.byError)) {
return;
}
write('\n' + log.error('- Errors : ') + '\n');
for (i in e.byError) {
write(
log.warn(chalk.red(
' - [' +
chalk.bold(e.byError[i].error.type) +
'] ' + i + ' : '
)) + '\n'
);
for (j = 0, max = e.byError[i].length; j < max; j += 1) {
write(
log.info(' - ' + e.byError[i][j].test.it + ' ') +
'\n'
);
}
write(chalk.grey(
JSON.stringify(e.byError[i].error.details, null, '\t')
) + '\n\n');
}
});
};