refactor(infra): reuse shared home prefix expansion

This commit is contained in:
Peter Steinberger
2026-02-18 17:38:11 +00:00
parent b51166e879
commit b73a2de9f6
3 changed files with 41 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import {
analyzeArgvCommand,
analyzeShellCommand,
@@ -19,6 +19,8 @@ import {
resolveCommandResolution,
resolveExecApprovals,
resolveExecApprovalsFromFile,
resolveExecApprovalsPath,
resolveExecApprovalsSocketPath,
type ExecAllowlistEntry,
type ExecApprovalsFile,
} from "./exec-approvals.js";
@@ -110,6 +112,28 @@ describe("mergeExecApprovalsSocketDefaults", () => {
});
});
describe("resolve exec approvals defaults", () => {
it("expands home-prefixed default file and socket paths", () => {
const dir = makeTempDir();
const prevOpenClawHome = process.env.OPENCLAW_HOME;
try {
process.env.OPENCLAW_HOME = dir;
expect(path.normalize(resolveExecApprovalsPath())).toBe(
path.normalize(path.join(dir, ".openclaw", "exec-approvals.json")),
);
expect(path.normalize(resolveExecApprovalsSocketPath())).toBe(
path.normalize(path.join(dir, ".openclaw", "exec-approvals.sock")),
);
} finally {
if (prevOpenClawHome === undefined) {
delete process.env.OPENCLAW_HOME;
} else {
process.env.OPENCLAW_HOME = prevOpenClawHome;
}
}
});
});
describe("exec approvals safe shell command builder", () => {
it("quotes only safeBins segments (leaves other segments untouched)", () => {
if (process.platform === "win32") {
@@ -599,9 +623,10 @@ describe("exec approvals policy helpers", () => {
describe("exec approvals wildcard agent", () => {
it("merges wildcard allowlist entries with agent entries", () => {
const dir = makeTempDir();
const homedirSpy = vi.spyOn(os, "homedir").mockReturnValue(dir);
const prevOpenClawHome = process.env.OPENCLAW_HOME;
try {
process.env.OPENCLAW_HOME = dir;
const approvalsPath = path.join(dir, ".openclaw", "exec-approvals.json");
fs.mkdirSync(path.dirname(approvalsPath), { recursive: true });
fs.writeFileSync(
@@ -625,7 +650,11 @@ describe("exec approvals wildcard agent", () => {
"/usr/bin/uname",
]);
} finally {
homedirSpy.mockRestore();
if (prevOpenClawHome === undefined) {
delete process.env.OPENCLAW_HOME;
} else {
process.env.OPENCLAW_HOME = prevOpenClawHome;
}
}
});
});