Skip to content

Commit

Permalink
Handle empty PATH better (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Aug 26, 2024
1 parent 8cfb372 commit 973c107
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const npmRunPath = ({
applyExecPath(result, pathParts, execPath, cwdPath);
}

return [...result, pathOption].join(path.delimiter);
return pathOption === '' || pathOption === path.delimiter
? `${result.join(path.delimiter)}${pathOption}`
: [...result, pathOption].join(path.delimiter);
};

const applyPreferLocal = (result, pathParts, cwdPath) => {
Expand Down
25 changes: 21 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test('the `cwd` option can be a file URL', t => {

test('push `execPath` later in the PATH', t => {
const pathEnv = npmRunPath({path: ''}).split(path.delimiter);
t.is(pathEnv.at(-2), path.dirname(process.execPath));
t.is(pathEnv.at(-1), path.dirname(process.execPath));
});

test('`execPath` is not added twice', t => {
Expand All @@ -74,7 +74,7 @@ const testExecPath = (t, preferLocal, addExecPath, expectedResult) => {
preferLocal,
addExecPath,
}).split(path.delimiter);
t.is(pathEnv.at(-2) === path.resolve('test'), expectedResult);
t.is(pathEnv.at(-1) === path.resolve('test'), expectedResult);
};

test('can change `execPath` with the `execPath` option - npmRunPath()', testExecPath, undefined, undefined, true);
Expand All @@ -99,7 +99,7 @@ test('"addExecPath: false", "preferLocal: false" does not add execPath - npmRunP

test('the `execPath` option can be a file URL', t => {
const pathEnv = npmRunPath({path: '', execPath: pathToFileURL('test/test')}).split(path.delimiter);
t.is(pathEnv.at(-2), path.resolve('test'));
t.is(pathEnv.at(-1), path.resolve('test'));
});

test('the `execPath` option is relative to the `cwd` option', t => {
Expand All @@ -108,5 +108,22 @@ test('the `execPath` option is relative to the `cwd` option', t => {
execPath: 'test/test',
cwd: './dir',
}).split(path.delimiter);
t.is(pathEnv.at(-2), path.resolve('./dir/test'));
t.is(pathEnv.at(-1), path.resolve('./dir/test'));
});

test('the PATH can remain empty', t => {
t.is(npmRunPath({path: '', preferLocal: false, addExecPath: false}), '');
});

const testEmptyPath = (t, pathValue, shouldEndWithDelimiter, hasTwoDelimiters) => {
const pathEnv = npmRunPath({path: pathValue});
t.not(pathEnv, '');
t.false(pathEnv.startsWith(path.delimiter));
t.is(pathEnv.endsWith(path.delimiter), shouldEndWithDelimiter);
t.is(pathEnv.includes(`${path.delimiter}${path.delimiter}`), hasTwoDelimiters);
};

test('the PATH can be empty', testEmptyPath, '', false, false);
test('the PATH can be ; or :', testEmptyPath, path.delimiter, true, false);
test('the PATH can start with ; or :', testEmptyPath, `${path.delimiter}foo`, false, true);
test('the PATH can end with ; or :', testEmptyPath, `foo${path.delimiter}`, true, false);

0 comments on commit 973c107

Please sign in to comment.