Skip to content

Commit 1df264d

Browse files
authored
test: use per-run temp roots in update-cli and migrate suites (#125171)
update-cli.test.ts used a fixed /tmp/openclaw-update-tests root with deterministic case-dir names; some cases write real fixture files there and cleanup rm -rf's them, so concurrent runs on one machine (CI shards, sibling checkouts) deleted each other's live fixtures — observed as 34 spurious failures on PR #124997 run 31992421445. migrate.test.ts had the same shape via a fixed mocked resolveStateDir root rm -rf'd in beforeEach/afterEach. Both suites now use per-run mkdtemp roots, realpath'd for the macOS /var -> /private/var symlink, removed on suite exit.
1 parent 185b1ab commit 1df264d

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

‎src/cli/update-cli.test.ts‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,18 @@ type UpdateCliScenario = {
530530
};
531531

532532
describe("update-cli", () => {
533-
const fixtureRoot = "/tmp/openclaw-update-tests";
533+
// Per-run unique root: concurrent runs on one machine (CI shards, sibling checkouts) must
534+
// never share fixture paths — some cases write real files and rm them in cleanup. Realpath'd
535+
// because macOS os.tmpdir() is a /var -> /private/var symlink.
536+
const fixtureRoot = fsSync.realpathSync(
537+
fsSync.mkdtempSync(path.join(os.tmpdir(), "openclaw-update-tests-")),
538+
);
534539
let fixtureCount = 0;
535540
const tempDirsToCleanup = new Set<string>();
536541

537542
const createCaseDir = (prefix: string) => {
538543
const dir = path.join(fixtureRoot, `${prefix}-${fixtureCount++}`);
539-
// Tests only need a stable path; the directory does not have to exist because all I/O is mocked.
544+
// Callers that only need a stable path skip creating it; real-I/O callers mkdir themselves.
540545
return dir;
541546
};
542547

@@ -1487,8 +1492,9 @@ describe("update-cli", () => {
14871492
setStdoutTty(false);
14881493
});
14891494

1490-
afterAll(() => {
1495+
afterAll(async () => {
14911496
serviceEnvSnapshot.restore();
1497+
await fs.rm(fixtureRoot, { recursive: true, force: true });
14921498
});
14931499

14941500
afterEach(async () => {
@@ -1668,7 +1674,8 @@ describe("update-cli", () => {
16681674
candidate === path.join(root, "package.json") ||
16691675
entrypoints.includes(candidate),
16701676
);
1671-
const managedState = "/tmp/openclaw-update-tests/managed-profile";
1677+
const managedState = path.join(fixtureRoot, "managed-profile");
1678+
const personalState = path.join(fixtureRoot, "personal-profile");
16721679
const managedConfig = {
16731680
...baseConfig,
16741681
update: { channel: "beta" as const },
@@ -1723,8 +1730,8 @@ describe("update-cli", () => {
17231730
await withEnvAsync(
17241731
{
17251732
OPENCLAW_PROFILE: "personal",
1726-
OPENCLAW_STATE_DIR: "/tmp/openclaw-update-tests/personal-profile",
1727-
OPENCLAW_CONFIG_PATH: "/tmp/openclaw-update-tests/personal-profile/openclaw.json",
1733+
OPENCLAW_STATE_DIR: personalState,
1734+
OPENCLAW_CONFIG_PATH: path.join(personalState, "openclaw.json"),
17281735
OPENCLAW_GATEWAY_PORT: "19111",
17291736
},
17301737
async () => {
@@ -1746,6 +1753,7 @@ describe("update-cli", () => {
17461753
});
17471754

17481755
it("keeps foreign-service updates in the caller profile", async () => {
1756+
const personalState = path.join(fixtureRoot, "personal-profile");
17491757
const { root, entrypoints } = setupUpdatedRootRefresh();
17501758
const foreignRoot = await createTrackedTempDir("openclaw-update-foreign-profile-");
17511759
const foreignEntrypoint = path.join(foreignRoot, "dist", "index.js");
@@ -1768,7 +1776,7 @@ describe("update-cli", () => {
17681776
programArguments: ["node", foreignEntrypoint, "gateway", "run"],
17691777
environment: {
17701778
OPENCLAW_PROFILE: "foreign",
1771-
OPENCLAW_STATE_DIR: "/tmp/openclaw-update-tests/foreign-profile",
1779+
OPENCLAW_STATE_DIR: path.join(fixtureRoot, "foreign-profile"),
17721780
OPENCLAW_GATEWAY_PORT: "19333",
17731781
},
17741782
});
@@ -1782,7 +1790,7 @@ describe("update-cli", () => {
17821790
await withEnvAsync(
17831791
{
17841792
OPENCLAW_PROFILE: "personal",
1785-
OPENCLAW_STATE_DIR: "/tmp/openclaw-update-tests/personal-profile",
1793+
OPENCLAW_STATE_DIR: personalState,
17861794
OPENCLAW_GATEWAY_PORT: "19111",
17871795
},
17881796
async () => {
@@ -1794,7 +1802,7 @@ describe("update-cli", () => {
17941802
expect(serviceEnabled).not.toHaveBeenCalled();
17951803
expect(spawnCall()?.[2]?.env).toMatchObject({
17961804
OPENCLAW_PROFILE: "personal",
1797-
OPENCLAW_STATE_DIR: "/tmp/openclaw-update-tests/personal-profile",
1805+
OPENCLAW_STATE_DIR: personalState,
17981806
OPENCLAW_GATEWAY_PORT: "19111",
17991807
});
18001808
});
@@ -1810,7 +1818,7 @@ describe("update-cli", () => {
18101818
after: { sha: "new-managed-sha", version: "2026.4.27" },
18111819
}),
18121820
);
1813-
const managedState = "/tmp/openclaw-update-tests/fallback-managed";
1821+
const managedState = path.join(fixtureRoot, "fallback-managed");
18141822
serviceReadCommand.mockResolvedValue({
18151823
programArguments: ["node", path.join(process.cwd(), "dist", "index.js"), "gateway", "run"],
18161824
environment: {
@@ -1835,7 +1843,7 @@ describe("update-cli", () => {
18351843
await withEnvAsync(
18361844
{
18371845
OPENCLAW_PROFILE: "personal",
1838-
OPENCLAW_STATE_DIR: "/tmp/openclaw-update-tests/fallback-personal",
1846+
OPENCLAW_STATE_DIR: path.join(fixtureRoot, "fallback-personal"),
18391847
OPENCLAW_GATEWAY_PORT: "19111",
18401848
},
18411849
async () => {
@@ -1868,7 +1876,7 @@ describe("update-cli", () => {
18681876
candidate === path.join(process.cwd(), "package.json") ||
18691877
candidate === path.join(process.cwd(), "openclaw.mjs"),
18701878
);
1871-
const managedState = "/tmp/openclaw-update-tests/restart-doctor-managed";
1879+
const managedState = path.join(fixtureRoot, "restart-doctor-managed");
18721880
serviceReadCommand.mockResolvedValue({
18731881
programArguments: ["node", path.join(process.cwd(), "dist", "index.js"), "gateway", "run"],
18741882
environment: {
@@ -1890,7 +1898,7 @@ describe("update-cli", () => {
18901898
await withEnvAsync(
18911899
{
18921900
OPENCLAW_PROFILE: "personal",
1893-
OPENCLAW_STATE_DIR: "/tmp/openclaw-update-tests/restart-doctor-personal",
1901+
OPENCLAW_STATE_DIR: path.join(fixtureRoot, "restart-doctor-personal"),
18941902
OPENCLAW_GATEWAY_PORT: "19111",
18951903
},
18961904
async () => {

‎src/commands/migrate.test.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,18 @@ vi.mock("../config/config.js", () => ({
3737
loadConfig: () => ({}),
3838
}));
3939

40+
// Per-run unique + realpath'd state dir: the apply flow writes real report dirs under it and
41+
// the suite rm -rf's it, so concurrent runs on one machine must never share the root (macOS
42+
// os.tmpdir() is a /var -> /private/var symlink).
43+
const testStateDir = await vi.hoisted(async () => {
44+
const { mkdtemp, realpath } = await import("node:fs/promises");
45+
const { tmpdir } = await import("node:os");
46+
const { join } = await import("node:path");
47+
return await realpath(await mkdtemp(join(tmpdir(), "openclaw-migrate-command-test-")));
48+
});
49+
4050
vi.mock("../config/paths.js", () => ({
41-
resolveStateDir: () => "/tmp/openclaw-migrate-command-test",
51+
resolveStateDir: () => testStateDir,
4252
}));
4353

4454
vi.mock("../cli/prompt.js", () => ({
@@ -290,7 +300,7 @@ describe("migrateApplyCommand", () => {
290300
const originalIsTty = process.stdin.isTTY;
291301

292302
beforeEach(async () => {
293-
await fs.rm("/tmp/openclaw-migrate-command-test", { force: true, recursive: true });
303+
await fs.rm(testStateDir, { force: true, recursive: true });
294304
Object.defineProperty(process.stdin, "isTTY", {
295305
configurable: true,
296306
value: false,
@@ -317,7 +327,7 @@ describe("migrateApplyCommand", () => {
317327
configurable: true,
318328
value: originalIsTty,
319329
});
320-
await fs.rm("/tmp/openclaw-migrate-command-test", { force: true, recursive: true });
330+
await fs.rm(testStateDir, { force: true, recursive: true });
321331
vi.clearAllMocks();
322332
});
323333

0 commit comments

Comments
 (0)