fix: stabilize npm owner update test on Windows (#60153) (thanks @jayeshp19)

This commit is contained in:
Peter Steinberger
2026-04-03 21:39:58 +09:00
parent b9ede82cc2
commit ab57d24f79
2 changed files with 54 additions and 44 deletions

View File

@@ -137,52 +137,61 @@ describe("update global helpers", () => {
});
it("prefers the owning npm prefix when PATH npm points at a different global root", async () => {
const base = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-update-npm-prefix-"));
const brewPrefix = path.join(base, "opt", "homebrew");
const brewBin = path.join(brewPrefix, "bin");
const brewRoot = path.join(brewPrefix, "lib", "node_modules");
const pkgRoot = path.join(brewRoot, "openclaw");
const pathNpmRoot = path.join(base, "nvm", "lib", "node_modules");
const brewNpm = path.join(brewBin, process.platform === "win32" ? "npm.cmd" : "npm");
await fs.mkdir(pkgRoot, { recursive: true });
await fs.mkdir(brewBin, { recursive: true });
await fs.writeFile(brewNpm, "", "utf8");
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
try {
const base = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-update-npm-prefix-"));
const brewPrefix = path.join(base, "opt", "homebrew");
const brewBin = path.join(brewPrefix, "bin");
const brewRoot = path.join(brewPrefix, "lib", "node_modules");
const pkgRoot = path.join(brewRoot, "openclaw");
const pathNpmRoot = path.join(base, "nvm", "lib", "node_modules");
const brewNpm = path.join(brewBin, "npm");
await fs.mkdir(pkgRoot, { recursive: true });
await fs.mkdir(brewBin, { recursive: true });
await fs.writeFile(brewNpm, "", "utf8");
const runCommand: CommandRunner = async (argv) => {
if (argv[0] === "npm") {
return { stdout: `${pathNpmRoot}\n`, stderr: "", code: 0 };
}
if (argv[0] === brewNpm) {
return { stdout: `${brewRoot}\n`, stderr: "", code: 0 };
}
if (argv[0] === "pnpm") {
return { stdout: "", stderr: "", code: 1 };
}
throw new Error(`unexpected command: ${argv.join(" ")}`);
};
const runCommand: CommandRunner = async (argv) => {
if (argv[0] === "npm") {
return { stdout: `${pathNpmRoot}\n`, stderr: "", code: 0 };
}
if (argv[0] === brewNpm) {
return { stdout: `${brewRoot}\n`, stderr: "", code: 0 };
}
if (argv[0] === "pnpm") {
return { stdout: "", stderr: "", code: 1 };
}
throw new Error(`unexpected command: ${argv.join(" ")}`);
};
await expect(detectGlobalInstallManagerForRoot(runCommand, pkgRoot, 1000)).resolves.toBe("npm");
await expect(resolveGlobalRoot("npm", runCommand, 1000, pkgRoot)).resolves.toBe(brewRoot);
await expect(resolveGlobalPackageRoot("npm", runCommand, 1000, pkgRoot)).resolves.toBe(pkgRoot);
expect(globalInstallArgs("npm", "openclaw@latest", pkgRoot)).toEqual([
brewNpm,
"i",
"-g",
"openclaw@latest",
"--no-fund",
"--no-audit",
"--loglevel=error",
]);
expect(globalInstallFallbackArgs("npm", "openclaw@latest", pkgRoot)).toEqual([
brewNpm,
"i",
"-g",
"openclaw@latest",
"--omit=optional",
"--no-fund",
"--no-audit",
"--loglevel=error",
]);
await expect(detectGlobalInstallManagerForRoot(runCommand, pkgRoot, 1000)).resolves.toBe(
"npm",
);
await expect(resolveGlobalRoot("npm", runCommand, 1000, pkgRoot)).resolves.toBe(brewRoot);
await expect(resolveGlobalPackageRoot("npm", runCommand, 1000, pkgRoot)).resolves.toBe(
pkgRoot,
);
expect(globalInstallArgs("npm", "openclaw@latest", pkgRoot)).toEqual([
brewNpm,
"i",
"-g",
"openclaw@latest",
"--no-fund",
"--no-audit",
"--loglevel=error",
]);
expect(globalInstallFallbackArgs("npm", "openclaw@latest", pkgRoot)).toEqual([
brewNpm,
"i",
"-g",
"openclaw@latest",
"--omit=optional",
"--no-fund",
"--no-audit",
"--loglevel=error",
]);
} finally {
platformSpy.mockRestore();
}
});
it("does not infer npm ownership from path shape alone when the owning npm binary is absent", async () => {