command.js
This file
Path
test/methods/command.js
163 lines · 6.8 KiB · JavaScript
Last changed by
Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164import assert from 'node:assert/strict';import test from 'node:test';import {assertThrows} from '../helpers/assert.js';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 () => { const {stdout} = await execa('echo.js', parseCommandString('foo bar')); assert.equal(stdout, 'foo\nbar');});
test('parseCommandString() + execaSync()', () => { const {stdout} = execaSync('echo.js', parseCommandString('foo bar')); assert.equal(stdout, 'foo\nbar');});
test('parseCommandString() + execa`...`', async () => { const {stdout} = await execa`${parseCommandString('echo.js foo bar')}`; assert.equal(stdout, 'foo\nbar');});
test('parseCommandString() + execa`...`, only arguments', async () => { const {stdout} = await execa`echo.js ${parseCommandString('foo bar')}`; assert.equal(stdout, 'foo\nbar');});
test('parseCommandString() + execa`...`, only some arguments', async () => { const {stdout} = await execa`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`; assert.equal(stdout, 'foo bar\nfoo\nbar');});
test('parseCommandString() + execaSync`...`', () => { const {stdout} = execaSync`${parseCommandString('echo.js foo bar')}`; assert.equal(stdout, 'foo\nbar');});
test('parseCommandString() + execaSync`...`, only arguments', () => { const {stdout} = execaSync`echo.js ${parseCommandString('foo bar')}`; assert.equal(stdout, 'foo\nbar');});
test('parseCommandString() + execaSync`...`, only some arguments', () => { const {stdout} = execaSync`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`; assert.equal(stdout, 'foo bar\nfoo\nbar');});
test('parseCommandString() + $', async () => { const {stdout} = await $`${parseCommandString('echo.js foo bar')}`; assert.equal(stdout, 'foo\nbar');});
test('parseCommandString() + $.sync', () => { const {stdout} = $.sync`${parseCommandString('echo.js foo bar')}`; assert.equal(stdout, 'foo\nbar');});
test('parseCommandString() + execaNode', async () => { const {stdout} = await execaNode(ECHO_FIXTURE_URL, parseCommandString('foo bar')); assert.equal(stdout, 'foo\nbar');});
const testInvalidArgumentsParse = command => { assertThrows(() => 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 (command, expectedOutput, execaMethod) => { const {stdout} = await execaMethod(command); assert.equal(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));
// An escaped space must not be merged with the space delimiter after ittest('parseCommandString() allows escaping spaces before a space delimiter', () => { assert.deepEqual(parseCommandString('echo foo\\ bar'), ['echo', 'foo ', 'bar']);});
test('parseCommandString() allows escaping spaces before consecutive space delimiters', () => { assert.deepEqual(parseCommandString('echo foo\\ bar'), ['echo', 'foo ', 'bar']);});
test('parseCommandString() allows escaping consecutive spaces', () => { assert.deepEqual(parseCommandString('echo foo\\ \\ bar'), ['echo', 'foo bar']);});
test('parseCommandString() allows escaped spaces as the only characters of an argument', () => { assert.deepEqual(parseCommandString('echo \\ bar'), ['echo', ' ', 'bar']);});
test('parseCommandString() allows escaping a trailing space', () => { assert.deepEqual(parseCommandString('echo foo\\ '), ['echo', 'foo ']);});
test('parseCommandString() allows escaping a trailing space followed by other spaces', () => { assert.deepEqual(parseCommandString('echo foo\\ '), ['echo', 'foo ']);});
test('parseCommandString() allows escaping a trailing space followed by other whitespaces', () => { assert.deepEqual(parseCommandString('echo foo\\ \n'), ['echo', 'foo ']);});
test('parseCommandString() does not escape trailing whitespaces other than spaces', () => { assert.deepEqual(parseCommandString('echo foo\\\n'), ['echo', 'foo\\']);});
test('parseCommandString() trims whitespaces other than spaces', () => { assert.deepEqual(parseCommandString('\t echo foo \n'), ['echo', 'foo']);});
test('parseCommandString() allows escaping a leading space', () => { assert.deepEqual(parseCommandString('echo \\ foo'), ['echo', ' foo']);});
test('parseCommandString() allows escaping a space in the first token', () => { assert.deepEqual(parseCommandString('foo\\ bar baz'), ['foo bar', 'baz']);});
test('parseCommandString() allows only an escaped space', () => { assert.deepEqual(parseCommandString('\\ '), [' ']);});
test('parseCommandString() keeps tabs inside tokens with escaped spaces', () => { assert.deepEqual(parseCommandString('echo foo\tbar\\ baz'), ['echo', 'foo\tbar baz']);});
test('parseCommandString() can get empty strings', () => { assert.deepEqual(parseCommandString(''), []);});
test('parseCommandString() can get only whitespaces', () => { // eslint-disable-next-line unicorn/prefer-string-repeat assert.deepEqual(parseCommandString(' '), []);});