-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
150 lines (123 loc) · 3.59 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
/**
* LCOV result merger
*
* @author Michael Weibel <[email protected]>
* @copyright 2013-2023 Michael Weibel
* @license MIT
*/
const { readFile, writeFile } = require('node:fs/promises');
const path = require('node:path');
const { Transform } = require('node:stream');
const Configuration = require('./lib/configuration');
const FullReport = require('./lib/full-report');
/**
* Process a lcov input file into the representing Objects
*
* @param {string} sourceDir - The absolute path to the lcov file directory.
* @param {Buffer} data
* @param {FullReport} lcov
* @param {Configuration} config
*
* @returns {FullReport}
*/
function processFile(sourceDir, data, lcov, config) {
/** @type {import("./lib/entries/coverage-file")|null} */
let currentCoverageFile = null;
const lines = data.toString('utf-8').split(/\r?\n/);
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (line === 'end_of_record' || line === '') {
currentCoverageFile = null;
continue;
}
const [prefix, ...suffixParts] = line.split(':');
const suffix = suffixParts.join(':');
switch (prefix) {
case 'SF': {
let sourceFilePath = suffix;
if (config.prependSourceFiles) {
const fullFilePathName = path.normalize(
path.join(sourceDir, config.prependPathFix, sourceFilePath)
);
const rootRelPathName = path.relative(
process.cwd(),
fullFilePathName
);
sourceFilePath = './' + rootRelPathName;
}
currentCoverageFile = lcov.addCoverageFile(sourceFilePath);
break;
}
case 'DA':
currentCoverageFile.parseDA(suffix);
break;
case 'BRDA':
currentCoverageFile.parseBRDA(suffix);
break;
default:
// do nothing with not implemented prefixes
}
}
return lcov;
}
/**
*
*
* @param {string[]} filePaths
* @param {import("./lib/configuration").ConfigurationPojo} options
*
* @return {Promise<string>}
*/
async function mergeCoverageReportFiles(filePaths, options) {
const config = new Configuration(options);
const report = new FullReport();
for (const filePath of filePaths) {
const fileContent = await readFile(filePath, 'utf-8');
processFile(path.dirname(filePath), fileContent, report, config);
}
const tmpFile = await config.getTempFilePath();
await writeFile(tmpFile, Buffer.from(report.toString()), {
encoding: 'utf-8',
flag: 'w+',
});
return tmpFile;
}
/**
*
*/
class WrappingTransform extends Transform {
constructor(filePathsOrMergeOptions, mergeOptions) {
super();
this.filePaths = Array.isArray(filePathsOrMergeOptions)
? filePathsOrMergeOptions
: [];
this.mergeOptions = mergeOptions
? mergeOptions
: Array.isArray(filePathsOrMergeOptions)
? {}
: filePathsOrMergeOptions || {};
}
_transform(chunk, encoding, callback) {
this.filePaths.push(chunk.toString());
callback(null);
}
_flush(callback) {
mergeCoverageReportFiles(this.filePaths, this.mergeOptions).then(
(tempFile) => callback(null, tempFile),
(error) => callback(error)
);
}
}
/**
* @param {string[] | import("./lib/configuration").ConfigurationPojo} [filePathsOrOptions]
* @param {import("./lib/configuration").ConfigurationPojo} [options]
*
* @return {module:stream.internal.Transform}
*/
function mergeCoverageReportFilesStream(filePathsOrOptions, options) {
return new WrappingTransform(filePathsOrOptions, options);
}
module.exports = {
mergeCoverageReportFiles,
mergeCoverageReportFilesStream,
};