|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { replaceFileAtomically } from "./atomicFile.js"; |
| 7 | + |
| 8 | +describe("replaceFileAtomically", () => { |
| 9 | + const dirs: string[] = []; |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); |
| 13 | + dirs.length = 0; |
| 14 | + }); |
| 15 | + |
| 16 | + it("writes the sibling completely before replacing the project file", () => { |
| 17 | + const dir = mkdtempSync(join(tmpdir(), "atomic-file-test-")); |
| 18 | + dirs.push(dir); |
| 19 | + const file = join(dir, "index.html"); |
| 20 | + writeFileSync(file, "old", { mode: 0o640 }); |
| 21 | + const events: string[] = []; |
| 22 | + const operations = { |
| 23 | + writeFileSync: (path: fs.PathLike, ...args: any[]) => { |
| 24 | + events.push(`write:${String(path)}`); |
| 25 | + return fs.writeFileSync(path, ...args); |
| 26 | + }, |
| 27 | + chmodSync: fs.chmodSync, |
| 28 | + renameSync: (from: fs.PathLike, to: fs.PathLike) => { |
| 29 | + events.push(`rename:${String(from)}:${String(to)}`); |
| 30 | + expect(readFileSync(from, "utf-8")).toBe("new complete html"); |
| 31 | + return fs.renameSync(from, to); |
| 32 | + }, |
| 33 | + unlinkSync: fs.unlinkSync, |
| 34 | + }; |
| 35 | + |
| 36 | + replaceFileAtomically(file, "new complete html", 0o640, operations); |
| 37 | + |
| 38 | + expect(events).toHaveLength(2); |
| 39 | + expect(events[0]).toMatch(new RegExp(`^write:${file}\\.\\d+\\.[0-9a-f-]+\\.tmp$`)); |
| 40 | + const tempPath = events[0]!.slice("write:".length); |
| 41 | + expect(events[1]).toBe(`rename:${tempPath}:${file}`); |
| 42 | + expect(readFileSync(file, "utf-8")).toBe("new complete html"); |
| 43 | + expect(fs.statSync(file).mode & 0o777).toBe(0o640); |
| 44 | + }); |
| 45 | + |
| 46 | + it("replaces an existing destination", () => { |
| 47 | + const dir = mkdtempSync(join(tmpdir(), "atomic-file-replace-test-")); |
| 48 | + dirs.push(dir); |
| 49 | + const file = join(dir, "index.html"); |
| 50 | + writeFileSync(file, "old destination"); |
| 51 | + |
| 52 | + replaceFileAtomically(file, "new destination", 0o640); |
| 53 | + |
| 54 | + expect(readFileSync(file, "utf-8")).toBe("new destination"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("allocates a distinct temporary sibling for each writer", () => { |
| 58 | + const dir = mkdtempSync(join(tmpdir(), "atomic-file-unique-test-")); |
| 59 | + dirs.push(dir); |
| 60 | + const file = join(dir, "index.html"); |
| 61 | + writeFileSync(file, "old"); |
| 62 | + const tempPaths: string[] = []; |
| 63 | + const operations = { |
| 64 | + writeFileSync: (path: fs.PathLike, ...args: any[]) => { |
| 65 | + tempPaths.push(String(path)); |
| 66 | + return fs.writeFileSync(path, ...args); |
| 67 | + }, |
| 68 | + chmodSync: fs.chmodSync, |
| 69 | + renameSync: fs.renameSync, |
| 70 | + unlinkSync: fs.unlinkSync, |
| 71 | + }; |
| 72 | + |
| 73 | + replaceFileAtomically(file, "first", 0o640, operations); |
| 74 | + replaceFileAtomically(file, "second", 0o640, operations); |
| 75 | + |
| 76 | + expect(tempPaths).toHaveLength(2); |
| 77 | + expect(tempPaths[0]).not.toBe(tempPaths[1]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("uses a unique temporary sibling and removes it when publication fails", () => { |
| 81 | + const dir = mkdtempSync(join(tmpdir(), "atomic-file-failure-test-")); |
| 82 | + dirs.push(dir); |
| 83 | + const file = join(dir, "index.html"); |
| 84 | + writeFileSync(file, "old"); |
| 85 | + const tempPaths: string[] = []; |
| 86 | + const removed: string[] = []; |
| 87 | + const operations = { |
| 88 | + writeFileSync: (path: fs.PathLike, ...args: any[]) => { |
| 89 | + tempPaths.push(String(path)); |
| 90 | + return fs.writeFileSync(path, ...args); |
| 91 | + }, |
| 92 | + chmodSync: fs.chmodSync, |
| 93 | + renameSync: () => { |
| 94 | + throw new Error("publish failed"); |
| 95 | + }, |
| 96 | + unlinkSync: (path: fs.PathLike) => { |
| 97 | + removed.push(String(path)); |
| 98 | + return fs.unlinkSync(path); |
| 99 | + }, |
| 100 | + }; |
| 101 | + |
| 102 | + expect(() => replaceFileAtomically(file, "new", 0o640, operations)).toThrow("publish failed"); |
| 103 | + expect(tempPaths).toHaveLength(1); |
| 104 | + expect(tempPaths[0]).toMatch(new RegExp(`^${file}\\.\\d+\\.[0-9a-f-]+\\.tmp$`)); |
| 105 | + expect(removed).toEqual(tempPaths); |
| 106 | + }); |
| 107 | +}); |
0 commit comments