mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 23:21:23 +00:00
refactor(test): replace manual PATH restore with env helpers
This commit is contained in:
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { withEnvAsync } from "../test-utils/env.js";
|
||||||
import {
|
import {
|
||||||
ensureTailscaleEndpoint,
|
ensureTailscaleEndpoint,
|
||||||
resetGmailSetupUtilsCachesForTest,
|
resetGmailSetupUtilsCachesForTest,
|
||||||
@@ -25,7 +26,6 @@ describe("resolvePythonExecutablePath", () => {
|
|||||||
"resolves a working python path and caches the result",
|
"resolves a working python path and caches the result",
|
||||||
async () => {
|
async () => {
|
||||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-python-"));
|
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-python-"));
|
||||||
const originalPath = process.env.PATH;
|
|
||||||
try {
|
try {
|
||||||
const realPython = path.join(tmp, "python-real");
|
const realPython = path.join(tmp, "python-real");
|
||||||
await fs.writeFile(realPython, "#!/bin/sh\nexit 0\n", "utf-8");
|
await fs.writeFile(realPython, "#!/bin/sh\nexit 0\n", "utf-8");
|
||||||
@@ -37,25 +37,25 @@ describe("resolvePythonExecutablePath", () => {
|
|||||||
await fs.writeFile(shim, "#!/bin/sh\nexit 0\n", "utf-8");
|
await fs.writeFile(shim, "#!/bin/sh\nexit 0\n", "utf-8");
|
||||||
await fs.chmod(shim, 0o755);
|
await fs.chmod(shim, 0o755);
|
||||||
|
|
||||||
process.env.PATH = `${shimDir}${path.delimiter}/usr/bin`;
|
await withEnvAsync({ PATH: `${shimDir}${path.delimiter}/usr/bin` }, async () => {
|
||||||
|
runCommandWithTimeoutMock.mockResolvedValue({
|
||||||
|
stdout: `${realPython}\n`,
|
||||||
|
stderr: "",
|
||||||
|
code: 0,
|
||||||
|
signal: null,
|
||||||
|
killed: false,
|
||||||
|
});
|
||||||
|
|
||||||
runCommandWithTimeoutMock.mockResolvedValue({
|
const resolved = await resolvePythonExecutablePath();
|
||||||
stdout: `${realPython}\n`,
|
expect(resolved).toBe(realPython);
|
||||||
stderr: "",
|
|
||||||
code: 0,
|
await withEnvAsync({ PATH: "/bin" }, async () => {
|
||||||
signal: null,
|
const cached = await resolvePythonExecutablePath();
|
||||||
killed: false,
|
expect(cached).toBe(realPython);
|
||||||
|
});
|
||||||
|
expect(runCommandWithTimeoutMock).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
const resolved = await resolvePythonExecutablePath();
|
|
||||||
expect(resolved).toBe(realPython);
|
|
||||||
|
|
||||||
process.env.PATH = "/bin";
|
|
||||||
const cached = await resolvePythonExecutablePath();
|
|
||||||
expect(cached).toBe(realPython);
|
|
||||||
expect(runCommandWithTimeoutMock).toHaveBeenCalledTimes(1);
|
|
||||||
} finally {
|
} finally {
|
||||||
process.env.PATH = originalPath;
|
|
||||||
await fs.rm(tmp, { recursive: true, force: true });
|
await fs.rm(tmp, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { withEnv } from "../test-utils/env.js";
|
||||||
import {
|
import {
|
||||||
buildTrustedSafeBinDirs,
|
buildTrustedSafeBinDirs,
|
||||||
getTrustedSafeBinDirs,
|
getTrustedSafeBinDirs,
|
||||||
@@ -56,16 +57,13 @@ describe("exec safe bin trust", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("uses startup PATH snapshot when pathEnv is omitted", () => {
|
it("uses startup PATH snapshot when pathEnv is omitted", () => {
|
||||||
const originalPath = process.env.PATH;
|
|
||||||
const injected = `/tmp/openclaw-path-injected-${Date.now()}`;
|
const injected = `/tmp/openclaw-path-injected-${Date.now()}`;
|
||||||
const initial = getTrustedSafeBinDirs({ refresh: true });
|
const initial = getTrustedSafeBinDirs({ refresh: true });
|
||||||
try {
|
|
||||||
process.env.PATH = `${injected}${path.delimiter}${originalPath ?? ""}`;
|
withEnv({ PATH: `${injected}${path.delimiter}${process.env.PATH ?? ""}` }, () => {
|
||||||
const refreshed = getTrustedSafeBinDirs({ refresh: true });
|
const refreshed = getTrustedSafeBinDirs({ refresh: true });
|
||||||
expect(refreshed.has(path.resolve(injected))).toBe(false);
|
expect(refreshed.has(path.resolve(injected))).toBe(false);
|
||||||
expect([...refreshed].toSorted()).toEqual([...initial].toSorted());
|
expect([...refreshed].toSorted()).toEqual([...initial].toSorted());
|
||||||
} finally {
|
});
|
||||||
process.env.PATH = originalPath;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user