mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-24 07:01:49 +00:00
test: remove thread-unsafe cwd mutations
This commit is contained in:
@@ -1881,15 +1881,14 @@ async function expectWorkspaceSummaryEmptyForAgentsAlias(
|
||||
createAlias: (outsidePath: string, agentsPath: string) => void,
|
||||
) {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-compaction-summary-"));
|
||||
const prevCwd = process.cwd();
|
||||
const cwdSpy = vi.spyOn(process, "cwd").mockReturnValue(root);
|
||||
try {
|
||||
const outside = path.join(root, "outside-secret.txt");
|
||||
fs.writeFileSync(outside, "secret");
|
||||
createAlias(outside, path.join(root, "AGENTS.md"));
|
||||
process.chdir(root);
|
||||
await expect(readWorkspaceContextForSummary()).resolves.toBe("");
|
||||
} finally {
|
||||
process.chdir(prevCwd);
|
||||
cwdSpy.mockRestore();
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../../../config/config.js";
|
||||
import { resolveAcpInstallCommandHint, resolveConfiguredAcpBackendId } from "./install-hints.js";
|
||||
|
||||
const originalCwd = process.cwd();
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
function withAcpConfig(acp: OpenClawConfig["acp"]): OpenClawConfig {
|
||||
@@ -13,7 +12,7 @@ function withAcpConfig(acp: OpenClawConfig["acp"]): OpenClawConfig {
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
process.chdir(originalCwd);
|
||||
vi.restoreAllMocks();
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
@@ -33,7 +32,7 @@ describe("ACP install hints", () => {
|
||||
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "acp-install-hint-"));
|
||||
tempDirs.push(tempRoot);
|
||||
fs.mkdirSync(path.join(tempRoot, "extensions", "acpx"), { recursive: true });
|
||||
process.chdir(tempRoot);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(tempRoot);
|
||||
|
||||
const cfg = withAcpConfig({ backend: "acpx" });
|
||||
const hint = resolveAcpInstallCommandHint(cfg);
|
||||
@@ -44,7 +43,7 @@ describe("ACP install hints", () => {
|
||||
it("falls back to scoped install hint for acpx when local extension is absent", () => {
|
||||
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "acp-install-hint-"));
|
||||
tempDirs.push(tempRoot);
|
||||
process.chdir(tempRoot);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(tempRoot);
|
||||
|
||||
const cfg = withAcpConfig({ backend: "acpx" });
|
||||
expect(resolveAcpInstallCommandHint(cfg)).toBe(
|
||||
|
||||
@@ -29,7 +29,6 @@ async function createWorkspace(): Promise<string> {
|
||||
|
||||
let registerMcpCli: typeof import("./mcp-cli.js").registerMcpCli;
|
||||
let sharedProgram: Command;
|
||||
let previousCwd = process.cwd();
|
||||
|
||||
async function runMcpCommand(args: string[]) {
|
||||
await sharedProgram.parseAsync(args, { from: "user" });
|
||||
@@ -45,11 +44,10 @@ describe("mcp cli", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
previousCwd = process.cwd();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
process.chdir(previousCwd);
|
||||
vi.restoreAllMocks();
|
||||
await Promise.all(
|
||||
tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })),
|
||||
);
|
||||
@@ -58,7 +56,7 @@ describe("mcp cli", () => {
|
||||
it("sets and shows a configured MCP server", async () => {
|
||||
await withTempHome("openclaw-cli-mcp-home-", async () => {
|
||||
const workspaceDir = await createWorkspace();
|
||||
process.chdir(workspaceDir);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(workspaceDir);
|
||||
|
||||
await runMcpCommand(["mcp", "set", "context7", '{"command":"uvx","args":["context7-mcp"]}']);
|
||||
expect(mockLog).toHaveBeenCalledWith(expect.stringContaining('Saved MCP server "context7"'));
|
||||
@@ -72,7 +70,7 @@ describe("mcp cli", () => {
|
||||
it("fails when removing an unknown MCP server", async () => {
|
||||
await withTempHome("openclaw-cli-mcp-home-", async () => {
|
||||
const workspaceDir = await createWorkspace();
|
||||
process.chdir(workspaceDir);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(workspaceDir);
|
||||
|
||||
await expect(runMcpCommand(["mcp", "unset", "missing"])).rejects.toThrow("__exit__:1");
|
||||
expect(mockError).toHaveBeenCalledWith(
|
||||
|
||||
@@ -20,11 +20,9 @@ vi.mock("./backup-verify.js", () => ({
|
||||
|
||||
describe("backup commands", () => {
|
||||
let tempHome: TempHomeEnv;
|
||||
let previousCwd: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
tempHome = await createTempHomeEnv("openclaw-backup-test-");
|
||||
previousCwd = process.cwd();
|
||||
backupVerifyCommandMock.mockReset();
|
||||
backupVerifyCommandMock.mockResolvedValue({
|
||||
ok: true,
|
||||
@@ -38,7 +36,7 @@ describe("backup commands", () => {
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
process.chdir(previousCwd);
|
||||
vi.restoreAllMocks();
|
||||
await tempHome.restore();
|
||||
});
|
||||
|
||||
@@ -269,7 +267,7 @@ describe("backup commands", () => {
|
||||
await fs.writeFile(path.join(stateDir, "openclaw.json"), JSON.stringify({}), "utf8");
|
||||
await fs.mkdir(workspaceDir, { recursive: true });
|
||||
await fs.writeFile(path.join(workspaceDir, "SOUL.md"), "# soul\n", "utf8");
|
||||
process.chdir(workspaceDir);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(workspaceDir);
|
||||
|
||||
const runtime = createRuntime();
|
||||
|
||||
@@ -296,7 +294,7 @@ describe("backup commands", () => {
|
||||
await fs.mkdir(workspaceDir, { recursive: true });
|
||||
await fs.writeFile(path.join(workspaceDir, "SOUL.md"), "# soul\n", "utf8");
|
||||
await fs.symlink(workspaceDir, workspaceLink);
|
||||
process.chdir(workspaceLink);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(workspaceLink);
|
||||
|
||||
const runtime = createRuntime();
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { resolveBundledPluginsDir } from "./bundled-dir.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
const originalCwd = process.cwd();
|
||||
const originalBundledDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
|
||||
const originalVitest = process.env.VITEST;
|
||||
|
||||
@@ -16,7 +15,7 @@ function makeRepoRoot(prefix: string): string {
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
process.chdir(originalCwd);
|
||||
vi.restoreAllMocks();
|
||||
if (originalBundledDir === undefined) {
|
||||
delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
|
||||
} else {
|
||||
@@ -43,7 +42,7 @@ describe("resolveBundledPluginsDir", () => {
|
||||
"utf8",
|
||||
);
|
||||
|
||||
process.chdir(repoRoot);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(repoRoot);
|
||||
|
||||
expect(fs.realpathSync(resolveBundledPluginsDir() ?? "")).toBe(
|
||||
fs.realpathSync(path.join(repoRoot, "dist-runtime", "extensions")),
|
||||
@@ -59,7 +58,7 @@ describe("resolveBundledPluginsDir", () => {
|
||||
"utf8",
|
||||
);
|
||||
|
||||
process.chdir(repoRoot);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(repoRoot);
|
||||
|
||||
expect(fs.realpathSync(resolveBundledPluginsDir() ?? "")).toBe(
|
||||
fs.realpathSync(path.join(repoRoot, "dist", "extensions")),
|
||||
@@ -77,7 +76,7 @@ describe("resolveBundledPluginsDir", () => {
|
||||
"utf8",
|
||||
);
|
||||
|
||||
process.chdir(repoRoot);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(repoRoot);
|
||||
process.env.VITEST = "true";
|
||||
|
||||
expect(fs.realpathSync(resolveBundledPluginsDir() ?? "")).toBe(
|
||||
@@ -98,7 +97,7 @@ describe("resolveBundledPluginsDir", () => {
|
||||
"utf8",
|
||||
);
|
||||
|
||||
process.chdir(repoRoot);
|
||||
vi.spyOn(process, "cwd").mockReturnValue(repoRoot);
|
||||
delete process.env.VITEST;
|
||||
|
||||
expect(fs.realpathSync(resolveBundledPluginsDir() ?? "")).toBe(
|
||||
|
||||
@@ -2,7 +2,7 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { afterAll, describe, expect, it } from "vitest";
|
||||
import { afterAll, describe, expect, it, vi } from "vitest";
|
||||
import { withEnv } from "../test-utils/env.js";
|
||||
import {
|
||||
buildPluginLoaderAliasMap,
|
||||
@@ -52,12 +52,11 @@ function makeTempDir() {
|
||||
}
|
||||
|
||||
function withCwd<T>(cwd: string, run: () => T): T {
|
||||
const previousCwd = process.cwd();
|
||||
process.chdir(cwd);
|
||||
const cwdSpy = vi.spyOn(process, "cwd").mockReturnValue(cwd);
|
||||
try {
|
||||
return run();
|
||||
} finally {
|
||||
process.chdir(previousCwd);
|
||||
cwdSpy.mockRestore();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
test/fixtures/test-parallel.behavior.json
vendored
12
test/fixtures/test-parallel.behavior.json
vendored
@@ -9,10 +9,6 @@
|
||||
"file": "src/security/temp-path-guard.test.ts",
|
||||
"reason": "Filesystem guard scans are sensitive to contention."
|
||||
},
|
||||
{
|
||||
"file": "src/infra/git-commit.test.ts",
|
||||
"reason": "Mutates process.cwd() and core loader seams."
|
||||
},
|
||||
{
|
||||
"file": "src/config/doc-baseline.integration.test.ts",
|
||||
"reason": "Rebuilds bundled config baselines through many channel schema subprocesses; keep out of the shared lane."
|
||||
@@ -381,6 +377,10 @@
|
||||
"file": "src/plugins/status.test.ts",
|
||||
"reason": "Measured ~10% faster under threads than forks on this host while keeping the file green."
|
||||
},
|
||||
{
|
||||
"file": "src/plugins/sdk-alias.test.ts",
|
||||
"reason": "Measured ~17% faster under threads than forks on this host after removing process.chdir() from the test."
|
||||
},
|
||||
{
|
||||
"file": "src/config/config.plugin-validation.test.ts",
|
||||
"reason": "Measured ~11% faster under threads than forks on this host while keeping the file green."
|
||||
@@ -537,6 +537,10 @@
|
||||
"file": "src/infra/provider-usage.test.ts",
|
||||
"reason": "Measured ~17% faster under threads than forks on this host while keeping the file green."
|
||||
},
|
||||
{
|
||||
"file": "src/infra/git-commit.test.ts",
|
||||
"reason": "Measured ~11% faster under threads than forks on this host after removing process.chdir() from related thread-blocking tests."
|
||||
},
|
||||
{
|
||||
"file": "src/infra/provider-usage.auth.normalizes-keys.test.ts",
|
||||
"reason": "Measured ~12% faster under threads than forks on this host while keeping the file green."
|
||||
|
||||
Reference in New Issue
Block a user