Skip to content

Commit 3edac64

Browse files
unit 1 of the #1914 split (6 files, whole-file module cut)
1 parent e487cd7 commit 3edac64

6 files changed

Lines changed: 2936 additions & 6 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as fs from "fs/promises"
2+
import * as os from "os"
3+
import * as path from "path"
4+
5+
import { safeWriteText } from "../safeWriteText"
6+
7+
// No fs mocks in this file: the point is to assert what a real filesystem ends up
8+
// holding after a publish attempt, which the mocked spec cannot show. The failure is
9+
// provoked with real filesystem semantics rather than with a stubbed call.
10+
describe("safeWriteText against a real filesystem", () => {
11+
let dir: string
12+
13+
beforeEach(async () => {
14+
dir = await fs.mkdtemp(path.join(os.tmpdir(), "safe-write-text-int-"))
15+
})
16+
17+
afterEach(async () => {
18+
await fs.rm(dir, { recursive: true, force: true })
19+
})
20+
21+
it("publishes the new bytes and leaves no staging or backup residue", async () => {
22+
const targetPath = path.join(dir, "target.txt")
23+
await fs.writeFile(targetPath, "old bytes")
24+
25+
// No platform override: the real platform's own durability and ACL steps run, and this
26+
// case is the successful restore. A failed restore is best-effort: it is logged and
27+
// the publish still resolves after the commit rename, so the residue assertions
28+
// below hold whichever way icacls went on the machine running the test.
29+
await safeWriteText(targetPath, "new bytes", { backup: true })
30+
31+
expect(await fs.readFile(targetPath, "utf8")).toBe("new bytes")
32+
expect(await fs.readdir(dir)).toEqual(["target.txt"])
33+
})
34+
35+
it("leaves the target bytes untouched when the commit cannot replace it", async () => {
36+
// A regular file cannot be renamed over a directory, so the backup copy and
37+
// the commit both fail on a real filesystem with no mocking at all.
38+
const targetPath = path.join(dir, "target-dir")
39+
await fs.mkdir(targetPath)
40+
const inside = path.join(targetPath, "payload.txt")
41+
await fs.writeFile(inside, "original bytes")
42+
43+
await expect(safeWriteText(targetPath, "new data", { backup: true })).rejects.toThrow()
44+
45+
// The directory and its content are exactly as they were, and no backup copy
46+
// or staging directory was left behind next to them.
47+
expect(await fs.readFile(inside, "utf8")).toBe("original bytes")
48+
expect(await fs.readdir(dir)).toEqual(["target-dir"])
49+
})
50+
51+
it("cleans up the staging file when the commit rename itself fails", async () => {
52+
// With backup:false the backup step is skipped, so this is the only shape of
53+
// this scenario that reaches Step 4: the rename of the staging FILE over the
54+
// target DIRECTORY fails (EISDIR / ENOTDIR / EPERM depending on platform), and
55+
// the cleanup must remove the staging file. The backup:true case above bails
56+
// out in the backup step and never exercises the commit-failure path.
57+
const targetPath = path.join(dir, "target-dir")
58+
await fs.mkdir(targetPath)
59+
const inside = path.join(targetPath, "payload.txt")
60+
await fs.writeFile(inside, "original bytes")
61+
62+
await expect(safeWriteText(targetPath, "new data", { backup: false })).rejects.toThrow()
63+
64+
expect(await fs.readFile(inside, "utf8")).toBe("original bytes")
65+
expect(await fs.readdir(dir)).toEqual(["target-dir"])
66+
})
67+
})

0 commit comments

Comments
 (0)