Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Merge unnamed functions by line in the coverage gate (#992)

Nguyen Xuan An0b8af3f072f8
diff --git a/coverage-baseline.json b/coverage-baseline.json
index caee4d8f..1240896c 100644
--- a/coverage-baseline.json
+++ b/coverage-baseline.json
@@ -1 +1 @@
-{"lines":93.79,"branches":78.45,"functions":93.4}
+{"lines":93.79,"branches":78.45,"functions":92.28}
diff --git a/scripts/coverage-gate.mjs b/scripts/coverage-gate.mjs
index 3445c63b..01228983 100644
--- a/scripts/coverage-gate.mjs
+++ b/scripts/coverage-gate.mjs
@@ -1,6 +1,7 @@
// Merges the lcov files of the test shards, prints and summarises line, branch and function coverage, and fails when
// any figure falls more than the allowed margin below the committed baseline (coverage-baseline.json).
-// Raising the baseline is a manual edit of that file.
+// Raising the baseline is a manual edit of that file. The function figure counts each function once per source line,
+// so the baseline was re-measured (93.4 to 92.28 on the same commit) when unnamed functions stopped being counted per shard.
// Usage: node scripts/coverage-gate.mjs <lcov file or folder>... [--baseline coverage-baseline.json] [--merged-out merged.lcov]
import { appendFileSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
@@ -35,16 +36,34 @@ function addHits(map, key, hits) {
map.set(key, (map.get(key) ?? 0) + hits);
}
+// V8 numbers the unnamed functions of a file per process (`anonymous_7`), so the same name points at different
+// functions in different shards. A function is therefore identified by its line, plus its name when it has one.
+const ANONYMOUS_FUNCTION_NAME = /^anonymous_\d+$/;
+
+function functionKey(line, name) {
+ return ANONYMOUS_FUNCTION_NAME.test(name) ? `${line},anonymous` : `${line},${name}`;
+}
+
+function functionEntry(record, key, line, name) {
+ if (!record.functions.has(key)) {
+ const mergedName = ANONYMOUS_FUNCTION_NAME.test(name) ? `anonymous_at_line_${line}` : name;
+ record.functions.set(key, { line, name: mergedName, hits: 0 });
+ }
+ return record.functions.get(key);
+}
+
export function mergeLcov(texts) {
const files = new Map();
for (const text of texts) {
let record = null;
+ let keyByName = new Map();
for (const row of text.split(/\r?\n/)) {
const separator = row.indexOf(':');
const tag = separator < 0 ? row : row.slice(0, separator);
const value = separator < 0 ? '' : row.slice(separator + 1);
if (tag === 'SF') {
record = fileRecord(files, value);
+ keyByName = new Map();
} else if (!record) {
continue;
} else if (tag === 'DA') {
@@ -55,12 +74,14 @@ export function mergeLcov(texts) {
addHits(record.branches, `${line},${block},${branch}`, hits === '-' ? 0 : Number(hits));
} else if (tag === 'FN') {
const [line, name] = value.split(',');
- if (!record.functions.has(name)) record.functions.set(name, { line, hits: 0 });
+ const key = functionKey(line, name);
+ keyByName.set(name, key);
+ functionEntry(record, key, line, name);
} else if (tag === 'FNDA') {
const [hits, name] = value.split(',');
- const entry = record.functions.get(name) ?? { line: '0', hits: 0 };
+ const key = keyByName.get(name) ?? functionKey('0', name);
+ const entry = functionEntry(record, key, key.split(',')[0], name);
entry.hits += Number(hits);
- record.functions.set(name, entry);
}
}
}
@@ -104,8 +125,8 @@ function toLcov(files) {
const rows = [];
for (const [path, record] of files) {
rows.push('TN:', `SF:${path}`);
- for (const [name, entry] of record.functions) rows.push(`FN:${entry.line},${name}`);
- for (const [name, entry] of record.functions) rows.push(`FNDA:${entry.hits},${name}`);
+ for (const entry of record.functions.values()) rows.push(`FN:${entry.line},${entry.name}`);
+ for (const entry of record.functions.values()) rows.push(`FNDA:${entry.hits},${entry.name}`);
for (const [key, hits] of record.branches) rows.push(`BRDA:${key},${hits}`);
for (const [line, hits] of record.lines) rows.push(`DA:${line},${hits}`);
rows.push('end_of_record');
diff --git a/tests/coverage-gate.test.js b/tests/coverage-gate.test.js
index 9e9231d5..acfe8d66 100644
--- a/tests/coverage-gate.test.js
+++ b/tests/coverage-gate.test.js
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
-import { compareWithBaseline } from '../scripts/coverage-gate.mjs';
+import { compareWithBaseline, mergeLcov, totalsOf } from '../scripts/coverage-gate.mjs';
const baseline = { lines: 90, branches: 80, functions: 90 };
@@ -15,3 +15,17 @@ test('branch coverage may wander by run-to-run noise without failing the gate',
test('a real drop fails each metric past its own allowance', () => {
assert.deepEqual(verdicts({ lines: 89.4, branches: 78.4, functions: 89.4 }), { lines: false, branches: false, functions: false });
});
+
+test('unnamed functions merge by line, since each shard numbers them on its own', () => {
+ const firstShard = ['SF:src/example.js', 'FN:10,anonymous_1', 'FN:20,anonymous_2', 'FN:30,named', 'FNDA:3,anonymous_1', 'FNDA:0,anonymous_2', 'FNDA:1,named', 'end_of_record'].join('\n');
+ const secondShard = ['SF:src/example.js', 'FN:20,anonymous_1', 'FN:30,named', 'FNDA:0,anonymous_1', 'FNDA:0,named', 'end_of_record'].join('\n');
+ const { totals } = totalsOf(mergeLcov([firstShard, secondShard]));
+ assert.deepEqual(totals.functions, [2, 3]);
+});
+
+test('a function hit in only one shard counts as covered', () => {
+ const firstShard = ['SF:src/example.js', 'FN:10,anonymous_4', 'FNDA:0,anonymous_4', 'end_of_record'].join('\n');
+ const secondShard = ['SF:src/example.js', 'FN:10,anonymous_9', 'FNDA:2,anonymous_9', 'end_of_record'].join('\n');
+ const { totals } = totalsOf(mergeLcov([firstShard, secondShard]));
+ assert.deepEqual(totals.functions, [1, 1]);
+});