diff --git a/.jscpd.json b/.jscpd.json index bf815cd2ee..d56e90afc2 100644 --- a/.jscpd.json +++ b/.jscpd.json @@ -26,6 +26,7 @@ "minTokens": 23, "path": [ "src", - "e2e-payments" + "e2e-payments", + "scripts" ] } diff --git a/deno.json b/deno.json index 446eeffc91..e988df86c6 100644 --- a/deno.json +++ b/deno.json @@ -15,7 +15,7 @@ "test:raw": "deno task build:static && deno test --no-check --allow-net --allow-env --allow-read --allow-write --allow-run --allow-sys --allow-ffi test/", "lint": "deno run -A scripts/biome.ts check --write --error-on-warnings src test scripts cli", "lint:ci": "deno run -A scripts/biome.ts check --error-on-warnings src test scripts cli", - "typecheck": "deno check src/**/*.ts src/**/*.tsx test/**/*.ts cli/**/*.ts && deno check --config=e2e-payments/deno.json e2e-payments/src/**/*.ts", + "typecheck": "deno check src/**/*.ts src/**/*.tsx test/**/*.ts cli/**/*.ts scripts/mutation.ts scripts/mutation/**/*.ts scripts/precommit.ts scripts/precommit/**/*.ts && deno check --config=e2e-payments/deno.json e2e-payments/src/**/*.ts", "cpd": "deno run -A scripts/cpd.ts --config .jscpd.json && deno run -A scripts/cpd.ts --config .jscpd.test.json", "precommit": "deno run -A scripts/precommit.ts", "precommit:mutation": "deno run -A scripts/precommit-mutation.ts", diff --git a/scripts/bench/bunny-crypto-smoke.ts b/scripts/bench/bunny-crypto-smoke.ts index 1a7379e96e..f1be42892f 100644 --- a/scripts/bench/bunny-crypto-smoke.ts +++ b/scripts/bench/bunny-crypto-smoke.ts @@ -13,6 +13,13 @@ const KEY = crypto.getRandomValues(new Uint8Array(32)); const SAMPLE = "attendee-pii-value-12345@example.com"; const ITERS = 2000; +/** Run `body` ITERS times and return the elapsed milliseconds. */ +const timeLoop = async (body: () => Promise | void): Promise => { + const t0 = performance.now(); + for (let i = 0; i < ITERS; i++) await body(); + return performance.now() - t0; +}; + // --- crypto.subtle (current approach) --- const subtleKey = await crypto.subtle.importKey( "raw", @@ -85,19 +92,14 @@ const run = async (): Promise => { out.subtleDecryptsNode = dec.decode(await subtleDec(n.iv, n.ct)) === SAMPLE; // 4) Micro-bench (small payload, the hot path) - let t0 = performance.now(); - for (let i = 0; i < ITERS; i++) { + const subtleMs = await timeLoop(async () => { const x = await subtleEnc(pt); await subtleDec(x.iv, x.ct); - } - const subtleMs = performance.now() - t0; - - t0 = performance.now(); - for (let i = 0; i < ITERS; i++) { + }); + const nodeMs = await timeLoop(() => { const x = nodeEnc(pt); nodeDec(x.iv, x.ct); - } - const nodeMs = performance.now() - t0; + }); out.iterations = ITERS; out.subtle_us_per_op = +((subtleMs * 1000) / ITERS).toFixed(2); diff --git a/scripts/build-site.ts b/scripts/build-site.ts index b52368135a..a2491c72f1 100644 --- a/scripts/build-site.ts +++ b/scripts/build-site.ts @@ -13,6 +13,7 @@ */ import { builderApi } from "#shared/builder.ts"; +import { runBuildEdge } from "./run-build-edge.ts"; const [siteName] = Deno.args; if (!siteName) { @@ -29,12 +30,7 @@ const repoRoot = new URL("..", import.meta.url).pathname; const bundlePath = `${repoRoot}bunny-script.ts`; console.log("Building edge bundle…"); -const build = await new Deno.Command(Deno.execPath(), { - args: ["task", "build:edge"], - cwd: repoRoot, - stderr: "inherit", - stdout: "inherit", -}).output(); +const build = await runBuildEdge(repoRoot); if (!build.success) { console.error("build:edge failed"); Deno.exit(build.code); diff --git a/scripts/compact-test-reporter.ts b/scripts/compact-test-reporter.ts index 3c19863dde..27adcb1163 100644 --- a/scripts/compact-test-reporter.ts +++ b/scripts/compact-test-reporter.ts @@ -1,5 +1,7 @@ import { isAbsolute, relative } from "node:path"; import { fileURLToPath } from "node:url"; +import { toDisplayPath } from "./project-root.ts"; +import { readStream } from "./stream-lines.ts"; type Location = { file: string; @@ -188,12 +190,6 @@ const countMatches = (text: string, re: RegExp): number => { return count; }; -const toDisplayPath = (cwd: string, file: string): string => { - if (!isAbsolute(file)) return file.replace(/^\.\//, ""); - const rel = relative(cwd, file); - return rel.startsWith("..") ? file : rel || "."; -}; - const locationFromDiagnostic = ( cwd: string, diagnostic: TapDiagnostic, @@ -465,51 +461,6 @@ export class CompactTapReporter { } } -const readLines = async ( - stream: ReadableStream, - onLine: (line: string) => void, -): Promise => { - const reader = stream.getReader(); - const decoder = new TextDecoder(); - let buffered = ""; - - try { - while (true) { - const { done, value } = await reader.read(); - if (done) break; - buffered += decoder.decode(value, { stream: true }); - const lines = buffered.split(/\r?\n/); - buffered = lines.pop() ?? ""; - for (const line of lines) onLine(line); - } - } finally { - buffered += decoder.decode(); - if (buffered) onLine(buffered); - reader.releaseLock(); - } -}; - -const readText = async ( - stream: ReadableStream, -): Promise => { - const reader = stream.getReader(); - const decoder = new TextDecoder(); - let text = ""; - - try { - while (true) { - const { done, value } = await reader.read(); - if (done) break; - text += decoder.decode(value, { stream: true }); - } - } finally { - text += decoder.decode(); - reader.releaseLock(); - } - - return text; -}; - const usefulStderr = (stderr: string): string => stderr .split(/\r?\n/) @@ -569,10 +520,10 @@ export const runCompactDenoTest = async ( hideProgress: Boolean(options.env.CI || options.env.GITHUB_ACTIONS), }); - const stdoutTask = readLines(child.stdout, (line) => + const stdoutTask = readStream(child.stdout, (line) => reporter.consumeLine(line), ); - const stderrTask = readText(child.stderr); + const stderrTask = readStream(child.stderr); const status = await child.status; await stdoutTask; const stderrText = await stderrTask; diff --git a/scripts/deploy-edge-lib.ts b/scripts/deploy-edge-lib.ts index 2353da3d4a..cca4c34c87 100644 --- a/scripts/deploy-edge-lib.ts +++ b/scripts/deploy-edge-lib.ts @@ -19,6 +19,14 @@ export type FetchText = ( export type BunnyDeployResult = { ok: true } | { error: string; ok: false }; +/** Shared shape of the script-code operations (upload, deploy). */ +type ScriptCodeFn = ( + scriptId: string, + code: string, + accessKey: string, + fetchText: FetchText, +) => Promise; + export interface BuildResult { code: number; success: boolean; @@ -102,12 +110,12 @@ const postScriptAction = async ( }; }; -export const uploadScriptCode = ( - scriptId: string, - code: string, - accessKey: string, - fetchText: FetchText, -): Promise => +export const uploadScriptCode: ScriptCodeFn = ( + scriptId, + code, + accessKey, + fetchText, +) => postScriptAction( scriptId, "code", @@ -131,12 +139,12 @@ export const publishScript = ( fetchText, ); -export const deployScriptCode = async ( - scriptId: string, - code: string, - accessKey: string, - fetchText: FetchText, -): Promise => { +export const deployScriptCode: ScriptCodeFn = async ( + scriptId, + code, + accessKey, + fetchText, +) => { const upload = await uploadScriptCode(scriptId, code, accessKey, fetchText); if (!upload.ok) return upload; return publishScript(scriptId, accessKey, fetchText); diff --git a/scripts/deploy-edge.ts b/scripts/deploy-edge.ts index 81bed41268..9f8dd62287 100644 --- a/scripts/deploy-edge.ts +++ b/scripts/deploy-edge.ts @@ -2,6 +2,7 @@ import { fromFileUrl } from "@std/path"; import { type FetchTextResult, runDeployEdge } from "./deploy-edge-lib.ts"; +import { runBuildEdge } from "./run-build-edge.ts"; const repoRoot = fromFileUrl(new URL("..", import.meta.url)); const bundlePath = fromFileUrl(new URL("../bunny-script.ts", import.meta.url)); @@ -18,17 +19,6 @@ const fetchText = async ( }; }; -const runBuildEdge = async (cwd: string) => { - const build = await new Deno.Command(Deno.execPath(), { - args: ["task", "build:edge"], - cwd, - stderr: "inherit", - stdout: "inherit", - }).output(); - - return { code: build.code, success: build.success }; -}; - const exitCode = await runDeployEdge({ args: Deno.args, bundlePath, diff --git a/scripts/find-slow-tests.ts b/scripts/find-slow-tests.ts index 9d0bdb1d10..9614f095c7 100644 --- a/scripts/find-slow-tests.ts +++ b/scripts/find-slow-tests.ts @@ -9,20 +9,14 @@ */ import { - JUNIT_PATH, readSlowTestsReport, SLOW_TEST_THRESHOLD_MS, } from "./test-durations.ts"; -import { runTests, withTestHarness } from "./test-harness.ts"; +import { runSuiteWithHarness } from "./test-harness.ts"; /** Main: run the suite, then fail if any test exceeded the slow-test threshold. */ const main = async (): Promise => { - // Remove any stale JUnit file so a killed prior run can't surface its - // timings; `deno test --junit-path` rewrites it on a completed run. - await Deno.remove(JUNIT_PATH).catch(() => {}); - const exitCode = await withTestHarness(() => - runTests(["test/"], false, JUNIT_PATH), - ); + const exitCode = await runSuiteWithHarness(false); // Propagate a suite failure before judging timings: a failed run may have // written an incomplete JUnit file, whose timings would be misleading. diff --git a/scripts/find-unused-src.ts b/scripts/find-unused-src.ts index 7025a2762a..489b6408ff 100644 --- a/scripts/find-unused-src.ts +++ b/scripts/find-unused-src.ts @@ -14,6 +14,7 @@ import * as fs from "node:fs"; import * as path from "node:path"; +import { flatMap } from "#fp"; const ROOT = path.resolve(import.meta.dirname!, ".."); @@ -61,6 +62,22 @@ function resolveImport(specifier: string, fromFile: string): string | null { const isTypeScriptFile = (name: string): boolean => name.endsWith(".ts") || name.endsWith(".tsx"); +/** Read a project-relative source file as UTF-8 text. */ +const readSource = (filePath: string): string => + fs.readFileSync(path.resolve(ROOT, filePath), "utf-8"); + +/** Split a comma-separated `a, b as c` list into bare names, dropping aliases. */ +const splitAliasedNames = (raw: string): string[] => + raw + .split(",") + .map((n: string) => + n + .trim() + .replace(/\s+as\s+\w+/, "") + .trim(), + ) + .filter(Boolean); + /** List direct entries in a directory, classifying as files or subdirs */ function listEntries(d: string): { dirs: string[]; files: string[] } { const dirs: string[] = []; @@ -94,7 +111,7 @@ function collectFiles(dir: string): string[] { function extractImports( filePath: string, ): { specifier: string; names: string[] }[] { - const content = fs.readFileSync(path.resolve(ROOT, filePath), "utf-8"); + const content = readSource(filePath); const imports: { specifier: string; names: string[] }[] = []; // Static imports/exports: @@ -110,16 +127,7 @@ function extractImports( for (const match of content.matchAll(staticRegex)) { const namedImports = (match[1] || "") + (match[3] || ""); const specifier = match[4]!; - const names = namedImports - .split(",") - .map((n: string) => - n - .trim() - .replace(/\s+as\s+\w+/, "") - .trim(), - ) - .filter(Boolean); - imports.push({ names, specifier }); + imports.push({ names: splitAliasedNames(namedImports), specifier }); } // Dynamic imports: @@ -132,16 +140,7 @@ function extractImports( for (const match of content.matchAll(dynamicRegex)) { const namedImports = match[1] || ""; const specifier = match[2]!; - const names = namedImports - .split(",") - .map((n: string) => - n - .trim() - .replace(/\s+as\s+\w+/, "") - .trim(), - ) - .filter(Boolean); - imports.push({ names, specifier }); + imports.push({ names: splitAliasedNames(namedImports), specifier }); } return imports; @@ -157,23 +156,14 @@ const EXPORT_PATTERNS: RegExp[] = [ ]; /** Extract names from `export { a, b as c }` blocks (not re-exports) */ -const extractBraceExports = (content: string): string[] => { - const names: string[] = []; - for (const m of content.matchAll(/export\s+\{([^}]+)\}(?!\s*from)/g)) { - for (const name of m[1]!.split(",")) { - const trimmed = name - .trim() - .replace(/\s+as\s+\w+/, "") - .trim(); - if (trimmed) names.push(trimmed); - } - } - return names; -}; +const extractBraceExports = (content: string): string[] => + flatMap((m: RegExpMatchArray) => splitAliasedNames(m[1]!))([ + ...content.matchAll(/export\s+\{([^}]+)\}(?!\s*from)/g), + ]); /** Extract exported names from a file */ function extractExports(filePath: string): string[] { - const content = fs.readFileSync(path.resolve(ROOT, filePath), "utf-8"); + const content = readSource(filePath); const exports: string[] = []; for (const pattern of EXPORT_PATTERNS) { @@ -218,6 +208,27 @@ type ImportInfo = { file: string; names: string[] }; const importedBySrc = new Map(); const importedByTest = new Map(); +/** The src/ and test/ importers recorded for one source file. */ +const importersOf = ( + srcFile: string, +): { src: ImportInfo[]; test: ImportInfo[] } => ({ + src: importedBySrc.get(srcFile) ?? [], + test: importedByTest.get(srcFile) ?? [], +}); + +// Files eligible for the "imported only by tests" and "dead code" checks: +// entry points and browser-only client code are expected to sit outside the +// module graph, so they are never reported. +const candidateSrcFiles = srcFiles.filter( + (f) => !entryPoints.has(f) && !f.startsWith("src/ui/client/"), +); + +/** True when `srcFile` is imported by neither src/ nor test/ files. */ +const isNeverImported = (srcFile: string): boolean => { + const { src, test } = importersOf(srcFile); + return src.length === 0 && test.length === 0; +}; + for (const file of allFiles) { const imports = extractImports(file); for (const { specifier, names } of imports) { @@ -254,14 +265,10 @@ console.log( ); let fileCount = 0; -for (const srcFile of srcFiles) { - if (entryPoints.has(srcFile)) continue; - if (srcFile.startsWith("src/ui/client/")) continue; - - const srcImporters = importedBySrc.get(srcFile) ?? []; - const testImporters = importedByTest.get(srcFile) ?? []; - - if (srcImporters.length === 0 && testImporters.length > 0) { +for (const srcFile of candidateSrcFiles) { + const { src: srcImporters, test: testImporters } = importersOf(srcFile); + const testOnly = srcImporters.length === 0 && testImporters.length > 0; + if (testOnly) { fileCount++; const testFileList = [...new Set(testImporters.map((i) => i.file))]; console.log(` ${srcFile}`); @@ -287,8 +294,7 @@ console.log( let exportCount = 0; for (const srcFile of srcFiles) { - const srcImporters = importedBySrc.get(srcFile) ?? []; - const testImporters = importedByTest.get(srcFile) ?? []; + const { src: srcImporters, test: testImporters } = importersOf(srcFile); if (srcImporters.length === 0) continue; if (testImporters.length === 0) continue; @@ -333,17 +339,10 @@ console.log( ); let deadCount = 0; -for (const srcFile of srcFiles) { - if (entryPoints.has(srcFile)) continue; - if (srcFile.startsWith("src/ui/client/")) continue; - - const srcImporters = importedBySrc.get(srcFile) ?? []; - const testImporters = importedByTest.get(srcFile) ?? []; - - if (srcImporters.length === 0 && testImporters.length === 0) { - deadCount++; - console.log(` ${srcFile}`); - } +for (const srcFile of candidateSrcFiles) { + if (!isNeverImported(srcFile)) continue; + deadCount++; + console.log(` ${srcFile}`); } if (deadCount === 0) { diff --git a/scripts/line-column.ts b/scripts/line-column.ts new file mode 100644 index 0000000000..2ed53a2a56 --- /dev/null +++ b/scripts/line-column.ts @@ -0,0 +1,8 @@ +/** The 1-based line and column of byte-offset `index` within `content`. */ +export const lineColumnAt = ( + content: string, + index: number, +): { column: number; line: number } => { + const lines = content.slice(0, index).split("\n"); + return { column: lines.at(-1)!.length + 1, line: lines.length }; +}; diff --git a/scripts/line-counts-lib.ts b/scripts/line-counts-lib.ts index fe1000d9f3..74a3a9163f 100644 --- a/scripts/line-counts-lib.ts +++ b/scripts/line-counts-lib.ts @@ -1,3 +1,5 @@ +import { walkFiles } from "./walk-files.ts"; + type LineCount = { files: number; lines: number; @@ -69,17 +71,6 @@ const formatLineCounts = (rows: LineCountRow[]): string => formatRow(totalLineCount(rows)), ].join("\n"); -async function* walkFiles(directory: string): AsyncGenerator { - for await (const entry of Deno.readDir(directory)) { - const path = `${directory}/${entry.name}`; - if (entry.isDirectory) { - yield* walkFiles(path); - continue; - } - yield path; - } -} - const collectLineCounts = async ( roots: readonly string[], ): Promise => { diff --git a/scripts/mutation/child-process.ts b/scripts/mutation/child-process.ts new file mode 100644 index 0000000000..7ed271a1ce --- /dev/null +++ b/scripts/mutation/child-process.ts @@ -0,0 +1,35 @@ +/** Run `deno ` to completion and return its exit code. */ +export const denoExitCode = async ( + args: string[], + options: Omit = {}, +): Promise => { + const { code } = await new Deno.Command(Deno.execPath(), { + args, + ...options, + }).output(); + return code; +}; + +// Signal handling is platform-dependent (e.g. SIGTERM on Windows), so every +// add/remove is best-effort — callers must still clean up via finally/exit. +const forEachTerminationSignal = ( + action: (signal: Deno.Signal) => void, +): void => { + for (const signal of ["SIGINT", "SIGTERM"] as Deno.Signal[]) { + try { + action(signal); + } catch { + // Best-effort; the caller's own cleanup still runs. + } + } +}; + +/** Register `handler` for SIGINT and SIGTERM. */ +export const onTerminationSignals = (handler: () => void): void => + forEachTerminationSignal((signal) => Deno.addSignalListener(signal, handler)); + +/** Unregister a `handler` previously passed to `onTerminationSignals`. */ +export const offTerminationSignals = (handler: () => void): void => + forEachTerminationSignal((signal) => + Deno.removeSignalListener(signal, handler), + ); diff --git a/scripts/mutation/generate.ts b/scripts/mutation/generate.ts index aa84d69719..3b35eb4491 100644 --- a/scripts/mutation/generate.ts +++ b/scripts/mutation/generate.ts @@ -22,6 +22,7 @@ import { parseSync } from "npm:oxc-parser@0.132.0"; import { flatMap, unique } from "#fp"; +import { lineColumnAt } from "../line-column.ts"; import { assignmentOperators, assignmentOperatorsExhaustive, @@ -64,13 +65,15 @@ interface AstNode { value?: unknown; } -const lineColumnAt = ( +/** + * A mutant generator for one node shape. `exhaustive` toggles the larger, + * slower mutant set on; the two-argument generators simply ignore it. + */ +type MutantFn = ( + node: AstNode, content: string, - index: number, -): { column: number; line: number } => { - const lines = content.slice(0, index).split("\n"); - return { column: lines.at(-1)!.length + 1, line: lines.length }; -}; + exhaustive: boolean, +) => Mutant[]; /** Build a mutant for the span [start, end), resolving its line/column. */ const spanMutant = ( @@ -86,6 +89,12 @@ const spanMutant = ( return replacement === undefined ? base : { ...base, replacement }; }; +/** Whitespace-collapsed source of [start, end), truncated to ~40 chars. */ +const clippedText = (content: string, start: number, end: number): string => { + const text = content.slice(start, end).replace(/\s+/g, " ").trim(); + return text.length > 40 ? `${text.slice(0, 39)}…` : text; +}; + // --- Binary / logical / assignment operators ----------------------------- const MUTABLE_NODES: Record = { @@ -94,11 +103,7 @@ const MUTABLE_NODES: Record = { LogicalExpression: [logicalOperators, logicalOperatorsExhaustive], }; -const operatorMutants = ( - node: AstNode, - content: string, - exhaustive: boolean, -): Mutant[] => { +const operatorMutants: MutantFn = (node, content, exhaustive) => { const { left, operator, right, type } = node as AstNode & { left: { end: number }; operator: string; @@ -194,13 +199,16 @@ const literalReplacementMutants = ( ); }; -const stringLiteralMutants = ( +/** The literal's `value` when it is `type`, else undefined (skip the node). */ +const literalValueOfType = ( node: AstNode, - content: string, - exhaustive: boolean, -): Mutant[] => { - const { value } = node; - if (typeof value !== "string") return []; + type: "number" | "string", +): T | undefined => + typeof node.value === type ? (node.value as T) : undefined; + +const stringLiteralMutants: MutantFn = (node, content, exhaustive) => { + const value = literalValueOfType(node, "string"); + if (value === undefined) return []; const replacements = value === "" ? ['"mutated"'] @@ -210,13 +218,9 @@ const stringLiteralMutants = ( const numberText = (value: number): string => String(value); -const numberLiteralMutants = ( - node: AstNode, - content: string, - exhaustive: boolean, -): Mutant[] => { - const { value } = node; - if (typeof value !== "number") return []; +const numberLiteralMutants: MutantFn = (node, content, exhaustive) => { + const value = literalValueOfType(node, "number"); + if (value === undefined) return []; const defaultValue = value === 0 ? 1 : 0; const values = [ defaultValue, @@ -226,11 +230,7 @@ const numberLiteralMutants = ( return literalReplacementMutants(node, content, replacements); }; -const literalMutants = ( - node: AstNode, - content: string, - exhaustive: boolean, -): Mutant[] => [ +const literalMutants: MutantFn = (node, content, exhaustive) => [ ...booleanMutants(node, content), ...numberLiteralMutants(node, content, exhaustive), ...stringLiteralMutants(node, content, exhaustive), @@ -247,19 +247,13 @@ const statementRemovalMutants = (node: AstNode, content: string): Mutant[] => { start: number; }; if (!REMOVABLE_EXPRESSIONS.has(expression.type)) return []; - const text = content.slice(start, end).replace(/\s+/g, " ").trim(); - const label = text.length > 40 ? `${text.slice(0, 39)}…` : text; + const label = clippedText(content, start, end); // Replace with an empty statement — valid even as a braceless if/for/while body. return [spanMutant(content, start, end, label, "(removed)", ";")]; }; // --- Control-flow removals ------------------------------------------------ -const clippedText = (content: string, start: number, end: number): string => { - const text = content.slice(start, end).replace(/\s+/g, " ").trim(); - return text.length > 40 ? `${text.slice(0, 39)}…` : text; -}; - const returnMutants = (node: AstNode, content: string): Mutant[] => { const { argument } = node; if (!argument) return []; @@ -289,11 +283,7 @@ const statementRemoval = (node: AstNode, content: string): Mutant[] => { ]; }; -const conditionalMutants = ( - node: AstNode, - content: string, - exhaustive: boolean, -): Mutant[] => { +const conditionalMutants: MutantFn = (node, content, exhaustive) => { const { alternate, consequent, end, start } = node as AstNode & { alternate: { end: number; start: number }; consequent: { end: number; start: number }; diff --git a/scripts/mutation/isolation-state.ts b/scripts/mutation/isolation-state.ts index 5669ffd4f5..d4a9a89743 100644 --- a/scripts/mutation/isolation-state.ts +++ b/scripts/mutation/isolation-state.ts @@ -13,7 +13,9 @@ import { resolve, SEPARATOR, } from "@std/path"; +import { rethrowUnlessNotFound } from "../not-found.ts"; import { projectRoot } from "../project-root.ts"; +import { denoExitCode } from "./child-process.ts"; export const MUTATION_RUNS_DIR = ".mutation-runs"; export const MUTATION_WORK_DIR = "work"; @@ -305,7 +307,7 @@ export const readRunRecords = async ( } } } catch (error) { - if (!(error instanceof Deno.errors.NotFound)) throw error; + rethrowUnlessNotFound(error); } return records.sort((left, right) => right.createdAt.localeCompare(left.createdAt), @@ -420,17 +422,11 @@ try { } `; -const lockProbeExitCode = async ( - path: string, - timeoutMs: number, -): Promise => { - const { code } = await new Deno.Command(Deno.execPath(), { - args: ["eval", LOCK_PROBE_SCRIPT, "--", path, String(timeoutMs)], +const lockProbeExitCode = (path: string, timeoutMs: number): Promise => + denoExitCode(["eval", LOCK_PROBE_SCRIPT, "--", path, String(timeoutMs)], { stderr: "null", stdout: "null", - }).output(); - return code; -}; + }); export const runLockIsHeld = async ( record: Pick, diff --git a/scripts/mutation/isolation.ts b/scripts/mutation/isolation.ts index 71713994d9..16879eeb4d 100644 --- a/scripts/mutation/isolation.ts +++ b/scripts/mutation/isolation.ts @@ -8,6 +8,10 @@ import { relative } from "@std/path"; import { processExists, stopProcess, stopProcessNow } from "../process.ts"; import { projectRoot } from "../project-root.ts"; +import { + offTerminationSignals, + onTerminationSignals, +} from "./child-process.ts"; import { copyMutationSnapshot, createRunId, @@ -169,14 +173,7 @@ export const runMutationInSnapshot = async ( } } }; - const signals: Deno.Signal[] = ["SIGINT", "SIGTERM"]; - for (const signal of signals) { - try { - Deno.addSignalListener(signal, stopChild); - } catch { - // Signal handling is platform-dependent; the child still owns cleanup. - } - } + onTerminationSignals(stopChild); let exitCode = 1; try { @@ -227,13 +224,7 @@ export const runMutationInSnapshot = async ( } console.error(error instanceof Error ? error.message : String(error)); } - for (const signal of signals) { - try { - Deno.removeSignalListener(signal, stopChild); - } catch { - // Matches the add above. - } - } + offTerminationSignals(stopChild); return exitCode; }; @@ -246,16 +237,25 @@ const listRuns = async (root = projectRoot): Promise => { return 0; }; -const killRuns = async ( +/** Run `body` over the runs matching `target`, or report + return 1 if none. */ +const withSelectedRuns = async ( target: string, - force: boolean, - root = projectRoot, + root: string, + body: (records: MutationRunRecord[]) => Promise, ): Promise => { const records = selectedRuns(await readRunRecords(root), target); if (records.length === 0) { console.error(`No isolated mutation run matched ${target}.`); return 1; } + return body(records); +}; + +const signalMatchedRuns = async ( + records: MutationRunRecord[], + force: boolean, + target: string, +): Promise => { const signalled = await Promise.all( records.map(async (record) => (await signalRun(record, force)) ? record : null, @@ -272,15 +272,19 @@ const killRuns = async ( return 0; }; -const cleanRuns = async ( +const killRuns = ( target: string, + force: boolean, root = projectRoot, +): Promise => + withSelectedRuns(target, root, (records) => + signalMatchedRuns(records, force, target), + ); + +const removeMatchedRuns = async ( + records: MutationRunRecord[], + target: string, ): Promise => { - const records = selectedRuns(await readRunRecords(root), target); - if (records.length === 0) { - console.error(`No isolated mutation run matched ${target}.`); - return 1; - } const { removable, skipped } = await cleanableRuns(records); const removeResults = await Promise.all(removable.map(removeRun)); const removed = removeResults @@ -313,6 +317,11 @@ const cleanRuns = async ( return 0; }; +const cleanRuns = (target: string, root = projectRoot): Promise => + withSelectedRuns(target, root, (records) => + removeMatchedRuns(records, target), + ); + export const runIsolatedMutationCommand = async ( args: string[], root = projectRoot, diff --git a/scripts/mutation/runner.ts b/scripts/mutation/runner.ts index 246caa25c1..1b63770c5b 100644 --- a/scripts/mutation/runner.ts +++ b/scripts/mutation/runner.ts @@ -19,6 +19,11 @@ import { stripeMockEnv } from "../stripe-mock.ts"; import { withTestHarness } from "../test-harness.ts"; import { type AssetRebuilder, createAssetRebuilder } from "./assets.ts"; import { batchTestFiles } from "./batch.ts"; +import { + denoExitCode, + offTerminationSignals, + onTerminationSignals, +} from "./child-process.ts"; import { applyMutant, generateMutants, type Mutant } from "./generate.ts"; import { type IgnoreList, @@ -132,12 +137,9 @@ const createLinter = async (): Promise => { }; /** Run one `deno test` process over `batch`, returning its exit code. */ -const runTestBatch = async ( - batch: string[], - signal: AbortSignal, -): Promise => { - const { code } = await new Deno.Command(Deno.execPath(), { - args: [ +const runTestBatch = (batch: string[], signal: AbortSignal): Promise => + denoExitCode( + [ "test", "--no-check", "--allow-all", @@ -145,14 +147,14 @@ const runTestBatch = async ( "--v8-flags=--expose-gc", ...batch, ], - cwd: projectRoot, - env: testEnv(), - signal, - stderr: "null", - stdout: "null", - }).output(); - return code; -}; + { + cwd: projectRoot, + env: testEnv(), + signal, + stderr: "null", + stdout: "null", + }, + ); /** * Run the test files once, returning the outcome and how long it took. @@ -576,15 +578,7 @@ const mutate = async (options: MutationOptions): Promise => { aborted = true; abortController.abort(); }; - const signals: Deno.Signal[] = ["SIGINT", "SIGTERM"]; - for (const signal of signals) { - try { - Deno.addSignalListener(signal, onSignal); - } catch { - // signal handling may be unavailable (e.g. SIGTERM on Windows); - // the finally below still restores. - } - } + onTerminationSignals(onSignal); try { return await runMutants({ @@ -602,13 +596,7 @@ const mutate = async (options: MutationOptions): Promise => { useHarness: options.useHarness, }); } finally { - for (const signal of signals) { - try { - Deno.removeSignalListener(signal, onSignal); - } catch { - // matches the add above - } - } + offTerminationSignals(onSignal); } }; diff --git a/scripts/not-found.ts b/scripts/not-found.ts new file mode 100644 index 0000000000..00fbfb251e --- /dev/null +++ b/scripts/not-found.ts @@ -0,0 +1,7 @@ +/** + * Re-throw `error` unless it is a Deno `NotFound` — the common "treat a missing + * file/dir as absent, surface everything else" guard for filesystem catches. + */ +export const rethrowUnlessNotFound = (error: unknown): void => { + if (!(error instanceof Deno.errors.NotFound)) throw error; +}; diff --git a/scripts/precommit-mutation.ts b/scripts/precommit-mutation.ts index 80301bae0d..18e9b82a63 100644 --- a/scripts/precommit-mutation.ts +++ b/scripts/precommit-mutation.ts @@ -15,7 +15,7 @@ */ import { runMutationInSnapshot } from "./mutation/isolation.ts"; -import { runCommand } from "./precommit/merge-warning.ts"; +import { runCommand } from "./precommit/git.ts"; import { runMutationStep } from "./precommit/mutation-step.ts"; /** Per-mutant timeout floor; mirrors `deno task mutation`'s default. */ diff --git a/scripts/precommit/git.ts b/scripts/precommit/git.ts new file mode 100644 index 0000000000..1bdf7932bb --- /dev/null +++ b/scripts/precommit/git.ts @@ -0,0 +1,75 @@ +export interface CommandResult { + code: number; + stdout: string; + stderr: string; + success: boolean; +} + +export type RunCommand = (cmd: string[]) => Promise; + +/** Split a command into [executable, args], rejecting an empty command. */ +export const splitCommand = ( + cmd: string[], + label = "command", +): [string, string[]] => { + const [command, ...args] = cmd; + if (!command) throw new Error(`No ${label} configured`); + return [command, args]; +}; + +/** Run a command, capturing its stdout/stderr as decoded strings. */ +export const runCommand: RunCommand = async (cmd) => { + const [command, args] = splitCommand(cmd); + const output = await new Deno.Command(command, { + args, + stderr: "piped", + stdout: "piped", + }).output(); + + const decoder = new TextDecoder(); + return { + code: output.code, + stderr: decoder.decode(output.stderr), + stdout: decoder.decode(output.stdout), + success: output.success, + }; +}; + +/** Run a command wired to the parent's stdio (for interactive steps). */ +export const runInteractiveCommand: RunCommand = async (cmd) => { + const [command, args] = splitCommand(cmd); + const status = await new Deno.Command(command, { + args, + stderr: "inherit", + stdin: "inherit", + stdout: "inherit", + }).spawn().status; + + return { + code: status.code, + stderr: "", + stdout: "", + success: status.success, + }; +}; + +/** Run a `git` subcommand through `run`. */ +export const runGit = ( + run: RunCommand, + args: string[], +): Promise => run(["git", ...args]); + +/** Trimmed stdout of a git command, or undefined when it fails or is empty. */ +export const commandValue = async ( + run: RunCommand, + args: string[], +): Promise => { + const result = await runGit(run, args); + if (!result.success) return undefined; + const value = result.stdout.trim(); + return value || undefined; +}; + +/** True when `run` executes inside a git work tree. */ +export const isInsideWorkTree = async (run: RunCommand): Promise => + (await runGit(run, ["rev-parse", "--is-inside-work-tree"])).success; diff --git a/scripts/precommit/merge-warning.ts b/scripts/precommit/merge-warning.ts index 3b6b53c265..fed337531f 100644 --- a/scripts/precommit/merge-warning.ts +++ b/scripts/precommit/merge-warning.ts @@ -1,53 +1,9 @@ -export interface CommandResult { - code: number; - stdout: string; - stderr: string; - success: boolean; -} - -export type RunCommand = (cmd: string[]) => Promise; - -export const runCommand: RunCommand = async ( - cmd: string[], -): Promise => { - const [command, ...args] = cmd; - if (!command) throw new Error("No command configured"); - - const output = await new Deno.Command(command, { - args, - stderr: "piped", - stdout: "piped", - }).output(); - - const decoder = new TextDecoder(); - return { - code: output.code, - stderr: decoder.decode(output.stderr), - stdout: decoder.decode(output.stdout), - success: output.success, - }; -}; - -export const runInteractiveCommand: RunCommand = async ( - cmd: string[], -): Promise => { - const [command, ...args] = cmd; - if (!command) throw new Error("No command configured"); - - const status = await new Deno.Command(command, { - args, - stderr: "inherit", - stdin: "inherit", - stdout: "inherit", - }).spawn().status; - - return { - code: status.code, - stderr: "", - stdout: "", - success: status.success, - }; -}; +import { + commandValue, + isInsideWorkTree, + type RunCommand, + runGit, +} from "./git.ts"; export const parseMergeTreeConflictedPaths = (stdout: string): string[] => { const lines = stdout.split(/\r?\n/); @@ -61,24 +17,10 @@ export const parseMergeTreeConflictedPaths = (stdout: string): string[] => { return Array.from(new Set(paths)); }; -const runGit = (run: RunCommand, args: string[]): Promise => - run(["git", ...args]); - -const commandValue = async ( - run: RunCommand, - args: string[], -): Promise => { - const result = await runGit(run, args); - if (!result.success) return undefined; - const value = result.stdout.trim(); - return value || undefined; -}; - export const getMergeConflictWarning = async ( run: RunCommand, ): Promise => { - const inWorkTree = await runGit(run, ["rev-parse", "--is-inside-work-tree"]); - if (!inWorkTree.success) return undefined; + if (!(await isInsideWorkTree(run))) return undefined; const originUrl = await commandValue(run, ["remote", "get-url", "origin"]); const head = await commandValue(run, ["rev-parse", "--verify", "HEAD"]); diff --git a/scripts/precommit/mutation-step.ts b/scripts/precommit/mutation-step.ts index 494b166ffe..abc6a88812 100644 --- a/scripts/precommit/mutation-step.ts +++ b/scripts/precommit/mutation-step.ts @@ -56,7 +56,7 @@ * `scripts/precommit-mutation.ts` wires in the real implementations. */ -import type { RunCommand } from "./merge-warning.ts"; +import type { RunCommand } from "./git.ts"; /** A changed-source count above this almost certainly means the local base ref * is stale (the merge-base fell far back), not a real change set — so the gate diff --git a/scripts/precommit/push.ts b/scripts/precommit/push.ts index aa67d3f873..edb5940a05 100644 --- a/scripts/precommit/push.ts +++ b/scripts/precommit/push.ts @@ -1,4 +1,9 @@ -import type { RunCommand } from "./merge-warning.ts"; +import { + commandValue, + isInsideWorkTree, + type RunCommand, + runGit, +} from "./git.ts"; export interface PushPromptContext { branchName: string; @@ -14,18 +19,6 @@ interface PromptToPushOptions { run: RunCommand; } -const runGit = (run: RunCommand, args: string[]) => run(["git", ...args]); - -const commandValue = async ( - run: RunCommand, - args: string[], -): Promise => { - const result = await runGit(run, args); - if (!result.success) return undefined; - const value = result.stdout.trim(); - return value || undefined; -}; - const commandNumber = async ( run: RunCommand, args: string[], @@ -64,8 +57,7 @@ const getUnpushedCommitCount = async ( export const getPushPromptContext = async ( run: RunCommand, ): Promise => { - const inWorkTree = await runGit(run, ["rev-parse", "--is-inside-work-tree"]); - if (!inWorkTree.success) return undefined; + if (!(await isInsideWorkTree(run))) return undefined; const status = await runGit(run, ["status", "--porcelain"]); if (!status.success || status.stdout.trim()) return undefined; diff --git a/scripts/precommit/runner.ts b/scripts/precommit/runner.ts index 2afbb290ec..91b38aa321 100644 --- a/scripts/precommit/runner.ts +++ b/scripts/precommit/runner.ts @@ -1,9 +1,7 @@ +import { readStream } from "../stream-lines.ts"; import { bold, dim, green, red, yellow } from "./colors.ts"; -import { - getMergeConflictWarning, - runCommand, - runInteractiveCommand, -} from "./merge-warning.ts"; +import { runCommand, runInteractiveCommand, splitCommand } from "./git.ts"; +import { getMergeConflictWarning } from "./merge-warning.ts"; import { promptToPushCheckedInChanges, shouldPushFromAnswer } from "./push.ts"; import { getSteps, type Step } from "./steps.ts"; import { @@ -43,43 +41,11 @@ const confirmPush = async (message: string): Promise => { return shouldPushFromAnswer(await readPromptLine()); }; -const readStream = async ( - stream: ReadableStream, - onLine: (line: string) => void, -): Promise => { - const reader = stream.getReader(); - const decoder = new TextDecoder(); - let buffered = ""; - let text = ""; - - try { - while (true) { - const { done, value } = await reader.read(); - if (done) break; - const chunk = decoder.decode(value, { stream: true }); - text += chunk; - buffered += chunk; - const lines = buffered.split(/\r?\n/); - buffered = lines.pop() ?? ""; - for (const line of lines) onLine(line); - } - } finally { - const chunk = decoder.decode(); - text += chunk; - buffered += chunk; - if (buffered) onLine(buffered); - reader.releaseLock(); - } - - return text; -}; - const runStep = async (step: Step): Promise => { const prefix = ` ${step.name} … `; write(prefix); const start = performance.now(); - const [command, ...args] = step.cmd; - if (!command) throw new Error(`No command configured for ${step.name}`); + const [command, args] = splitCommand(step.cmd, `command for ${step.name}`); const cmd = new Deno.Command(command, { args, diff --git a/scripts/profile-cold-boot.ts b/scripts/profile-cold-boot.ts index 7456d2a8f7..4880621b42 100644 --- a/scripts/profile-cold-boot.ts +++ b/scripts/profile-cold-boot.ts @@ -15,24 +15,45 @@ interface Timing { const timings: Timing[] = []; +const recordTiming = (name: string, start: number, result: T): T => { + timings.push({ duration: performance.now() - start, name }); + return result; +}; + const measure = async (name: string, fn: () => Promise): Promise => { const start = performance.now(); - const result = await fn(); - const duration = performance.now() - start; - timings.push({ duration, name }); - return result; + return recordTiming(name, start, await fn()); }; const measureSync = (name: string, fn: () => T): T => { const start = performance.now(); - const result = fn(); - const duration = performance.now() - start; - timings.push({ duration, name }); - return result; + return recordTiming(name, start, fn()); }; const log = console.log.bind(console); +/** + * Time a cached call: one cold `call()` (queries + caches) then five warm ones, + * logging the first-call cost, the warm average, and the speedup. + */ +const profileCaching = async (call: () => Promise): Promise => { + const firstStart = performance.now(); + await call(); + const firstDuration = performance.now() - firstStart; + log(` First call (queries DB + caches): ${firstDuration.toFixed(2)}ms`); + + const cachedTimings: number[] = []; + for (let i = 0; i < 5; i++) { + const start = performance.now(); + await call(); + cachedTimings.push(performance.now() - start); + } + const avgCached = + cachedTimings.reduce((a, b) => a + b, 0) / cachedTimings.length; + log(` Cached calls (avg of 5): ${avgCached.toFixed(4)}ms`); + log(` ✅ ${(firstDuration / avgCached).toFixed(0)}x faster with caching!\n`); +}; + const printReport = () => { log(`\n${"=".repeat(60)}`); log("COLD BOOT PERFORMANCE PROFILE"); @@ -127,23 +148,7 @@ const main = async () => { log("Testing isSetupComplete() caching:\n"); // First call after setup - should query DB and cache - const firstStart = performance.now(); - await settings.setup.isComplete(); - const firstDuration = performance.now() - firstStart; - log(` First call (queries DB + caches): ${firstDuration.toFixed(2)}ms\n`); - - // Subsequent calls - should return cached value instantly - const cachedTimings: number[] = []; - for (let i = 0; i < 5; i++) { - const start = performance.now(); - await settings.setup.isComplete(); - const duration = performance.now() - start; - cachedTimings.push(duration); - } - const avgCached = - cachedTimings.reduce((a, b) => a + b, 0) / cachedTimings.length; - log(` Cached calls (avg of 5): ${avgCached.toFixed(4)}ms`); - log(` ✅ ${(firstDuration / avgCached).toFixed(0)}x faster with caching!\n`); + await profileCaching(() => settings.setup.isComplete()); // Test session caching log("Testing session caching (10s TTL):\n"); @@ -152,28 +157,7 @@ const main = async () => { // Create a session await createSession("test-token", "test-csrf", Date.now() + 3600000); - // First call - queries DB and caches - const sessionStart1 = performance.now(); - await getSession("test-token"); - const sessionDuration1 = performance.now() - sessionStart1; - log(` First call (queries DB + caches): ${sessionDuration1.toFixed(2)}ms`); - - // Cached calls - const sessionTimings: number[] = []; - for (let i = 0; i < 5; i++) { - const start = performance.now(); - await getSession("test-token"); - const duration = performance.now() - start; - sessionTimings.push(duration); - } - const avgSession = - sessionTimings.reduce((a, b) => a + b, 0) / sessionTimings.length; - log(` Cached calls (avg of 5): ${avgSession.toFixed(4)}ms`); - log( - ` ✅ ${(sessionDuration1 / avgSession).toFixed( - 0, - )}x faster with caching!\n`, - ); + await profileCaching(() => getSession("test-token")); // Network latency reality check log("=".repeat(60)); diff --git a/scripts/project-root.ts b/scripts/project-root.ts index 8be544b341..03af164a70 100644 --- a/scripts/project-root.ts +++ b/scripts/project-root.ts @@ -1,4 +1,17 @@ -import { dirname, join } from "node:path"; +import { dirname, isAbsolute, join, relative, sep } from "node:path"; import { fileURLToPath } from "node:url"; export const projectRoot = join(dirname(fileURLToPath(import.meta.url)), ".."); + +/** + * Render `file` for display relative to `base`: strip a leading `./` from + * relative paths, and show absolute paths as a `base`-relative path unless they + * escape `base` (in which case the absolute path is kept). A file whose name + * merely starts with `..` (e.g. `..cache.ts`) does not escape. + */ +export const toDisplayPath = (base: string, file: string): string => { + if (!isAbsolute(file)) return file.replace(/^\.\//, ""); + const rel = relative(base, file); + const escapesBase = rel === ".." || rel.startsWith(`..${sep}`); + return escapesBase ? file : rel || "."; +}; diff --git a/scripts/run-build-edge.ts b/scripts/run-build-edge.ts new file mode 100644 index 0000000000..7e0407d431 --- /dev/null +++ b/scripts/run-build-edge.ts @@ -0,0 +1,16 @@ +import type { BuildResult } from "./deploy-edge-lib.ts"; + +/** + * Run `deno task build:edge` in `cwd`, inheriting stdio so the bundle build's + * output streams straight through. Returns the child's exit code and success. + */ +export const runBuildEdge = async (cwd: string): Promise => { + const build = await new Deno.Command(Deno.execPath(), { + args: ["task", "build:edge"], + cwd, + stderr: "inherit", + stdout: "inherit", + }).output(); + + return { code: build.code, success: build.success }; +}; diff --git a/scripts/run-tests.ts b/scripts/run-tests.ts index d42922952d..67e9a94623 100644 --- a/scripts/run-tests.ts +++ b/scripts/run-tests.ts @@ -8,8 +8,8 @@ import { COVERAGE_OUTPUT_DIR } from "./coverage-output.ts"; import { projectRoot } from "./project-root.ts"; -import { JUNIT_PATH, readSlowTestsReport } from "./test-durations.ts"; -import { runTests, withTestHarness } from "./test-harness.ts"; +import { readSlowTestsReport } from "./test-durations.ts"; +import { runSuiteWithHarness } from "./test-harness.ts"; type CoverageMetricFailure = { covered: number; @@ -66,16 +66,19 @@ const formatRanges = (nums: number[]): string => { const ranges: string[] = []; let start = sorted[0]!; let end = start; + const flushRange = (): void => { + ranges.push(start === end ? String(start) : `${start}-${end}`); + }; for (const line of sorted.slice(1)) { if (line === end + 1) { end = line; continue; } - ranges.push(start === end ? String(start) : `${start}-${end}`); + flushRange(); start = line; end = line; } - ranges.push(start === end ? String(start) : `${start}-${end}`); + flushRange(); return ranges.join(", "); }; @@ -206,20 +209,17 @@ const githubAnnotationEntries = ( ): { file: string; line: number; message: string }[] => { const entries: { file: string; line: number; message: string }[] = []; for (const failure of failures) { - for (const line of failure.lines?.uncovered ?? []) { - entries.push({ - file: failure.file, - line, - message: `line coverage missing at ${failure.file}:${line}`, - }); - } - for (const line of failure.branches?.uncovered ?? []) { - entries.push({ - file: failure.file, - line, - message: `branch coverage missing at ${failure.file}:${line}`, - }); - } + const pushUncovered = (kind: string, lines: number[] | undefined): void => { + for (const line of lines ?? []) { + entries.push({ + file: failure.file, + line, + message: `${kind} coverage missing at ${failure.file}:${line}`, + }); + } + }; + pushUncovered("line", failure.lines?.uncovered); + pushUncovered("branch", failure.branches?.uncovered); } return entries; }; @@ -320,12 +320,7 @@ const checkCoverage = async (): Promise => { /** Main: run the whole suite inside the harness, then enforce coverage */ const main = async (): Promise => { const useCoverage = Deno.args.includes("--coverage"); - // Remove any stale JUnit file so a killed run can't surface a previous run's - // timings; `deno test --junit-path` rewrites it on a completed run. - await Deno.remove(JUNIT_PATH).catch(() => {}); - const exitCode = await withTestHarness(() => - runTests(["test/"], useCoverage, JUNIT_PATH), - ); + const exitCode = await runSuiteWithHarness(useCoverage); if (exitCode !== 0) Deno.exit(exitCode); if (useCoverage) await checkCoverage(); diff --git a/scripts/safe-upgrade.ts b/scripts/safe-upgrade.ts index 9de479080a..a60709c9b2 100644 --- a/scripts/safe-upgrade.ts +++ b/scripts/safe-upgrade.ts @@ -132,17 +132,9 @@ async function checkUpgrade( let pkgName: string; let versionRange: string; - if (specifier.startsWith("npm:")) { - // npm:@scope/name@version or npm:name@version - const withoutPrefix = specifier.slice(4); - const atIdx = withoutPrefix.lastIndexOf("@"); - if (atIdx <= 0) { - result.error = "no version specifier found"; - return result; - } - pkgName = withoutPrefix.slice(0, atIdx); - versionRange = withoutPrefix.slice(atIdx + 1); - } else if (specifier.startsWith("jsr:")) { + // npm: and jsr: share the same `@scope/name@version` / `name@version` + // shape after their (equal-length) prefix, so both parse identically. + if (specifier.startsWith("npm:") || specifier.startsWith("jsr:")) { const withoutPrefix = specifier.slice(4); const atIdx = withoutPrefix.lastIndexOf("@"); if (atIdx <= 0) { diff --git a/scripts/stream-lines.ts b/scripts/stream-lines.ts new file mode 100644 index 0000000000..7756e782ed --- /dev/null +++ b/scripts/stream-lines.ts @@ -0,0 +1,42 @@ +/** + * Read a byte stream to completion, decoding as UTF-8 and returning the full + * text. When `onLine` is supplied, each newline-terminated line is delivered as + * it arrives (with the trailing partial line flushed at the end), so callers can + * render live progress while still receiving the accumulated text. + */ +export const readStream = async ( + stream: ReadableStream, + onLine?: (line: string) => void, +): Promise => { + const reader = stream.getReader(); + const decoder = new TextDecoder(); + let buffered = ""; + let text = ""; + + const flush = (chunk: string, final: boolean): void => { + text += chunk; + if (!onLine) return; + buffered += chunk; + if (final) { + if (buffered) onLine(buffered); + return; + } + const lines = buffered.split(/\r?\n/); + // `split` always yields at least one element, so `pop` is never undefined. + buffered = lines.pop()!; + for (const line of lines) onLine(line); + }; + + try { + while (true) { + const { done, value } = await reader.read(); + if (done) break; + flush(decoder.decode(value, { stream: true }), false); + } + } finally { + flush(decoder.decode(), true); + reader.releaseLock(); + } + + return text; +}; diff --git a/scripts/stripe-mock.ts b/scripts/stripe-mock.ts index b19a8767d1..3209fba139 100644 --- a/scripts/stripe-mock.ts +++ b/scripts/stripe-mock.ts @@ -12,6 +12,7 @@ import { stopProcess, stopProcessNow } from "./process.ts"; import { defaultStripeMockPaths, downloadStripeMock, + ensureBinDir, type StripeMockCommands, type StripeMockInstallOptions, type StripeMockPaths, @@ -223,8 +224,9 @@ const withStripeMockStartLock = async ( paths: StripeMockPaths, body: () => Promise, ): Promise => { - await Deno.mkdir(paths.binDir, { recursive: true }); - const lock = await Deno.open(stripeMockStartLockPath(paths), { + const lockPath = stripeMockStartLockPath(paths); + await ensureBinDir(paths); + const lock = await Deno.open(lockPath, { create: true, read: true, write: true, diff --git a/scripts/stripe-mock/install.ts b/scripts/stripe-mock/install.ts index 1c65f50791..0a581d3c47 100644 --- a/scripts/stripe-mock/install.ts +++ b/scripts/stripe-mock/install.ts @@ -1,4 +1,5 @@ import { join } from "node:path"; +import { rethrowUnlessNotFound } from "../not-found.ts"; import { projectRoot } from "../project-root.ts"; const STRIPE_MOCK_VERSION = "0.188.0"; @@ -23,6 +24,10 @@ export type StripeMockPaths = { binaryPath: string; }; +/** Ensure the bin directory (and any parents) exists. */ +export const ensureBinDir = (paths: StripeMockPaths): Promise => + Deno.mkdir(paths.binDir, { recursive: true }); + export type StripeMockCommands = { chmod: string; curl: string; @@ -194,7 +199,7 @@ const removeLockIfOwned = async ( await Deno.remove(lockPath); } } catch (error) { - if (!(error instanceof Deno.errors.NotFound)) throw error; + rethrowUnlessNotFound(error); } }; @@ -269,9 +274,9 @@ const withInstallLock = async ( options: StripeMockInstallOptions, body: () => Promise, ): Promise => { - await Deno.mkdir(paths.binDir, { recursive: true }); const lockPath = installLockPath(paths); const settings = installLockSettings(options); + await ensureBinDir(paths); const lock = await acquireInstallLock(lockPath, settings); const stopRefreshingLock = startInstallLockRefresh( lockPath, diff --git a/scripts/test-durations.ts b/scripts/test-durations.ts index d70a1534e5..fc959edd47 100644 --- a/scripts/test-durations.ts +++ b/scripts/test-durations.ts @@ -18,9 +18,10 @@ * they locate the slow area without losing either signal — so no test slower * than the threshold is ever dropped. */ -import { dirname, isAbsolute, join, relative } from "node:path"; +import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { filter, mapNotNullish, pipe, sort } from "#fp"; +import { toDisplayPath } from "./project-root.ts"; const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url)); const PROJECT_ROOT = join(SCRIPT_DIR, ".."); @@ -66,12 +67,6 @@ const toDurationMs = (time: string | undefined): number | undefined => { const isUserFile = (classname: string): boolean => !/^(?:ext:|https?:|node:)/.test(classname); -const toDisplayPath = (file: string): string => { - if (!isAbsolute(file)) return file.replace(/^\.\//, ""); - const rel = relative(PROJECT_ROOT, file); - return rel.startsWith("..") ? file : rel; -}; - /** Parse JUnit XML into per-test durations (one entry per ``). */ export const parseJunitDurations = (xml: string): TestDuration[] => mapNotNullish((match) => { @@ -81,7 +76,9 @@ export const parseJunitDurations = (xml: string): TestDuration[] => if (name === undefined || durationMs === undefined) return undefined; const classname = attrs.classname; const file = - classname && isUserFile(classname) ? toDisplayPath(classname) : ""; + classname && isUserFile(classname) + ? toDisplayPath(PROJECT_ROOT, classname) + : ""; const lineText = attrs.line; const line = lineText ? Number(lineText) : undefined; return { durationMs, file, line, name }; diff --git a/scripts/test-harness.ts b/scripts/test-harness.ts index 0a49867ef1..77a8bde17d 100644 --- a/scripts/test-harness.ts +++ b/scripts/test-harness.ts @@ -24,8 +24,10 @@ import { COVERAGE_OUTPUT_DIR, removeOldCoverageOutput, } from "./coverage-output.ts"; +import { rethrowUnlessNotFound } from "./not-found.ts"; import { projectRoot } from "./project-root.ts"; import { startStripeMock, stripeMockEnv } from "./stripe-mock.ts"; +import { JUNIT_PATH } from "./test-durations.ts"; const verboseHarness = Deno.env.get("TICKETS_TEST_HARNESS_VERBOSE") === "1"; @@ -170,3 +172,18 @@ export const withTestHarness = async ( await cleanupStaticAssets(); } }; + +/** + * Run the whole `test/` suite inside the harness, emitting per-test timings to + * the shared JUnit path. Any stale JUnit file is removed first so a killed prior + * run can't surface its timings; `deno test --junit-path` rewrites it on a + * completed run. + */ +export const runSuiteWithHarness = async ( + useCoverage: boolean, +): Promise => { + // Only a missing file is expected here; a real removal failure (e.g. + // permissions) must surface rather than leave a stale JUnit file behind. + await Deno.remove(JUNIT_PATH).catch(rethrowUnlessNotFound); + return withTestHarness(() => runTests(["test/"], useCoverage, JUNIT_PATH)); +}; diff --git a/scripts/test-quality-audit.ts b/scripts/test-quality-audit.ts index 1d779d3b1a..db6708f951 100644 --- a/scripts/test-quality-audit.ts +++ b/scripts/test-quality-audit.ts @@ -1,6 +1,8 @@ #!/usr/bin/env -S deno run --allow-read import { relative } from "node:path"; +import { lineColumnAt } from "./line-column.ts"; +import { walkFiles } from "./walk-files.ts"; type Finding = { column: number; @@ -35,12 +37,6 @@ const WEAK_ASSERTION_PATTERNS: { message: string; pattern: RegExp }[] = [ }, ]; -const lineColumnAt = (content: string, index: number) => { - const before = content.slice(0, index); - const lines = before.split("\n"); - return { column: lines.at(-1)!.length + 1, line: lines.length }; -}; - const testBlockRanges = (content: string): { end: number; start: number }[] => { const ranges: { end: number; start: number }[] = []; // Match `test(`/`it(` declarations and `Deno.test(`, but not predicate method @@ -98,22 +94,14 @@ const auditFile = async (path: string): Promise => { ]; }; -const collectTestFiles = async (directory: string): Promise => { +const testFiles = async (): Promise => { const files: string[] = []; - for await (const entry of Deno.readDir(directory)) { - const path = `${directory}/${entry.name}`; - if (entry.isDirectory) { - files.push(...(await collectTestFiles(path))); - continue; - } - if (entry.isFile && TEST_FILE_PATTERN.test(path)) files.push(path); + for await (const path of walkFiles("test")) { + if (TEST_FILE_PATTERN.test(path)) files.push(path); } - return files; + return files.sort(); }; -const testFiles = async (): Promise => - (await collectTestFiles("test")).sort(); - const formatFinding = (finding: Finding): string => `${relative( Deno.cwd(), diff --git a/scripts/unit-tests-report.ts b/scripts/unit-tests-report.ts index 621a3aca85..f1434b836f 100644 --- a/scripts/unit-tests-report.ts +++ b/scripts/unit-tests-report.ts @@ -25,17 +25,7 @@ import { isTestPath, toJsonReport, } from "./unit-tests-report-lib.ts"; - -async function* walkFiles(directory: string): AsyncGenerator { - for await (const entry of Deno.readDir(directory)) { - const path = `${directory}/${entry.name}`; - if (entry.isDirectory) { - yield* walkFiles(path); - continue; - } - yield path; - } -} +import { walkFiles } from "./walk-files.ts"; const collect = async ( root: string, diff --git a/scripts/walk-files.ts b/scripts/walk-files.ts new file mode 100644 index 0000000000..15a52a4067 --- /dev/null +++ b/scripts/walk-files.ts @@ -0,0 +1,11 @@ +/** Recursively yield every file path beneath `directory` (depth-first). */ +export async function* walkFiles(directory: string): AsyncGenerator { + for await (const entry of Deno.readDir(directory)) { + const path = `${directory}/${entry.name}`; + if (entry.isDirectory) { + yield* walkFiles(path); + continue; + } + yield path; + } +} diff --git a/test/scripts/mutation-step.test.ts b/test/scripts/mutation-step.test.ts index 67b5c8909d..e343a7600c 100644 --- a/test/scripts/mutation-step.test.ts +++ b/test/scripts/mutation-step.test.ts @@ -1,9 +1,6 @@ import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; -import type { - CommandResult, - RunCommand, -} from "../../scripts/precommit/merge-warning.ts"; +import type { CommandResult, RunCommand } from "../../scripts/precommit/git.ts"; import { type ChangedFiles, changedFiles, diff --git a/test/scripts/precommit.test.ts b/test/scripts/precommit.test.ts index 2cf8756381..a32bd1e545 100644 --- a/test/scripts/precommit.test.ts +++ b/test/scripts/precommit.test.ts @@ -2,10 +2,12 @@ import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { type CommandResult, - getMergeConflictWarning, - parseMergeTreeConflictedPaths, runCommand, runInteractiveCommand, +} from "../../scripts/precommit/git.ts"; +import { + getMergeConflictWarning, + parseMergeTreeConflictedPaths, } from "../../scripts/precommit/merge-warning.ts"; import { formatPushPrompt, diff --git a/test/scripts/project-root.test.ts b/test/scripts/project-root.test.ts new file mode 100644 index 0000000000..4c626a05ad --- /dev/null +++ b/test/scripts/project-root.test.ts @@ -0,0 +1,33 @@ +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { toDisplayPath } from "../../scripts/project-root.ts"; + +describe("toDisplayPath", () => { + test("strips a leading ./ from a relative path", () => { + expect(toDisplayPath("/base", "./src/index.ts")).toBe("src/index.ts"); + }); + + test("leaves a bare relative path unchanged", () => { + expect(toDisplayPath("/base", "src/index.ts")).toBe("src/index.ts"); + }); + + test("shows an absolute path inside the base as base-relative", () => { + expect(toDisplayPath("/base", "/base/src/index.ts")).toBe("src/index.ts"); + }); + + test("renders the base itself as '.'", () => { + expect(toDisplayPath("/base", "/base")).toBe("."); + }); + + test("keeps an absolute path that escapes the base", () => { + expect(toDisplayPath("/base/sub", "/other/x.ts")).toBe("/other/x.ts"); + }); + + test("keeps the parent directory itself (relative '..')", () => { + expect(toDisplayPath("/base/sub", "/base")).toBe("/base"); + }); + + test("does not treat a '..'-prefixed filename as escaping", () => { + expect(toDisplayPath("/base", "/base/..cache.ts")).toBe("..cache.ts"); + }); +}); diff --git a/test/scripts/stream-lines.test.ts b/test/scripts/stream-lines.test.ts new file mode 100644 index 0000000000..61e3ef3799 --- /dev/null +++ b/test/scripts/stream-lines.test.ts @@ -0,0 +1,32 @@ +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { readStream } from "../../scripts/stream-lines.ts"; + +const streamOf = (...chunks: string[]): ReadableStream => { + const encoder = new TextEncoder(); + return ReadableStream.from(chunks.map((chunk) => encoder.encode(chunk))); +}; + +describe("readStream", () => { + test("returns the full text and delivers each line, flushing the tail", async () => { + const lines: string[] = []; + const text = await readStream(streamOf("a\nb", "c\nd"), (line) => + lines.push(line), + ); + + expect(text).toBe("a\nbc\nd"); + // "b" and "c" arrive in separate chunks but belong to one line. + expect(lines).toEqual(["a", "bc", "d"]); + }); + + test("accumulates text without a line callback", async () => { + const text = await readStream(streamOf("hello ", "world")); + expect(text).toBe("hello world"); + }); + + test("does not emit a trailing empty line when input ends in a newline", async () => { + const lines: string[] = []; + await readStream(streamOf("x\n"), (line) => lines.push(line)); + expect(lines).toEqual(["x"]); + }); +});