Skip to content

Commit

Permalink
Improve tests for execaCommand() (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 5, 2024
1 parent 9de7335 commit 97c0a0b
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions test/methods/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,28 @@ test('execaCommandSync() bound options have lower priority', t => {
t.is(stdout, 'foo\nbar');
});

test('execaCommand() ignores consecutive spaces', async t => {
const {stdout} = await execaCommand('echo.js foo bar');
t.is(stdout, 'foo\nbar');
});

test('execaCommand() allows escaping spaces in commands', async t => {
const {stdout} = await execaCommand('command\\ with\\ space.js foo bar');
t.is(stdout, 'foo\nbar');
});

test('execaCommand() allows escaping spaces in arguments', async t => {
const {stdout} = await execaCommand('echo.js foo\\ bar');
t.is(stdout, 'foo bar');
});

test('execaCommand() escapes other whitespaces', async t => {
const {stdout} = await execaCommand('echo.js foo\tbar');
t.is(stdout, 'foo\tbar');
});

test('execaCommand() trims', async t => {
const {stdout} = await execaCommand(' echo.js foo bar ');
t.is(stdout, 'foo\nbar');
});

const testExecaCommandOutput = async (t, commandArguments, expectedOutput) => {
const {stdout} = await execaCommand(`echo.js ${commandArguments}`);
t.is(stdout, expectedOutput);
};

test('execaCommand() ignores consecutive spaces', testExecaCommandOutput, 'foo bar', 'foo\nbar');
test('execaCommand() escapes other whitespaces', testExecaCommandOutput, 'foo\tbar', 'foo\tbar');
test('execaCommand() allows escaping spaces', testExecaCommandOutput, 'foo\\ bar', 'foo bar');
test('execaCommand() allows escaping backslashes before spaces', testExecaCommandOutput, 'foo\\\\ bar', 'foo\\ bar');
test('execaCommand() allows escaping multiple backslashes before spaces', testExecaCommandOutput, 'foo\\\\\\\\ bar', 'foo\\\\\\ bar');
test('execaCommand() allows escaping backslashes not before spaces', testExecaCommandOutput, 'foo\\bar baz', 'foo\\bar\nbaz');

const testInvalidArgumentsArray = (t, execaMethod) => {
t.throws(() => {
execaMethod('echo', ['foo']);
Expand Down

0 comments on commit 97c0a0b

Please sign in to comment.