|
| 1 | +import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 2 | +import { dirname, join, relative } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { expect, it } from "vitest"; |
| 5 | + |
| 6 | +const packagesDir = join(dirname(fileURLToPath(import.meta.url)), "..", ".."); |
| 7 | +const SOURCE = /\.(ts|mjs)$/; |
| 8 | +const NOT_SOURCE = /\.(test|fixture)\.|[\\/](tests?|node_modules|dist)[\\/]/; |
| 9 | +const MKDTEMP = /\bmkdtemp(Sync)?\(/; |
| 10 | + |
| 11 | +// Not beside a project or output path: a system temp root, a standalone skill script, a cloud function's scratch root. |
| 12 | +const ALLOWED = new Set([ |
| 13 | + "cli/src/media-use/lib/lut-preset-provider.mjs", |
| 14 | + "producer/src/server.ts", |
| 15 | + "producer/src/regression-harness.ts", |
| 16 | + "producer/src/services/render/renderDirOwner.ts", |
| 17 | + "aws-lambda/src/s3PlanV2Publisher.ts", |
| 18 | + "gcp-cloud-run/src/gcsPlanV2Publisher.ts", |
| 19 | +]); |
| 20 | + |
| 21 | +function sources(dir: string): string[] { |
| 22 | + return readdirSync(dir).flatMap((name) => { |
| 23 | + const path = join(dir, name); |
| 24 | + if (NOT_SOURCE.test(path)) return []; |
| 25 | + return statSync(path).isDirectory() ? sources(path) : SOURCE.test(name) ? [path] : []; |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +it("no package makes a staging folder beside a project path with mkdtemp, which fails past 260 characters on Windows", () => { |
| 30 | + const offenders = readdirSync(packagesDir).flatMap((pkg) => { |
| 31 | + const src = join(packagesDir, pkg, "src"); |
| 32 | + try { |
| 33 | + statSync(src); |
| 34 | + } catch { |
| 35 | + return []; |
| 36 | + } |
| 37 | + return sources(src).flatMap((file) => { |
| 38 | + const rel = relative(packagesDir, file).split("\\").join("/"); |
| 39 | + if (ALLOWED.has(rel)) return []; |
| 40 | + return readFileSync(file, "utf8") |
| 41 | + .split("\n") |
| 42 | + .flatMap((line, i) => |
| 43 | + MKDTEMP.test(line) && !/tmpdir|^\s*(\/\/|\*)/.test(line) ? [`${rel}:${i + 1}`] : [], |
| 44 | + ); |
| 45 | + }); |
| 46 | + }); |
| 47 | + expect(offenders, "use createStagingDir from @hyperframes/core/atomic-file").toEqual([]); |
| 48 | +}); |
0 commit comments