escape.js
This file
Path
test/arguments/escape.js
158 lines · 9.4 KiB · JavaScript
Last changed by
Fix `escapedCommand` and `verbose` quoting arguments when using the `shell` optionSindre Sorhusbefore adoptionb2c3a54
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 2why, one step back2import {platform} from 'node:process';TweaksSindre Sorhusbefore adoption698a1b8
lines 3–4why, one step back3import test from 'node:test';4import {assertThrows, assertRejects, assertLike} from '../helpers/assert.js';Fix `escapedCommand` and `verbose` quoting arguments when using the `shell` optionSindre Sorhusbefore adoptionb2c3a54
lines 5–6why, one step back5import {execa, execaSync, execaNode} from '../../index.js';6import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 7–11why, one step back78setFixtureDirectory();910const isWindows = platform === 'win32';11TweaksSindre Sorhusbefore adoption698a1b8
lines 12–14why, one step back12const testResultCommand = async (expected, ...commandArguments) => {13 const {command: failCommand} = await assertRejects(execa('fail.js', commandArguments));14 assert.equal(failCommand, `fail.js${expected}`);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 15–16why, one step back1516 const {command} = await execa('noop.js', commandArguments);TweaksSindre Sorhusbefore adoption698a1b8
line 17why, one step back17 assert.equal(command, `noop.js${expected}`);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 18–19why, one step back18};19TweaksSindre Sorhusbefore adoption698a1b8
lines 20–22why, one step back20test('result.command is: " foo bar"', () => testResultCommand(' foo bar', 'foo', 'bar'));21test('result.command is: " baz quz"', () => testResultCommand(' baz quz', 'baz', 'quz'));22test('result.command is: ""', () => testResultCommand(''));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 23why, one step back23Fix Node.js without ICU support (#1144)ehmickybefore adoption074ea4a
line 24why, one step back24// eslint-disable-next-line max-paramsTweaksSindre Sorhusbefore adoption698a1b8
line 25why, one step back25const testEscapedCommand = async (commandArguments, expectedUnix, expectedWindows, expectedUnixNoIcu = expectedUnix, expectedWindowsNoIcu = expectedWindows) => {Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 26why, one step back26 // eslint-disable-next-line no-use-extend-native/no-use-extend-native, unicorn/no-nonstandard-builtin-properties -- `RegExp.isMocked` is a static property added by `escape-no-icu.js`, not a native extensionFix Node.js without ICU support (#1144)ehmickybefore adoption074ea4a
lines 27–29why, one step back27 const expected = RegExp.isMocked28 ? (isWindows ? expectedWindowsNoIcu : expectedUnixNoIcu)29 : (isWindows ? expectedWindows : expectedUnix);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 30why, one step back30TweaksSindre Sorhusbefore adoption698a1b8
lines 31–32why, one step back31 assertLike(32 await assertRejects(execa('fail.js', commandArguments)),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 33–35why, one step back33 {escapedCommand: `fail.js ${expected}`},34 );35TweaksSindre Sorhusbefore adoption698a1b8
line 36why, one step back36 assertLike(assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 37–39why, one step back37 execaSync('fail.js', commandArguments);38 }), {escapedCommand: `fail.js ${expected}`});39TweaksSindre Sorhusbefore adoption698a1b8
line 40why, one step back40 assertLike(100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 41–44why, one step back41 await execa('noop.js', commandArguments),42 {escapedCommand: `noop.js ${expected}`},43 );44TweaksSindre Sorhusbefore adoption698a1b8
line 45why, one step back45 assertLike(100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 46–50why, one step back46 execaSync('noop.js', commandArguments),47 {escapedCommand: `noop.js ${expected}`},48 );49};50TweaksSindre Sorhusbefore adoption698a1b8
lines 51–104why, one step back51test('result.escapedCommand - foo bar', () => testEscapedCommand(['foo', 'bar'], 'foo bar', 'foo bar'));52test('result.escapedCommand - foo\\ bar', () => testEscapedCommand(['foo bar'], '\'foo bar\'', '"foo bar"'));53test('result.escapedCommand - "foo"', () => testEscapedCommand(['"foo"'], '\'"foo"\'', '"""foo"""'));54test('result.escapedCommand - \'foo\'', () => testEscapedCommand(['\'foo\''], '\'\'\\\'\'foo\'\\\'\'\'', '"\'foo\'"'));55test('result.escapedCommand - "0"', () => testEscapedCommand(['0'], '0', '0'));56test('result.escapedCommand - 0', () => testEscapedCommand([0], '0', '0'));57test('result.escapedCommand - *', () => testEscapedCommand(['*'], '\'*\'', '"*"'));58test('result.escapedCommand - .', () => testEscapedCommand(['.'], '.', '.'));59test('result.escapedCommand - -', () => testEscapedCommand(['-'], '-', '-'));60test('result.escapedCommand - _', () => testEscapedCommand(['_'], '_', '_'));61test('result.escapedCommand - /', () => testEscapedCommand(['/'], '/', '/'));62test('result.escapedCommand - ,', () => testEscapedCommand([','], '\',\'', '","'));63test('result.escapedCommand - :', () => testEscapedCommand([':'], '\':\'', '":"'));64test('result.escapedCommand - ;', () => testEscapedCommand([';'], '\';\'', '";"'));65test('result.escapedCommand - ~', () => testEscapedCommand(['~'], '\'~\'', '"~"'));66test('result.escapedCommand - %', () => testEscapedCommand(['%'], '\'%\'', '"%"'));67test('result.escapedCommand - $', () => testEscapedCommand(['$'], '\'$\'', '"$"'));68test('result.escapedCommand - !', () => testEscapedCommand(['!'], '\'!\'', '"!"'));69test('result.escapedCommand - ?', () => testEscapedCommand(['?'], '\'?\'', '"?"'));70test('result.escapedCommand - #', () => testEscapedCommand(['#'], '\'#\'', '"#"'));71test('result.escapedCommand - &', () => testEscapedCommand(['&'], '\'&\'', '"&"'));72test('result.escapedCommand - =', () => testEscapedCommand(['='], '\'=\'', '"="'));73test('result.escapedCommand - @', () => testEscapedCommand(['@'], '\'@\'', '"@"'));74test('result.escapedCommand - ^', () => testEscapedCommand(['^'], '\'^\'', '"^"'));75test('result.escapedCommand - `', () => testEscapedCommand(['`'], '\'`\'', '"`"'));76test('result.escapedCommand - |', () => testEscapedCommand(['|'], '\'|\'', '"|"'));77test('result.escapedCommand - +', () => testEscapedCommand(['+'], '\'+\'', '"+"'));78test('result.escapedCommand - \\', () => testEscapedCommand(['\\'], '\'\\\'', '"\\"'));79test('result.escapedCommand - ()', () => testEscapedCommand(['()'], '\'()\'', '"()"'));80test('result.escapedCommand - {}', () => testEscapedCommand(['{}'], '\'{}\'', '"{}"'));81test('result.escapedCommand - []', () => testEscapedCommand(['[]'], '\'[]\'', '"[]"'));82test('result.escapedCommand - <>', () => testEscapedCommand(['<>'], '\'<>\'', '"<>"'));83test('result.escapedCommand - ã', () => testEscapedCommand(['ã'], '\'ã\'', '"ã"'));84test('result.escapedCommand - \\a', () => testEscapedCommand(['\u{7}'], '\'\\u0007\'', '"\\u0007"'));85test('result.escapedCommand - \\b', () => testEscapedCommand(['\b'], '\'\\b\'', '"\\b"'));86test('result.escapedCommand - \\e', () => testEscapedCommand(['\u{1B}'], '\'\\u001b\'', '"\\u001b"'));87test('result.escapedCommand - \\f', () => testEscapedCommand(['\f'], '\'\\f\'', '"\\f"'));88test('result.escapedCommand - \\n', () => testEscapedCommand(['\n'], '\'\\n\'', '"\\n"'));89test('result.escapedCommand - \\r\\n', () => testEscapedCommand(['\r\n'], '\'\\r\\n\'', '"\\r\\n"'));90test('result.escapedCommand - \\t', () => testEscapedCommand(['\t'], '\'\\t\'', '"\\t"'));91test('result.escapedCommand - \\v', () => testEscapedCommand(['\v'], '\'\\u000b\'', '"\\u000b"'));92test('result.escapedCommand - \\x01', () => testEscapedCommand(['\u{1}'], '\'\\u0001\'', '"\\u0001"'));93test('result.escapedCommand - \\x7f', () => testEscapedCommand(['\u{7F}'], '\'\\u007f\'', '"\\u007f"'));94test('result.escapedCommand - \\u0085', () => testEscapedCommand(['\u{85}'], '\'\\u0085\'', '"\\u0085"'));95test('result.escapedCommand - \\u2000', () => testEscapedCommand(['\u{2000}'], '\'\\u2000\'', '"\\u2000"'));96test('result.escapedCommand - \\u200E', () => testEscapedCommand(['\u{200E}'], '\'\\u200e\'', '"\\u200e"', '\'\u{200E}\'', '"\u{200E}"'));97test('result.escapedCommand - \\u2028', () => testEscapedCommand(['\u{2028}'], '\'\\u2028\'', '"\\u2028"'));98test('result.escapedCommand - \\u2029', () => testEscapedCommand(['\u{2029}'], '\'\\u2029\'', '"\\u2029"'));99test('result.escapedCommand - \\u5555', () => testEscapedCommand(['\u{5555}'], '\'\u{5555}\'', '"\u{5555}"'));100test('result.escapedCommand - \\uD800', () => testEscapedCommand(['\u{D800}'], '\'\\ud800\'', '"\\ud800"', '\'\u{D800}\'', '"\u{D800}"'));101test('result.escapedCommand - \\uE000', () => testEscapedCommand(['\u{E000}'], '\'\\ue000\'', '"\\ue000"', '\'\u{E000}\'', '"\u{E000}"'));102test('result.escapedCommand - \\U1D172', () => testEscapedCommand(['\u{1D172}'], '\'\u{1D172}\'', '"\u{1D172}"'));103test('result.escapedCommand - \\U1D173', () => testEscapedCommand(['\u{1D173}'], '\'\\U1d173\'', '"\\U1d173"', '\'\u{1D173}\'', '"\u{1D173}"'));104test('result.escapedCommand - \\U10FFFD', () => testEscapedCommand(['\u{10FFFD}'], '\'\\U10fffd\'', '"\\U10fffd"', '\'\u{10FFFD}\'', '"\u{10FFFD}"'));Fix template string tokenization of escape sequencesSindre Sorhusbefore adoptionb67f199
lines 105–110why, one step back105106/*107`result.escapedCommand` is documented as safe to copy and paste in a terminal, so a shell must parse it back to the exact same arguments.108Control characters are excluded, since those are intentionally escaped with a Bash-specific notation instead.109*/110if (!isWindows) {TweaksSindre Sorhusbefore adoption698a1b8
line 111why, one step back111 const testShellRoundTrip = async commandArguments => {Fix template string tokenization of escape sequencesSindre Sorhusbefore adoptionb67f199
lines 112–113why, one step back112 const {escapedCommand} = await execa('print-arguments.js', commandArguments);113 const {stdout} = await execa('/bin/sh', ['-c', escapedCommand]);TweaksSindre Sorhusbefore adoption698a1b8
line 114why, one step back114 assert.deepEqual(JSON.parse(stdout), commandArguments);Fix template string tokenization of escape sequencesSindre Sorhusbefore adoptionb67f199
lines 115–116why, one step back115 };116TweaksSindre Sorhusbefore adoption698a1b8
lines 117–132why, one step back117 const shellRoundTripArguments = [118 ['foo', 'bar'],119 ['foo bar'],120 ['\'foo\'', '"bar"'],121 ['it\'s', 'a\'\'b'],122 ['foo\\bar', '\\', '\\\\'],123 // eslint-disable-next-line no-template-curly-in-string124 ['$FOO', '${FOO}', '$(echo foo)'],125 ['`echo foo`'],126 ['foo;bar', 'foo&&bar', 'foo|bar', 'foo>bar'],127 ['*', '?', '[a-z]', '{a,b}'],128 ['~', '#foo', '!foo', '-foo', '--foo=bar'],129 ['', 'foo'],130 ['ã', '😀'],131 ['\'"`$\\!*&;| '],132 ];Fix template string tokenization of escape sequencesSindre Sorhusbefore adoptionb67f199
line 133why, one step back133TweaksSindre Sorhusbefore adoption698a1b8
lines 134–136why, one step back134 for (const commandArguments of shellRoundTripArguments) {135 test(`result.escapedCommand can be pasted in a shell: ${JSON.stringify(commandArguments)}`, () => testShellRoundTrip(commandArguments));136 }Fix template string tokenization of escape sequencesSindre Sorhusbefore adoptionb67f199
line 137why, one step back137}Fix `escapedCommand` and `verbose` quoting arguments when using the `shell` optionSindre Sorhusbefore adoptionb2c3a54
lines 138–158why, one step back138139// With `shell`, the shell interprets the command, so `result.escapedCommand` must not quote it.140const testShellEscapedCommand = async (execaMethod, commandArguments, expected) => {141 const {escapedCommand} = await execaMethod('echo', commandArguments, {shell: true});142 assert.equal(escapedCommand, expected);143};144145test('result.escapedCommand does not quote with "shell"', () => testShellEscapedCommand(execa, ['foo', '|', 'sort'], 'echo foo | sort'));146test('result.escapedCommand does not quote with "shell", sync', () => testShellEscapedCommand(execaSync, ['foo', '|', 'sort'], 'echo foo | sort'));147test('result.escapedCommand escapes control characters with "shell"', () => testShellEscapedCommand(execa, ['\'foo\tbar\''], 'echo \'foo\\tbar\''));148149// The `node` option ignores `shell`150test('result.escapedCommand quotes with "shell" and "node"', async () => {151 const {escapedCommand} = await execaNode('noop.js', ['foo bar'], {shell: true, cwd: FIXTURES_DIRECTORY});152 assert.equal(escapedCommand, isWindows ? 'noop.js "foo bar"' : 'noop.js \'foo bar\'');153});154155test('result.escapedCommand does not quote with "shell" and template strings', async () => {156 const {escapedCommand} = await execa({shell: true})`echo 'foo' | sort`;157 assert.equal(escapedCommand, 'echo \'foo\' | sort');158});