fix(artifacts): close mounted directory resources before DMG detach

Wait for directory operations, close owned streams and traversal handles, and retain the mounted reader when cleanup fails so close can retry before detach. Consolidate stable-artifact filesystem regressions into the boundary suite.

Validation: 35 artifact tests and temporary-directory hygiene, including real hdiutil diagnostics. Directory-close and traversal races use injected faults.
This commit is contained in:
morluto
2026-10-12 05:24:15 +08:00
parent c2407689cb
commit 2260887f20
4 changed files with 319 additions and 96 deletions
@@ -1,4 +1,11 @@
import { rename, symlink, writeFile } from "node:fs/promises";
import {
lstat,
open,
rename,
symlink,
unlink,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { readStableArtifact } from "../../../src/artifacts/readStableArtifact.js";
@@ -28,3 +35,43 @@ it("reads exact binary and empty bytes and rejects symlink/size/cancellation bou
controller.signal.reason,
);
});
const checkChange = async (
stage: "before-open" | "after-read" | "symlink-replacement",
): Promise<void> => {
const root = await createTestTempDirectory("rea-stable-read-");
const path = join(root, "selected");
await writeFile(path, "original evidence");
let inspected = 0;
const reading = readStableArtifact(path, 1024, undefined, {
async lstat(selected) {
inspected++;
if (stage === "after-read" && inspected === 2) await unlink(selected);
const metadata = await lstat(selected, { bigint: true });
if (inspected === 1 && stage !== "after-read") {
await unlink(selected);
if (stage === "symlink-replacement") await symlink("missing", selected);
}
return metadata;
},
open,
});
await expect(reading).rejects.toMatchObject({ reason: "integrity" });
};
it.each(["before-open", "after-read"] as const)(
"retains an actual post-validation %s change as integrity failure",
checkChange,
);
it.skipIf(process.platform === "win32")(
"retains a post-validation symlink replacement as an integrity failure",
() => checkChange("symlink-replacement"),
);
it("keeps initially missing inputs distinguishable from later changes", async () => {
const root = await createTestTempDirectory("rea-stable-missing-");
await expect(
readStableArtifact(join(root, "missing"), 1024),
).rejects.toMatchObject({ code: "ENOENT" });
});