|
| 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 | + it('preserves existing 0600 mode on overwrite when no mode specified', async () => { |
| 52 | + const d = await makeDir(); |
| 53 | + const target = join(d, 'secret.json'); |
| 54 | + await atomicWriteFile(target, 'original', { mode: 0o600 }); |
| 55 | + await atomicWriteFile(target, 'overwritten'); // no mode option |
| 56 | + const s = await stat(target); |
| 57 | + expect(s.mode & 0o777).toBe(0o600); |
| 58 | + expect(await readFile(target, 'utf8')).toBe('overwritten'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('sets exact mode even when umask would narrow it', async () => { |
| 62 | + const d = await makeDir(); |
| 63 | + const target = join(d, 'wide.json'); |
| 64 | + const prev = process.umask(0o022); |
| 65 | + try { |
| 66 | + await atomicWriteFile(target, 'data', { mode: 0o666 }); |
| 67 | + } finally { |
| 68 | + process.umask(prev); |
| 69 | + } |
| 70 | + const s = await stat(target); |
| 71 | + expect(s.mode & 0o777).toBe(0o666); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('atomicWriteFileSync (sync)', () => { |
| 76 | + it('writes content to the target path', async () => { |
| 77 | + const d = await makeDir(); |
| 78 | + const target = join(d, 'out.json'); |
| 79 | + atomicWriteFileSync(target, '{"ok":true}'); |
| 80 | + const content = await readFile(target, 'utf8'); |
| 81 | + expect(content).toBe('{"ok":true}'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('overwrites existing file', async () => { |
| 85 | + const d = await makeDir(); |
| 86 | + const target = join(d, 'out.json'); |
| 87 | + atomicWriteFileSync(target, 'first'); |
| 88 | + atomicWriteFileSync(target, 'second'); |
| 89 | + expect(await readFile(target, 'utf8')).toBe('second'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('sets file mode when provided', async () => { |
| 93 | + const d = await makeDir(); |
| 94 | + const target = join(d, 'secret.json'); |
| 95 | + atomicWriteFileSync(target, 'data', { mode: 0o600 }); |
| 96 | + const s = await stat(target); |
| 97 | + expect(s.mode & 0o777).toBe(0o600); |
| 98 | + }); |
| 99 | + |
| 100 | + it('preserves existing 0600 mode on overwrite when no mode specified', async () => { |
| 101 | + const d = await makeDir(); |
| 102 | + const target = join(d, 'secret.json'); |
| 103 | + atomicWriteFileSync(target, 'original', { mode: 0o600 }); |
| 104 | + atomicWriteFileSync(target, 'overwritten'); // no mode option |
| 105 | + const s = await stat(target); |
| 106 | + expect(s.mode & 0o777).toBe(0o600); |
| 107 | + expect(await readFile(target, 'utf8')).toBe('overwritten'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('sets exact mode even when umask would narrow it', async () => { |
| 111 | + const d = await makeDir(); |
| 112 | + const target = join(d, 'wide.json'); |
| 113 | + const prev = process.umask(0o022); |
| 114 | + try { |
| 115 | + atomicWriteFileSync(target, 'data', { mode: 0o666 }); |
| 116 | + } finally { |
| 117 | + process.umask(prev); |
| 118 | + } |
| 119 | + const s = await stat(target); |
| 120 | + expect(s.mode & 0o777).toBe(0o666); |
| 121 | + }); |
| 122 | +}); |
0 commit comments