Files
openclaw/test/helpers/temp-dir.test.ts
Mason Huang 5ff247b99e test: prefer auto-cleaning temp dir helper (#93209)
Summary:
- The branch adds `useAutoCleanupTempDirTracker()`, broadens the temp-dir warning reporter to flag new manual helper imports/usages, updates docs, and migrates two script tests to the new helper.
- PR surface: Tests +301, Docs +1, Other +248. Total +550 across 8 files.
- Reproducibility: not applicable. this is test/tooling cleanup, and the changed behavior is exercised through helper/reporter tests and CI evidence rather than a user reproduction path.

Automerge notes:
- PR branch already contained follow-up commit before automerge: test: harden temp dir helper guard
- PR branch already contained follow-up commit before automerge: test: clarify auto cleanup temp dir helper name
- PR branch already contained follow-up commit before automerge: test: cover existing mkdtemp temp dir forms
- PR branch already contained follow-up commit before automerge: test: read staged temp helper source from index

Validation:
- ClawSweeper review passed for head 1fdd7d2a9a.
- Required merge gates passed before the squash merge.

Prepared head SHA: 1fdd7d2a9a
Review: https://github.com/openclaw/openclaw/pull/93209#issuecomment-4705653665

Co-authored-by: Mason Huang <masonxhuang@tencent.com>
Approved-by: hxy91819
2026-07-02 02:46:28 +00:00

70 lines
2.1 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
cleanupTempDirs,
createTempDirTracker,
makeTempDir,
useAutoCleanupTempDirTracker,
} from "./temp-dir.js";
const tempDirs = new Set<string>();
afterEach(() => {
cleanupTempDirs(tempDirs);
});
describe("temp-dir test helpers", () => {
it("keeps a non-executed temp warning fixture for CI proof", () => {
// openclaw-temp-dir: allow test fixture for the temp warning report
const warningFixture = 'tmp.dirSync({ prefix: "openclaw-warning-fixture-" })';
expect(warningFixture).toContain("tmp.dirSync");
});
it("tracks created temp dirs and removes populated dirs", () => {
const tracker = createTempDirTracker();
const dir = tracker.make("openclaw-temp-dir-helper-");
tempDirs.add(dir);
fs.writeFileSync(path.join(dir, "artifact.txt"), "artifact\n", "utf8");
tracker.cleanup();
tempDirs.delete(dir);
expect(fs.existsSync(dir)).toBe(false);
expect([...tracker.dirs]).toEqual([]);
});
it("supports existing caller-owned temp dir collections", () => {
const dir = makeTempDir(tempDirs, "openclaw-temp-dir-existing-");
fs.mkdirSync(path.join(dir, "nested"), { recursive: true });
cleanupTempDirs(tempDirs);
expect(fs.existsSync(dir)).toBe(false);
expect([...tempDirs]).toEqual([]);
});
describe("auto-cleaning tracker", () => {
const createdDirs: string[] = [];
afterEach(() => {
for (const dir of createdDirs.splice(0)) {
expect(fs.existsSync(dir)).toBe(false);
}
expect([...autoCleanupTracker.dirs]).toEqual([]);
});
const autoCleanupTracker = useAutoCleanupTempDirTracker();
it("tracks temp dirs with Vitest cleanup", () => {
const autoCleanedDir = autoCleanupTracker.make("openclaw-temp-dir-auto-");
createdDirs.push(autoCleanedDir);
fs.writeFileSync(path.join(autoCleanedDir, "artifact.txt"), "artifact\n", "utf8");
expect(fs.existsSync(autoCleanedDir)).toBe(true);
expect("cleanup" in autoCleanupTracker).toBe(false);
});
});
});