|
| 1 | +import { readFileSync, writeFileSync } from "node:fs" |
| 2 | +import process from "node:process" |
| 3 | + |
| 4 | +const parseCount = (value, description) => { |
| 5 | + const count = Number(value) |
| 6 | + if (!Number.isSafeInteger(count) || count < 0) throw new Error(`Invalid ${description}: ${value}`) |
| 7 | + return count |
| 8 | +} |
| 9 | + |
| 10 | +const mergeCount = (records, key, count) => records.set(key, Math.max(records.get(key) ?? 0, count)) |
| 11 | + |
| 12 | +const parseLcov = (lcov, label) => { |
| 13 | + const sources = new Map() |
| 14 | + let record |
| 15 | + |
| 16 | + for (const line of lcov.split(/\r?\n/)) { |
| 17 | + if (!line || line.startsWith("TN:")) continue |
| 18 | + if (line.startsWith("SF:")) { |
| 19 | + if (record) throw new Error(`${label} contains an unfinished source record: ${record.source}`) |
| 20 | + const source = line.slice(3) |
| 21 | + if (!source) throw new Error(`${label} contains an empty source path`) |
| 22 | + record = { |
| 23 | + source, |
| 24 | + functions: new Map(), |
| 25 | + functionCounts: new Map(), |
| 26 | + branches: new Map(), |
| 27 | + lines: new Map(), |
| 28 | + } |
| 29 | + } else if (line === "end_of_record") { |
| 30 | + if (!record) throw new Error(`${label} contains a record terminator outside a source record`) |
| 31 | + if (sources.has(record.source)) |
| 32 | + throw new Error(`${label} contains duplicate source record: ${record.source}`) |
| 33 | + sources.set(record.source, record) |
| 34 | + record = undefined |
| 35 | + } else if (record && line.startsWith("FN:")) { |
| 36 | + const separator = line.indexOf(",") |
| 37 | + if (separator < 4) throw new Error(`${label} contains invalid FN for ${record.source}`) |
| 38 | + const name = line.slice(separator + 1) |
| 39 | + const location = line.slice(3, separator) |
| 40 | + const existing = record.functions.get(name) |
| 41 | + if (existing && existing !== location) |
| 42 | + throw new Error(`${label} contains conflicting FN for ${record.source}:${name}`) |
| 43 | + record.functions.set(name, location) |
| 44 | + } else if (record && line.startsWith("FNDA:")) { |
| 45 | + const [count, ...name] = line.slice(5).split(",") |
| 46 | + if (name.length === 0) throw new Error(`${label} contains invalid FNDA for ${record.source}`) |
| 47 | + mergeCount(record.functionCounts, name.join(","), parseCount(count, `FNDA for ${record.source}`)) |
| 48 | + } else if (record && line.startsWith("BRDA:")) { |
| 49 | + const [lineNumber, block, branch, taken] = line.slice(5).split(",") |
| 50 | + const key = `${lineNumber},${block},${branch}` |
| 51 | + const count = taken === "-" ? 0 : parseCount(taken, `BRDA for ${record.source}`) |
| 52 | + mergeCount(record.branches, key, count) |
| 53 | + } else if (record && line.startsWith("DA:")) { |
| 54 | + const [lineNumber, count, checksum] = line.slice(3).split(",") |
| 55 | + const key = parseCount(lineNumber, `DA line for ${record.source}`) |
| 56 | + if (key < 1) throw new Error(`${label} contains invalid DA line for ${record.source}`) |
| 57 | + const existing = record.lines.get(key) |
| 58 | + if (existing?.checksum && checksum && existing.checksum !== checksum) |
| 59 | + throw new Error(`${label} contains conflicting DA checksum for ${record.source}:${key}`) |
| 60 | + record.lines.set(key, { |
| 61 | + count: Math.max(existing?.count ?? 0, parseCount(count, `DA count for ${record.source}`)), |
| 62 | + checksum: existing?.checksum ?? checksum, |
| 63 | + }) |
| 64 | + } else if (record && !/^(?:FNF|FNH|BRF|BRH|LF|LH):/.test(line)) { |
| 65 | + throw new Error(`${label} contains unsupported LCOV data for ${record.source}: ${line}`) |
| 66 | + } else if (!record) { |
| 67 | + throw new Error(`${label} contains data outside a source record: ${line}`) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (record) throw new Error(`${label} contains an unfinished source record: ${record.source}`) |
| 72 | + return sources |
| 73 | +} |
| 74 | + |
| 75 | +export const mergeLcov = (reports) => { |
| 76 | + const merged = new Map() |
| 77 | + for (const [label, lcov] of reports) { |
| 78 | + for (const [source, incoming] of parseLcov(lcov, label)) { |
| 79 | + const record = merged.get(source) ?? { |
| 80 | + source, |
| 81 | + functions: new Map(), |
| 82 | + functionCounts: new Map(), |
| 83 | + branches: new Map(), |
| 84 | + lines: new Map(), |
| 85 | + } |
| 86 | + for (const [name, location] of incoming.functions) { |
| 87 | + const existing = record.functions.get(name) |
| 88 | + if (existing && existing !== location) throw new Error(`Conflicting FN for ${source}:${name}`) |
| 89 | + record.functions.set(name, location) |
| 90 | + } |
| 91 | + for (const [name, count] of incoming.functionCounts) mergeCount(record.functionCounts, name, count) |
| 92 | + for (const [key, count] of incoming.branches) mergeCount(record.branches, key, count) |
| 93 | + for (const [line, value] of incoming.lines) { |
| 94 | + const existing = record.lines.get(line) |
| 95 | + if (existing?.checksum && value.checksum && existing.checksum !== value.checksum) |
| 96 | + throw new Error(`Conflicting DA checksum for ${source}:${line}`) |
| 97 | + record.lines.set(line, { |
| 98 | + count: Math.max(existing?.count ?? 0, value.count), |
| 99 | + checksum: existing?.checksum ?? value.checksum, |
| 100 | + }) |
| 101 | + } |
| 102 | + merged.set(source, record) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return [...merged.values()] |
| 107 | + .sort((a, b) => a.source.localeCompare(b.source)) |
| 108 | + .flatMap((record) => { |
| 109 | + const functions = [...record.functions].sort(([a], [b]) => a.localeCompare(b)) |
| 110 | + const functionCounts = [...record.functionCounts].sort(([a], [b]) => a.localeCompare(b)) |
| 111 | + const branches = [...record.branches].sort(([a], [b]) => a.localeCompare(b, undefined, { numeric: true })) |
| 112 | + const lines = [...record.lines].sort(([a], [b]) => a - b) |
| 113 | + return [ |
| 114 | + `SF:${record.source}`, |
| 115 | + ...functions.map(([name, location]) => `FN:${location},${name}`), |
| 116 | + ...functionCounts.map(([name, count]) => `FNDA:${count},${name}`), |
| 117 | + `FNF:${functions.length}`, |
| 118 | + `FNH:${functionCounts.filter(([, count]) => count > 0).length}`, |
| 119 | + ...branches.map(([key, count]) => `BRDA:${key},${count || "-"}`), |
| 120 | + `BRF:${branches.length}`, |
| 121 | + `BRH:${branches.filter(([, count]) => count > 0).length}`, |
| 122 | + ...lines.map(([line, { count, checksum }]) => `DA:${line},${count}${checksum ? `,${checksum}` : ""}`), |
| 123 | + `LF:${lines.length}`, |
| 124 | + `LH:${lines.filter(([, { count }]) => count > 0).length}`, |
| 125 | + "end_of_record", |
| 126 | + ] |
| 127 | + }) |
| 128 | + .join("\n") |
| 129 | +} |
| 130 | + |
| 131 | +if (process.argv[1] === import.meta.filename) { |
| 132 | + const [output, ...inputs] = process.argv.slice(2) |
| 133 | + if (!output || inputs.length < 1) throw new Error("Usage: merge-lcov.mjs <output> <input...>") |
| 134 | + writeFileSync(output, `${mergeLcov(inputs.map((input) => [input, readFileSync(input, "utf8")]))}\n`) |
| 135 | +} |
0 commit comments