Skip to content

Commit 732f41c

Browse files
committed
test: use tracked approvals temp directories
1 parent e59835e commit 732f41c

1 file changed

Lines changed: 17 additions & 24 deletions

File tree

src/cli/exec-approvals-cli.test.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Exec approvals CLI tests cover approval command registration and output handling.
22
import fs from "node:fs";
3-
import os from "node:os";
43
import path from "node:path";
54
import { Readable } from "node:stream";
65
import { Command } from "commander";
7-
import { beforeEach, describe, expect, it, vi } from "vitest";
6+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
7+
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
88
import * as execApprovals from "../infra/exec-approvals.js";
99
import type { ExecApprovalsFile } from "../infra/exec-approvals.js";
1010
import { registerExecApprovalsCli, testing } from "./exec-approvals-cli.js";
@@ -67,6 +67,7 @@ const mocks = vi.hoisted(() => {
6767
});
6868

6969
const { callGatewayFromCli, defaultRuntime, readBestEffortConfig, runtimeErrors } = mocks;
70+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
7071

7172
const localSnapshot = {
7273
path: "/tmp/local-exec-approvals.json",
@@ -403,7 +404,7 @@ describe("exec approvals CLI", () => {
403404
});
404405

405406
it("writes host-native node approvals with the current hash", async () => {
406-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-native-approvals-"));
407+
const dir = tempDirs.make("openclaw-native-approvals-");
407408
const policyPath = path.join(dir, "policy.json");
408409
fs.writeFileSync(
409410
policyPath,
@@ -426,19 +427,15 @@ describe("exec approvals CLI", () => {
426427
},
427428
);
428429

429-
try {
430-
await runApprovalsCommand([
431-
"approvals",
432-
"set",
433-
"--node",
434-
"windows",
435-
"--file",
436-
policyPath,
437-
"--json",
438-
]);
439-
} finally {
440-
fs.rmSync(dir, { recursive: true, force: true });
441-
}
430+
await runApprovalsCommand([
431+
"approvals",
432+
"set",
433+
"--node",
434+
"windows",
435+
"--file",
436+
policyPath,
437+
"--json",
438+
]);
442439

443440
expect(callGatewayFromCli.mock.calls[1]?.[0]).toBe("exec.approvals.node.set");
444441
expect(callGatewayFromCli.mock.calls[1]?.[2]).toEqual({
@@ -454,7 +451,7 @@ describe("exec approvals CLI", () => {
454451
});
455452

456453
it("rejects unknown host-native policy fields instead of dropping them", async () => {
457-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-native-approvals-"));
454+
const dir = tempDirs.make("openclaw-native-approvals-");
458455
const policyPath = path.join(dir, "policy.json");
459456
fs.writeFileSync(
460457
policyPath,
@@ -467,13 +464,9 @@ describe("exec approvals CLI", () => {
467464
rules: [],
468465
} as never);
469466

470-
try {
471-
await expect(
472-
runApprovalsCommand(["approvals", "set", "--node", "windows", "--file", policyPath]),
473-
).rejects.toThrow("__exit__:1");
474-
} finally {
475-
fs.rmSync(dir, { recursive: true, force: true });
476-
}
467+
await expect(
468+
runApprovalsCommand(["approvals", "set", "--node", "windows", "--file", policyPath]),
469+
).rejects.toThrow("__exit__:1");
477470

478471
expect(callGatewayFromCli).toHaveBeenCalledTimes(1);
479472
expect(runtimeErrors[0]).toContain("Unknown host-native exec approval rule 1 field: shell");

0 commit comments

Comments
 (0)