Files
openclaw/src/process/exec.test.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import { runCommandWithTimeout, shouldSpawnWithShell } from "./exec.js";
describe("runCommandWithTimeout", () => {
it("never enables shell execution (Windows cmd.exe injection hardening)", () => {
expect(
shouldSpawnWithShell({
resolvedCommand: "npm.cmd",
platform: "win32",
}),
).toBe(false);
});
it("passes env overrides to child", async () => {
const result = await runCommandWithTimeout(
2026-01-30 03:15:10 +01:00
[process.execPath, "-e", 'process.stdout.write(process.env.OPENCLAW_TEST_ENV ?? "")'],
{
timeoutMs: 5_000,
2026-01-30 03:15:10 +01:00
env: { OPENCLAW_TEST_ENV: "ok" },
},
);
expect(result.code).toBe(0);
expect(result.stdout).toBe("ok");
});
it("merges custom env with process.env", async () => {
const envSnapshot = captureEnv(["OPENCLAW_BASE_ENV"]);
2026-01-30 03:15:10 +01:00
process.env.OPENCLAW_BASE_ENV = "base";
try {
const result = await runCommandWithTimeout(
[
process.execPath,
"-e",
2026-01-30 03:15:10 +01:00
'process.stdout.write((process.env.OPENCLAW_BASE_ENV ?? "") + "|" + (process.env.OPENCLAW_TEST_ENV ?? ""))',
],
{
timeoutMs: 5_000,
2026-01-30 03:15:10 +01:00
env: { OPENCLAW_TEST_ENV: "ok" },
},
);
expect(result.code).toBe(0);
expect(result.stdout).toBe("base|ok");
} finally {
envSnapshot.restore();
}
});
});