|
| 1 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 2 | +import { mkdtemp, rm, readFile, stat } from 'fs/promises'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { tmpdir } from 'os'; |
| 5 | +import { atomicWriteFile, atomicWriteFileSync } from './atomic.js'; |
| 6 | + |
| 7 | +let dir: string; |
| 8 | + |
| 9 | +afterEach(async () => { |
| 10 | + if (dir) await rm(dir, { recursive: true, force: true }); |
| 11 | +}); |
| 12 | + |
| 13 | +async function makeDir() { |
| 14 | + dir = await mkdtemp(join(tmpdir(), 'atomic-test-')); |
| 15 | + return dir; |
| 16 | +} |
| 17 | + |
| 18 | +describe('atomicWriteFile (async)', () => { |
| 19 | + it('writes content to the target path', async () => { |
| 20 | + const d = await makeDir(); |
| 21 | + const target = join(d, 'out.json'); |
| 22 | + await atomicWriteFile(target, '{"ok":true}'); |
| 23 | + const content = await readFile(target, 'utf8'); |
| 24 | + expect(content).toBe('{"ok":true}'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('overwrites existing file', async () => { |
| 28 | + const d = await makeDir(); |
| 29 | + const target = join(d, 'out.json'); |
| 30 | + await atomicWriteFile(target, 'first'); |
| 31 | + await atomicWriteFile(target, 'second'); |
| 32 | + expect(await readFile(target, 'utf8')).toBe('second'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('leaves no temp file on success', async () => { |
| 36 | + const d = await makeDir(); |
| 37 | + const target = join(d, 'out.json'); |
| 38 | + await atomicWriteFile(target, 'hello'); |
| 39 | + const files = await import('fs/promises').then((m) => m.readdir(d)); |
| 40 | + expect(files).toEqual(['out.json']); |
| 41 | + }); |
| 42 | + |
| 43 | + it('sets file mode when provided', async () => { |
| 44 | + const d = await makeDir(); |
| 45 | + const target = join(d, 'secret.json'); |
| 46 | + await atomicWriteFile(target, 'data', { mode: 0o600 }); |
| 47 | + const s = await stat(target); |
| 48 | + expect(s.mode & 0o777).toBe(0o600); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('atomicWriteFileSync (sync)', () => { |
| 53 | + it('writes content to the target path', async () => { |
| 54 | + const d = await makeDir(); |
| 55 | + const target = join(d, 'out.json'); |
| 56 | + atomicWriteFileSync(target, '{"ok":true}'); |
| 57 | + const content = await readFile(target, 'utf8'); |
| 58 | + expect(content).toBe('{"ok":true}'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('overwrites existing file', async () => { |
| 62 | + const d = await makeDir(); |
| 63 | + const target = join(d, 'out.json'); |
| 64 | + atomicWriteFileSync(target, 'first'); |
| 65 | + atomicWriteFileSync(target, 'second'); |
| 66 | + expect(await readFile(target, 'utf8')).toBe('second'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('sets file mode when provided', async () => { |
| 70 | + const d = await makeDir(); |
| 71 | + const target = join(d, 'secret.json'); |
| 72 | + atomicWriteFileSync(target, 'data', { mode: 0o600 }); |
| 73 | + const s = await stat(target); |
| 74 | + expect(s.mode & 0o777).toBe(0o600); |
| 75 | + }); |
| 76 | +}); |
0 commit comments