command.js
This file
Path
test/methods/command.js
109 lines · 4.3 KiB · JavaScript
Last changed by
Improve tests for `execaCommand()` (#1020)ehmickybefore adoption97c0a0b
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–3why, one step back1import {join} from 'node:path';2import test from 'ava';3import {execaCommand, execaCommandSync} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
line 4why, one step back4import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 5–6why, one step back5import {QUOTE} from '../helpers/verbose.js';6Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 7–8why, one step back7setFixtureDirectory();8const STDIN_FIXTURE = join(FIXTURES_DIRECTORY, 'stdin.js');Rename files (#977)ehmickybefore adoption4b2e316
lines 9–78why, one step back910test('execaCommand()', async t => {11 const {stdout} = await execaCommand('echo.js foo bar');12 t.is(stdout, 'foo\nbar');13});1415test('execaCommandSync()', t => {16 const {stdout} = execaCommandSync('echo.js foo bar');17 t.is(stdout, 'foo\nbar');18});1920test('execaCommand`...`', async t => {21 const {stdout} = await execaCommand`${'echo.js foo bar'}`;22 t.is(stdout, 'foo\nbar');23});2425test('execaCommandSync`...`', t => {26 const {stdout} = execaCommandSync`${'echo.js foo bar'}`;27 t.is(stdout, 'foo\nbar');28});2930test('execaCommand(options)`...`', async t => {31 const {stdout} = await execaCommand({stripFinalNewline: false})`${'echo.js foo bar'}`;32 t.is(stdout, 'foo\nbar\n');33});3435test('execaCommandSync(options)`...`', t => {36 const {stdout} = execaCommandSync({stripFinalNewline: false})`${'echo.js foo bar'}`;37 t.is(stdout, 'foo\nbar\n');38});3940test('execaCommand(options)()', async t => {41 const {stdout} = await execaCommand({stripFinalNewline: false})('echo.js foo bar');42 t.is(stdout, 'foo\nbar\n');43});4445test('execaCommandSync(options)()', t => {46 const {stdout} = execaCommandSync({stripFinalNewline: false})('echo.js foo bar');47 t.is(stdout, 'foo\nbar\n');48});4950test('execaCommand().pipe(execaCommand())', async t => {51 const {stdout} = await execaCommand('echo.js foo bar').pipe(execaCommand(`node ${STDIN_FIXTURE}`));52 t.is(stdout, 'foo\nbar');53});5455test('execaCommand().pipe(...) does not use execaCommand', async t => {56 const {escapedCommand} = await execaCommand('echo.js foo bar').pipe(`node ${STDIN_FIXTURE}`, {reject: false});57 t.true(escapedCommand.startsWith(`${QUOTE}node `));58});5960test('execaCommand() bound options have lower priority', async t => {61 const {stdout} = await execaCommand({stripFinalNewline: false})('echo.js foo bar', {stripFinalNewline: true});62 t.is(stdout, 'foo\nbar');63});6465test('execaCommandSync() bound options have lower priority', t => {66 const {stdout} = execaCommandSync({stripFinalNewline: false})('echo.js foo bar', {stripFinalNewline: true});67 t.is(stdout, 'foo\nbar');68});6970test('execaCommand() allows escaping spaces in commands', async t => {71 const {stdout} = await execaCommand('command\\ with\\ space.js foo bar');72 t.is(stdout, 'foo\nbar');73});7475test('execaCommand() trims', async t => {76 const {stdout} = await execaCommand(' echo.js foo bar ');77 t.is(stdout, 'foo\nbar');78});Improve tests for `execaCommand()` (#1020)ehmickybefore adoption97c0a0b
lines 79–90why, one step back7980const testExecaCommandOutput = async (t, commandArguments, expectedOutput) => {81 const {stdout} = await execaCommand(`echo.js ${commandArguments}`);82 t.is(stdout, expectedOutput);83};8485test('execaCommand() ignores consecutive spaces', testExecaCommandOutput, 'foo bar', 'foo\nbar');86test('execaCommand() escapes other whitespaces', testExecaCommandOutput, 'foo\tbar', 'foo\tbar');87test('execaCommand() allows escaping spaces', testExecaCommandOutput, 'foo\\ bar', 'foo bar');88test('execaCommand() allows escaping backslashes before spaces', testExecaCommandOutput, 'foo\\\\ bar', 'foo\\ bar');89test('execaCommand() allows escaping multiple backslashes before spaces', testExecaCommandOutput, 'foo\\\\\\\\ bar', 'foo\\\\\\ bar');90test('execaCommand() allows escaping backslashes not before spaces', testExecaCommandOutput, 'foo\\bar baz', 'foo\\bar\nbaz');Rename files (#977)ehmickybefore adoption4b2e316
line 91why, one step back91Upgrade xo (#980)ehmickybefore adoptionccfa412
line 92why, one step back92const testInvalidArgumentsArray = (t, execaMethod) => {Rename files (#977)ehmickybefore adoption4b2e316
lines 93–97why, one step back93 t.throws(() => {94 execaMethod('echo', ['foo']);95 }, {message: /The command and its arguments must be passed as a single string/});96};97Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 98–99why, one step back98test('execaCommand() must not pass an array of arguments', testInvalidArgumentsArray, execaCommand);99test('execaCommandSync() must not pass an array of arguments', testInvalidArgumentsArray, execaCommandSync);Rename files (#977)ehmickybefore adoption4b2e316
line 100why, one step back100Upgrade xo (#980)ehmickybefore adoptionccfa412
line 101why, one step back101const testInvalidArgumentsTemplate = (t, execaMethod) => {Rename files (#977)ehmickybefore adoption4b2e316
lines 102–107why, one step back102 t.throws(() => {103 // eslint-disable-next-line no-unused-expressions104 execaMethod`echo foo`;105 }, {message: /The command and its arguments must be passed as a single string/});106};107Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 108–109why, one step back108test('execaCommand() must not pass an array of arguments with a template string', testInvalidArgumentsTemplate, execaCommand);109test('execaCommandSync() must not pass an array of arguments with a template string', testInvalidArgumentsTemplate, execaCommandSync);