forked from scottcorgan/tap-out
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
309 lines (245 loc) · 7.53 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
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
'use strict';
var PassThrough = require('readable-stream/passthrough');
var split = require('split');
var trim = require('trim');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var reemit = require('re-emitter');
var expr = require('./lib/utils/regexes');
var parseLine = require('./lib/parse-line');
var error = require('./lib/error');
function Parser() {
if (!(this instanceof Parser)) {
return new Parser();
}
EventEmitter.call(this);
this.results = {
tests: [],
asserts: [],
versions: [],
results: [],
comments: [],
plans: [],
pass: [],
fail: [],
errors: [],
};
this.testNumber = 0;
this.previousLine = '';
this.currentNextLineError = null;
this.writingErrorOutput = false;
this.writingErrorStackOutput = false;
this.tmpErrorOutput = '';
}
util.inherits(Parser, EventEmitter);
Parser.prototype.handleLine = function handleLine(line) {
var parsed = parseLine(line);
// This will handle all the error stuff
this._handleError(line);
// This is weird, but it's the only way to distinguish a
// console.log type output from an error output
if (
!this.writingErrorOutput
&& !parsed
&& !isErrorOutputEnd(line)
&& !isRawTapTestStatus(line)
)
{
var comment = {
type: 'comment',
raw: line,
test: this.testNumber
};
this.emit('comment', comment);
this.results.comments.push(comment);
}
// Invalid line
if (!parsed) {
this.previousLine = line;
return;
}
// Handle tests
if (parsed.type === 'test') {
this.testNumber += 1;
parsed.number = this.testNumber;
}
// Handle asserts
if (parsed.type === 'assert') {
parsed.test = this.testNumber;
this.results[parsed.ok ? 'pass' : 'fail'].push(parsed);
if (parsed.ok) {
// No need to have the error object
// in a passing assertion
delete parsed.error;
this.emit('pass', parsed);
}
}
if (!isOkLine(this.previousLine)) {
this.emit(parsed.type, parsed);
this.results[parsed.type + 's'].push(parsed);
}
// This is all so we can determine if the "# ok" output on the last line
// should be skipped
function isOkLine (previousLine) {
return line === '# ok' && previousLine.indexOf('# pass') > -1;
}
this.previousLine = line;
};
Parser.prototype._handleError = function _handleError(line) {
var lastAssert;
// Start of error output
if (isErrorOutputStart(line)) {
this.writingErrorOutput = true;
this.lastAsserRawErrorString = '';
}
// End of error output
else if (isErrorOutputEnd(line)) {
this.writingErrorOutput = false;
this.currentNextLineError = null;
this.writingErrorStackOutput = false;
// Emit error here so it has the full error message with it
var lastAssert = this.results.fail[this.results.fail.length - 1];
if (this.tmpErrorOutput) {
lastAssert.error.stack = this.tmpErrorOutput;
this.lastAsserRawErrorString += this.tmpErrorOutput + '\n';
this.tmpErrorOutput = '';
}
// right-trimmed raw error string
lastAssert.error.raw = this.lastAsserRawErrorString.replace(/\s+$/g, '');
this.emit('fail', lastAssert);
}
// Append to stack
else if (this.writingErrorStackOutput) {
this.tmpErrorOutput += trim(line) + '\n';
}
// Not the beginning of the error message but it's the body
else if (this.writingErrorOutput) {
var m = splitFirst(trim(line), (':'));
lastAssert = this.results.fail[this.results.fail.length - 1];
// Rebuild raw error output
this.lastAsserRawErrorString += line + '\n';
if (m[0] === 'stack') {
this.writingErrorStackOutput = true;
return;
}
var msg = trim((m[1] || '').replace(/['"]+/g, ''));
if (m[0] === 'at') {
// Example string: Object.async.eachSeries (/Users/scott/www/modules/nash/node_modules/async/lib/async.js:145:20)
msg = msg
.split(' ')[1]
.replace('(', '')
.replace(')', '');
var values = msg.split(':');
var file = values.slice(0, values.length-2).join(':');
msg = {
file: file,
line: values[values.length-2],
character: values[values.length-1]
};
}
// This is a plan failure
if (lastAssert.name === 'plan != count') {
lastAssert.type = 'plan';
delete lastAssert.error.at;
lastAssert.error.operator = 'count';
// Need to set this value
if (m[0] === 'actual') {
lastAssert.error.actual = trim(m[1]);
}
}
// outputting expected/actual object or array
if (this.currentNextLineError) {
lastAssert.error[this.currentNextLineError] = trim(line);
this.currentNextLineError = null;
}
else if (trim(m[1]) === '|-') {
this.currentNextLineError = m[0];
}
else {
lastAssert.error[m[0]] = msg;
}
}
// Emit fail when error on previous line had no diagnostics
else if (this.previousLine && isFailAssertionLine(this.previousLine)) {
lastAssert = this.results.fail[this.results.fail.length - 1];
this.emit('fail', lastAssert);
}
};
Parser.prototype._handleEnd = function _handleEnd() {
var plan = this.results.plans.length ? this.results.plans[0] : null;
var count = this.results.asserts.length;
var first = count && this.results.asserts.reduce(firstAssertion);
var last = count && this.results.asserts.reduce(lastAssertion);
// Emit fail when error on previous line had no diagnostics
if (this.previousLine && isFailAssertionLine(this.previousLine)) {
var lastAssert = this.results.fail[this.results.fail.length - 1];
this.emit('fail', lastAssert);
}
if (!plan) {
if (count > 0) {
this.results.errors.push(error('no plan provided'));
}
return;
}
if (this.results.fail.length > 0) {
return;
}
if (count !== (plan.to - plan.from + 1)) {
this.results.errors.push(error('incorrect number of assertions made'));
} else if (first && first.number !== plan.from) {
this.results.errors.push(error('first assertion number does not equal the plan start'));
} else if (last && last.number !== plan.to) {
this.results.errors.push(error('last assertion number does not equal the plan end'));
}
};
module.exports = function (done) {
done = done || function () {};
var stream = new PassThrough();
var parser = Parser();
reemit(parser, stream, [
'test', 'assert', 'version', 'result', 'pass', 'fail', 'comment', 'plan'
]);
stream
.pipe(split())
.on('data', function (data) {
if (!data) {
return;
}
var line = data.toString();
parser.handleLine(line);
})
.on('close', function () {
parser._handleEnd();
stream.emit('output', parser.results);
done(null, parser.results);
})
.on('error', done);
return stream;
};
module.exports.Parser = Parser;
function isFailAssertionLine (line) {
return line.indexOf('not ok') === 0;
}
function isErrorOutputStart (line) {
return line.indexOf(' ---') === 0;
}
function isErrorOutputEnd (line) {
return line.indexOf(' ...') === 0;
}
function splitFirst(str, pattern) {
var parts = str.split(pattern);
if (parts.length <= 1) {
return parts;
}
return [parts[0], parts.slice(1).join(pattern)];
}
function isRawTapTestStatus (str) {
var rawTapTestStatusRegex = new RegExp('(\\d+)(\\.)(\\.)(\\d+)');;
return rawTapTestStatusRegex.exec(str);
}
function firstAssertion(first, assert) {
return assert.number < first.number ? assert : first;
}
function lastAssertion(last, assert) {
return assert.number > last.number ? assert : last;
}