command.js
This file
Path
test/command.js
87 lines · 2.9 KiB · JavaScript
Last changed by
Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788import test from 'ava';import {execa, execaSync, execaCommand, execaCommandSync} from '../index.js';import {setFixtureDir} from './helpers/fixtures-dir.js';
setFixtureDir();
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');});