1+ import { existsSync , promises as fs } from 'node:fs'
2+ import { tmpdir } from 'node:os'
13import path from 'node:path'
24
3- import { afterAll , afterEach , beforeAll , describe , expect } from 'vitest'
4-
5- import { logger } from '@socketsecurity/registry/lib/logger'
6- import { spawn } from '@socketsecurity/registry/lib/spawn'
5+ import trash from 'trash'
6+ import { describe , expect } from 'vitest'
77
88import constants , {
99 FLAG_CONFIG ,
@@ -18,48 +18,42 @@ import { cmdit, spawnSocketCli, testPath } from '../../../test/utils.mts'
1818const fixtureBaseDir = path . join ( testPath , 'fixtures/commands/fix' )
1919const pnpmFixtureDir = path . join ( fixtureBaseDir , 'pnpm' )
2020
21- async function revertFixtureChanges ( ) {
22- // Reset only the lockfiles that fix command might modify.
23- try {
24- await spawn ( 'git' , [ 'checkout' , 'HEAD' , '--' , 'monorepo/pnpm-lock.yaml' ] , {
25- cwd : pnpmFixtureDir ,
26- stdio : 'ignore' ,
27- } )
28- } catch ( e ) {
29- // Log warning but continue - lockfile might not exist or have no changes.
30- logger . warn ( 'Failed to revert lockfile:' , e )
31- }
32- // Clean up any untracked files (node_modules, etc.).
33- try {
34- await spawn ( 'git' , [ 'clean' , '-fd' , '.' ] , {
35- cwd : pnpmFixtureDir ,
36- stdio : 'ignore' ,
37- } )
38- } catch ( e ) {
39- logger . warn ( 'Failed to clean untracked files:' , e )
21+ async function copyDirectory ( src : string , dest : string ) : Promise < void > {
22+ await fs . mkdir ( dest , { recursive : true } )
23+ const entries = await fs . readdir ( src , { withFileTypes : true } )
24+
25+ for ( const entry of entries ) {
26+ const srcPath = path . join ( src , entry . name )
27+ const destPath = path . join ( dest , entry . name )
28+
29+ if ( entry . isDirectory ( ) ) {
30+ // eslint-disable-next-line no-await-in-loop
31+ await copyDirectory ( srcPath , destPath )
32+ } else {
33+ // eslint-disable-next-line no-await-in-loop
34+ await fs . copyFile ( srcPath , destPath )
35+ }
4036 }
4137}
4238
39+ async function createTempFixture ( sourceDir : string ) : Promise < string > {
40+ // Create a temporary directory with a unique name.
41+ const tempDir = path . join (
42+ tmpdir ( ) ,
43+ `socket-fix-test-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } ` ,
44+ )
45+
46+ // Copy the fixture directory to the temp directory.
47+ await copyDirectory ( sourceDir , tempDir )
48+
49+ return tempDir
50+ }
51+
4352describe ( 'socket fix' , async ( ) => {
4453 const { binCliPath } = constants
4554 // Increase timeout for CI environments and Windows where operations can be slower.
4655 const testTimeout = constants . ENV . CI || constants . WIN32 ? 60_000 : 30_000
4756
48- beforeAll ( async ( ) => {
49- // Ensure fixtures are in clean state before tests.
50- await revertFixtureChanges ( )
51- } )
52-
53- afterEach ( async ( ) => {
54- // Revert all changes after each test using git.
55- await revertFixtureChanges ( )
56- } )
57-
58- afterAll ( async ( ) => {
59- // Clean up once after all tests.
60- await revertFixtureChanges ( )
61- } )
62-
6357 describe ( 'environment variable handling' , ( ) => {
6458 // Note: The warning messages about missing env vars are only shown when:
6559 // 1. NOT in dry-run mode
0 commit comments