test(update-cli): cleanup temp dirs after each case (#78408)

This commit is contained in:
Mason Huang
2026-05-06 18:20:47 +08:00
committed by GitHub
parent 1c2915677b
commit cbba122cdd

View File

@@ -4,7 +4,7 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { Command } from "commander";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { TEST_BUNDLED_RUNTIME_SIDECAR_PATHS } from "../../test/helpers/bundled-runtime-sidecars.js";
import type { OpenClawConfig, ConfigFileSnapshot } from "../config/types.openclaw.js";
import { writePackageDistInventory } from "../infra/package-dist-inventory.js";
@@ -269,6 +269,7 @@ type UpdateCliScenario = {
describe("update-cli", () => {
const fixtureRoot = "/tmp/openclaw-update-tests";
let fixtureCount = 0;
const tempDirsToCleanup = new Set<string>();
const createCaseDir = (prefix: string) => {
const dir = path.join(fixtureRoot, `${prefix}-${fixtureCount++}`);
@@ -276,6 +277,12 @@ describe("update-cli", () => {
return dir;
};
const createTrackedTempDir = async (prefix: string) => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
tempDirsToCleanup.add(dir);
return dir;
};
const baseConfig = {} as OpenClawConfig;
const baseSnapshot: ConfigFileSnapshot = {
path: "/tmp/openclaw-config.json",
@@ -560,6 +567,18 @@ describe("update-cli", () => {
setStdoutTty(false);
});
afterEach(async () => {
if (tempDirsToCleanup.size === 0) {
return;
}
await Promise.allSettled(
[...tempDirsToCleanup].map(async (dir) => {
await fs.rm(dir, { recursive: true, force: true });
}),
);
tempDirsToCleanup.clear();
});
it("bounds completion cache refresh during update follow-up", async () => {
const root = createCaseDir("openclaw-completion-timeout");
pathExists.mockResolvedValue(true);
@@ -1746,7 +1765,7 @@ describe("update-cli", () => {
});
it("stops package post-update work when staged npm install verification fails", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-update-staged-fail-"));
const tempDir = await createTrackedTempDir("openclaw-update-staged-fail-");
const prefix = path.join(tempDir, "prefix");
const nodeModules = path.join(prefix, "lib", "node_modules");
const pkgRoot = path.join(nodeModules, "openclaw");
@@ -1828,7 +1847,7 @@ describe("update-cli", () => {
});
it("marks package post-update doctor as update-in-progress", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-update-package-"));
const tempDir = await createTrackedTempDir("openclaw-update-package-");
const nodeModules = path.join(tempDir, "node_modules");
const pkgRoot = path.join(nodeModules, "openclaw");
const entryPath = path.join(pkgRoot, "dist", "index.js");
@@ -1883,7 +1902,7 @@ describe("update-cli", () => {
});
it("stops a running managed gateway before package replacement", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-update-stop-service-"));
const tempDir = await createTrackedTempDir("openclaw-update-stop-service-");
const nodeModules = path.join(tempDir, "node_modules");
const pkgRoot = path.join(nodeModules, "openclaw");
const entryPath = path.join(pkgRoot, "dist", "index.js");
@@ -1970,7 +1989,7 @@ describe("update-cli", () => {
});
it("refreshes package installs even when the current version already matches the target", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-update-current-"));
const tempDir = await createTrackedTempDir("openclaw-update-current-");
const nodeModules = path.join(tempDir, "node_modules");
const pkgRoot = path.join(nodeModules, "openclaw");
const entryPath = path.join(pkgRoot, "dist", "index.js");
@@ -2053,7 +2072,7 @@ describe("update-cli", () => {
});
it("retries package updates without optional deps when npm global update fails", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-update-optional-"));
const tempDir = await createTrackedTempDir("openclaw-update-optional-");
const nodeModules = path.join(tempDir, "node_modules");
const pkgRoot = path.join(nodeModules, "openclaw");
mockPackageInstallStatus(pkgRoot);