Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(tests): preserve temporary fixture ownership

gensokyo-dev093ce695f0a7
diff --git a/docs/testing.md b/docs/testing.md
index 1987abe58..6a0b7984a 100644
--- a/docs/testing.md
+++ b/docs/testing.md
@@ -355,6 +355,11 @@ The process-global Vitest configuration contract rejects new direct temporary-ro
creation outside the workspace seam and its narrowly documented boundary/package
exceptions.
+The shared temporary-directory and workspace helpers preserve caller labels
+under an owned `rea-` prefix when needed. `npm run verify:test-temp-hygiene`
+uses the same ownership rule to find retained fixtures under a private temporary
+root. Third-party caches remain outside that ownership rule.
+
Real Hopper, Ghidra, IDA, browser, package, and managed-code claims belong to their
explicit `npm run verify:*` lanes. The reconstruction-readiness lane also
checks deterministic rerun, tamper, and stale-input handling; those checks do
diff --git a/scripts/lib/test-temporary-ownership.d.mts b/scripts/lib/test-temporary-ownership.d.mts
new file mode 100644
index 000000000..9005d4d62
--- /dev/null
+++ b/scripts/lib/test-temporary-ownership.d.mts
@@ -0,0 +1,2 @@
+export function isOwnedTestTemporaryName(name: string): boolean;
+export function normalizeTestTemporaryPrefix(prefix: string): string;
diff --git a/scripts/lib/test-temporary-ownership.mjs b/scripts/lib/test-temporary-ownership.mjs
new file mode 100644
index 000000000..80cd209e7
--- /dev/null
+++ b/scripts/lib/test-temporary-ownership.mjs
@@ -0,0 +1,18 @@
+const OWNED_ENTRY = /^(?:rea-|bb-session-)/u;
+const SAFE_PREFIX = /^[A-Za-z0-9][A-Za-z0-9._-]*-$/u;
+
+/** Recognize test resources independently of their readiness or contents. */
+export const isOwnedTestTemporaryName = (name) => OWNED_ENTRY.test(name);
+
+/** Preserve caller labels while making every shared fixture root recognizable. */
+export function normalizeTestTemporaryPrefix(prefix) {
+ if (
+ typeof prefix !== "string" ||
+ !SAFE_PREFIX.test(prefix) ||
+ prefix.length > 80
+ )
+ throw new TypeError(
+ "Test temporary directory prefixes must be safe basenames ending in '-'",
+ );
+ return isOwnedTestTemporaryName(prefix) ? prefix : `rea-${prefix}`;
+}
diff --git a/scripts/verify-test-temp-hygiene.mjs b/scripts/verify-test-temp-hygiene.mjs
index cdfdb9fa9..cdd7a47db 100644
--- a/scripts/verify-test-temp-hygiene.mjs
+++ b/scripts/verify-test-temp-hygiene.mjs
@@ -4,15 +4,16 @@ import { dirname, join, relative, resolve } from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
+import { isOwnedTestTemporaryName } from "./lib/test-temporary-ownership.mjs";
+
const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const vitestPath = resolve(repositoryRoot, "node_modules/vitest/vitest.mjs");
-const OWNED_ENTRY = /^(?:rea-|bb-session-)/u;
const findOwnedEntries = async (root, directory = root) => {
const owned = [];
for (const entry of await readdir(directory, { withFileTypes: true })) {
const path = join(directory, entry.name);
- if (OWNED_ENTRY.test(entry.name)) {
+ if (isOwnedTestTemporaryName(entry.name)) {
owned.push(relative(root, path));
continue;
}
diff --git a/tests/boundary/filesystem/testTempHygiene.test.ts b/tests/boundary/filesystem/testTempHygiene.test.ts
new file mode 100644
index 000000000..298dc5b51
--- /dev/null
+++ b/tests/boundary/filesystem/testTempHygiene.test.ts
@@ -0,0 +1,97 @@
+import { copyFile, mkdir, readFile, stat, writeFile } from "node:fs/promises";
+import { basename, join } from "node:path";
+import { pathToFileURL } from "node:url";
+
+import { expect, it } from "vitest";
+
+import { execFileOutput } from "../../../src/process/ExecFileOutput.js";
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+import {
+ createTestWorkspace,
+ removeTestWorkspace,
+} from "../../support/workspace/workspaceFixture.js";
+
+it("preserves caller labels and recognizable ownership in both shared factories", async () => {
+ for (const prefix of ["reference-unicode-", "rea-example-", "bb-session-"]) {
+ const expected = prefix === "reference-unicode-" ? `rea-${prefix}` : prefix;
+ const directory = await createTestTempDirectory(prefix);
+ expect(basename(directory).startsWith(expected)).toBe(true);
+ const workspace = await createTestWorkspace(prefix);
+ try {
+ expect(basename(workspace.root).startsWith(expected)).toBe(true);
+ } finally {
+ await removeTestWorkspace(workspace.root);
+ }
+ }
+});
+
+it.each(["clean", "file", "directory", "cache", "symlink", "failed-test"])(
+ "verifies shared fixture ownership with a %s producer",
+ async (scenario) => {
+ const root = await createTestTempDirectory("rea-test-hygiene-boundary-");
+ await mkdir(join(root, "scripts/lib"), { recursive: true });
+ await mkdir(join(root, "node_modules/vitest"), { recursive: true });
+ for (const path of [
+ "scripts/verify-test-temp-hygiene.mjs",
+ "scripts/lib/test-temporary-ownership.mjs",
+ ])
+ await copyFile(join(process.cwd(), path), join(root, path));
+ await mkdir(join(root, "external"));
+ await writeFile(join(root, "external", "evidence"), "preserved");
+ const workspaceModule = pathToFileURL(
+ join(process.cwd(), "tests/support/workspace/workspaceFixture.ts"),
+ ).href;
+ // The real shared factory produces the fixture. A stand-in runner controls
+ // its cleanup outcome and a foreign cache without running a nested suite.
+ await writeFile(
+ join(root, "node_modules/vitest/vitest.mjs"),
+ `import { mkdir, symlink, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { createTestWorkspace, removeTestWorkspace } from ${JSON.stringify(workspaceModule)};
+await writeFile("temporary-root.txt", tmpdir());
+const { root } = await createTestWorkspace("reference-unicode-");
+await writeFile("owned-root.txt", root);
+const scenario = ${JSON.stringify(scenario)};
+if (["clean", "file", "cache", "symlink"].includes(scenario)) await removeTestWorkspace(root);
+if (scenario === "file") await writeFile(root, "retained");
+if (scenario === "cache") await mkdir(join(tmpdir(), "tsx-fixture-cache"));
+if (scenario === "symlink")
+ await symlink(join(process.cwd(), "external"), root, process.platform === "win32" ? "junction" : "dir");
+if (scenario === "failed-test") process.exitCode = 7;
+`,
+ );
+ const result = execFileOutput(
+ process.execPath,
+ ["scripts/verify-test-temp-hygiene.mjs"],
+ {
+ cwd: root,
+ stopSignal: "SIGTERM",
+ timeout: 10_000,
+ maxBuffer: 64 * 1024,
+ },
+ );
+ if (["clean", "cache"].includes(scenario))
+ await expect(result).resolves.toMatchObject({
+ stdout: expect.stringContaining("no owned residues remain"),
+ });
+ else
+ await expect(result).rejects.toMatchObject({
+ code: 1,
+ stderr:
+ scenario === "failed-test"
+ ? expect.stringMatching(/status 7[\s\S]*rea-reference-unicode-/u)
+ : expect.stringContaining("rea-reference-unicode-"),
+ });
+ const ownedRoot = await readFile(join(root, "owned-root.txt"), "utf8");
+ expect(basename(ownedRoot).startsWith("rea-reference-unicode-")).toBe(true);
+ const temporaryRoot = await readFile(
+ join(root, "temporary-root.txt"),
+ "utf8",
+ );
+ await expect(stat(temporaryRoot)).rejects.toMatchObject({ code: "ENOENT" });
+ expect(await readFile(join(root, "external", "evidence"), "utf8")).toBe(
+ "preserved",
+ );
+ },
+);
diff --git a/tests/fixtures/temporaryDirectory.ts b/tests/fixtures/temporaryDirectory.ts
index 87c8a1a85..b6405eb71 100644
--- a/tests/fixtures/temporaryDirectory.ts
+++ b/tests/fixtures/temporaryDirectory.ts
@@ -4,10 +4,9 @@ import { join } from "node:path";
import { onTestFinished } from "vitest";
+import { normalizeTestTemporaryPrefix } from "../../scripts/lib/test-temporary-ownership.mjs";
import { removeTestWorkspace } from "../support/workspace/workspaceFixture.js";
-const TEMPORARY_PREFIX = /^[A-Za-z0-9][A-Za-z0-9._-]*-$/u;
-
/**
* Creates an empty canonical temporary directory owned by the current Vitest
* case.
@@ -22,14 +21,10 @@ const TEMPORARY_PREFIX = /^[A-Za-z0-9][A-Za-z0-9._-]*-$/u;
export const createTestTempDirectory = async (
prefix: string,
): Promise<string> => {
- if (!TEMPORARY_PREFIX.test(prefix) || prefix.length > 80) {
- throw new TypeError(
- "Test temporary directory prefixes must be safe basenames ending in '-'",
- );
- }
-
const canonicalDirectory = await realpath(
- await mkdtemp(join(await realpath(tmpdir()), prefix)),
+ await mkdtemp(
+ join(await realpath(tmpdir()), normalizeTestTemporaryPrefix(prefix)),
+ ),
);
onTestFinished(async () => {
await removeTestWorkspace(canonicalDirectory);
diff --git a/tests/support/workspace/workspaceFixture.ts b/tests/support/workspace/workspaceFixture.ts
index e59929951..b742a90c0 100644
--- a/tests/support/workspace/workspaceFixture.ts
+++ b/tests/support/workspace/workspaceFixture.ts
@@ -2,6 +2,8 @@ import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
+import { normalizeTestTemporaryPrefix } from "../../../scripts/lib/test-temporary-ownership.mjs";
+
/** An isolated filesystem and environment projection owned by one test. */
export interface TestWorkspace {
readonly root: string;
@@ -28,7 +30,9 @@ export const createTestWorkspace = async (
): Promise<TestWorkspace> => {
const canonicalTemporaryRoot = await realpath(tmpdir());
const root = await realpath(
- await mkdtemp(join(canonicalTemporaryRoot, prefix)),
+ await mkdtemp(
+ join(canonicalTemporaryRoot, normalizeTestTemporaryPrefix(prefix)),
+ ),
);
const home = join(root, "home");
const xdgConfigHome = join(root, "xdg", "config");