command.js
This file
Path
test/methods/command.js
199 lines · 8.3 KiB · JavaScript
Last changed by
Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–2why, one step back1import {join} from 'node:path';2import test from 'ava';Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 3–16why, one step back3import {4 execa,5 execaSync,6 $,7 execaNode,8 execaCommand,9 execaCommandSync,10 parseCommandString,11} from '../../index.js';12import {13 setFixtureDirectory,14 FIXTURES_DIRECTORY,15 FIXTURES_DIRECTORY_URL,16} from '../helpers/fixtures-directory.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 17–18why, one step back17import {QUOTE} from '../helpers/verbose.js';18Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 19–20why, one step back19setFixtureDirectory();20const STDIN_FIXTURE = join(FIXTURES_DIRECTORY, 'stdin.js');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 21why, one step back21const ECHO_FIXTURE_URL = new URL('echo.js', FIXTURES_DIRECTORY_URL);Rename files (#977)ehmickybefore adoption4b2e316
line 22why, one step back22Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 23–24why, one step back23const parseAndRunCommand = command => execa`${parseCommandString(command)}`;24Rename files (#977)ehmickybefore adoption4b2e316
lines 25–29why, one step back25test('execaCommand()', async t => {26 const {stdout} = await execaCommand('echo.js foo bar');27 t.is(stdout, 'foo\nbar');28});29Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 30–34why, one step back30test('parseCommandString() + execa()', async t => {31 const {stdout} = await execa('echo.js', parseCommandString('foo bar'));32 t.is(stdout, 'foo\nbar');33});34Rename files (#977)ehmickybefore adoption4b2e316
lines 35–39why, one step back35test('execaCommandSync()', t => {36 const {stdout} = execaCommandSync('echo.js foo bar');37 t.is(stdout, 'foo\nbar');38});39Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 40–44why, one step back40test('parseCommandString() + execaSync()', t => {41 const {stdout} = execaSync('echo.js', parseCommandString('foo bar'));42 t.is(stdout, 'foo\nbar');43});44Rename files (#977)ehmickybefore adoption4b2e316
lines 45–49why, one step back45test('execaCommand`...`', async t => {46 const {stdout} = await execaCommand`${'echo.js foo bar'}`;47 t.is(stdout, 'foo\nbar');48});49Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 50–64why, one step back50test('parseCommandString() + execa`...`', async t => {51 const {stdout} = await execa`${parseCommandString('echo.js foo bar')}`;52 t.is(stdout, 'foo\nbar');53});5455test('parseCommandString() + execa`...`, only arguments', async t => {56 const {stdout} = await execa`echo.js ${parseCommandString('foo bar')}`;57 t.is(stdout, 'foo\nbar');58});5960test('parseCommandString() + execa`...`, only some arguments', async t => {61 const {stdout} = await execa`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`;62 t.is(stdout, 'foo bar\nfoo\nbar');63});64Rename files (#977)ehmickybefore adoption4b2e316
lines 65–69why, one step back65test('execaCommandSync`...`', t => {66 const {stdout} = execaCommandSync`${'echo.js foo bar'}`;67 t.is(stdout, 'foo\nbar');68});69Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 70–99why, one step back70test('parseCommandString() + execaSync`...`', t => {71 const {stdout} = execaSync`${parseCommandString('echo.js foo bar')}`;72 t.is(stdout, 'foo\nbar');73});7475test('parseCommandString() + execaSync`...`, only arguments', t => {76 const {stdout} = execaSync`echo.js ${parseCommandString('foo bar')}`;77 t.is(stdout, 'foo\nbar');78});7980test('parseCommandString() + execaSync`...`, only some arguments', t => {81 const {stdout} = execaSync`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`;82 t.is(stdout, 'foo bar\nfoo\nbar');83});8485test('parseCommandString() + $', async t => {86 const {stdout} = await $`${parseCommandString('echo.js foo bar')}`;87 t.is(stdout, 'foo\nbar');88});8990test('parseCommandString() + $.sync', t => {91 const {stdout} = $.sync`${parseCommandString('echo.js foo bar')}`;92 t.is(stdout, 'foo\nbar');93});9495test('parseCommandString() + execaNode', async t => {96 const {stdout} = await execaNode(ECHO_FIXTURE_URL, parseCommandString('foo bar'));97 t.is(stdout, 'foo\nbar');98});99Rename files (#977)ehmickybefore adoption4b2e316
lines 100–139why, one step back100test('execaCommand(options)`...`', async t => {101 const {stdout} = await execaCommand({stripFinalNewline: false})`${'echo.js foo bar'}`;102 t.is(stdout, 'foo\nbar\n');103});104105test('execaCommandSync(options)`...`', t => {106 const {stdout} = execaCommandSync({stripFinalNewline: false})`${'echo.js foo bar'}`;107 t.is(stdout, 'foo\nbar\n');108});109110test('execaCommand(options)()', async t => {111 const {stdout} = await execaCommand({stripFinalNewline: false})('echo.js foo bar');112 t.is(stdout, 'foo\nbar\n');113});114115test('execaCommandSync(options)()', t => {116 const {stdout} = execaCommandSync({stripFinalNewline: false})('echo.js foo bar');117 t.is(stdout, 'foo\nbar\n');118});119120test('execaCommand().pipe(execaCommand())', async t => {121 const {stdout} = await execaCommand('echo.js foo bar').pipe(execaCommand(`node ${STDIN_FIXTURE}`));122 t.is(stdout, 'foo\nbar');123});124125test('execaCommand().pipe(...) does not use execaCommand', async t => {126 const {escapedCommand} = await execaCommand('echo.js foo bar').pipe(`node ${STDIN_FIXTURE}`, {reject: false});127 t.true(escapedCommand.startsWith(`${QUOTE}node `));128});129130test('execaCommand() bound options have lower priority', async t => {131 const {stdout} = await execaCommand({stripFinalNewline: false})('echo.js foo bar', {stripFinalNewline: true});132 t.is(stdout, 'foo\nbar');133});134135test('execaCommandSync() bound options have lower priority', t => {136 const {stdout} = execaCommandSync({stripFinalNewline: false})('echo.js foo bar', {stripFinalNewline: true});137 t.is(stdout, 'foo\nbar');138});139Upgrade xo (#980)ehmickybefore adoptionccfa412
line 140why, one step back140const testInvalidArgumentsArray = (t, execaMethod) => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 141–143why, one step back141 t.throws(() => execaMethod('echo', ['foo']), {142 message: /The command and its arguments must be passed as a single string/,143 });Rename files (#977)ehmickybefore adoption4b2e316
lines 144–145why, one step back144};145Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 146–147why, one step back146test('execaCommand() must not pass an array of arguments', testInvalidArgumentsArray, execaCommand);147test('execaCommandSync() must not pass an array of arguments', testInvalidArgumentsArray, execaCommandSync);Rename files (#977)ehmickybefore adoption4b2e316
line 148why, one step back148Upgrade xo (#980)ehmickybefore adoptionccfa412
line 149why, one step back149const testInvalidArgumentsTemplate = (t, execaMethod) => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 150–152why, one step back150 t.throws(() => execaMethod`echo foo`, {151 message: /The command and its arguments must be passed as a single string/,152 });Rename files (#977)ehmickybefore adoption4b2e316
lines 153–154why, one step back153};154Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 155–156why, one step back155test('execaCommand() must not pass an array of arguments with a template string', testInvalidArgumentsTemplate, execaCommand);156test('execaCommandSync() must not pass an array of arguments with a template string', testInvalidArgumentsTemplate, execaCommandSync);Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 157–199why, one step back157158const testInvalidArgumentsParse = (t, command) => {159 t.throws(() => parseCommandString(command), {160 message: /The command must be a string/,161 });162};163164test('execaCommand() must not pass a number', testInvalidArgumentsParse, 0);165test('execaCommand() must not pass undefined', testInvalidArgumentsParse, undefined);166test('execaCommand() must not pass null', testInvalidArgumentsParse, null);167test('execaCommand() must not pass a symbol', testInvalidArgumentsParse, Symbol('test'));168test('execaCommand() must not pass an object', testInvalidArgumentsParse, {});169test('execaCommand() must not pass an array', testInvalidArgumentsParse, []);170171const testExecaCommandOutput = async (t, command, expectedOutput, execaMethod) => {172 const {stdout} = await execaMethod(command);173 t.is(stdout, expectedOutput);174};175176test('execaCommand() allows escaping spaces in commands', testExecaCommandOutput, 'command\\ with\\ space.js foo bar', 'foo\nbar', execaCommand);177test('execaCommand() trims', testExecaCommandOutput, ' echo.js foo bar ', 'foo\nbar', execaCommand);178test('execaCommand() ignores consecutive spaces', testExecaCommandOutput, 'echo.js foo bar', 'foo\nbar', execaCommand);179test('execaCommand() escapes other whitespaces', testExecaCommandOutput, 'echo.js foo\tbar', 'foo\tbar', execaCommand);180test('execaCommand() allows escaping spaces', testExecaCommandOutput, 'echo.js foo\\ bar', 'foo bar', execaCommand);181test('execaCommand() allows escaping backslashes before spaces', testExecaCommandOutput, 'echo.js foo\\\\ bar', 'foo\\ bar', execaCommand);182test('execaCommand() allows escaping multiple backslashes before spaces', testExecaCommandOutput, 'echo.js foo\\\\\\\\ bar', 'foo\\\\\\ bar', execaCommand);183test('execaCommand() allows escaping backslashes not before spaces', testExecaCommandOutput, 'echo.js foo\\bar baz', 'foo\\bar\nbaz', execaCommand);184test('parseCommandString() allows escaping spaces in commands', testExecaCommandOutput, 'command\\ with\\ space.js foo bar', 'foo\nbar', parseAndRunCommand);185test('parseCommandString() trims', testExecaCommandOutput, ' echo.js foo bar ', 'foo\nbar', parseAndRunCommand);186test('parseCommandString() ignores consecutive spaces', testExecaCommandOutput, 'echo.js foo bar', 'foo\nbar', parseAndRunCommand);187test('parseCommandString() escapes other whitespaces', testExecaCommandOutput, 'echo.js foo\tbar', 'foo\tbar', parseAndRunCommand);188test('parseCommandString() allows escaping spaces', testExecaCommandOutput, 'echo.js foo\\ bar', 'foo bar', parseAndRunCommand);189test('parseCommandString() allows escaping backslashes before spaces', testExecaCommandOutput, 'echo.js foo\\\\ bar', 'foo\\ bar', parseAndRunCommand);190test('parseCommandString() allows escaping multiple backslashes before spaces', testExecaCommandOutput, 'echo.js foo\\\\\\\\ bar', 'foo\\\\\\ bar', parseAndRunCommand);191test('parseCommandString() allows escaping backslashes not before spaces', testExecaCommandOutput, 'echo.js foo\\bar baz', 'foo\\bar\nbaz', parseAndRunCommand);192193test('parseCommandString() can get empty strings', t => {194 t.deepEqual(parseCommandString(''), []);195});196197test('parseCommandString() can get only whitespaces', t => {198 t.deepEqual(parseCommandString(' '), []);199});