Skip to content

Commit 6032a97

Browse files
authored
Switch exec to execFile to simply protect against spaces in path (#1390)
1 parent 0192d66 commit 6032a97

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

tests/command.default.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ const commander = require('../');
33
const path = require('path');
44
const util = require('util');
55

6-
const execAsync = util.promisify(childProcess.exec);
6+
const execFileAsync = util.promisify(childProcess.execFile);
77

88
describe('default executable command', () => {
99
// Calling node explicitly so pm works without file suffix cross-platform.
1010
const pm = path.join(__dirname, './fixtures/pm');
1111

1212
test('when default subcommand and no command then call default', async() => {
13-
const { stdout } = await execAsync(`node ${pm}`);
13+
const { stdout } = await execFileAsync('node', [pm]);
1414
expect(stdout).toBe('default\n');
1515
});
1616

1717
test('when default subcommand and unrecognised argument then call default with argument', async() => {
18-
const { stdout } = await execAsync(`node ${pm} an-argument`);
18+
const { stdout } = await execFileAsync('node', [pm, 'an-argument']);
1919
expect(stdout).toBe("default\n[ 'an-argument' ]\n");
2020
});
2121

2222
test('when default subcommand and unrecognised option then call default with option', async() => {
23-
const { stdout } = await execAsync(`node ${pm} --an-option`);
23+
const { stdout } = await execFileAsync('node', [pm, '--an-option']);
2424
expect(stdout).toBe("default\n[ '--an-option' ]\n");
2525
});
2626
});

tests/command.executableSubcommand.lookup.test.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const childProcess = require('child_process');
22
const path = require('path');
33
const util = require('util');
4-
const execAsync = util.promisify(childProcess.exec);
4+
const execFileAsync = util.promisify(childProcess.execFile);
55

66
// Calling node explicitly so pm works without file suffix cross-platform.
77

@@ -13,7 +13,7 @@ const pm = path.join(__dirname, './fixtures/pm');
1313

1414
test('when subcommand file missing then error', () => {
1515
expect.assertions(1);
16-
return execAsync(`node ${pm} list`).catch((err) => {
16+
return execFileAsync('node', [pm, 'list']).catch((err) => {
1717
if (process.platform === 'win32') {
1818
// Get uncaught thrown error on Windows
1919
// eslint-disable-next-line jest/no-conditional-expect
@@ -27,7 +27,7 @@ test('when subcommand file missing then error', () => {
2727

2828
test('when alias subcommand file missing then error', () => {
2929
expect.assertions(1);
30-
return execAsync(`node ${pm} lst`).catch((err) => {
30+
return execFileAsync('node', [pm, 'lst']).catch((err) => {
3131
if (process.platform === 'win32') {
3232
// Get uncaught thrown error on Windows
3333
// eslint-disable-next-line jest/no-conditional-expect
@@ -40,44 +40,44 @@ test('when alias subcommand file missing then error', () => {
4040
});
4141

4242
test('when subcommand file has no suffix then lookup succeeds', async() => {
43-
const { stdout } = await execAsync(`node ${pm} install`);
43+
const { stdout } = await execFileAsync('node', [pm, 'install']);
4444
expect(stdout).toBe('install\n');
4545
});
4646

4747
test('when alias subcommand file has no suffix then lookup succeeds', async() => {
48-
const { stdout } = await execAsync(`node ${pm} i`);
48+
const { stdout } = await execFileAsync('node', [pm, 'i']);
4949
expect(stdout).toBe('install\n');
5050
});
5151

5252
test('when subcommand target executablefile has no suffix then lookup succeeds', async() => {
53-
const { stdout } = await execAsync(`node ${pm} specifyInstall`);
53+
const { stdout } = await execFileAsync('node', [pm, 'specifyInstall']);
5454
expect(stdout).toBe('install\n');
5555
});
5656

5757
test('when subcommand file suffix .js then lookup succeeds', async() => {
58-
const { stdout } = await execAsync(`node ${pm} publish`);
58+
const { stdout } = await execFileAsync('node', [pm, 'publish']);
5959
expect(stdout).toBe('publish\n');
6060
});
6161

6262
test('when alias subcommand file suffix .js then lookup succeeds', async() => {
63-
const { stdout } = await execAsync(`node ${pm} p`);
63+
const { stdout } = await execFileAsync('node', [pm, 'p']);
6464
expect(stdout).toBe('publish\n');
6565
});
6666

6767
test('when subcommand target executablefile has suffix .js then lookup succeeds', async() => {
68-
const { stdout } = await execAsync(`node ${pm} specifyPublish`);
68+
const { stdout } = await execFileAsync('node', [pm, 'specifyPublish']);
6969
expect(stdout).toBe('publish\n');
7070
});
7171

7272
testOrSkipOnWindows('when subcommand file is symlink then lookup succeeds', async() => {
7373
const pmlink = path.join(__dirname, 'fixtures', 'pmlink');
74-
const { stdout } = await execAsync(`node ${pmlink} install`);
74+
const { stdout } = await execFileAsync('node', [pmlink, 'install']);
7575
expect(stdout).toBe('install\n');
7676
});
7777

7878
testOrSkipOnWindows('when subcommand file is double symlink then lookup succeeds', async() => {
7979
const pmlink = path.join(__dirname, 'fixtures', 'another-dir', 'pm');
80-
const { stdout } = await execAsync(`node ${pmlink} install`);
80+
const { stdout } = await execFileAsync('node', [pmlink, 'install']);
8181
expect(stdout).toBe('install\n');
8282
});
8383

@@ -86,11 +86,11 @@ test('when subcommand suffix is .ts then lookup succeeds', async() => {
8686
// The program and the subcommand `pm-install.ts` are both plain JavaScript code.
8787
const binLinkTs = path.join(__dirname, 'fixtures-ts', 'pm.ts');
8888
// childProcess.execFile('node', ['-r', 'ts-node/register', binLinkTs, 'install'], function(_error, stdout, stderr) {
89-
const { stdout } = await execAsync(`node ${binLinkTs} install`);
89+
const { stdout } = await execFileAsync('node', [binLinkTs, 'install']);
9090
expect(stdout).toBe('install\n');
9191
});
9292

9393
test('when subsubcommand then lookup sub-sub-command', async() => {
94-
const { stdout } = await execAsync(`node ${pm} cache clear`);
94+
const { stdout } = await execFileAsync('node', [pm, 'cache', 'clear']);
9595
expect(stdout).toBe('cache-clear\n');
9696
});

0 commit comments

Comments
 (0)