Skip to content

Commit 6d0bc87

Browse files
test(file-safety): pin the durability wrapper's cause and the exact staging release
Port of the U3 review follow-ups to this unit's copies: the injected fsync failure targets a real directory descriptor and the test asserts the PostCommitDurabilityError carries it as its cause; the staging-release assertions compare the exact directory path read back from the create instead of a substring.
1 parent 75dd374 commit 6d0bc87

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

‎src/services/file-safety/__tests__/safeWriteText.spec.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@ describe("safeWriteText", () => {
143143

144144
await safeWriteText(targetPath, "hello", { platform: "linux" })
145145

146-
// the staging subdir is removed best-effort after the commit rename
147-
// (stringContaining: the SUT and the test helper resolve Windows
148-
// drive-relative paths differently, as in the existing staging tests)
146+
// the staging subdir is removed best-effort after the commit rename.
147+
// Exact path read back from the create: the SUT releases the same
148+
// directory it staged in, and a substring match would also accept a
149+
// release aimed at some other staging directory.
149150
expect(fs.rmdir).toHaveBeenCalledTimes(1)
150-
expect(fs.rmdir).toHaveBeenCalledWith(expect.stringContaining(".file-safety-staging"))
151+
const stagingDir = String(vi.mocked(fsSync.mkdirSync).mock.calls[0]?.[0])
152+
expect(stagingDir).toContain(".file-safety-staging")
153+
expect(fs.rmdir).toHaveBeenCalledWith(stagingDir)
151154
// the win32 DACL restore gate must stay closed on other platforms:
152155
// no icacls save or restore is attempted
153156
expect(execFile).not.toHaveBeenCalled()
@@ -163,7 +166,9 @@ describe("safeWriteText", () => {
163166
await expect(safeWriteText(targetPath, "hello")).resolves.toBeUndefined()
164167

165168
expect(fs.rmdir).toHaveBeenCalledTimes(1)
166-
expect(fs.rmdir).toHaveBeenCalledWith(expect.stringContaining(".file-safety-staging"))
169+
const stagingDir = String(vi.mocked(fsSync.mkdirSync).mock.calls[0]?.[0])
170+
expect(stagingDir).toContain(".file-safety-staging")
171+
expect(fs.rmdir).toHaveBeenCalledWith(stagingDir)
167172
if (process.platform === "win32") {
168173
// default platform is win32: the DACL save + restore still ran
169174
// through the default icacls path (options?.execFileRunner must
@@ -197,7 +202,9 @@ describe("safeWriteText", () => {
197202
// the commit rename still happened and the rmdir error was swallowed
198203
expect(fs.rename).toHaveBeenCalledWith(expect.stringContaining(".file-safety-staging"), targetPath)
199204
expect(fs.rmdir).toHaveBeenCalledTimes(1)
200-
expect(fs.rmdir).toHaveBeenCalledWith(expect.stringContaining(".file-safety-staging"))
205+
const stagingDir = String(vi.mocked(fsSync.mkdirSync).mock.calls[0]?.[0])
206+
expect(stagingDir).toContain(".file-safety-staging")
207+
expect(fs.rmdir).toHaveBeenCalledWith(stagingDir)
201208
})
202209

203210
it("gives each self-staged write its own staging directory so a concurrent write cannot remove it", async () => {

‎src/utils/__tests__/safeWriteJson.test.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -707,29 +707,37 @@ describe("safeWriteJson", () => {
707707
const fsSyncMod = await import("fs")
708708
const realOpen = fsSyncMod.openSync
709709
const realFsync = fsSyncMod.fsyncSync
710-
// A sentinel fd identifies the parent-directory open that safeWriteText
711-
// performs after the commit rename; only that fsync fails.
712-
const sentinel = 987654
710+
// The parent-directory open that safeWriteText performs after the commit
711+
// rename gets a REAL descriptor: safeWriteText closes it in its finally
712+
// block, and a fabricated number would make closeSync fail with EBADF -
713+
// a second failure behind the same wrapper, hiding which fsync broke.
714+
const fsyncFailure = new Error("EIO fsync")
715+
let dirFd: number | undefined
713716
vi.spyOn(process, "platform", "get").mockReturnValue("linux")
714717
vi.spyOn(fsSyncMod, "openSync").mockImplementation(((...args: unknown[]) => {
715718
if (String(args[0]) === tempDir) {
716-
return sentinel
719+
dirFd = (realOpen as (p: string, f: string) => number)(tempDir, "r")
720+
return dirFd
717721
}
718722
return (realOpen as (...a: unknown[]) => unknown)(...args)
719723
}) as never)
720724
vi.spyOn(fsSyncMod, "fsyncSync").mockImplementation(((fd: number) => {
721-
if (fd === sentinel) {
722-
throw new Error("EIO fsync")
725+
if (fd === dirFd) {
726+
throw fsyncFailure
723727
}
724728
return (realFsync as (f: number) => unknown)(fd)
725729
}) as never)
726730

727731
// Earlier tests in this file leave unlink history on the shared mock.
728732
vi.mocked(fs.unlink).mockClear()
729733

730-
await expect(safeWriteJson(currentTestFilePath, { fresh: "content" })).rejects.toBeInstanceOf(
731-
PostCommitDurabilityError,
734+
const failure: unknown = await safeWriteJson(currentTestFilePath, { fresh: "content" }).then(
735+
() => null,
736+
(error: unknown) => error,
732737
)
738+
expect(failure).toBeInstanceOf(PostCommitDurabilityError)
739+
// The wrapper must carry the injected fsync failure, not a cleanup error.
740+
expect((failure as Error).cause).toBe(fsyncFailure)
733741

734742
// The rename committed: the target must still hold the new JSON, not be
735743
// unlinked away by the catch cleanup of the consumed staging path.

0 commit comments

Comments
 (0)