From a814cce359a11b688605fd62c0d17fb9f76fbb06 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 18:49:43 +0000 Subject: [PATCH] refactor(test): share temp command dir helper in shell utils e2e --- src/agents/shell-utils.e2e.test.ts | 46 ++++++++++++------------------ 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/agents/shell-utils.e2e.test.ts b/src/agents/shell-utils.e2e.test.ts index c13ec178a98..9f4cb869ba1 100644 --- a/src/agents/shell-utils.e2e.test.ts +++ b/src/agents/shell-utils.e2e.test.ts @@ -7,21 +7,24 @@ import { getShellConfig, resolveShellFromPath } from "./shell-utils.js"; const isWin = process.platform === "win32"; +function createTempCommandDir( + tempDirs: string[], + files: Array<{ name: string; executable?: boolean }>, +): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-shell-")); + tempDirs.push(dir); + for (const file of files) { + const filePath = path.join(dir, file.name); + fs.writeFileSync(filePath, ""); + fs.chmodSync(filePath, file.executable === false ? 0o644 : 0o755); + } + return dir; +} + describe("getShellConfig", () => { let envSnapshot: ReturnType; const tempDirs: string[] = []; - const createTempBin = (files: string[]) => { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-shell-")); - tempDirs.push(dir); - for (const name of files) { - const filePath = path.join(dir, name); - fs.writeFileSync(filePath, ""); - fs.chmodSync(filePath, 0o755); - } - return dir; - }; - beforeEach(() => { envSnapshot = captureEnv(["SHELL", "PATH"]); if (!isWin) { @@ -45,14 +48,14 @@ describe("getShellConfig", () => { } it("prefers bash when fish is default and bash is on PATH", () => { - const binDir = createTempBin(["bash"]); + const binDir = createTempCommandDir(tempDirs, [{ name: "bash" }]); process.env.PATH = binDir; const { shell } = getShellConfig(); expect(shell).toBe(path.join(binDir, "bash")); }); it("falls back to sh when fish is default and bash is missing", () => { - const binDir = createTempBin(["sh"]); + const binDir = createTempCommandDir(tempDirs, [{ name: "sh" }]); process.env.PATH = binDir; const { shell } = getShellConfig(); expect(shell).toBe(path.join(binDir, "sh")); @@ -76,19 +79,6 @@ describe("resolveShellFromPath", () => { let envSnapshot: ReturnType; const tempDirs: string[] = []; - const createTempBin = (name: string, executable: boolean) => { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-shell-path-")); - tempDirs.push(dir); - const filePath = path.join(dir, name); - fs.writeFileSync(filePath, ""); - if (executable) { - fs.chmodSync(filePath, 0o755); - } else { - fs.chmodSync(filePath, 0o644); - } - return dir; - }; - beforeEach(() => { envSnapshot = captureEnv(["PATH"]); }); @@ -114,8 +104,8 @@ describe("resolveShellFromPath", () => { }); it("returns the first executable match from PATH", () => { - const notExecutable = createTempBin("bash", false); - const executable = createTempBin("bash", true); + const notExecutable = createTempCommandDir(tempDirs, [{ name: "bash", executable: false }]); + const executable = createTempCommandDir(tempDirs, [{ name: "bash", executable: true }]); process.env.PATH = [notExecutable, executable].join(path.delimiter); expect(resolveShellFromPath("bash")).toBe(path.join(executable, "bash")); });