@@ -5,42 +5,49 @@ const { spawnSync } = require('node:child_process')
5
5
const path = require ( 'node:path' )
6
6
const { describe, it } = require ( 'node:test' )
7
7
8
+ const spawn = require ( '@npmcli/promise-spawn' )
9
+
8
10
const constants = require ( '../scripts/constants' )
9
11
const { distPath } = constants
10
12
11
13
const testPath = __dirname
12
14
const 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' )
26
16
27
17
// These aliases are defined in package.json.
28
18
for ( 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
+ )
34
30
35
31
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
+ }
44
51
} )
45
52
} )
46
53
}
0 commit comments