Skip to content
Myr

Fix `escapedCommand` and `verbose` quoting arguments when using the `shell` option

Before adoption: this commit came in with the history the project adopted, so no landing records it.

With `shell`, the shell receives the file and arguments joined with spaces, so quoting each argument showed a different command than the one that ran. Fixes #1264

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 2141147 · Browse files · 7 files+42 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+42 added−8 removed

  • tests:+28 added−2 removed
  • other:+14 added−6 removed

7 files+42 added−8 removed

modified:docs/api.md+2 added
630 unmodified lines
631_Type:_ `string`632633Same as [`command`](#resultcommand) but escaped.634635With the [`shell`](#optionsshell) option, the command is not quoted, since the shell interprets it.636637[More info.](debugging.md#command)638639### result.cwd640
731 unmodified lines
modified:docs/debugging.md+1 added-1 removed
8 unmodified lines
9## Command1011[`error.command`](api.md#resultcommand) contains the file and [arguments](input.md#command-arguments) that were run. It is intended for logging or debugging.1213[`error.escapedCommand`](api.md#resultescapedcommand) is the same, except control characters are escaped. This makes it safe to either print or copy and paste in a terminal, for debugging purposes.13[`error.escapedCommand`](api.md#resultescapedcommand) is the same, except control characters are escaped. This makes it safe to either print or copy and paste in a terminal, for debugging purposes. With the [`shell`](api.md#optionsshell) option, the command is not quoted, since the shell interprets it.1415Since the escaping is fairly basic, neither `error.command` nor `error.escapedCommand` should be executed directly, including using [`execa()`](api.md#execafile-arguments-options) or [`parseCommandString()`](api.md#parsecommandstringcommand).1617```js
187 unmodified lines
modified:lib/arguments/command.js+2 added-1 removed
modified:lib/arguments/escape.js+7 added-4 removed
modified:test/arguments/escape.js+23 added-2 removed
1import assert from 'node:assert/strict';2import {platform} from 'node:process';3import test from 'node:test';4import {assertThrows, assertRejects, assertLike} from '../helpers/assert.js';5import {execa, execaSync} from '../../index.js';6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';5import {execa, execaSync, execaNode} from '../../index.js';6import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';78setFixtureDirectory();910const isWindows = platform === 'win32';
123 unmodified lines
134 for (const commandArguments of shellRoundTripArguments) {135 test(`result.escapedCommand can be pasted in a shell: ${JSON.stringify(commandArguments)}`, () => testShellRoundTrip(commandArguments));136 }137}138139// 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});
modified:test/verbose/start.js+5 added
145 unmodified lines
146 const {stderr} = await nestedSubprocess('noop.js', ['foo bar'], {verbose: 'short'});147 assert.equal(getCommandLine(stderr), `${testTimestamp} [0] $ noop.js ${QUOTE}foo bar${QUOTE}`);148});149150test('Does not quote command with "shell"', async () => {151 const {stderr} = await nestedSubprocess('echo', ['foo', '|', 'sort'], {verbose: 'short', shell: true});152 assert.equal(getCommandLine(stderr), `${testTimestamp} [0] $ echo foo | sort`);153});154155test('Quotes special punctuation from command', async () => {156 const {stderr} = await nestedSubprocess('noop.js', ['%'], {verbose: 'short'});157 assert.equal(getCommandLine(stderr), `${testTimestamp} [0] $ noop.js ${QUOTE}%${QUOTE}`);158});
15 unmodified lines
modified:types/return/result.d.ts+2 added
70 unmodified lines
71 command: string;7273 /**74 Same as `command` but escaped.7576 With the `shell` option, the command is not quoted, since the shell interprets it.77 */78 escapedCommand: string;7980 /**
127 unmodified lines