|
1 | | -import { describe, it, expect, afterEach } from "vitest"; |
| 1 | +import { describe, it, expect, afterEach, vi } from "vitest"; |
| 2 | +import { promises as fsp } from "fs"; |
2 | 3 | import { |
3 | 4 | normalizePath, |
4 | 5 | normalizePathFormat, |
5 | 6 | expandHome, |
6 | 7 | convertToWindowsPath, |
| 8 | + realpathWithRetry, |
7 | 9 | } from "../path-utils.js"; |
8 | 10 |
|
9 | 11 | describe("Path Utilities", () => { |
@@ -421,3 +423,34 @@ describe("Path Utilities", () => { |
421 | 423 | }); |
422 | 424 | }); |
423 | 425 | }); |
| 426 | + |
| 427 | +// #5185: the directories resolved once and kept get one retry. |
| 428 | +describe("realpathWithRetry", () => { |
| 429 | + afterEach(() => { |
| 430 | + vi.restoreAllMocks(); |
| 431 | + }); |
| 432 | + |
| 433 | + it("returns the first call's result without retrying", async () => { |
| 434 | + const realpath = vi.spyOn(fsp, "realpath").mockResolvedValue("/real"); |
| 435 | + expect(await realpathWithRetry("/given", 0)).toBe("/real"); |
| 436 | + expect(realpath).toHaveBeenCalledTimes(1); |
| 437 | + }); |
| 438 | + |
| 439 | + it("retries once after a failure and returns the retry's result", async () => { |
| 440 | + const realpath = vi |
| 441 | + .spyOn(fsp, "realpath") |
| 442 | + .mockRejectedValueOnce(new Error("ENOENT")) |
| 443 | + .mockResolvedValueOnce("/real"); |
| 444 | + expect(await realpathWithRetry("/given", 0)).toBe("/real"); |
| 445 | + expect(realpath).toHaveBeenCalledTimes(2); |
| 446 | + }); |
| 447 | + |
| 448 | + it("rejects with the retry's error when both calls fail", async () => { |
| 449 | + const realpath = vi |
| 450 | + .spyOn(fsp, "realpath") |
| 451 | + .mockRejectedValueOnce(new Error("first")) |
| 452 | + .mockRejectedValueOnce(new Error("second")); |
| 453 | + await expect(realpathWithRetry("/given", 0)).rejects.toThrow("second"); |
| 454 | + expect(realpath).toHaveBeenCalledTimes(2); |
| 455 | + }); |
| 456 | +}); |
0 commit comments