command.js
This file
Path
test/methods/command.js
112 lines · 3.9 KiB · JavaScript
Last changed by
Rename files (#977)ehmickybefore adoption4b2e316
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–112why, one step back1import {join} from 'node:path';2import test from 'ava';3import {execaCommand, execaCommandSync} from '../../index.js';4import {setFixtureDir, FIXTURES_DIR} from '../helpers/fixtures-dir.js';5import {QUOTE} from '../helpers/verbose.js';67setFixtureDir();8const STDIN_FIXTURE = join(FIXTURES_DIR, 'stdin.js');910test('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() ignores consecutive spaces', async t => {71 const {stdout} = await execaCommand('echo.js foo bar');72 t.is(stdout, 'foo\nbar');73});7475test('execaCommand() allows escaping spaces in commands', async t => {76 const {stdout} = await execaCommand('command\\ with\\ space.js foo bar');77 t.is(stdout, 'foo\nbar');78});7980test('execaCommand() allows escaping spaces in arguments', async t => {81 const {stdout} = await execaCommand('echo.js foo\\ bar');82 t.is(stdout, 'foo bar');83});8485test('execaCommand() escapes other whitespaces', async t => {86 const {stdout} = await execaCommand('echo.js foo\tbar');87 t.is(stdout, 'foo\tbar');88});8990test('execaCommand() trims', async t => {91 const {stdout} = await execaCommand(' echo.js foo bar ');92 t.is(stdout, 'foo\nbar');93});9495const testInvalidArgsArray = (t, execaMethod) => {96 t.throws(() => {97 execaMethod('echo', ['foo']);98 }, {message: /The command and its arguments must be passed as a single string/});99};100101test('execaCommand() must not pass an array of arguments', testInvalidArgsArray, execaCommand);102test('execaCommandSync() must not pass an array of arguments', testInvalidArgsArray, execaCommandSync);103104const testInvalidArgsTemplate = (t, execaMethod) => {105 t.throws(() => {106 // eslint-disable-next-line no-unused-expressions107 execaMethod`echo foo`;108 }, {message: /The command and its arguments must be passed as a single string/});109};110111test('execaCommand() must not pass an array of arguments with a template string', testInvalidArgsTemplate, execaCommand);112test('execaCommandSync() must not pass an array of arguments with a template string', testInvalidArgsTemplate, execaCommandSync);