|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Which site/ functions the test suites never call. Runs both suites under |
| 4 | + * NODE_V8_COVERAGE (built into Node, no dependency), then prints, per file, |
| 5 | + * the share of functions that ran and the uncovered ones with their lines. |
| 6 | + * node site/coverage.mjs [--all] |
| 7 | + * By default only files with an uncovered function are listed; --all lists |
| 8 | + * every file. Informational: it never fails (exit 1 only if a suite fails). |
| 9 | + */ |
| 10 | +import { execFileSync } from "node:child_process"; |
| 11 | +import fs from "node:fs"; |
| 12 | +import os from "node:os"; |
| 13 | +import path from "node:path"; |
| 14 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 15 | +import { cliArgs } from "./lib/cli.mjs"; |
| 16 | + |
| 17 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 18 | +const ROOT = path.resolve(__dirname, ".."); |
| 19 | +const SUITES = ["site/checks.test.mjs", "site/lib/lib.test.mjs"]; |
| 20 | + |
| 21 | +/** |
| 22 | + * V8 script coverage => `{ total, uncovered: [{ name, line }] }` per url. |
| 23 | + * A function counts as covered when its own range ran at least once in any |
| 24 | + * of the runs (a module loaded by both suites appears once per run); the |
| 25 | + * module's top-level pseudo-function (empty name at offset 0) is skipped. |
| 26 | + */ |
| 27 | +export function functionCoverage(scripts, sourceOf) { |
| 28 | + const byUrl = new Map(); // url => Map(startOffset => { name, ran }) |
| 29 | + for (const script of scripts) { |
| 30 | + if (!byUrl.has(script.url)) byUrl.set(script.url, new Map()); |
| 31 | + const fns = byUrl.get(script.url); |
| 32 | + for (const fn of script.functions) { |
| 33 | + const range = fn.ranges[0]; |
| 34 | + if (!range || (range.startOffset === 0 && !fn.functionName)) continue; |
| 35 | + const seen = fns.get(range.startOffset) || { name: fn.functionName || "(anonymous)", ran: false }; |
| 36 | + seen.ran ||= range.count > 0; |
| 37 | + fns.set(range.startOffset, seen); |
| 38 | + } |
| 39 | + } |
| 40 | + const out = new Map(); |
| 41 | + for (const [url, fns] of byUrl) { |
| 42 | + const src = sourceOf(url); |
| 43 | + if (src === null) continue; |
| 44 | + const lineAt = (offset) => src.slice(0, offset).split("\n").length; |
| 45 | + const uncovered = [...fns] |
| 46 | + .filter(([, f]) => !f.ran) |
| 47 | + .sort(([a], [b]) => a - b) |
| 48 | + .map(([offset, f]) => ({ name: f.name, line: lineAt(offset) })); |
| 49 | + out.set(url, { total: fns.size, uncovered }); |
| 50 | + } |
| 51 | + return out; |
| 52 | +} |
| 53 | + |
| 54 | +function main() { |
| 55 | + const { values } = cliArgs({ all: { type: "boolean" } }, "node site/coverage.mjs [--all]"); |
| 56 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fgda-coverage-")); |
| 57 | + try { |
| 58 | + for (const suite of SUITES) { |
| 59 | + execFileSync(process.execPath, [suite], { cwd: ROOT, stdio: ["ignore", "ignore", "inherit"], env: { ...process.env, NODE_V8_COVERAGE: dir } }); |
| 60 | + } |
| 61 | + const siteUrl = pathToFileURL(__dirname).href + "/"; |
| 62 | + const scripts = []; |
| 63 | + for (const name of fs.readdirSync(dir)) { |
| 64 | + for (const s of JSON.parse(fs.readFileSync(path.join(dir, name), "utf8")).result) { |
| 65 | + if (s.url.startsWith(siteUrl) && !/\.test\.mjs$/.test(s.url) && !s.url.includes("/dist/")) scripts.push(s); |
| 66 | + } |
| 67 | + } |
| 68 | + const sourceOf = (url) => { |
| 69 | + try { |
| 70 | + return fs.readFileSync(fileURLToPath(url), "utf8"); |
| 71 | + } catch { |
| 72 | + return null; |
| 73 | + } |
| 74 | + }; |
| 75 | + let total = 0; |
| 76 | + let missed = 0; |
| 77 | + for (const [url, cov] of [...functionCoverage(scripts, sourceOf)].sort(([a], [b]) => a.localeCompare(b))) { |
| 78 | + total += cov.total; |
| 79 | + missed += cov.uncovered.length; |
| 80 | + if (!cov.uncovered.length && !values.all) continue; |
| 81 | + const rel = path.relative(ROOT, fileURLToPath(url)).split(path.sep).join("/"); |
| 82 | + const pct = cov.total ? Math.round((100 * (cov.total - cov.uncovered.length)) / cov.total) : 100; |
| 83 | + console.log(`${rel} ${pct}% of ${cov.total} functions`); |
| 84 | + for (const u of cov.uncovered) console.log(` line ${u.line} ${u.name}`); |
| 85 | + } |
| 86 | + console.log(`coverage: ${total - missed} of ${total} functions in loaded site/ modules ran under the tests (scripts never imported by a test are not listed)`); |
| 87 | + } finally { |
| 88 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) main(); |
0 commit comments