This repository has been archived by the owner on Oct 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
zuul.js
394 lines (317 loc) · 10.1 KB
/
zuul.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
'use strict';
// TODO(shtylman)
// we can do something good with this
// cause we have the mappings file
// we can actually show where in the source this is!!
// before we boot anything we should install this to get reasonable debugging
window.onerror = function(msg, file, line) {
//var item = document.createTextNode(msg + ':' + file + ':' + line);
//document.body.appendChild(item);
}
global.JSON = global.JSON || require('JSON2');
var load = require('load-script');
var stacktrace = require('stacktrace-js');
var ajax = require('superagent');
var render_stacktrace = require('./render-stacktrace');
try {
var stack_mapper = require('stack-mapper');
} catch (err) {}
// post messages here to send back to clients
var zuul_msg_bus = window.zuul_msg_bus = [];
// shim console.log so we can report back to user
if (typeof console === 'undefined') {
console = {};
}
var originalLog = console.log;
console.log = function (msg) {
var args = [].slice.call(arguments);
zuul_msg_bus.push({
type: 'console',
args: args
});
if (typeof originalLog === 'function') {
return originalLog.apply(this, arguments);
}
// old ghetto ass IE doesn't report typeof correctly
// so we just have to call log
else if (originalLog) {
return originalLog(arguments[0]);
}
};
var ZuulReporter = function(run_fn) {
if (!(this instanceof ZuulReporter)) {
return new ZuulReporter(run_fn);
}
var self = this;
self.run_fn = run_fn;
self.stats = {
passed: 0,
pending: 0,
failed: 0
};
var main_div = document.getElementById('zuul');
var header = self.header = document.createElement('div');
header.className = 'heading pending';
/*global zuul */
header.innerHTML = zuul.title;
main_div.appendChild(header);
self.status = header.appendChild(document.createElement('div'));
self.status.className = 'status';
self._set_status(self.stats);
var sub = document.createElement('div');
sub.className = 'sub-heading';
sub.innerHTML = navigator.userAgent;
main_div.appendChild(sub);
// Add tab selector
var tab_selector = document.createElement('div');
tab_selector.id = 'tab-selector';
var results_selector = document.createElement('a');
results_selector.className = 'selected';
results_selector.href = '/__zuul';
results_selector.innerHTML = 'Test results';
results_selector.onclick = function(e) {
var selectors = document.querySelectorAll('#tab-selector a');
for (var i = 0; i < selectors.length; i++) {
selectors[i].className = ''
}
e.target.className = 'selected';
document.getElementById('test-results-tab').className = 'tab';
document.getElementById('code-coverage-tab').className = 'tab hidden';
e.preventDefault();
};
tab_selector.appendChild(results_selector);
var coverage_selector = document.createElement('a');
coverage_selector.href = '/__zuul/coverage';
coverage_selector.innerHTML = 'Code coverage';
coverage_selector.onclick = function(e) {
var selectors = document.querySelectorAll('#tab-selector a');
for (var i = 0; i < selectors.length; i++) {
selectors[i].className = ''
}
e.target.className = 'selected';
document.getElementById('test-results-tab').className = 'tab hidden';
document.getElementById('code-coverage-tab').className = 'tab';
e.preventDefault();
};
tab_selector.appendChild(coverage_selector);
main_div.appendChild(tab_selector);
// Add tabs and their content containers
var tabs = document.createElement('div');
tabs.className = 'tabs';
var test_results_tab = document.createElement('div');
test_results_tab.className = 'tab';
test_results_tab.id = 'test-results-tab';
tabs.appendChild(test_results_tab);
var code_coverage_tab = document.createElement('div');
code_coverage_tab.className = 'tab hidden';
code_coverage_tab.id = 'code-coverage-tab';
tabs.appendChild(code_coverage_tab);
main_div.appendChild(tabs);
// status info
var status = document.createElement('div');
document.body.appendChild(main_div);
self._current_container = test_results_tab;
self._mapper = undefined;
// load test bundle and trigger tests to start
// this is a problem for auto starting tests like tape
// we need map file first
// load map file first then test bundle
load('/__zuul/test-bundle.js', load_map);
function load_map(err) {
if (err) {
self.done(err);
}
if (!stack_mapper) {
return self.start();
}
var map_path = '/__zuul/test-bundle.map.json';
ajax.get(map_path).end(function(err, res) {
if (err) {
// ignore map load error
return self.start();
}
self._source_map = res.body;
try {
self._mapper = stack_mapper(res.body);
} catch (err) {}
self.start();
});
}
};
ZuulReporter.prototype._set_status = function(info) {
var self = this;
var html = '';
html += '<span>' + info.failed + ' <small>failing</small></span> ';
html += '<span>' + info.passed + ' <small>passing</small></span> ';
if(self.stats.pending){
html += '<span>' + info.pending + ' <small>pending</small></span>';
}
self.status.innerHTML = html;
};
// tests are starting
ZuulReporter.prototype.start = function() {
var self = this;
self.run_fn();
};
// all tests done
ZuulReporter.prototype.done = function(err) {
var self = this;
var stats = self.stats;
var passed = stats.failed === 0 && stats.passed > 0;
if (passed) {
self.header.className += ' passed';
}
else {
self.header.className += ' failed';
}
// add coverage tab content
if (window.__coverage__) {
var coverage_tab = document.getElementById('code-coverage-tab');
coverage_tab.innerHTML = '<iframe frameborder="0" src="/__zuul/coverage"></iframe>';
}
post_message({
type: 'done',
stats: stats,
passed: passed
});
};
// new test starting
ZuulReporter.prototype.test = function(test) {
var self = this;
var container = document.createElement('div');
container.className = 'test pending';
var header = container.appendChild(document.createElement('h1'));
header.innerHTML = test.name;
self._current_container = self._current_container.appendChild(container);
post_message({
type: 'test',
name: test.name
});
};
// reports on skipped tests
ZuulReporter.prototype.skippedTest = function(test){
var self = this;
self.stats.pending++;
var container = document.createElement('div');
container.className = 'test pending skipped';
var header = container.appendChild(document.createElement('h1'));
header.innerHTML = test.name;
self._current_container.appendChild(container);
self._set_status(self.stats);
post_message({
type: 'test',
name: test.name
});
};
// test ended
ZuulReporter.prototype.test_end = function(test) {
var self = this;
var name = test.name;
var cls = test.passed ? 'passed' : 'failed';
if (test.passed) {
self.stats.passed++;
}
else {
self.stats.failed++;
}
// current test element
self._current_container.className += ' ' + cls;
// use parentNode for legacy browsers (firefox)
self._current_container = self._current_container.parentNode;
self._set_status(self.stats);
var cov = window.__coverage__ ;
if (cov) {
ajax.post('/__zuul/coverage/client')
.send(cov)
.end(function(err, res) {
if (err) {
console.log('error in coverage reports');
console.log(err);
}
});
}
post_message({
type: 'test_end',
name: test.name,
passed: test.passed
});
};
// new suite starting
ZuulReporter.prototype.suite = function(suite) {
var self = this;
};
// suite ended
ZuulReporter.prototype.suite_end = function(suite) {
var self = this;
};
// assertion within test
ZuulReporter.prototype.assertion = function(details) {
var self = this;
// result (true | false)
// actual
// expected
// message
// error
// source (stack) if available
var passed = details.result;
if (passed) {
return;
}
if (details.message) {
var pre = document.createElement('pre');
pre.innerHTML = details.message;
self._current_container.appendChild(pre);
}
// TODO actual, expected
var message = details.message;
var error = details.error;
var stack = details.source;
if (!stack && error) {
// rethrow to try and get the stack
// IE needs this (of course)
try {
throw error;
} catch (ex) {
error = ex;
stack = error.stack;
}
}
var frames = [];
try {
frames = stacktrace(error);
} catch (err) {}
self._renderError(stack, frames, message, error);
post_message({
type: 'assertion',
actual: details.actual,
expected: details.expected,
message: details.message,
source: details.source,
frames: frames
});
};
ZuulReporter.prototype._renderError = function (stack, frames, message, error) {
var self = this;
var mapper = self._mapper;
var str;
if (mapper && frames.length) {
var mapped = mapper.map(frames);
str = render_stacktrace(mapped, self._source_map);
}
var div = document.createElement('div');
div.innerHTML = str ? str : (stack || message || error.toString());
self._current_container.appendChild(div);
};
function plainString (mapped) {
var str = '';
for (var i = 0; i <mapped.length; ++i) {
var frame = mapped[i];
str += '\n\tat ';
str += frame.func + ' (' + frame.filename + ':' + frame.line + ':';
str += (frame.column || 0) + ')';
}
}
function post_message(msg) {
zuul_msg_bus.push(msg);
}
module.exports = ZuulReporter;