/
index.js
344 lines (310 loc) · 7.61 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
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
/**
* LCOV result merger
*
* @author Michael Weibel <michael.weibel@gmail.com>
* @copyright 2013-2016 Michael Weibel
* @license MIT
*/
var through = require('through2')
var File = require('vinyl')
/**
* Represents a DA record
*
* @param {number} lineNumber
* @param {number} hits
*
* @constructor
*/
function DA (lineNumber, hits) {
this.lineNumber = lineNumber
this.hits = hits
}
/**
* Generates a DA string
*
* @returns {string}
*/
DA.prototype.toString = function () {
return 'DA:' + this.lineNumber + ',' + this.hits + '\n'
}
/**
* Represents a BRDA record
*
* @param {number} lineNumber
* @param {number} blockNumber
* @param {number} branchNumber
* @param {number} hits
*
* @constructor
*/
function BRDA (lineNumber, blockNumber, branchNumber, hits) {
this.lineNumber = lineNumber
this.blockNumber = blockNumber
this.branchNumber = branchNumber
this.hits = hits
}
/**
* Generates a BRDA string
*
* @returns {string}
*/
BRDA.prototype.toString = function () {
var str = 'BRDA:'
str += [this.lineNumber, this.blockNumber, this.branchNumber, this.hits].join(',')
str += '\n'
return str
}
/**
* Represents a coverage file and it's DA/BRDA records
*
* @param {string} filename
*
* @constructor
*/
function CoverageFile (filename) {
this.filename = filename
this.DARecords = []
this.BRDARecords = []
}
/**
* Generates a coverage report for a file.
*
* @returns {string}
*/
CoverageFile.prototype.toString = function () {
var header = 'SF:' + this.filename + '\n'
var footer = 'end_of_record\n'
var body = this.DARecords.map(function (daRecord) {
return daRecord.toString()
}).join('')
body += this.BRDARecords.map(function (brdaRecord) {
return brdaRecord.toString()
}).join('')
return header + body + footer
}
/**
* Find an existing DA record
*
* @param {DA[]} source
* @param {number} lineNumber
*
* @returns {DA|null}
*/
function findDA (source, lineNumber) {
for (var i = 0; i < source.length; i++) {
var da = source[i]
if (da.lineNumber === lineNumber) {
return da
}
}
return null
}
/**
* Find an existing BRDA record
*
* @param {BRDA[]} source
* @param {number} blockNumber
* @param {number} branchNumber
* @param {number} lineNumber
*
* @returns {BRDA|null}
*/
function findBRDA (source, blockNumber, branchNumber, lineNumber) {
for (var i = 0; i < source.length; i++) {
var brda = source[i]
if (brda.blockNumber === blockNumber &&
brda.branchNumber === branchNumber &&
brda.lineNumber === lineNumber) {
return brda
}
}
return null
}
/**
* Find an existing coverage file
*
* @param {CoverageFile[]} source
* @param {string} filename
*
* @returns {CoverageFile|null}
*/
function findCoverageFile (source, filename) {
for (var i = 0; i < source.length; i++) {
var file = source[i]
if (file.filename === filename) {
return file
}
}
return null
}
/**
* Returns appropriate number of hits based on the string value of hits.
*
* @param {string} hits
*
* @returns {number}
*/
function numericHits (hits) {
if (hits === '-') {
return 0
}
return parseInt(hits, 10)
}
/**
* Merges BRDA hits.
*
* @param {string} existingBRDAHits
* @param {string} newBRDAHits
*
* @returns {number|string}
*/
function mergedBRDAHits (existingBRDAHits, newBRDAHits) {
// If we've never executed the branch code path in an existing coverage
// record and we've never executed it here either, then keep it as '-'
// (eg, never executed). If either of them is a number, then
// use the number value.
if (existingBRDAHits !== '-' || newBRDAHits !== '-') {
return numericHits(existingBRDAHits) + numericHits(newBRDAHits)
}
return '-'
}
/**
* Splits the second part of the prefix into an array. The array
* is a list of strings which represent numbers.
*
* @param {string[]} prefixSplit
*
* @returns {string[]}
*/
function splitNumbers (prefixSplit) {
return prefixSplit[1].split(',')
}
/**
* Parses a SF section
*
* @param {CoverageFile[]} lcov
* @param {string[]} prefixSplit
*
* @returns {CoverageFile|null}
*/
function parseSF (lcov, prefixSplit) {
// If the filepath contains a ':', we want to preserve it.
prefixSplit.shift()
var currentFileName = prefixSplit.join(':')
var currentCoverageFile = findCoverageFile(lcov, currentFileName)
if (currentCoverageFile) {
return currentCoverageFile
}
currentCoverageFile = new CoverageFile(currentFileName)
lcov.push(currentCoverageFile)
return currentCoverageFile
}
/**
* Parses a DA section
*
* @param {CoverageFile} currentCoverageFile
* @param {string[]} prefixSplit
*/
function parseDA (currentCoverageFile, prefixSplit) {
var numberSplit = splitNumbers(prefixSplit)
var lineNumber = parseInt(numberSplit[0], 10)
var hits = parseInt(numberSplit[1], 10)
var existingDA = findDA(currentCoverageFile.DARecords, lineNumber)
if (existingDA) {
existingDA.hits += hits
return
}
currentCoverageFile.DARecords.push(new DA(lineNumber, hits))
}
/**
* Parses a BRDA section
*
* @param {CoverageFile} currentCoverageFile
* @param {string[]} prefixSplit
*/
function parseBRDA (currentCoverageFile, prefixSplit) {
var numberSplit = splitNumbers(prefixSplit)
var lineNumber = parseInt(numberSplit[0], 10)
var blockNumber = parseInt(numberSplit[1], 10)
var branchNumber = parseInt(numberSplit[2], 10)
var existingBRDA = findBRDA(currentCoverageFile.BRDARecords,
blockNumber, branchNumber, lineNumber)
// Special case, hits might be a '-'. This means that the code block
// where the branch was contained was never executed at all (as opposed
// to the code being executed, but the branch not being taken). Keep
// it as a string and let mergedBRDAHits work it out.
var hits = numberSplit[3]
if (existingBRDA) {
existingBRDA.hits = mergedBRDAHits(existingBRDA.hits, hits)
return
}
currentCoverageFile.BRDARecords.push(new BRDA(lineNumber, blockNumber, branchNumber, hits))
}
/**
* Process a lcov input file into the representing Objects
*
* @param {string} data
* @param {CoverageFile[]} lcov
*
* @returns {CoverageFile[]}
*/
function processFile (data, lcov) {
var lines = data.split(/\r?\n/)
var currentCoverageFile = null
for (var i = 0, l = lines.length; i < l; i++) {
var line = lines[i]
if (line === 'end_of_record' || line === '') {
currentCoverageFile = null
continue
}
var prefixSplit = line.split(':')
var prefix = prefixSplit[0]
switch (prefix) {
case 'SF':
currentCoverageFile = parseSF(lcov, prefixSplit)
break
case 'DA':
parseDA(currentCoverageFile, prefixSplit)
break
case 'BRDA':
parseBRDA(currentCoverageFile, prefixSplit)
break
default:
// do nothing with not implemented prefixes
}
}
return lcov
}
/**
* Creates LCOV records for given list of files.
*
* @param {CoverageFile[]} coverageFiles
*
* @returns {string}
*/
function createRecords (coverageFiles) {
return coverageFiles.map(function (coverageFile) {
return coverageFile.toString()
}).join('')
}
module.exports = function () {
var coverageFiles = []
return through.obj(function process (file, encoding, callback) {
if (file.isNull()) {
callback()
return
}
if (file.isStream()) {
throw new Error('Streaming not supported')
}
coverageFiles = processFile(file.contents.toString(), coverageFiles)
callback()
}, function flush () {
var file = new File({
path: 'lcov.info',
contents: Buffer.from(createRecords(coverageFiles))
})
this.push(file)
this.emit('end')
})
}