@@ -4,11 +4,11 @@ import {fileURLToPath, pathToFileURL} from 'node:url';
4
4
import test from 'ava' ;
5
5
import { npmRunPath , npmRunPathEnv } from './index.js' ;
6
6
7
- const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
7
+ const localBinaryDirectory = fileURLToPath ( new URL ( 'node_modules/.bin' , import . meta. url ) ) ;
8
8
9
9
const testLocalDirectory = ( t , addExecPath , preferLocal , expectedResult ) => {
10
10
t . is (
11
- npmRunPath ( { path : '' , addExecPath, preferLocal} ) . split ( path . delimiter ) [ 0 ] === path . join ( __dirname , 'node_modules/.bin' ) ,
11
+ npmRunPath ( { path : '' , addExecPath, preferLocal} ) . split ( path . delimiter ) [ 0 ] === localBinaryDirectory ,
12
12
expectedResult ,
13
13
) ;
14
14
} ;
@@ -20,7 +20,7 @@ test('"preferLocal: false", "addExecPath: false" does not add node_modules/.bin
20
20
21
21
const testLocalDirectoryEnv = ( t , addExecPath , preferLocal , expectedResult ) => {
22
22
t . is (
23
- npmRunPathEnv ( { env : { PATH : 'foo' } , addExecPath, preferLocal} ) . PATH . split ( path . delimiter ) [ 0 ] === path . join ( __dirname , 'node_modules/.bin' ) ,
23
+ npmRunPathEnv ( { env : { PATH : 'foo' } , addExecPath, preferLocal} ) . PATH . split ( path . delimiter ) [ 0 ] === localBinaryDirectory ,
24
24
expectedResult ,
25
25
) ;
26
26
} ;
@@ -30,6 +30,15 @@ test('"addExecPath: false" still adds node_modules/.bin - npmRunPathEnv()', test
30
30
test ( '"preferLocal: false" does not add node_modules/.bin - npmRunPathEnv()' , testLocalDirectoryEnv , undefined , false , false ) ;
31
31
test ( '"preferLocal: false", "addExecPath: false" does not add node_modules/.bin - npmRunPathEnv()' , testLocalDirectoryEnv , false , false , false ) ;
32
32
33
+ test ( 'node_modules/.bin is not added twice' , t => {
34
+ const firstPathEnv = npmRunPath ( { path : '' } ) ;
35
+ const pathEnv = npmRunPath ( { path : firstPathEnv } ) ;
36
+ const execPaths = pathEnv
37
+ . split ( path . delimiter )
38
+ . filter ( pathPart => pathPart === localBinaryDirectory ) ;
39
+ t . is ( execPaths . length , 1 ) ;
40
+ } ) ;
41
+
33
42
test ( 'the `cwd` option changes the current directory' , t => {
34
43
t . is (
35
44
npmRunPath ( { path : '' , cwd : './dir' } ) . split ( path . delimiter ) [ 0 ] ,
@@ -49,6 +58,15 @@ test('push `execPath` later in the PATH', t => {
49
58
t . is ( pathEnv . at ( - 2 ) , path . dirname ( process . execPath ) ) ;
50
59
} ) ;
51
60
61
+ test ( '`execPath` is not added twice' , t => {
62
+ const firstPathEnv = npmRunPath ( { path : '' } ) ;
63
+ const pathEnv = npmRunPath ( { path : firstPathEnv } ) ;
64
+ const execPaths = pathEnv
65
+ . split ( path . delimiter )
66
+ . filter ( pathPart => pathPart === path . dirname ( process . execPath ) ) ;
67
+ t . is ( execPaths . length , 1 ) ;
68
+ } ) ;
69
+
52
70
const testExecPath = ( t , preferLocal , addExecPath , expectedResult ) => {
53
71
const pathEnv = npmRunPath ( {
54
72
path : '' ,
0 commit comments