diff --git a/src/cli/update-cli/shared.command-runner.test.ts b/src/cli/update-cli/shared.command-runner.test.ts new file mode 100644 index 000000000..678a8a3d6 --- /dev/null +++ b/src/cli/update-cli/shared.command-runner.test.ts @@ -0,0 +1,52 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const runCommandWithTimeout = vi.fn(); + +vi.mock("../../process/exec.js", () => ({ + runCommandWithTimeout, +})); + +const { createGlobalCommandRunner } = await import("./shared.js"); + +describe("createGlobalCommandRunner", () => { + beforeEach(() => { + vi.clearAllMocks(); + runCommandWithTimeout.mockResolvedValue({ + stdout: "", + stderr: "", + code: 0, + signal: null, + killed: false, + termination: "exit", + }); + }); + + it("forwards argv/options and maps exec result shape", async () => { + runCommandWithTimeout.mockResolvedValueOnce({ + stdout: "out", + stderr: "err", + code: 17, + signal: null, + killed: false, + termination: "exit", + }); + const runCommand = createGlobalCommandRunner(); + + const result = await runCommand(["npm", "root", "-g"], { + timeoutMs: 1200, + cwd: "/tmp/openclaw", + env: { OPENCLAW_TEST: "1" }, + }); + + expect(runCommandWithTimeout).toHaveBeenCalledWith(["npm", "root", "-g"], { + timeoutMs: 1200, + cwd: "/tmp/openclaw", + env: { OPENCLAW_TEST: "1" }, + }); + expect(result).toEqual({ + stdout: "out", + stderr: "err", + code: 17, + }); + }); +}); diff --git a/src/cli/update-cli/shared.ts b/src/cli/update-cli/shared.ts index c97e02160..2cf53e201 100644 --- a/src/cli/update-cli/shared.ts +++ b/src/cli/update-cli/shared.ts @@ -11,6 +11,7 @@ import { fetchNpmTagVersion } from "../../infra/update-check.js"; import { detectGlobalInstallManagerByPresence, detectGlobalInstallManagerForRoot, + type CommandRunner, type GlobalInstallManager, } from "../../infra/update-global.js"; import type { UpdateStepProgress, UpdateStepResult } from "../../infra/update-runner.js"; @@ -236,10 +237,7 @@ export async function resolveGlobalManager(params: { installKind: "git" | "package" | "unknown"; timeoutMs: number; }): Promise { - const runCommand = async (argv: string[], options: { timeoutMs: number }) => { - const res = await runCommandWithTimeout(argv, options); - return { stdout: res.stdout, stderr: res.stderr, code: res.code }; - }; + const runCommand = createGlobalCommandRunner(); if (params.installKind === "package") { const detected = await detectGlobalInstallManagerForRoot( @@ -281,3 +279,10 @@ export async function tryWriteCompletionCache(root: string, jsonMode: boolean): defaultRuntime.log(theme.warn(`Completion cache update failed${detail}.`)); } } + +export function createGlobalCommandRunner(): CommandRunner { + return async (argv, options) => { + const res = await runCommandWithTimeout(argv, options); + return { stdout: res.stdout, stderr: res.stderr, code: res.code }; + }; +} diff --git a/src/cli/update-cli/update-command.ts b/src/cli/update-cli/update-command.ts index a2a923d3a..58536704d 100644 --- a/src/cli/update-cli/update-command.ts +++ b/src/cli/update-cli/update-command.ts @@ -47,6 +47,7 @@ import { createUpdateProgress, printResult } from "./progress.js"; import { prepareRestartScript, runRestartScript } from "./restart-helper.js"; import { DEFAULT_PACKAGE_NAME, + createGlobalCommandRunner, ensureGitCheckout, normalizeTag, parseTimeoutMsOrExit, @@ -208,10 +209,7 @@ async function runPackageInstallUpdate(params: { installKind: params.installKind, timeoutMs: params.timeoutMs, }); - const runCommand = async (argv: string[], options: { timeoutMs: number }) => { - const res = await runCommandWithTimeout(argv, options); - return { stdout: res.stdout, stderr: res.stderr, code: res.code }; - }; + const runCommand = createGlobalCommandRunner(); const pkgRoot = await resolveGlobalPackageRoot(manager, runCommand, params.timeoutMs); const packageName =