mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-05 04:33:33 +00:00
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 head1fdd7d2a9a. - Required merge gates passed before the squash merge. Prepared head SHA:1fdd7d2a9aReview: https://github.com/openclaw/openclaw/pull/93209#issuecomment-4705653665 Co-authored-by: Mason Huang <masonxhuang@tencent.com> Approved-by: hxy91819
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
// Test temp directory helper creates and cleans up temporary directories.
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach } from "vitest";
|
|
|
|
// Synchronous temporary directory helpers for tests.
|
|
|
|
export type TempDirCollection = string[] | Set<string>;
|
|
|
|
export interface TestTempDirTracker {
|
|
readonly dirs: ReadonlySet<string>;
|
|
make(prefix: string): string;
|
|
cleanup(): void;
|
|
}
|
|
|
|
export interface AutoCleanupTempDirTracker {
|
|
readonly dirs: ReadonlySet<string>;
|
|
make(prefix: string): string;
|
|
}
|
|
|
|
/** Create a temp dir and register it in an array or set for cleanup. */
|
|
export function makeTempDir(tempDirs: TempDirCollection, prefix: string): string {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
|
if (Array.isArray(tempDirs)) {
|
|
tempDirs.push(dir);
|
|
} else {
|
|
tempDirs.add(dir);
|
|
}
|
|
return dir;
|
|
}
|
|
|
|
/** Remove all tracked temporary directories and clear the tracker. */
|
|
export function cleanupTempDirs(tempDirs: TempDirCollection): void {
|
|
const dirs = Array.isArray(tempDirs) ? tempDirs.splice(0) : [...tempDirs];
|
|
for (const dir of dirs) {
|
|
fs.rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 20 });
|
|
}
|
|
if (!Array.isArray(tempDirs)) {
|
|
tempDirs.clear();
|
|
}
|
|
}
|
|
|
|
export function createTempDirTracker(): TestTempDirTracker {
|
|
const dirs = new Set<string>();
|
|
return {
|
|
dirs,
|
|
make(prefix: string): string {
|
|
return makeTempDir(dirs, prefix);
|
|
},
|
|
cleanup(): void {
|
|
cleanupTempDirs(dirs);
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Create a temp dir tracker that Vitest cleans up after each test. */
|
|
export function useAutoCleanupTempDirTracker(): AutoCleanupTempDirTracker {
|
|
const tracker = createTempDirTracker();
|
|
afterEach(() => {
|
|
tracker.cleanup();
|
|
});
|
|
return {
|
|
dirs: tracker.dirs,
|
|
make(prefix: string): string {
|
|
return tracker.make(prefix);
|
|
},
|
|
};
|
|
}
|