coveredRun
uses node:fs
Definitions analyzed from source as a codebase holding only them, each named by its own name; and what a run of
them executed, given the functions that ran (by name: a function's own, or a function in a constant's value), as
V8 coverage of every module loaded would have it: run, the names; executedBy, the names and their code, given
the names as the run started (or undefined: what ran isn't known), loaded from the codebase's cache or cache.
import type { Codebase } from "#codebase/tcmts/cli/codebase"; import { executed } from "#codebase/tcmts/cli/suites"; import { codebaseOf } from "#codebase/tcmts/cli/test/shapes"; import { analyze } from "#codebase/tcmts/core/test/hash"; import type { DefRecord } from "#codebase/tcmts/core/types"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { pathToFileURL } from "node:url"; export function coveredRun(source: string) { const defs: DefRecord[] = analyze(source).defs; const cb = codebaseOf(defs) as Codebase; const def = (name: string) => defs.find((d) => d.nameHint === name)!; fs.mkdirSync(cb.lay.cacheDir, { recursive: true }); const ownCache = fs.realpathSync(cb.lay.cacheDir); const executedBy = ( names: ReadonlyMap<string, string> | undefined, ran: [definition: string, fn: string][], cache = ownCache, ) => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm-coverage-")); const result = [...new Set(defs.map((d) => d.componentHash))].map((c) => ({ url: pathToFileURL(path.join(cache, `${c}.ts`)).href, functions: [ { functionName: "", ranges: [{ startOffset: 0, count: 1 }] }, ...ran .filter(([d]) => def(d).componentHash === c) .map(([, fn]) => ({ functionName: fn, ranges: [{ startOffset: 10, count: 1 }] })), ], })); fs.writeFileSync(path.join(dir, "coverage-1.json"), JSON.stringify({ result })); try { return executed(cb, dir, names); } finally { fs.rmSync(dir, { recursive: true, force: true }); } }; const run = (...ran: [definition: string, fn: string][]) => Object.keys(executedBy(undefined, ran)!).sort(); return { cb, def, run, executedBy }; }
uses
- tcmts.cli.codebase.Codebase
- tcmts.cli.suites.executed
- tcmts.cli.test.shapes.codebaseOf
- tcmts.core.test.hash.analyze
- tcmts.core.types.DefRecord
used by
- tcmts.cli.suites.test.testComputedConstantChangingStateCountsWhenTheStateDoes
- tcmts.cli.suites.test.testComputedConstantCountsWhenAFunctionInItRan
- tcmts.cli.suites.test.testComputedConstantsCountWhenRead
- tcmts.cli.suites.test.testUnnamedCodeCountsThroughWhatRefersToIt
- tcmts.cli.suites.test.testUnnamedCodeOfATestsOwnCodebaseIsntThisOnes
- tcmts.cli.suites.test.testWhatASaveReplacedDuringARunStillCounts