Skip to content

Commit 6aed302

Browse files
test(file-safety): pin the durability wrapper's cause and the exact staging release
Review follow-ups on the durability regression coverage: - the injected fsync failure now targets a REAL directory descriptor (safeWriteText closes it in its finally block; a fabricated fd made closeSync fail with EBADF behind the same wrapper, so the test could pass without confirming its stated cause), and the test asserts the PostCommitDurabilityError carries the injected EIO error as its cause. - the staging-release assertions compare the exact directory path read back from the create instead of a substring, so a release aimed at a different staging directory no longer passes. Mutation check: releasing stagingDir + "_other" reddens the strengthened tests.
1 parent b890914 commit 6aed302

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,14 @@ describe("safeWriteText", () => {
164164

165165
await safeWriteText(targetPath, "hello", { platform: "linux" })
166166

167-
// the staging subdir is removed best-effort after the commit rename
168-
// (stringContaining: the SUT and the test helper resolve Windows
169-
// drive-relative paths differently, as in the existing staging tests)
167+
// the staging subdir is removed best-effort after the commit rename.
168+
// Exact path read back from the create: the SUT releases the same
169+
// directory it staged in, and a substring match would also accept a
170+
// release aimed at some other staging directory.
170171
expect(fs.rmdir).toHaveBeenCalledTimes(1)
171-
expect(fs.rmdir).toHaveBeenCalledWith(expect.stringContaining(".file-safety-staging"))
172+
const stagingDir = String(vi.mocked(fsSync.mkdirSync).mock.calls[0]?.[0])
173+
expect(stagingDir).toContain(".file-safety-staging")
174+
expect(fs.rmdir).toHaveBeenCalledWith(stagingDir)
172175
// the win32 DACL restore gate must stay closed on other platforms:
173176
// no icacls save or restore is attempted
174177
expect(execFile).not.toHaveBeenCalled()
@@ -184,7 +187,9 @@ describe("safeWriteText", () => {
184187
await expect(safeWriteText(targetPath, "hello")).resolves.toBeUndefined()
185188

186189
expect(fs.rmdir).toHaveBeenCalledTimes(1)
187-
expect(fs.rmdir).toHaveBeenCalledWith(expect.stringContaining(".file-safety-staging"))
190+
const stagingDir = String(vi.mocked(fsSync.mkdirSync).mock.calls[0]?.[0])
191+
expect(stagingDir).toContain(".file-safety-staging")
192+
expect(fs.rmdir).toHaveBeenCalledWith(stagingDir)
188193
if (process.platform === "win32") {
189194
// default platform is win32: the DACL save + restore still ran
190195
// through the default icacls path (options?.execFileRunner must

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,29 +1108,37 @@ describe("safeWriteJson", () => {
11081108
const fsSyncMod = await import("fs")
11091109
const realOpen = fsSyncMod.openSync
11101110
const realFsync = fsSyncMod.fsyncSync
1111-
// A sentinel fd identifies the parent-directory open that safeWriteText
1112-
// performs after the commit rename; only that fsync fails.
1113-
const sentinel = 987654
1111+
// The parent-directory open that safeWriteText performs after the commit
1112+
// rename gets a REAL descriptor: safeWriteText closes it in its finally
1113+
// block, and a fabricated number would make closeSync fail with EBADF -
1114+
// a second failure behind the same wrapper, hiding which fsync broke.
1115+
const fsyncFailure = new Error("EIO fsync")
1116+
let dirFd: number | undefined
11141117
vi.spyOn(process, "platform", "get").mockReturnValue("linux")
11151118
vi.spyOn(fsSyncMod, "openSync").mockImplementation(((...args: unknown[]) => {
11161119
if (String(args[0]) === tempDir) {
1117-
return sentinel
1120+
dirFd = (realOpen as (p: string, f: string) => number)(tempDir, "r")
1121+
return dirFd
11181122
}
11191123
return (realOpen as (...a: unknown[]) => unknown)(...args)
11201124
}) as never)
11211125
vi.spyOn(fsSyncMod, "fsyncSync").mockImplementation(((fd: number) => {
1122-
if (fd === sentinel) {
1123-
throw new Error("EIO fsync")
1126+
if (fd === dirFd) {
1127+
throw fsyncFailure
11241128
}
11251129
return (realFsync as (f: number) => unknown)(fd)
11261130
}) as never)
11271131

11281132
// Earlier tests in this file leave unlink history on the shared mock.
11291133
vi.mocked(fs.unlink).mockClear()
11301134

1131-
await expect(safeWriteJson(currentTestFilePath, { fresh: "content" })).rejects.toBeInstanceOf(
1132-
PostCommitDurabilityError,
1135+
const failure: unknown = await safeWriteJson(currentTestFilePath, { fresh: "content" }).then(
1136+
() => null,
1137+
(error: unknown) => error,
11331138
)
1139+
expect(failure).toBeInstanceOf(PostCommitDurabilityError)
1140+
// The wrapper must carry the injected fsync failure, not a cleanup error.
1141+
expect((failure as Error).cause).toBe(fsyncFailure)
11341142

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

0 commit comments

Comments
 (0)