Files
reverse-engineering-anything/tests/fixtures/temporaryDirectory.ts
T
David Muñoz Rey 559956cf6b refactor(inspector): own the Node discovery path rule in one module (#10680)
Node Inspector discovery reports a target as `file://` plus the pathname,
with quotes and backslashes replaced by underscores. Whether such a
pathname is identifiable was decided twice: inline in
authorizeRuntimeTargetLocation and again as NODE_DISCOVERY_AMBIGUOUS in
the createNodeDiscoveryTempDirectory fixture. The copies could drift, and
the fixture could not import the product rule without loading
JavaScriptRuntimeScope and its Electron file-scope chain.

Move the rule to src/inspector/nodeDiscoveryPath.ts, a module with no
imports, and use it from both places. Behavior is unchanged. A unit test
covers the Windows branch, which Linux CI cannot reach through the
boundary tests.

Validation: nodeDiscoveryPath and cancellation tests 12/12;
javascriptRuntimeTargetLocations and javascriptRuntimeObservation 42/42
with TMPDIR containing an underscore (exercises the /tmp fallback);
check:fast passes.

Refs morluto/rea#9996
2026-11-21 20:59:38 +01:00

69 lines
2.7 KiB
TypeScript

import { mkdtemp, realpath } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { onTestFinished } from "vitest";
import { normalizeTestTemporaryPrefix } from "../../scripts/lib/test-temporary-ownership.mjs";
import { isLosslessNodeDiscoveryPath } from "../../src/inspector/nodeDiscoveryPath.js";
import { removeTestWorkspace } from "../support/workspace/workspaceFixture.js";
/**
* Creates an empty canonical temporary directory owned by the current Vitest
* case.
*
* This deliberately does not build a workspace first. It used to call
* `createTestWorkspace`, which creates `home` and `xdg/{config,cache}`, then
* immediately delete two of them so callers would receive an empty root. Every
* caller therefore paid three `mkdir` calls and two `rm` calls per temporary
* directory for scaffolding it never used. Callers that do need a home or XDG
* tree should call `createTestWorkspace` directly.
*/
export const createTestTempDirectory = async (
prefix: string,
): Promise<string> => {
const canonicalDirectory = await realpath(
await mkdtemp(
join(await realpath(tmpdir()), normalizeTestTemporaryPrefix(prefix)),
),
);
onTestFinished(async () => {
await removeTestWorkspace(canonicalDirectory);
});
return canonicalDirectory;
};
/**
* Like `createTestTempDirectory`, for fixtures whose paths Node reports through
* Inspector discovery metadata.
*
* Node replaces `"` and `\` with `_` in that metadata, so
* `authorizeRuntimeTargetLocation` never stats a reported POSIX path containing
* `_`, `"` or `\`. A temporary root such as macOS's per-user
* `/private/var/folders/<x>/<y>/T`, where `<y>` often contains `_`, would make
* every fixture unverifiable. On POSIX this uses the canonical temporary
* directory when it is free of those characters, otherwise the canonical
* `/tmp`, and fails when neither is. Windows keeps `createTestTempDirectory`:
* Node discovery paths are never verified there.
*/
export const createNodeDiscoveryTempDirectory = async (
prefix: string,
): Promise<string> => {
if (process.platform === "win32") return createTestTempDirectory(prefix);
const primary = await realpath(tmpdir());
const base = isLosslessNodeDiscoveryPath(primary, process.platform)
? primary
: await realpath("/tmp");
const canonicalDirectory = await realpath(
await mkdtemp(join(base, normalizeTestTemporaryPrefix(prefix))),
);
onTestFinished(async () => {
await removeTestWorkspace(canonicalDirectory);
});
if (!isLosslessNodeDiscoveryPath(canonicalDirectory, process.platform))
throw new Error(
`No temporary directory free of '_', '"' and '\\' for Node discovery fixtures: ${canonicalDirectory}`,
);
return canonicalDirectory;
};