test(fs-safe): cover writeFileAtomic failure cleanup and default mode

This commit is contained in:
Chris Scott committed 2026-10-11 00:18:47 -04:00
1 parent bda123b6e1
commit 0b76b6d3c7
1 file changed
+12 -1
+12 -1
View File
@@ -1,4 +1,4 @@
import { mkdtemp, readdir, readFile, realpath, rm, stat, writeFile } from 'node:fs/promises'
import { mkdir, mkdtemp, readdir, readFile, realpath, rm, stat, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
@@ -86,6 +86,17 @@ describe('writeFileAtomic', () => {
await writeFileAtomic(target, bytes)
expect(await readFile(target)).toEqual(bytes)
expect((await stat(target)).mode & 0o777).toBe(0o600)
await expect(readdir(workDir)).resolves.toEqual(['audio.mp3'])
})
it('removes the temp file and rethrows when the rename fails', async () => {
const target = path.join(workDir, 'target')
await mkdir(target)
await writeFile(path.join(target, 'blocker'), 'x')
await expect(writeFileAtomic(target, 'content')).rejects.toThrow()
await expect(readdir(workDir)).resolves.toEqual(['target'])
})
})