mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 18:04:32 +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";
|
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", () => {
|
describe("getShellConfig", () => {
|
||||||
let envSnapshot: ReturnType<typeof captureEnv>;
|
let envSnapshot: ReturnType<typeof captureEnv>;
|
||||||
const tempDirs: string[] = [];
|
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(() => {
|
beforeEach(() => {
|
||||||
envSnapshot = captureEnv(["SHELL", "PATH"]);
|
envSnapshot = captureEnv(["SHELL", "PATH"]);
|
||||||
if (!isWin) {
|
if (!isWin) {
|
||||||
@@ -45,14 +48,14 @@ describe("getShellConfig", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
it("prefers bash when fish is default and bash is on PATH", () => {
|
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;
|
process.env.PATH = binDir;
|
||||||
const { shell } = getShellConfig();
|
const { shell } = getShellConfig();
|
||||||
expect(shell).toBe(path.join(binDir, "bash"));
|
expect(shell).toBe(path.join(binDir, "bash"));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("falls back to sh when fish is default and bash is missing", () => {
|
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;
|
process.env.PATH = binDir;
|
||||||
const { shell } = getShellConfig();
|
const { shell } = getShellConfig();
|
||||||
expect(shell).toBe(path.join(binDir, "sh"));
|
expect(shell).toBe(path.join(binDir, "sh"));
|
||||||
@@ -76,19 +79,6 @@ describe("resolveShellFromPath", () => {
|
|||||||
let envSnapshot: ReturnType<typeof captureEnv>;
|
let envSnapshot: ReturnType<typeof captureEnv>;
|
||||||
const tempDirs: string[] = [];
|
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(() => {
|
beforeEach(() => {
|
||||||
envSnapshot = captureEnv(["PATH"]);
|
envSnapshot = captureEnv(["PATH"]);
|
||||||
});
|
});
|
||||||
@@ -114,8 +104,8 @@ describe("resolveShellFromPath", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("returns the first executable match from PATH", () => {
|
it("returns the first executable match from PATH", () => {
|
||||||
const notExecutable = createTempBin("bash", false);
|
const notExecutable = createTempCommandDir(tempDirs, [{ name: "bash", executable: false }]);
|
||||||
const executable = createTempBin("bash", true);
|
const executable = createTempCommandDir(tempDirs, [{ name: "bash", executable: true }]);
|
||||||
process.env.PATH = [notExecutable, executable].join(path.delimiter);
|
process.env.PATH = [notExecutable, executable].join(path.delimiter);
|
||||||
expect(resolveShellFromPath("bash")).toBe(path.join(executable, "bash"));
|
expect(resolveShellFromPath("bash")).toBe(path.join(executable, "bash"));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user