mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 00:01:24 +00:00
refactor(test): share temp command dir helper in shell utils e2e
This commit is contained in:
@@ -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<typeof captureEnv>;
|
||||
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<typeof captureEnv>;
|
||||
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"));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user