@@ -5,42 +5,49 @@ const { spawnSync } = require('node:child_process')
55const path = require ( 'node:path' )
66const { describe, it } = require ( 'node:test' )
77
8+ const spawn = require ( '@npmcli/promise-spawn' )
9+
810const constants = require ( '../scripts/constants' )
911const { distPath } = constants
1012
1113const testPath = __dirname
1214const entryPath = path . join ( distPath , 'cli.js' )
13-
14- function spawnNPM ( { args = [ ] , cwd, installDir } ) {
15- return spawnSync ( process . execPath , [ entryPath , 'npm' , ...args ] , {
16- cwd : path . join ( testPath , cwd ) ,
17- encoding : 'utf8' ,
18- env : {
19- // Make sure we don't borrow TTY from parent.
20- SOCKET_SECURITY_TTY_IPC : undefined ,
21- PATH : `${ path . join ( installDir , 'node_modules' , '.bin' ) } :${ process . env . PATH } `
22- } ,
23- stdio : [ 'pipe' , 'pipe' , 'pipe' ]
24- } )
25- }
15+ const npmFixturesPath = path . join ( testPath , 'socket-npm-fixtures' )
2616
2717// These aliases are defined in package.json.
2818for ( const npm of [ 'npm8' , 'npm10' ] ) {
29- const installDir = path . join ( testPath , `/socket-npm-fixtures/${ npm } ` )
30- spawnSync ( 'npm' , [ 'install' ] , {
31- cwd : installDir ,
32- stdio : 'ignore'
33- } )
19+ const npmPath = path . join ( npmFixturesPath , npm )
20+ const npmBinPath = path . join ( npmPath , 'node_modules' , '.bin' )
21+
22+ spawnSync (
23+ 'npm' ,
24+ [ 'install' , '--no-audit' , '--no-fund' , '--no-progress' , '--quiet' ] ,
25+ {
26+ cwd : npmPath ,
27+ stdio : 'ignore'
28+ }
29+ )
3430
3531 describe ( `Socket npm wrapper for ${ npm } ` , ( ) => {
36- it ( 'should bail on new typosquat' , ( ) => {
37- const ret = spawnNPM ( {
38- cwd : './socket-npm-fixtures/lacking-typosquat' ,
39- installDir,
40- args : [ 'i' , 'bowserify' ]
41- } )
42- assert . strictEqual ( ret . status , 1 , ret . stderr )
43- assert . ok ( ret . stderr . includes ( 'Unable to prompt' ) , ret . stderr )
32+ it ( 'should bail on new typosquat' , async ( ) => {
33+ try {
34+ await spawn (
35+ process . execPath ,
36+ [ entryPath , 'npm' , 'install' , 'bowserify' ] ,
37+ {
38+ cwd : path . join ( npmFixturesPath , 'lacking-typosquat' ) ,
39+ encoding : 'utf8' ,
40+ env : {
41+ // Make sure we don't borrow TTY from parent.
42+ SOCKET_SECURITY_TTY_IPC : undefined ,
43+ PATH : `${ npmBinPath } :${ process . env . PATH } `
44+ }
45+ }
46+ )
47+ assert . ok ( false , 'typosquat not error' )
48+ } catch ( e ) {
49+ assert . ok ( e ?. stderr . includes ( 'Unable to prompt' ) , e ?. stderr )
50+ }
4451 } )
4552 } )
4653}
0 commit comments