test(artifacts): retire simulated mounts after detach failures

This commit is contained in:
morluto
2026-10-09 04:12:49 +08:00
parent a0784c0d2c
commit f591e37a1e
@@ -77,6 +77,11 @@ describe("native DMG artifact reader", () => {
),
});
let mountRoot: string | undefined;
onTestFinished(async () => {
if (mountRoot !== undefined)
await rm(mountRoot, { recursive: true, force: true });
});
const host: NativeDmgHost = {
run(arguments_) {
if (arguments_[0] === "detach")
@@ -92,6 +97,8 @@ describe("native DMG artifact reader", () => {
});
if (arguments_[0] !== "attach")
return Promise.resolve({ stdout: "", exitCode: 0 });
mountRoot = arguments_[arguments_.indexOf("-mountroot") + 1];
if (mountRoot === undefined) throw new Error("missing mount root");
return Promise.resolve({
stdout: build({
"system-entities": [
@@ -387,53 +394,58 @@ it("owns the canonical mount root and detaches each observed whole image once",
]);
});
describe("APFS disk image detach", () => {
// hdiutil reports the image disk and its synthesized APFS container as two
// whole disks; detaching the container ejects the image disk as well.
const apfsHost = (stillListed: readonly string[]) => {
const calls: string[][] = [];
const host: NativeDmgHost = {
async run(arguments_) {
calls.push([...arguments_]);
if (arguments_[0] === "detach" && arguments_[1] === "/dev/disk4")
throw new Error("hdiutil: detach failed - No such file or directory");
if (arguments_[0] === "info")
return {
stdout: build({
images:
stillListed.length === 0
? []
: [
{
"system-entities": stillListed.map((device) => ({
"dev-entry": device,
})),
},
],
}),
exitCode: 0,
};
if (arguments_[0] !== "attach") return { stdout: "", exitCode: 0 };
const mountRoot = arguments_[arguments_.indexOf("-mountroot") + 1];
if (mountRoot === undefined) throw new Error("missing mount root");
const mountPoint = join(mountRoot, "Fixture");
await mkdir(mountPoint);
// hdiutil reports the image disk and synthesized APFS container separately;
// detaching the container can already eject the image disk.
const apfsHost = (stillListed: readonly string[]) => {
const calls: string[][] = [];
let mountRoot: string | undefined;
onTestFinished(async () => {
if (mountRoot !== undefined)
await rm(mountRoot, { recursive: true, force: true });
});
const host: NativeDmgHost = {
async run(arguments_) {
calls.push([...arguments_]);
if (arguments_[0] === "detach" && arguments_[1] === "/dev/disk4")
throw new Error("hdiutil: detach failed - No such file or directory");
if (arguments_[0] === "info")
return {
stdout: build({
"system-entities": [
{ "dev-entry": "/dev/disk4" },
{ "dev-entry": "/dev/disk4s1" },
{ "dev-entry": "/dev/disk5s1", "mount-point": mountPoint },
{ "dev-entry": "/dev/disk5" },
],
images:
stillListed.length === 0
? []
: [
{
"system-entities": stillListed.map((device) => ({
"dev-entry": device,
})),
},
],
}),
exitCode: 0,
};
},
};
return { calls, host };
if (arguments_[0] !== "attach") return { stdout: "", exitCode: 0 };
mountRoot = arguments_[arguments_.indexOf("-mountroot") + 1];
if (mountRoot === undefined) throw new Error("missing mount root");
const mountPoint = join(mountRoot, "Fixture");
await mkdir(mountPoint);
return {
stdout: build({
"system-entities": [
{ "dev-entry": "/dev/disk4" },
{ "dev-entry": "/dev/disk4s1" },
{ "dev-entry": "/dev/disk5s1", "mount-point": mountPoint },
{ "dev-entry": "/dev/disk5" },
],
}),
exitCode: 0,
};
},
};
return { calls, host };
};
describe("APFS disk image detach", () => {
it("accepts an image disk that its container detach already ejected", async () => {
const { calls, host } = apfsHost([]);
const reader = await NativeDmgArtifactReader.create(