mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:14:33 +00:00
test: make shell env path cache tests platform deterministic
This commit is contained in:
@@ -224,35 +224,34 @@ describe("infra runtime", () => {
|
|||||||
afterEach(() => resetShellPathCacheForTests());
|
afterEach(() => resetShellPathCacheForTests());
|
||||||
|
|
||||||
it("returns PATH from login shell env", () => {
|
it("returns PATH from login shell env", () => {
|
||||||
if (process.platform === "win32") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const exec = vi
|
const exec = vi
|
||||||
.fn()
|
.fn()
|
||||||
.mockReturnValue(Buffer.from("PATH=/custom/bin\0HOME=/home/user\0", "utf-8"));
|
.mockReturnValue(Buffer.from("PATH=/custom/bin\0HOME=/home/user\0", "utf-8"));
|
||||||
const result = getShellPathFromLoginShell({ env: { SHELL: "/bin/sh" }, exec });
|
const result = getShellPathFromLoginShell({
|
||||||
|
env: { SHELL: "/bin/sh" },
|
||||||
|
exec,
|
||||||
|
platform: "linux",
|
||||||
|
});
|
||||||
expect(result).toBe("/custom/bin");
|
expect(result).toBe("/custom/bin");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("caches the value", () => {
|
it("caches the value", () => {
|
||||||
if (process.platform === "win32") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const exec = vi.fn().mockReturnValue(Buffer.from("PATH=/custom/bin\0", "utf-8"));
|
const exec = vi.fn().mockReturnValue(Buffer.from("PATH=/custom/bin\0", "utf-8"));
|
||||||
const env = { SHELL: "/bin/sh" } as NodeJS.ProcessEnv;
|
const env = { SHELL: "/bin/sh" } as NodeJS.ProcessEnv;
|
||||||
expect(getShellPathFromLoginShell({ env, exec })).toBe("/custom/bin");
|
expect(getShellPathFromLoginShell({ env, exec, platform: "linux" })).toBe("/custom/bin");
|
||||||
expect(getShellPathFromLoginShell({ env, exec })).toBe("/custom/bin");
|
expect(getShellPathFromLoginShell({ env, exec, platform: "linux" })).toBe("/custom/bin");
|
||||||
expect(exec).toHaveBeenCalledTimes(1);
|
expect(exec).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns null on exec failure", () => {
|
it("returns null on exec failure", () => {
|
||||||
if (process.platform === "win32") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const exec = vi.fn(() => {
|
const exec = vi.fn(() => {
|
||||||
throw new Error("boom");
|
throw new Error("boom");
|
||||||
});
|
});
|
||||||
const result = getShellPathFromLoginShell({ env: { SHELL: "/bin/sh" }, exec });
|
const result = getShellPathFromLoginShell({
|
||||||
|
env: { SHELL: "/bin/sh" },
|
||||||
|
exec,
|
||||||
|
platform: "linux",
|
||||||
|
});
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -81,19 +81,14 @@ describe("shell env fallback", () => {
|
|||||||
const first = getShellPathFromLoginShell({
|
const first = getShellPathFromLoginShell({
|
||||||
env: {} as NodeJS.ProcessEnv,
|
env: {} as NodeJS.ProcessEnv,
|
||||||
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
||||||
|
platform: "linux",
|
||||||
});
|
});
|
||||||
const second = getShellPathFromLoginShell({
|
const second = getShellPathFromLoginShell({
|
||||||
env: {} as NodeJS.ProcessEnv,
|
env: {} as NodeJS.ProcessEnv,
|
||||||
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
||||||
|
platform: "linux",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (process.platform === "win32") {
|
|
||||||
expect(first).toBeNull();
|
|
||||||
expect(second).toBeNull();
|
|
||||||
expect(exec).not.toHaveBeenCalled();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(first).toBe("/usr/local/bin:/usr/bin");
|
expect(first).toBe("/usr/local/bin:/usr/bin");
|
||||||
expect(second).toBe("/usr/local/bin:/usr/bin");
|
expect(second).toBe("/usr/local/bin:/usr/bin");
|
||||||
expect(exec).toHaveBeenCalledOnce();
|
expect(exec).toHaveBeenCalledOnce();
|
||||||
@@ -108,18 +103,36 @@ describe("shell env fallback", () => {
|
|||||||
const first = getShellPathFromLoginShell({
|
const first = getShellPathFromLoginShell({
|
||||||
env: {} as NodeJS.ProcessEnv,
|
env: {} as NodeJS.ProcessEnv,
|
||||||
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
||||||
|
platform: "linux",
|
||||||
});
|
});
|
||||||
const second = getShellPathFromLoginShell({
|
const second = getShellPathFromLoginShell({
|
||||||
env: {} as NodeJS.ProcessEnv,
|
env: {} as NodeJS.ProcessEnv,
|
||||||
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
||||||
|
platform: "linux",
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(first).toBeNull();
|
expect(first).toBeNull();
|
||||||
expect(second).toBeNull();
|
expect(second).toBeNull();
|
||||||
if (process.platform === "win32") {
|
|
||||||
expect(exec).not.toHaveBeenCalled();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
expect(exec).toHaveBeenCalledOnce();
|
expect(exec).toHaveBeenCalledOnce();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns null without invoking shell on win32", () => {
|
||||||
|
resetShellPathCacheForTests();
|
||||||
|
const exec = vi.fn(() => Buffer.from("PATH=/usr/local/bin:/usr/bin\0HOME=/tmp\0"));
|
||||||
|
|
||||||
|
const first = getShellPathFromLoginShell({
|
||||||
|
env: {} as NodeJS.ProcessEnv,
|
||||||
|
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
||||||
|
platform: "win32",
|
||||||
|
});
|
||||||
|
const second = getShellPathFromLoginShell({
|
||||||
|
env: {} as NodeJS.ProcessEnv,
|
||||||
|
exec: exec as unknown as Parameters<typeof getShellPathFromLoginShell>[0]["exec"],
|
||||||
|
platform: "win32",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(first).toBeNull();
|
||||||
|
expect(second).toBeNull();
|
||||||
|
expect(exec).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -140,11 +140,13 @@ export function getShellPathFromLoginShell(opts: {
|
|||||||
env: NodeJS.ProcessEnv;
|
env: NodeJS.ProcessEnv;
|
||||||
timeoutMs?: number;
|
timeoutMs?: number;
|
||||||
exec?: typeof execFileSync;
|
exec?: typeof execFileSync;
|
||||||
|
platform?: NodeJS.Platform;
|
||||||
}): string | null {
|
}): string | null {
|
||||||
if (cachedShellPath !== undefined) {
|
if (cachedShellPath !== undefined) {
|
||||||
return cachedShellPath;
|
return cachedShellPath;
|
||||||
}
|
}
|
||||||
if (process.platform === "win32") {
|
const platform = opts.platform ?? process.platform;
|
||||||
|
if (platform === "win32") {
|
||||||
cachedShellPath = null;
|
cachedShellPath = null;
|
||||||
return cachedShellPath;
|
return cachedShellPath;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user