mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-23 22:18:14 +00:00
Summary: - Merged test: add temp directory helper guidance after ClawSweeper review. Automerge notes: - PR branch already contained follow-up commit before automerge: fix(scripts): honor temp report failure mode - PR branch already contained follow-up commit before automerge: fix(scripts): reduce temp report noise - PR branch already contained follow-up commit before automerge: fix(scripts): cover test support temp reports - PR branch already contained follow-up commit before automerge: fix(scripts): report temp use in test helpers - PR branch already contained follow-up commit before automerge: fix(scripts): broaden temp report test surface - PR branch already contained follow-up commit before automerge: fix(scripts): cover nested test temp reports Validation: - ClawSweeper review passed for head132f14a381. - Required merge gates passed before the squash merge. Prepared head SHA:132f14a381Review: https://github.com/openclaw/openclaw/pull/87298#issuecomment-4704338581 Co-authored-by: masonxhuang <masonxhuang@tencent.com> Co-authored-by: Mason Huang <masonxhuang@tencent.com> Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com> Co-authored-by: clawsweeper[bot] <274271284+clawsweeper[bot]@users.noreply.github.com> Approved-by: hxy91819 Co-authored-by: hxy91819 <8814856+hxy91819@users.noreply.github.com>
50 lines
1.4 KiB
TypeScript
50 lines
1.4 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";
|
|
|
|
// 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;
|
|
}
|
|
|
|
/** 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);
|
|
},
|
|
};
|
|
}
|