Skip to content

Commit

Permalink
fix: detect path key based on correct environment (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgotink authored May 24, 2020
1 parent 7501971 commit 159e7e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/util/resolveCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

const path = require('path');
const which = require('which');
const pathKey = require('path-key')();
const getPathKey = require('path-key');

function resolveCommandAttempt(parsed, withoutPathExt) {
const env = parsed.options.env || process.env;
const cwd = process.cwd();
const hasCustomCwd = parsed.options.cwd != null;
// Worker threads do not have process.chdir()
Expand All @@ -24,7 +25,7 @@ function resolveCommandAttempt(parsed, withoutPathExt) {

try {
resolved = which.sync(parsed.command, {
path: (parsed.options.env || process.env)[pathKey],
path: env[getPathKey({ env })],
pathExt: withoutPathExt ? path.delimiter : undefined,
});
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/whoami.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
echo you sure are someone
17 changes: 17 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,5 +434,22 @@ run.methods.forEach((method) => {
expect(Number(stdout.trim())).toBe(process.pid);
});
}

if (isWin) {
const differentPathKey = pathKey.startsWith('p') ? 'PATH' : 'path';

it('should work if the path key is different in options.env', async () => {
const env = {
...process.env,
[differentPathKey]: `${__dirname}\\fixtures;${process.env[pathKey]}`,
};

delete env[pathKey];

const { stdout } = await run(method, 'whoami', { env });

expect(stdout.trim()).toBe('you sure are someone');
});
}
});
});

0 comments on commit 159e7e9

Please sign in to comment.