-
Notifications
You must be signed in to change notification settings - Fork 1
/
reporter-filter
executable file
·153 lines (134 loc) · 4.31 KB
/
reporter-filter
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
#!/usr/bin/env node
/**
* @fileOverview Filter used to format TAP report coming from browsers.
* @name Reporter Filter
*/
var split = require("split");
var through2 = require("through2");
var once = require("once");
var faucet = require("faucet");
var PassThrough = require("stream").PassThrough;
/**
* Empty function. Useful to initialize functions.
*/
var noop = function noop() {};
/**
* Regex functional helper. Receives a regular expression and returns a new function
* that tests a string.
* @param {RegExp} regex
* @returns {Function}
*/
var matchRegex = function matchRegex(regex) {
return function(str) {
return regex.test(str);
};
};
/** Validates if the function is the first line of the TAP report. */
var isTAPFirstLine = matchRegex(/(.*) LOG: 'TAP (.*)'/);
/** Validates if the function is part of the TAP report. */
var isLogLine = matchRegex(/(.*) LOG: '(.*)'/);
/**
* Object used to capture a block of content. It receives data until that data doesn't
* meet a defined requirement and then runs a callback with all the caught info.
*/
var sequentialStringCatcher = {
queue: [],
done: true,
callbacks: {
readingStart: noop,
reading: noop,
end: noop,
addSuccess: noop,
addFailure: noop
},
/**
* Add a new event handler.
* @param {String} event Event target for handler. It can use readingStart, reading, end, addSuccess or addFailure
* @param {Function} handler Function to bind to the event.
* @returns {sequentialStringCatcher} this instance.
*/
on: function(event, handler) {
if (typeof handler !== "function" || !this.callbacks[event]) return this;
this.callbacks[event] = handler;
return this;
},
/**
* Add a new line to the block if the block has started and the line meets the
* requirements specified on the 'reading' function.
* @param {String} str Line to verify.
* @returns {Boolean} True if the line was added. False otherwise.
*/
add: function(str) {
if (this.done && !this.callbacks.readingStart(str)) {
this.callbacks.addFailure("\n" + str);
return false;
}
this.done = false;
if (this.callbacks.reading(str)) {
this.queue.push(str);
this.callbacks.addSuccess();
return true;
}
this.done = true;
this.callbacks.end(this.queue.join("\n"));
this.queue.length = 0;
return false;
}
};
/**
* Set different colors and removes unnecessary info for each line in the TAP report.
* @param {String} report Raw string.
* @param {Function} onFormatEnd Function to be called when the formatting ends.
* @returns {String} Properly formatted TAP report.
*/
var formatTAPReport = function(report, onFormatEnd) {
var stream = new PassThrough();
var formattedReport = "\n\n";
stream.write(report.replace(/(.*) LOG: '(.*)'/g, '$2'));
stream.end();
stream.pipe(faucet())
.on("data", function(chunk) {
formattedReport += chunk;
})
.on("end", function() {
onFormatEnd(formattedReport);
});
};
/**
* Configures a sequentialStringCatcher the first time to capture a TAP block.
* @param {Object} callbacks Layer object used to set the handlers for the sequentialStringCatcher
* @param {Function} callbacks.onAddSuccess addSuccess callback.
* @param {Function} callbacks.onAddFailure addFailure callback.
* @param {Function} callbacks.onEnd end callback.
* @returns {sequentialStringCatcher} Configured instance
*/
var configSequentialCatcher = once(function(callbacks) {
return Object.create(sequentialStringCatcher)
.on("addSuccess", callbacks.onAddSuccess)
.on("addFailure", callbacks.onAddFailure)
.on("readingStart", isTAPFirstLine)
.on("reading", isLogLine)
.on("end", function(block) {
formatTAPReport(block, callbacks.onEnd);
});
});
/**
* Read the lines of the stream and formats the TAP block.
* @returns {Destroyabvarransform} Though2 return object to be used in the stream line.
*/
var formatLogs = function() {
var sequentialCatcher;
return through2(function(chunk, enc, callback) {
var str = chunk.toString();
sequentialCatcher = configSequentialCatcher({
onAddSuccess: callback.bind(this),
onAddFailure: callback.bind(this, null),
onEnd: callback.bind(this, null)
});
sequentialCatcher.add(str);
});
};
process.stdin
.pipe(split())
.pipe(formatLogs())
.pipe(process.stdout);