command.js
This file
Path
test/command.js
62 lines · 2.3 KiB · JavaScript
Last changed by
Improve error message when passing wrong arguments to `execaCommand()` (#899)ehmickybefore adoptiond1889f6
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import test from 'ava';import {execaCommand, execaCommandSync} from '../index.js';import {setFixtureDir} from './helpers/fixtures-dir.js';
setFixtureDir();
test('execaCommand()', async t => { const {stdout} = await execaCommand('echo.js foo bar'); 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');});
test('execaCommandSync()', t => { const {stdout} = execaCommandSync('echo.js foo bar'); t.is(stdout, 'foo\nbar');});
const testInvalidCommand = (t, execaCommand, invalidArgument) => { t.throws(() => { execaCommand(invalidArgument); }, {message: /First argument must be a string/});};
test('execaCommand() must use a string', testInvalidCommand, execaCommand, true);test('execaCommandSync() must use a string', testInvalidCommand, execaCommandSync, true);test('execaCommand() must have an argument', testInvalidCommand, execaCommand, undefined);test('execaCommandSync() must have an argument', testInvalidCommand, execaCommandSync, undefined);
const testInvalidArgs = (t, secondArgument, execaCommand) => { t.throws(() => { execaCommand('echo', secondArgument); }, {message: /The command and its arguments must be passed as a single string/});};
test('execaCommand() must not pass an array of arguments', testInvalidArgs, [''], execaCommand);test('execaCommandSync() must not pass an array of arguments', testInvalidArgs, [''], execaCommandSync);test('execaCommand() must not pass non-options as second argument', testInvalidArgs, '', execaCommand);test('execaCommandSync() must not pass non-options as second argument', testInvalidArgs, '', execaCommandSync);