test: stabilize exec approvals homedir

This commit is contained in:
Peter Steinberger
2026-01-21 10:49:01 +00:00
parent 4e4f5558fc
commit cdb35c3aae

View File

@@ -2,7 +2,7 @@ import fs from "node:fs";
import os from "node:os"; import os from "node:os";
import path from "node:path"; import path from "node:path";
import { describe, expect, it } from "vitest"; import { describe, expect, it, vi } from "vitest";
import { import {
matchAllowlist, matchAllowlist,
@@ -111,32 +111,33 @@ describe("exec approvals policy helpers", () => {
describe("exec approvals wildcard agent", () => { describe("exec approvals wildcard agent", () => {
it("merges wildcard allowlist entries with agent entries", () => { it("merges wildcard allowlist entries with agent entries", () => {
const dir = makeTempDir(); const dir = makeTempDir();
const oldHome = process.env.HOME; const homedirSpy = vi.spyOn(os, "homedir").mockReturnValue(dir);
process.env.HOME = dir;
const approvalsPath = path.join(dir, ".clawdbot", "exec-approvals.json"); try {
fs.mkdirSync(path.dirname(approvalsPath), { recursive: true }); const approvalsPath = path.join(dir, ".clawdbot", "exec-approvals.json");
fs.writeFileSync( fs.mkdirSync(path.dirname(approvalsPath), { recursive: true });
approvalsPath, fs.writeFileSync(
JSON.stringify( approvalsPath,
{ JSON.stringify(
version: 1, {
agents: { version: 1,
"*": { allowlist: [{ pattern: "/bin/hostname" }] }, agents: {
main: { allowlist: [{ pattern: "/usr/bin/uname" }] }, "*": { allowlist: [{ pattern: "/bin/hostname" }] },
main: { allowlist: [{ pattern: "/usr/bin/uname" }] },
},
}, },
}, null,
null, 2,
2, ),
), );
);
const resolved = resolveExecApprovals("main"); const resolved = resolveExecApprovals("main");
expect(resolved.allowlist.map((entry) => entry.pattern)).toEqual([ expect(resolved.allowlist.map((entry) => entry.pattern)).toEqual([
"/bin/hostname", "/bin/hostname",
"/usr/bin/uname", "/usr/bin/uname",
]); ]);
} finally {
process.env.HOME = oldHome; homedirSpy.mockRestore();
}
}); });
}); });