Skip to content

Commit e079439

Browse files
committed
refactor(test): use crypto.randomUUID in temp-file-helper
Replace Date.now() with Node.js built-in crypto.randomUUID() for temp file name generation. Prevents potential race conditions when multiple tests create temp files simultaneously. Changes: - withTempFile: Use randomUUID instead of Date.now() Benefits: - Guaranteed uniqueness even when called in rapid succession - More straightforward than timestamp-based naming - Consistent with socket-lib temp-file-helper implementation
1 parent f98d884 commit e079439

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

test/utils/temp-file-helper.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Provides automatic cleanup and error handling for temp resources.
44
*/
55

6+
import { randomUUID } from 'node:crypto'
67
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
78
import os from 'node:os'
89
import path from 'node:path'
@@ -114,7 +115,7 @@ export async function withTempFile(
114115
options: { extension?: string; prefix?: string } = {},
115116
): Promise<TempFileResult> {
116117
const { extension = '.txt', prefix = 'test-file-' } = options
117-
const tmpFile = path.join(os.tmpdir(), `${prefix}${Date.now()}${extension}`)
118+
const tmpFile = path.join(os.tmpdir(), `${prefix}${randomUUID()}${extension}`)
118119

119120
writeFileSync(tmpFile, content)
120121

0 commit comments

Comments
 (0)