Skip to content

Commit 281c245

Browse files
test(safeWriteJson): key the backup-stat injection on inode, not path spelling
The three "preserve backups when another backup stat fails with ENOENT/ EACCES/EIO" rows went red on the windows platform-unit shard (#1914 CI run 42448302148 job 128631851822): the double compared the stat target against os.tmpdir()'s spelling while the writer stats the canonical form of the same file - on windows realpath spells it differently - so the injection missed, stat succeeded, and the write resolved instead of rejecting (the failure reads: 'promise resolved "undefined" instead of rejecting' at safeWriteJson.test.ts:675 on run 42461215177 job 128670729075). This adopts #2359's (025fb7d) hardened version of the test file, which keys the injection on the leftover's inode and restores the fs.stat implementation after the row (inode recycling otherwise rejects later tests). Test-only: src/utils/safeWriteJson.ts is byte-identical between 74f5eb2 and 025fb7d, so the json implementation stays at this branch's pin and json + its specs still travel as one generation - with the pair windows-proven: 025fb7d's windows platform-unit passed with this exact test file against the identical implementation (run 42455872567 job 128654644966). Copy source named: src/utils/__tests__/safeWriteJson.test.ts from 025fb7d (#2359 head).
1 parent 74f5eb2 commit 281c245

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -660,22 +660,44 @@ describe("safeWriteJson", () => {
660660
await fs.writeFile(unreadableBackup, JSON.stringify({ stale: true }))
661661
await fs.writeFile(readableBackup, JSON.stringify({ committed: true }))
662662
const actual = await vi.importActual<typeof import("fs/promises")>("fs/promises")
663+
// The injection is keyed on the leftover's inode, not on the spelling of its path:
664+
// the writer stats the canonical form of the same file, which realpath can spell
665+
// differently from os.tmpdir() on the machine running the test. Production cannot tell
666+
// the two spellings apart - they are one file - so the double matches the inode.
667+
const injectedInode = await actual.stat(unreadableBackup)
668+
const isInjectedInode = async (candidate: Parameters<typeof actual.stat>[0]) => {
669+
try {
670+
const inode = await actual.stat(candidate)
671+
return inode.dev === injectedInode.dev && inode.ino === injectedInode.ino
672+
} catch {
673+
return false // a candidate that cannot be stat-ed is not the injected leftover
674+
}
675+
}
676+
// The double is installed for this test only. vi.restoreAllMocks() in afterEach does not
677+
// clear implementations set on the fs/promises factory mocks, and the operating system
678+
// recycles inodes, so a matcher left installed rejects a later test's unrelated file the
679+
// moment that file is handed the inode this row captured.
680+
const previousStat = vi.mocked(fs.stat).getMockImplementation()
663681
vi.mocked(fs.stat).mockImplementation(async (backupPath) => {
664-
if (backupPath === unreadableBackup) {
682+
if (await isInjectedInode(backupPath)) {
665683
throw Object.assign(new Error("Backup stat failed"), { code })
666684
}
667685
return actual.stat(backupPath)
668686
})
669687
const merge = vi.fn((existing: unknown) => existing)
670688

671-
if (code === "ENOENT") {
672-
await safeWriteJson(currentTestFilePath, {}, { merge })
673-
expect(merge).toHaveBeenCalledWith({ committed: true }, {})
674-
} else {
675-
await expect(safeWriteJson(currentTestFilePath, {}, { merge })).rejects.toMatchObject({ code })
676-
expect(merge).not.toHaveBeenCalled()
677-
expect(await readFileContent(unreadableBackup)).toEqual({ stale: true })
678-
expect(await readFileContent(readableBackup)).toEqual({ committed: true })
689+
try {
690+
if (code === "ENOENT") {
691+
await safeWriteJson(currentTestFilePath, {}, { merge })
692+
expect(merge).toHaveBeenCalledWith({ committed: true }, {})
693+
} else {
694+
await expect(safeWriteJson(currentTestFilePath, {}, { merge })).rejects.toMatchObject({ code })
695+
expect(merge).not.toHaveBeenCalled()
696+
expect(await readFileContent(unreadableBackup)).toEqual({ stale: true })
697+
expect(await readFileContent(readableBackup)).toEqual({ committed: true })
698+
}
699+
} finally {
700+
vi.mocked(fs.stat).mockImplementation(previousStat ?? actual.stat)
679701
}
680702
},
681703
)

0 commit comments

Comments
 (0)