command.js
This file
Path
test/methods/command.js
108 lines · 4.4 KiB · JavaScript
Last changed by
Remove `execaCommand()` and `execaCommandSync()` (#1244)Sindre Sorhusbefore adoption3ced394
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import test from 'ava';import { execa, execaSync, $, execaNode, parseCommandString,} from '../../index.js';import { setFixtureDirectory, FIXTURES_DIRECTORY_URL,} from '../helpers/fixtures-directory.js';
setFixtureDirectory();const ECHO_FIXTURE_URL = new URL('echo.js', FIXTURES_DIRECTORY_URL);
const parseAndRunCommand = command => execa`${parseCommandString(command)}`;
test('parseCommandString() + execa()', async t => { const {stdout} = await execa('echo.js', parseCommandString('foo bar')); t.is(stdout, 'foo\nbar');});
test('parseCommandString() + execaSync()', t => { const {stdout} = execaSync('echo.js', parseCommandString('foo bar')); t.is(stdout, 'foo\nbar');});
test('parseCommandString() + execa`...`', async t => { const {stdout} = await execa`${parseCommandString('echo.js foo bar')}`; t.is(stdout, 'foo\nbar');});
test('parseCommandString() + execa`...`, only arguments', async t => { const {stdout} = await execa`echo.js ${parseCommandString('foo bar')}`; t.is(stdout, 'foo\nbar');});
test('parseCommandString() + execa`...`, only some arguments', async t => { const {stdout} = await execa`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`; t.is(stdout, 'foo bar\nfoo\nbar');});
test('parseCommandString() + execaSync`...`', t => { const {stdout} = execaSync`${parseCommandString('echo.js foo bar')}`; t.is(stdout, 'foo\nbar');});
test('parseCommandString() + execaSync`...`, only arguments', t => { const {stdout} = execaSync`echo.js ${parseCommandString('foo bar')}`; t.is(stdout, 'foo\nbar');});
test('parseCommandString() + execaSync`...`, only some arguments', t => { const {stdout} = execaSync`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`; t.is(stdout, 'foo bar\nfoo\nbar');});
test('parseCommandString() + $', async t => { const {stdout} = await $`${parseCommandString('echo.js foo bar')}`; t.is(stdout, 'foo\nbar');});
test('parseCommandString() + $.sync', t => { const {stdout} = $.sync`${parseCommandString('echo.js foo bar')}`; t.is(stdout, 'foo\nbar');});
test('parseCommandString() + execaNode', async t => { const {stdout} = await execaNode(ECHO_FIXTURE_URL, parseCommandString('foo bar')); t.is(stdout, 'foo\nbar');});
const testInvalidArgumentsParse = (t, command) => { t.throws(() => parseCommandString(command), { message: /The command must be a string/, });};
test('parseCommandString() must not pass a number', testInvalidArgumentsParse, 0);test('parseCommandString() must not pass undefined', testInvalidArgumentsParse, undefined);test('parseCommandString() must not pass null', testInvalidArgumentsParse, null);test('parseCommandString() must not pass a symbol', testInvalidArgumentsParse, Symbol('test'));test('parseCommandString() must not pass an object', testInvalidArgumentsParse, {});test('parseCommandString() must not pass an array', testInvalidArgumentsParse, []);
const testParseCommandOutput = async (t, command, expectedOutput, execaMethod) => { const {stdout} = await execaMethod(command); t.is(stdout, expectedOutput);};
test('parseCommandString() allows escaping spaces in commands', testParseCommandOutput, 'command\\ with\\ space.js foo bar', 'foo\nbar', parseAndRunCommand);test('parseCommandString() trims', testParseCommandOutput, ' echo.js foo bar ', 'foo\nbar', parseAndRunCommand);test('parseCommandString() ignores consecutive spaces', testParseCommandOutput, 'echo.js foo bar', 'foo\nbar', parseAndRunCommand);test('parseCommandString() escapes other whitespaces', testParseCommandOutput, 'echo.js foo\tbar', 'foo\tbar', parseAndRunCommand);test('parseCommandString() allows escaping spaces', testParseCommandOutput, 'echo.js foo\\ bar', 'foo bar', parseAndRunCommand);test('parseCommandString() allows escaping backslashes before spaces', testParseCommandOutput, 'echo.js foo\\\\ bar', 'foo\\ bar', parseAndRunCommand);test('parseCommandString() allows escaping multiple backslashes before spaces', testParseCommandOutput, 'echo.js foo\\\\\\\\ bar', 'foo\\\\\\ bar', parseAndRunCommand);test('parseCommandString() allows escaping backslashes not before spaces', testParseCommandOutput, 'echo.js foo\\bar baz', 'foo\\bar\nbaz', parseAndRunCommand);
test('parseCommandString() can get empty strings', t => { t.deepEqual(parseCommandString(''), []);});
test('parseCommandString() can get only whitespaces', t => { // eslint-disable-next-line unicorn/prefer-string-repeat t.deepEqual(parseCommandString(' '), []);});