1111 */
1212
1313import fs from 'node:fs'
14+ import os from 'node:os'
1415import path from 'node:path'
1516import { afterEach , beforeEach , describe , expect , it } from 'vitest'
1617
@@ -38,16 +39,34 @@ import { getSocketDlxDir } from '@socketsecurity/lib/paths'
3839
3940describe . sequential ( 'dlx' , ( ) => {
4041 const testPackageName = 'test-package'
41- const dlxDir = getSocketDlxDir ( )
42+ let originalEnv : string | undefined
43+ let testDlxDir : string
4244
4345 beforeEach ( async ( ) => {
46+ // Save original env and create isolated test directory
47+ originalEnv = process . env . SOCKET_DLX_DIR
48+ testDlxDir = path . join ( os . tmpdir ( ) , `socket-dlx-test-${ Date . now ( ) } ` )
49+ process . env . SOCKET_DLX_DIR = testDlxDir
50+
4451 // Clean up any existing test artifacts
4552 await clearDlx ( ) . catch ( ( ) => { } )
4653 } )
4754
4855 afterEach ( async ( ) => {
4956 // Clean up after tests
5057 await clearDlx ( ) . catch ( ( ) => { } )
58+
59+ // Remove test directory
60+ try {
61+ await fs . promises . rm ( testDlxDir , { recursive : true , force : true } )
62+ } catch { }
63+
64+ // Restore original env
65+ if ( originalEnv === undefined ) {
66+ delete process . env . SOCKET_DLX_DIR
67+ } else {
68+ process . env . SOCKET_DLX_DIR = originalEnv
69+ }
5170 } )
5271
5372 describe ( 'generateCacheKey' , ( ) => {
@@ -79,8 +98,8 @@ describe.sequential('dlx', () => {
7998 describe ( 'dlxDirExists / dlxDirExistsAsync' , ( ) => {
8099 it ( 'should return false when DLX directory does not exist' , ( ) => {
81100 // Ensure it doesn't exist
82- if ( fs . existsSync ( dlxDir ) ) {
83- fs . rmSync ( dlxDir , { recursive : true , force : true } )
101+ if ( fs . existsSync ( getSocketDlxDir ( ) ) ) {
102+ fs . rmSync ( getSocketDlxDir ( ) , { recursive : true , force : true } )
84103 }
85104 expect ( dlxDirExists ( ) ) . toBe ( false )
86105 } )
@@ -109,11 +128,11 @@ describe.sequential('dlx', () => {
109128 describe ( 'ensureDlxDir / ensureDlxDirSync' , ( ) => {
110129 it ( 'should create DLX directory if it does not exist' , async ( ) => {
111130 // Ensure it doesn't exist
112- if ( fs . existsSync ( dlxDir ) ) {
113- fs . rmSync ( dlxDir , { recursive : true , force : true } )
131+ if ( fs . existsSync ( getSocketDlxDir ( ) ) ) {
132+ fs . rmSync ( getSocketDlxDir ( ) , { recursive : true , force : true } )
114133 }
115134 await ensureDlxDir ( )
116- expect ( fs . existsSync ( dlxDir ) ) . toBe ( true )
135+ expect ( fs . existsSync ( getSocketDlxDir ( ) ) ) . toBe ( true )
117136 } )
118137
119138 it ( 'should not throw if DLX directory already exists' , async ( ) => {
@@ -123,11 +142,11 @@ describe.sequential('dlx', () => {
123142
124143 it ( 'sync version should create DLX directory if it does not exist' , ( ) => {
125144 // Ensure it doesn't exist
126- if ( fs . existsSync ( dlxDir ) ) {
127- fs . rmSync ( dlxDir , { recursive : true , force : true } )
145+ if ( fs . existsSync ( getSocketDlxDir ( ) ) ) {
146+ fs . rmSync ( getSocketDlxDir ( ) , { recursive : true , force : true } )
128147 }
129148 ensureDlxDirSync ( )
130- expect ( fs . existsSync ( dlxDir ) ) . toBe ( true )
149+ expect ( fs . existsSync ( getSocketDlxDir ( ) ) ) . toBe ( true )
131150 } )
132151
133152 it ( 'sync version should not throw if DLX directory already exists' , ( ) => {
@@ -139,7 +158,7 @@ describe.sequential('dlx', () => {
139158 describe ( 'getDlxPackageDir' , ( ) => {
140159 it ( 'should return path to package directory' , ( ) => {
141160 const packageDir = getDlxPackageDir ( testPackageName )
142- expect ( packageDir ) . toContain ( dlxDir )
161+ expect ( packageDir ) . toContain ( getSocketDlxDir ( ) )
143162 expect ( packageDir ) . toContain ( testPackageName )
144163 } )
145164
@@ -153,7 +172,7 @@ describe.sequential('dlx', () => {
153172 describe ( 'getDlxPackageNodeModulesDir' , ( ) => {
154173 it ( 'should return path to node_modules directory' , ( ) => {
155174 const nodeModulesDir = getDlxPackageNodeModulesDir ( testPackageName )
156- expect ( nodeModulesDir ) . toContain ( dlxDir )
175+ expect ( nodeModulesDir ) . toContain ( getSocketDlxDir ( ) )
157176 expect ( nodeModulesDir ) . toContain ( testPackageName )
158177 expect ( nodeModulesDir ) . toContain ( 'node_modules' )
159178 } )
@@ -162,31 +181,31 @@ describe.sequential('dlx', () => {
162181 describe ( 'getDlxInstalledPackageDir' , ( ) => {
163182 it ( 'should return path to installed package directory' , ( ) => {
164183 const installedDir = getDlxInstalledPackageDir ( testPackageName )
165- expect ( installedDir ) . toContain ( dlxDir )
184+ expect ( installedDir ) . toContain ( getSocketDlxDir ( ) )
166185 expect ( installedDir ) . toContain ( testPackageName )
167186 expect ( installedDir ) . toContain ( 'node_modules' )
168187 } )
169188
170189 it ( 'should handle scoped packages' , ( ) => {
171190 const scopedPackage = '@socket/test'
172191 const installedDir = getDlxInstalledPackageDir ( scopedPackage )
173- expect ( installedDir ) . toContain ( dlxDir )
192+ expect ( installedDir ) . toContain ( getSocketDlxDir ( ) )
174193 expect ( installedDir ) . toContain ( '@socket/test' )
175194 } )
176195 } )
177196
178197 describe ( 'getDlxPackageJsonPath' , ( ) => {
179198 it ( 'should return path to package.json' , ( ) => {
180199 const packageJsonPath = getDlxPackageJsonPath ( testPackageName )
181- expect ( packageJsonPath ) . toContain ( dlxDir )
200+ expect ( packageJsonPath ) . toContain ( getSocketDlxDir ( ) )
182201 expect ( packageJsonPath ) . toContain ( testPackageName )
183202 expect ( packageJsonPath ) . toContain ( 'package.json' )
184203 } )
185204 } )
186205
187206 describe ( 'isInSocketDlx' , ( ) => {
188207 it ( 'should return true for paths within DLX directory' , ( ) => {
189- const dlxPath = path . join ( dlxDir , 'some-package' , 'bin' , 'binary' )
208+ const dlxPath = path . join ( getSocketDlxDir ( ) , 'some-package' , 'bin' , 'binary' )
190209 expect ( isInSocketDlx ( dlxPath ) ) . toBe ( true )
191210 } )
192211
@@ -205,7 +224,7 @@ describe.sequential('dlx', () => {
205224 } )
206225
207226 it ( 'should handle paths with trailing separators' , ( ) => {
208- const dlxPath = path . join ( dlxDir , 'package' , '' )
227+ const dlxPath = path . join ( getSocketDlxDir ( ) , 'package' , '' )
209228 expect ( isInSocketDlx ( dlxPath ) ) . toBe ( true )
210229 } )
211230 } )
@@ -282,7 +301,7 @@ describe.sequential('dlx', () => {
282301 const packages = await listDlxPackagesAsync ( )
283302 expect ( packages ) . toContain ( 'package-1' )
284303 expect ( packages ) . toContain ( 'package-2' )
285- expect ( packages . length ) . toBeGreaterThanOrEqual ( 2 )
304+ expect ( packages ) . toHaveLength ( 2 )
286305 } )
287306 } )
288307
@@ -341,8 +360,9 @@ describe.sequential('dlx', () => {
341360
342361 it ( 'should not throw when DLX directory does not exist' , async ( ) => {
343362 // Ensure directory doesn't exist
344- if ( fs . existsSync ( dlxDir ) ) {
345- fs . rmSync ( dlxDir , { recursive : true , force : true } )
363+ const dlxDir = getSocketDlxDir ( )
364+ if ( fs . existsSync ( getSocketDlxDir ( ) ) ) {
365+ fs . rmSync ( getSocketDlxDir ( ) , { recursive : true , force : true } )
346366 }
347367 await expect ( clearDlx ( ) ) . resolves . not . toThrow ( )
348368 } )
@@ -367,8 +387,9 @@ describe.sequential('dlx', () => {
367387
368388 it ( 'sync version should not throw when DLX directory does not exist' , ( ) => {
369389 // Ensure directory doesn't exist
370- if ( fs . existsSync ( dlxDir ) ) {
371- fs . rmSync ( dlxDir , { recursive : true , force : true } )
390+ const dlxDir = getSocketDlxDir ( )
391+ if ( fs . existsSync ( getSocketDlxDir ( ) ) ) {
392+ fs . rmSync ( getSocketDlxDir ( ) , { recursive : true , force : true } )
372393 }
373394 expect ( ( ) => clearDlxSync ( ) ) . not . toThrow ( )
374395 } )
0 commit comments