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
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
lines 1–3why, one step back1import assert from 'node:assert/strict';2import test from 'node:test';3import {assertThrows} from '../helpers/assert.js';Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 4–14why, one step back4import {5 execa,6 execaSync,7 $,8 execaNode,9 parseCommandString,10} from '../../index.js';11import {12 setFixtureDirectory,13 FIXTURES_DIRECTORY_URL,14} from '../helpers/fixtures-directory.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 15–16why, one step back1516setFixtureDirectory();Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 17why, one step back17const ECHO_FIXTURE_URL = new URL('echo.js', FIXTURES_DIRECTORY_URL);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 18why, one step back18Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 19–20why, one step back19const parseAndRunCommand = command => execa`${parseCommandString(command)}`;20TweaksSindre Sorhusbefore adoption698a1b8
line 21why, one step back21test('parseCommandString() + execa()', async () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 22why, one step back22 const {stdout} = await execa('echo.js', parseCommandString('foo bar'));TweaksSindre Sorhusbefore adoption698a1b8
line 23why, one step back23 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 24–25why, one step back24});25TweaksSindre Sorhusbefore adoption698a1b8
line 26why, one step back26test('parseCommandString() + execaSync()', () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 27why, one step back27 const {stdout} = execaSync('echo.js', parseCommandString('foo bar'));TweaksSindre Sorhusbefore adoption698a1b8
line 28why, one step back28 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 29–30why, one step back29});30TweaksSindre Sorhusbefore adoption698a1b8
line 31why, one step back31test('parseCommandString() + execa`...`', async () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 32why, one step back32 const {stdout} = await execa`${parseCommandString('echo.js foo bar')}`;TweaksSindre Sorhusbefore adoption698a1b8
line 33why, one step back33 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 34–35why, one step back34});35TweaksSindre Sorhusbefore adoption698a1b8
line 36why, one step back36test('parseCommandString() + execa`...`, only arguments', async () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 37why, one step back37 const {stdout} = await execa`echo.js ${parseCommandString('foo bar')}`;TweaksSindre Sorhusbefore adoption698a1b8
line 38why, one step back38 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 39–40why, one step back39});40TweaksSindre Sorhusbefore adoption698a1b8
line 41why, one step back41test('parseCommandString() + execa`...`, only some arguments', async () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 42why, one step back42 const {stdout} = await execa`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`;TweaksSindre Sorhusbefore adoption698a1b8
line 43why, one step back43 assert.equal(stdout, 'foo bar\nfoo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 44–45why, one step back44});45TweaksSindre Sorhusbefore adoption698a1b8
line 46why, one step back46test('parseCommandString() + execaSync`...`', () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 47why, one step back47 const {stdout} = execaSync`${parseCommandString('echo.js foo bar')}`;TweaksSindre Sorhusbefore adoption698a1b8
line 48why, one step back48 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 49–50why, one step back49});50TweaksSindre Sorhusbefore adoption698a1b8
line 51why, one step back51test('parseCommandString() + execaSync`...`, only arguments', () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 52why, one step back52 const {stdout} = execaSync`echo.js ${parseCommandString('foo bar')}`;TweaksSindre Sorhusbefore adoption698a1b8
line 53why, one step back53 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 54–55why, one step back54});55TweaksSindre Sorhusbefore adoption698a1b8
line 56why, one step back56test('parseCommandString() + execaSync`...`, only some arguments', () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 57why, one step back57 const {stdout} = execaSync`echo.js ${'foo bar'} ${parseCommandString('foo bar')}`;TweaksSindre Sorhusbefore adoption698a1b8
line 58why, one step back58 assert.equal(stdout, 'foo bar\nfoo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 59–60why, one step back59});60TweaksSindre Sorhusbefore adoption698a1b8
line 61why, one step back61test('parseCommandString() + $', async () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 62why, one step back62 const {stdout} = await $`${parseCommandString('echo.js foo bar')}`;TweaksSindre Sorhusbefore adoption698a1b8
line 63why, one step back63 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 64–65why, one step back64});65TweaksSindre Sorhusbefore adoption698a1b8
line 66why, one step back66test('parseCommandString() + $.sync', () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 67why, one step back67 const {stdout} = $.sync`${parseCommandString('echo.js foo bar')}`;TweaksSindre Sorhusbefore adoption698a1b8
line 68why, one step back68 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 69–70why, one step back69});70TweaksSindre Sorhusbefore adoption698a1b8
line 71why, one step back71test('parseCommandString() + execaNode', async () => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 72why, one step back72 const {stdout} = await execaNode(ECHO_FIXTURE_URL, parseCommandString('foo bar'));TweaksSindre Sorhusbefore adoption698a1b8
line 73why, one step back73 assert.equal(stdout, 'foo\nbar');Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 74–75why, one step back74});75TweaksSindre Sorhusbefore adoption698a1b8
lines 76–77why, one step back76const testInvalidArgumentsParse = command => {77 assertThrows(() => parseCommandString(command), {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 78–81why, one step back78 message: /The command must be a string/,79 });80};81TweaksSindre Sorhusbefore adoption698a1b8
lines 82–87why, one step back82test('parseCommandString() must not pass a number', () => testInvalidArgumentsParse(0));83test('parseCommandString() must not pass undefined', () => testInvalidArgumentsParse(undefined));84test('parseCommandString() must not pass null', () => testInvalidArgumentsParse(null));85test('parseCommandString() must not pass a symbol', () => testInvalidArgumentsParse(Symbol('test')));86test('parseCommandString() must not pass an object', () => testInvalidArgumentsParse({}));87test('parseCommandString() must not pass an array', () => testInvalidArgumentsParse([]));Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 88why, one step back88TweaksSindre Sorhusbefore adoption698a1b8
line 89why, one step back89const testParseCommandOutput = async (command, expectedOutput, execaMethod) => {Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 90why, one step back90 const {stdout} = await execaMethod(command);TweaksSindre Sorhusbefore adoption698a1b8
line 91why, one step back91 assert.equal(stdout, expectedOutput);Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 92–93why, one step back92};93TweaksSindre Sorhusbefore adoption698a1b8
lines 94–101why, one step back94test('parseCommandString() allows escaping spaces in commands', () => testParseCommandOutput('command\\ with\\ space.js foo bar', 'foo\nbar', parseAndRunCommand));95test('parseCommandString() trims', () => testParseCommandOutput(' echo.js foo bar ', 'foo\nbar', parseAndRunCommand));96test('parseCommandString() ignores consecutive spaces', () => testParseCommandOutput('echo.js foo bar', 'foo\nbar', parseAndRunCommand));97test('parseCommandString() escapes other whitespaces', () => testParseCommandOutput('echo.js foo\tbar', 'foo\tbar', parseAndRunCommand));98test('parseCommandString() allows escaping spaces', () => testParseCommandOutput('echo.js foo\\ bar', 'foo bar', parseAndRunCommand));99test('parseCommandString() allows escaping backslashes before spaces', () => testParseCommandOutput('echo.js foo\\\\ bar', 'foo\\ bar', parseAndRunCommand));100test('parseCommandString() allows escaping multiple backslashes before spaces', () => testParseCommandOutput('echo.js foo\\\\\\\\ bar', 'foo\\\\\\ bar', parseAndRunCommand));101test('parseCommandString() allows escaping backslashes not before spaces', () => testParseCommandOutput('echo.js foo\\bar baz', 'foo\\bar\nbaz', parseAndRunCommand));Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 102why, one step back102Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1
lines 103–155why, one step back103// An escaped space must not be merged with the space delimiter after it104test('parseCommandString() allows escaping spaces before a space delimiter', () => {105 assert.deepEqual(parseCommandString('echo foo\\ bar'), ['echo', 'foo ', 'bar']);106});107108test('parseCommandString() allows escaping spaces before consecutive space delimiters', () => {109 assert.deepEqual(parseCommandString('echo foo\\ bar'), ['echo', 'foo ', 'bar']);110});111112test('parseCommandString() allows escaping consecutive spaces', () => {113 assert.deepEqual(parseCommandString('echo foo\\ \\ bar'), ['echo', 'foo bar']);114});115116test('parseCommandString() allows escaped spaces as the only characters of an argument', () => {117 assert.deepEqual(parseCommandString('echo \\ bar'), ['echo', ' ', 'bar']);118});119120test('parseCommandString() allows escaping a trailing space', () => {121 assert.deepEqual(parseCommandString('echo foo\\ '), ['echo', 'foo ']);122});123124test('parseCommandString() allows escaping a trailing space followed by other spaces', () => {125 assert.deepEqual(parseCommandString('echo foo\\ '), ['echo', 'foo ']);126});127128test('parseCommandString() allows escaping a trailing space followed by other whitespaces', () => {129 assert.deepEqual(parseCommandString('echo foo\\ \n'), ['echo', 'foo ']);130});131132test('parseCommandString() does not escape trailing whitespaces other than spaces', () => {133 assert.deepEqual(parseCommandString('echo foo\\\n'), ['echo', 'foo\\']);134});135136test('parseCommandString() trims whitespaces other than spaces', () => {137 assert.deepEqual(parseCommandString('\t echo foo \n'), ['echo', 'foo']);138});139140test('parseCommandString() allows escaping a leading space', () => {141 assert.deepEqual(parseCommandString('echo \\ foo'), ['echo', ' foo']);142});143144test('parseCommandString() allows escaping a space in the first token', () => {145 assert.deepEqual(parseCommandString('foo\\ bar baz'), ['foo bar', 'baz']);146});147148test('parseCommandString() allows only an escaped space', () => {149 assert.deepEqual(parseCommandString('\\ '), [' ']);150});151152test('parseCommandString() keeps tabs inside tokens with escaped spaces', () => {153 assert.deepEqual(parseCommandString('echo foo\tbar\\ baz'), ['echo', 'foo\tbar baz']);154});155TweaksSindre Sorhusbefore adoption698a1b8
lines 156–157why, one step back156test('parseCommandString() can get empty strings', () => {157 assert.deepEqual(parseCommandString(''), []);Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
lines 158–159why, one step back158});159TweaksSindre Sorhusbefore adoption698a1b8
line 160why, one step back160test('parseCommandString() can get only whitespaces', () => {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 161why, one step back161 // eslint-disable-next-line unicorn/prefer-string-repeatTweaksSindre Sorhusbefore adoption698a1b8
line 162why, one step back162 assert.deepEqual(parseCommandString(' '), []);Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 163why, one step back163});