|
| 1 | +import fs from "node:fs"; |
1 | 2 | import * as path from "node:path"; |
2 | 3 | import { describe, it } from "vitest"; |
3 | 4 | import { |
4 | 5 | getBasePath, |
5 | 6 | getWranglerHiddenDirPath, |
6 | 7 | readableRelative, |
| 8 | + sweepStaleWranglerTmpDirs, |
7 | 9 | } from "../paths"; |
| 10 | +import { runInTempDir } from "./helpers/run-in-tmp"; |
8 | 11 |
|
9 | 12 | describe("paths", () => { |
10 | 13 | describe("getBasePath()", () => { |
@@ -39,6 +42,54 @@ describe("paths", () => { |
39 | 42 | }); |
40 | 43 | }); |
41 | 44 |
|
| 45 | +describe("sweepStaleWranglerTmpDirs()", () => { |
| 46 | + runInTempDir(); |
| 47 | + |
| 48 | + const ageDir = (dir: string, ageMs: number): void => { |
| 49 | + const seconds = (Date.now() - ageMs) / 1000; |
| 50 | + fs.utimesSync(dir, seconds, seconds); |
| 51 | + }; |
| 52 | + |
| 53 | + it("removes orphaned dirs older than the staleness threshold", ({ |
| 54 | + expect, |
| 55 | + }) => { |
| 56 | + const tmpRoot = path.join(process.cwd(), ".wrangler", "tmp"); |
| 57 | + fs.mkdirSync(tmpRoot, { recursive: true }); |
| 58 | + const stale = path.join(tmpRoot, "bundle-stale"); |
| 59 | + const fresh = path.join(tmpRoot, "bundle-fresh"); |
| 60 | + fs.mkdirSync(stale); |
| 61 | + fs.mkdirSync(fresh); |
| 62 | + ageDir(stale, 2 * 24 * 60 * 60 * 1000); |
| 63 | + |
| 64 | + sweepStaleWranglerTmpDirs(tmpRoot); |
| 65 | + |
| 66 | + expect(fs.existsSync(stale)).toBe(false); |
| 67 | + expect(fs.existsSync(fresh)).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it("does not throw when the tmp root is missing", ({ expect }) => { |
| 71 | + const tmpRoot = path.join(process.cwd(), ".wrangler", "tmp"); |
| 72 | + expect(() => sweepStaleWranglerTmpDirs(tmpRoot)).not.toThrow(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("only sweeps a given root once per process", ({ expect }) => { |
| 76 | + const tmpRoot = path.join(process.cwd(), ".wrangler", "tmp"); |
| 77 | + fs.mkdirSync(tmpRoot, { recursive: true }); |
| 78 | + const orphan = path.join(tmpRoot, "bundle-orphan"); |
| 79 | + fs.mkdirSync(orphan); |
| 80 | + ageDir(orphan, 2 * 24 * 60 * 60 * 1000); |
| 81 | + |
| 82 | + sweepStaleWranglerTmpDirs(tmpRoot); |
| 83 | + expect(fs.existsSync(orphan)).toBe(false); |
| 84 | + |
| 85 | + // Recreate an equally stale entry; the cached root must skip rescanning. |
| 86 | + fs.mkdirSync(orphan); |
| 87 | + ageDir(orphan, 2 * 24 * 60 * 60 * 1000); |
| 88 | + sweepStaleWranglerTmpDirs(tmpRoot); |
| 89 | + expect(fs.existsSync(orphan)).toBe(true); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
42 | 93 | describe("readableRelative", () => { |
43 | 94 | const base = process.cwd(); |
44 | 95 |
|
|
0 commit comments