feat: 添加 retryRm 工具函数及测试
This commit is contained in:
21
tests/helpers/cleanup.ts
Normal file
21
tests/helpers/cleanup.ts
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user