mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 09:28:37 +00:00
refactor: share global update test harness
This commit is contained in:
@@ -187,6 +187,21 @@ describe("runGatewayUpdate", () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function writeGlobalPackageVersion(pkgRoot: string, version = "2.0.0") {
|
||||||
|
await fs.writeFile(
|
||||||
|
path.join(pkgRoot, "package.json"),
|
||||||
|
JSON.stringify({ name: "openclaw", version }),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createGlobalPackageFixture(rootDir: string) {
|
||||||
|
const nodeModules = path.join(rootDir, "node_modules");
|
||||||
|
const pkgRoot = path.join(nodeModules, "openclaw");
|
||||||
|
await seedGlobalPackageRoot(pkgRoot);
|
||||||
|
return { nodeModules, pkgRoot };
|
||||||
|
}
|
||||||
|
|
||||||
function createGlobalNpmUpdateRunner(params: {
|
function createGlobalNpmUpdateRunner(params: {
|
||||||
pkgRoot: string;
|
pkgRoot: string;
|
||||||
nodeModules: string;
|
nodeModules: string;
|
||||||
@@ -366,13 +381,17 @@ describe("runGatewayUpdate", () => {
|
|||||||
pkgRoot: string;
|
pkgRoot: string;
|
||||||
npmRootOutput?: string;
|
npmRootOutput?: string;
|
||||||
installCommand: string;
|
installCommand: string;
|
||||||
onInstall?: () => Promise<void>;
|
gitRootMode?: "not-git" | "missing";
|
||||||
|
onInstall?: (options?: { env?: NodeJS.ProcessEnv }) => Promise<void>;
|
||||||
}) => {
|
}) => {
|
||||||
const calls: string[] = [];
|
const calls: string[] = [];
|
||||||
const runCommand = async (argv: string[]) => {
|
const runCommand = async (argv: string[], options?: { env?: NodeJS.ProcessEnv }) => {
|
||||||
const key = argv.join(" ");
|
const key = argv.join(" ");
|
||||||
calls.push(key);
|
calls.push(key);
|
||||||
if (key === `git -C ${params.pkgRoot} rev-parse --show-toplevel`) {
|
if (key === `git -C ${params.pkgRoot} rev-parse --show-toplevel`) {
|
||||||
|
if (params.gitRootMode === "missing") {
|
||||||
|
throw Object.assign(new Error("spawn git ENOENT"), { code: "ENOENT" });
|
||||||
|
}
|
||||||
return { stdout: "", stderr: "not a git repository", code: 128 };
|
return { stdout: "", stderr: "not a git repository", code: 128 };
|
||||||
}
|
}
|
||||||
if (key === "npm root -g") {
|
if (key === "npm root -g") {
|
||||||
@@ -385,7 +404,7 @@ describe("runGatewayUpdate", () => {
|
|||||||
return { stdout: "", stderr: "", code: 1 };
|
return { stdout: "", stderr: "", code: 1 };
|
||||||
}
|
}
|
||||||
if (key === params.installCommand) {
|
if (key === params.installCommand) {
|
||||||
await params.onInstall?.();
|
await params.onInstall?.(options);
|
||||||
return { stdout: "ok", stderr: "", code: 0 };
|
return { stdout: "ok", stderr: "", code: 0 };
|
||||||
}
|
}
|
||||||
return { stdout: "", stderr: "", code: 0 };
|
return { stdout: "", stderr: "", code: 0 };
|
||||||
@@ -423,32 +442,14 @@ describe("runGatewayUpdate", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("falls back to global npm update when git is missing from PATH", async () => {
|
it("falls back to global npm update when git is missing from PATH", async () => {
|
||||||
const nodeModules = path.join(tempDir, "node_modules");
|
const { nodeModules, pkgRoot } = await createGlobalPackageFixture(tempDir);
|
||||||
const pkgRoot = path.join(nodeModules, "openclaw");
|
const { calls, runCommand } = createGlobalInstallHarness({
|
||||||
await seedGlobalPackageRoot(pkgRoot);
|
pkgRoot,
|
||||||
|
npmRootOutput: nodeModules,
|
||||||
const calls: string[] = [];
|
installCommand: "npm i -g openclaw@latest --no-fund --no-audit --loglevel=error",
|
||||||
const runCommand = async (argv: string[]): Promise<CommandResult> => {
|
gitRootMode: "missing",
|
||||||
const key = argv.join(" ");
|
onInstall: async () => writeGlobalPackageVersion(pkgRoot),
|
||||||
calls.push(key);
|
});
|
||||||
if (key === `git -C ${pkgRoot} rev-parse --show-toplevel`) {
|
|
||||||
throw Object.assign(new Error("spawn git ENOENT"), { code: "ENOENT" });
|
|
||||||
}
|
|
||||||
if (key === "npm root -g") {
|
|
||||||
return { stdout: nodeModules, stderr: "", code: 0 };
|
|
||||||
}
|
|
||||||
if (key === "pnpm root -g") {
|
|
||||||
return { stdout: "", stderr: "", code: 1 };
|
|
||||||
}
|
|
||||||
if (key === "npm i -g openclaw@latest --no-fund --no-audit --loglevel=error") {
|
|
||||||
await fs.writeFile(
|
|
||||||
path.join(pkgRoot, "package.json"),
|
|
||||||
JSON.stringify({ name: "openclaw", version: "2.0.0" }),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return { stdout: "ok", stderr: "", code: 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = await runWithCommand(runCommand, { cwd: pkgRoot });
|
const result = await runWithCommand(runCommand, { cwd: pkgRoot });
|
||||||
|
|
||||||
@@ -537,35 +538,17 @@ describe("runGatewayUpdate", () => {
|
|||||||
await fs.mkdir(portableGitMingw, { recursive: true });
|
await fs.mkdir(portableGitMingw, { recursive: true });
|
||||||
await fs.mkdir(portableGitUsr, { recursive: true });
|
await fs.mkdir(portableGitUsr, { recursive: true });
|
||||||
|
|
||||||
const nodeModules = path.join(tempDir, "node_modules");
|
|
||||||
const pkgRoot = path.join(nodeModules, "openclaw");
|
|
||||||
await seedGlobalPackageRoot(pkgRoot);
|
|
||||||
|
|
||||||
let installEnv: NodeJS.ProcessEnv | undefined;
|
let installEnv: NodeJS.ProcessEnv | undefined;
|
||||||
const runCommand = async (
|
const { nodeModules, pkgRoot } = await createGlobalPackageFixture(tempDir);
|
||||||
argv: string[],
|
const { runCommand } = createGlobalInstallHarness({
|
||||||
options?: { env?: NodeJS.ProcessEnv },
|
pkgRoot,
|
||||||
): Promise<CommandResult> => {
|
npmRootOutput: nodeModules,
|
||||||
const key = argv.join(" ");
|
installCommand: "npm i -g openclaw@latest --no-fund --no-audit --loglevel=error",
|
||||||
if (key === `git -C ${pkgRoot} rev-parse --show-toplevel`) {
|
onInstall: async (options) => {
|
||||||
return { stdout: "", stderr: "not a git repository", code: 128 };
|
|
||||||
}
|
|
||||||
if (key === "npm root -g") {
|
|
||||||
return { stdout: nodeModules, stderr: "", code: 0 };
|
|
||||||
}
|
|
||||||
if (key === "pnpm root -g") {
|
|
||||||
return { stdout: "", stderr: "", code: 1 };
|
|
||||||
}
|
|
||||||
if (key === "npm i -g openclaw@latest --no-fund --no-audit --loglevel=error") {
|
|
||||||
installEnv = options?.env;
|
installEnv = options?.env;
|
||||||
await fs.writeFile(
|
await writeGlobalPackageVersion(pkgRoot);
|
||||||
path.join(pkgRoot, "package.json"),
|
},
|
||||||
JSON.stringify({ name: "openclaw", version: "2.0.0" }),
|
});
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return { stdout: "ok", stderr: "", code: 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
await withEnvAsync({ LOCALAPPDATA: localAppData }, async () => {
|
await withEnvAsync({ LOCALAPPDATA: localAppData }, async () => {
|
||||||
const result = await runWithCommand(runCommand, { cwd: pkgRoot });
|
const result = await runWithCommand(runCommand, { cwd: pkgRoot });
|
||||||
@@ -584,35 +567,15 @@ describe("runGatewayUpdate", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("uses OPENCLAW_UPDATE_PACKAGE_SPEC for global package updates", async () => {
|
it("uses OPENCLAW_UPDATE_PACKAGE_SPEC for global package updates", async () => {
|
||||||
const nodeModules = path.join(tempDir, "node_modules");
|
const { nodeModules, pkgRoot } = await createGlobalPackageFixture(tempDir);
|
||||||
const pkgRoot = path.join(nodeModules, "openclaw");
|
const expectedInstallCommand =
|
||||||
await seedGlobalPackageRoot(pkgRoot);
|
"npm i -g http://10.211.55.2:8138/openclaw-next.tgz --no-fund --no-audit --loglevel=error";
|
||||||
|
const { calls, runCommand } = createGlobalInstallHarness({
|
||||||
const calls: string[] = [];
|
pkgRoot,
|
||||||
const runCommand = async (argv: string[]): Promise<CommandResult> => {
|
npmRootOutput: nodeModules,
|
||||||
const key = argv.join(" ");
|
installCommand: expectedInstallCommand,
|
||||||
calls.push(key);
|
onInstall: async () => writeGlobalPackageVersion(pkgRoot),
|
||||||
if (key === `git -C ${pkgRoot} rev-parse --show-toplevel`) {
|
});
|
||||||
return { stdout: "", stderr: "not a git repository", code: 128 };
|
|
||||||
}
|
|
||||||
if (key === "npm root -g") {
|
|
||||||
return { stdout: nodeModules, stderr: "", code: 0 };
|
|
||||||
}
|
|
||||||
if (key === "pnpm root -g") {
|
|
||||||
return { stdout: "", stderr: "", code: 1 };
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
key ===
|
|
||||||
"npm i -g http://10.211.55.2:8138/openclaw-next.tgz --no-fund --no-audit --loglevel=error"
|
|
||||||
) {
|
|
||||||
await fs.writeFile(
|
|
||||||
path.join(pkgRoot, "package.json"),
|
|
||||||
JSON.stringify({ name: "openclaw", version: "2.0.0" }),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return { stdout: "ok", stderr: "", code: 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
await withEnvAsync(
|
await withEnvAsync(
|
||||||
{ OPENCLAW_UPDATE_PACKAGE_SPEC: "http://10.211.55.2:8138/openclaw-next.tgz" },
|
{ OPENCLAW_UPDATE_PACKAGE_SPEC: "http://10.211.55.2:8138/openclaw-next.tgz" },
|
||||||
@@ -622,27 +585,21 @@ describe("runGatewayUpdate", () => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(calls).toContain(
|
expect(calls).toContain(expectedInstallCommand);
|
||||||
"npm i -g http://10.211.55.2:8138/openclaw-next.tgz --no-fund --no-audit --loglevel=error",
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("updates global bun installs when detected", async () => {
|
it("updates global bun installs when detected", async () => {
|
||||||
const bunInstall = path.join(tempDir, "bun-install");
|
const bunInstall = path.join(tempDir, "bun-install");
|
||||||
await withEnvAsync({ BUN_INSTALL: bunInstall }, async () => {
|
await withEnvAsync({ BUN_INSTALL: bunInstall }, async () => {
|
||||||
const bunGlobalRoot = path.join(bunInstall, "install", "global", "node_modules");
|
const { pkgRoot } = await createGlobalPackageFixture(
|
||||||
const pkgRoot = path.join(bunGlobalRoot, "openclaw");
|
path.join(bunInstall, "install", "global"),
|
||||||
await seedGlobalPackageRoot(pkgRoot);
|
);
|
||||||
|
|
||||||
const { calls, runCommand } = createGlobalInstallHarness({
|
const { calls, runCommand } = createGlobalInstallHarness({
|
||||||
pkgRoot,
|
pkgRoot,
|
||||||
installCommand: "bun add -g openclaw@latest",
|
installCommand: "bun add -g openclaw@latest",
|
||||||
onInstall: async () => {
|
onInstall: async () => {
|
||||||
await fs.writeFile(
|
await writeGlobalPackageVersion(pkgRoot);
|
||||||
path.join(pkgRoot, "package.json"),
|
|
||||||
JSON.stringify({ name: "openclaw", version: "2.0.0" }),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user