command.js
This file
Path
test/command.js
89 lines · 3.0 KiB · JavaScript
Last changed by
Meta tweaksSindre Sorhusbefore adoption5bac052
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990import path from 'node:path';import process from 'node:process';import {fileURLToPath} from 'node:url';import test from 'ava';import {execa, execaSync, execaCommand, execaCommandSync} from '../index.js';
process.env.PATH = fileURLToPath(new URL('fixtures', import.meta.url)) + path.delimiter + process.env.PATH;
const command = async (t, expected, ...args) => { const {command: failCommand} = await t.throwsAsync(execa('fail.js', args)); t.is(failCommand, `fail.js${expected}`);
const {command} = await execa('noop.js', args); t.is(command, `noop.js${expected}`);};
command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;
test(command, ' foo bar', 'foo', 'bar');test(command, ' baz quz', 'baz', 'quz');test(command, '');
const testEscapedCommand = async (t, expected, args) => { const {escapedCommand: failEscapedCommand} = await t.throwsAsync(execa('fail.js', args)); t.is(failEscapedCommand, `fail.js ${expected}`);
const {escapedCommand: failEscapedCommandSync} = t.throws(() => { execaSync('fail.js', args); }); t.is(failEscapedCommandSync, `fail.js ${expected}`);
const {escapedCommand} = await execa('noop.js', args); t.is(escapedCommand, `noop.js ${expected}`);
const {escapedCommand: escapedCommandSync} = execaSync('noop.js', args); t.is(escapedCommandSync, `noop.js ${expected}`);};
testEscapedCommand.title = (message, expected) => `escapedCommand is: ${JSON.stringify(expected)}`;
test(testEscapedCommand, 'foo bar', ['foo', 'bar']);test(testEscapedCommand, '"foo bar"', ['foo bar']);test(testEscapedCommand, '"\\"foo\\""', ['"foo"']);test(testEscapedCommand, '"*"', ['*']);
test('allow commands with spaces and no array arguments', async t => { const {stdout} = await execa('command with space.js'); t.is(stdout, '');});
test('allow commands with spaces and array arguments', async t => { const {stdout} = await execa('command with space.js', ['foo', 'bar']); t.is(stdout, 'foo\nbar');});
test('execaCommand()', async t => { const {stdout} = await execaCommand('node test/fixtures/echo.js foo bar'); t.is(stdout, 'foo\nbar');});
test('execaCommand() ignores consecutive spaces', async t => { const {stdout} = await execaCommand('node test/fixtures/echo.js foo bar'); t.is(stdout, 'foo\nbar');});
test('execaCommand() allows escaping spaces in commands', async t => { const {stdout} = await execaCommand('command\\ with\\ space.js foo bar'); t.is(stdout, 'foo\nbar');});
test('execaCommand() allows escaping spaces in arguments', async t => { const {stdout} = await execaCommand('node test/fixtures/echo.js foo\\ bar'); t.is(stdout, 'foo bar');});
test('execaCommand() escapes other whitespaces', async t => { const {stdout} = await execaCommand('node test/fixtures/echo.js foo\tbar'); t.is(stdout, 'foo\tbar');});
test('execaCommand() trims', async t => { const {stdout} = await execaCommand(' node test/fixtures/echo.js foo bar '); t.is(stdout, 'foo\nbar');});
test('execaCommandSync()', t => { const {stdout} = execaCommandSync('node test/fixtures/echo.js foo bar'); t.is(stdout, 'foo\nbar');});