Skip to content

Commit a4005db

Browse files
committed
refactor(test): use crypto.randomUUID for temp directories
Replace Date.now() + Math.random() patterns with Node.js built-in crypto.randomUUID() for cleaner and more robust unique directory generation in tests. Fixes race condition in CI where tests running in parallel could share the same directory name. Changes: - test/unit/fs-sync.test.ts: Use randomUUID and sequential execution - test/unit/dlx.test.ts: Use randomUUID for test DLX directory - test/unit/utils/temp-file-helper.mts: Use randomUUID in createTempDir and withTempFile Benefits: - Fixes ENOENT errors in CI from parallel test interference - More straightforward than Date.now() + random string concatenation - Guaranteed uniqueness with standard UUID format - Sequential execution prevents shared variable race conditions - Cleaner, more readable code
1 parent dadd4e3 commit a4005db

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

test/unit/dlx.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Used by Socket CLI for pnpm dlx / npx-style package execution.
1111
*/
1212

13+
import { randomUUID } from 'node:crypto'
1314
import fs from 'node:fs'
1415
import os from 'node:os'
1516
import path from 'node:path'
@@ -45,7 +46,7 @@ describe.sequential('dlx', () => {
4546
beforeEach(async () => {
4647
// Save original env and create isolated test directory
4748
originalEnv = process.env.SOCKET_DLX_DIR
48-
testDlxDir = path.join(os.tmpdir(), `socket-dlx-test-${Date.now()}`)
49+
testDlxDir = path.join(os.tmpdir(), `socket-dlx-test-${randomUUID()}`)
4950
process.env.SOCKET_DLX_DIR = testDlxDir
5051

5152
// Clean up any existing test artifacts

test/unit/fs-sync.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* - readFileBinary() for reading binary files
1111
*/
1212

13+
import { randomUUID } from 'node:crypto'
1314
import {
1415
existsSync,
1516
mkdirSync,
@@ -30,11 +31,11 @@ import {
3031
} from '@socketsecurity/lib/fs'
3132
import { beforeEach, describe, expect, it, afterEach } from 'vitest'
3233

33-
describe('fs - Sync Functions', () => {
34+
describe.sequential('fs - Sync Functions', () => {
3435
let testDir: string
3536

3637
beforeEach(() => {
37-
testDir = join(tmpdir(), `socket-lib-test-${Date.now()}`)
38+
testDir = join(tmpdir(), `socket-lib-test-${randomUUID()}`)
3839
mkdirSync(testDir, { recursive: true })
3940
})
4041

test/unit/utils/temp-file-helper.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @fileoverview Temporary file and directory utilities for tests.
33
*/
44

5+
import { randomUUID } from 'node:crypto'
56
import { promises as fs } from 'node:fs'
67
import os from 'node:os'
78
import path from 'node:path'
@@ -69,7 +70,7 @@ export function mockHomeDir(homeDir: string): () => void {
6970
*/
7071
export async function createTempDir(prefix: string): Promise<string> {
7172
const tempBaseDir = os.tmpdir()
72-
const tempDirName = `${prefix}${Date.now()}-${Math.random().toString(36).slice(2)}`
73+
const tempDirName = `${prefix}${randomUUID()}`
7374
const tempDir = path.join(tempBaseDir, tempDirName)
7475

7576
await fs.mkdir(tempDir, { recursive: true })
@@ -130,7 +131,7 @@ export async function withTempFile(
130131
const { extension = '.txt', prefix = 'temp-file-' } = options
131132

132133
const tempBaseDir = os.tmpdir()
133-
const tempFileName = `${prefix}${Date.now()}-${Math.random().toString(36).slice(2)}${extension}`
134+
const tempFileName = `${prefix}${randomUUID()}${extension}`
134135
const tempFile = path.join(tempBaseDir, tempFileName)
135136

136137
await fs.writeFile(tempFile, content, 'utf8')

0 commit comments

Comments
 (0)