feat: 添加 retryRm 工具函数及测试

This commit is contained in:
2026-06-11 23:31:33 +08:00
parent ecccf5eef0
commit 3cac492e78
2 changed files with 140 additions and 0 deletions

21
tests/helpers/cleanup.ts Normal file
View File

@@ -0,0 +1,21 @@
import { rm } from "node:fs/promises";
const RETRY_CODES = new Set(["EBUSY", "EPERM"]);
export async function retryRm(
path: string,
{ maxRetries = 5, baseDelay = 100 }: { maxRetries?: number; baseDelay?: number } = {},
): Promise<void> {
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
await rm(path, { recursive: true, force: true });
return;
} catch (err: any) {
if (!RETRY_CODES.has(err.code) || attempt === maxRetries) {
throw err;
}
const delay = baseDelay * 2 ** attempt;
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}