testBuildLeases
tcmts.cli.web.testBuildLeases · #33gzqynz · term
import { clearLeftovers, heldBuilds, leaseBuilds, ownId } from "#codebase/tcmts/cli/web";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
export const testBuildLeases = () => {
const work = fs.mkdtempSync(path.join(os.tmpdir(), "tcm-leases-"));
try {
const a = leaseBuilds(work);
const b = leaseBuilds(work);
a.hold({ builds: ["x", "x"] });
b.hold({ builds: ["y"], assets: ["y.js"] });
a.hold({ assets: ["x.js"] });
assert.deepEqual(a.builds, ["x"]);
assert.deepEqual(heldBuilds(work), {
builds: new Set(["x", "y"]),
assets: new Set(["x.js", "y.js"]),
});
b.release();
assert.deepEqual(heldBuilds(work), { builds: new Set(["x"]), assets: new Set(["x.js"]) });
// a process that has ended holds nothing: what it left is cleared
const ended = spawnSync(process.execPath, ["-e", ""]).pid;
fs.writeFileSync(
path.join(work, `lease.${ended}-0123abcd.json`),
JSON.stringify({ builds: ["z"] }),
);
fs.mkdirSync(path.join(work, `h.${ended}-0123abcd`));
fs.mkdirSync(path.join(work, `h.${ownId()}`));
fs.mkdirSync(path.join(work, "other"));
assert.deepEqual(heldBuilds(work).builds, new Set(["x"]));
clearLeftovers(work);
assert.deepEqual(
fs
.readdirSync(work)
.filter((e) => !e.startsWith("lease."))
.map((e) => e.replace(/\d+-[0-9a-f]{8}/, "mine")),
["h.mine", "other"],
);
a.release();
assert.deepEqual(
fs.readdirSync(work).filter((e) => e.startsWith("lease.")),
[],
);
} finally {
fs.rmSync(work, { recursive: true, force: true });
}
}
uses