Files
Rune-Spec/tests/helpers/cleanup.test.ts

69 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from "bun:test";
import { mkdir, access } from "node:fs/promises";
import { join } from "node:path";
import { retryRm } from "./cleanup.ts";
const TMP_DIR = join(import.meta.dir, "__tmp_cleanup_test__");
async function dirExists(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
describe("retryRm", () => {
it("删除存在的目录", async () => {
await mkdir(TMP_DIR, { recursive: true });
await retryRm(TMP_DIR);
expect(await dirExists(TMP_DIR)).toBe(false);
});
it("删除不存在的路径不报错force:true", async () => {
const nonExist = join(import.meta.dir, "__non_exist__");
await expect(retryRm(nonExist)).resolves.toBeUndefined();
});
it("EBUSY 后重试成功", async () => {
let callCount = 0;
const fakeRm = async () => {
callCount++;
if (callCount === 1) {
const err: any = new Error("EBUSY");
err.code = "EBUSY";
throw err;
}
};
await retryRm("/some/path", { maxRetries: 3, baseDelay: 10, _rm: fakeRm as any });
expect(callCount).toBe(2);
});
it("maxRetries 耗尽后抛出最后一个错误", async () => {
let callCount = 0;
const fakeRm = async () => {
callCount++;
const err: any = new Error("EBUSY");
err.code = "EBUSY";
throw err;
};
await expect(
retryRm("/some/path", { maxRetries: 2, baseDelay: 10, _rm: fakeRm as any }),
).rejects.toThrow("EBUSY");
expect(callCount).toBe(3);
});
it("ENOENT 错误直接抛出不重试", async () => {
let callCount = 0;
const fakeRm = async () => {
callCount++;
const err: any = new Error("ENOENT: no such file or directory");
err.code = "ENOENT";
throw err;
};
await expect(retryRm("/some/path", { _rm: fakeRm as any })).rejects.toThrow("ENOENT");
expect(callCount).toBe(1);
});
});