Split files (#791)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 4328670 · Browse files · 7 files·+415 added−403 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+415 added−403 removed
- tests:+311 added−301 removed
- other:+104 added−102 removed
Provenance
- Commit
932fb26- Tree
dd147c0- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
4328670
7 files changed+415 added−403 removed
modified:lib/command.js-101 removed
12345678910111213141516171819202122232425123410 unmodified lines151617184445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119import {ChildProcess} from 'node:child_process';import {isBinary, binaryToString} from './stdio/utils.js';
const normalizeArgs = (file, args = []) => { if (!Array.isArray(args)) { return [file]; }
return [file, ...args];};
const NO_ESCAPE_REGEXP = /^[\w.-]+$/;
const escapeArg = arg => { if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) { return arg; }
return `"${arg.replaceAll('"', '\\"')}"`;};
export const joinCommand = (file, rawArgs) => normalizeArgs(file, rawArgs).join(' ');
export const getEscapedCommand = (file, rawArgs) => normalizeArgs(file, rawArgs).map(arg => escapeArg(arg)).join(' ');
const SPACES_REGEXP = / +/g;
// Handle `execaCommand()`export const parseCommand = command => {10 unmodified lines }
return tokens;};
const parseExpression = expression => { const typeOfExpression = typeof expression;
if (typeOfExpression === 'string') { return expression; }
if (typeOfExpression === 'number') { return String(expression); }
if ( typeOfExpression === 'object' && expression !== null && !(expression instanceof ChildProcess) && 'stdout' in expression ) { const typeOfStdout = typeof expression.stdout;
if (typeOfStdout === 'string') { return expression.stdout; }
if (isBinary(expression.stdout)) { return binaryToString(expression.stdout); }
throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`); }
throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);};
const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0 ? [...tokens, ...nextTokens] : [ ...tokens.slice(0, -1), `${tokens.at(-1)}${nextTokens[0]}`, ...nextTokens.slice(1), ];
const parseTemplate = ({templates, expressions, tokens, index, template}) => { const templateString = template ?? templates.raw[index]; const templateTokens = templateString.split(SPACES_REGEXP).filter(Boolean); const newTokens = concatTokens( tokens, templateTokens, templateString.startsWith(' '), );
if (index === expressions.length) { return newTokens; }
const expression = expressions[index]; const expressionTokens = Array.isArray(expression) ? expression.map(expression => parseExpression(expression)) : [parseExpression(expression)]; return concatTokens( newTokens, expressionTokens, templateString.endsWith(' '), );};
export const parseTemplates = (templates, expressions) => { let tokens = [];
for (const [index, template] of templates.entries()) { tokens = parseTemplate({templates, expressions, tokens, index, template}); }
return tokens;};
added:lib/escape.js+21 added
123456789101112131415161718192021const normalizeArgs = (file, args = []) => { if (!Array.isArray(args)) { return [file]; }
return [file, ...args];};
const NO_ESCAPE_REGEXP = /^[\w.-]+$/;
const escapeArg = arg => { if (typeof arg !== 'string' || NO_ESCAPE_REGEXP.test(arg)) { return arg; }
return `"${arg.replaceAll('"', '\\"')}"`;};
export const joinCommand = (file, rawArgs) => normalizeArgs(file, rawArgs).join(' ');
export const getEscapedCommand = (file, rawArgs) => normalizeArgs(file, rawArgs).map(arg => escapeArg(arg)).join(' ');added:lib/script.js+80 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import {ChildProcess} from 'node:child_process';import {isBinary, binaryToString} from './stdio/utils.js';
const parseExpression = expression => { const typeOfExpression = typeof expression;
if (typeOfExpression === 'string') { return expression; }
if (typeOfExpression === 'number') { return String(expression); }
if ( typeOfExpression === 'object' && expression !== null && !(expression instanceof ChildProcess) && 'stdout' in expression ) { const typeOfStdout = typeof expression.stdout;
if (typeOfStdout === 'string') { return expression.stdout; }
if (isBinary(expression.stdout)) { return binaryToString(expression.stdout); }
throw new TypeError(`Unexpected "${typeOfStdout}" stdout in template expression`); }
throw new TypeError(`Unexpected "${typeOfExpression}" in template expression`);};
const concatTokens = (tokens, nextTokens, isNew) => isNew || tokens.length === 0 || nextTokens.length === 0 ? [...tokens, ...nextTokens] : [ ...tokens.slice(0, -1), `${tokens.at(-1)}${nextTokens[0]}`, ...nextTokens.slice(1), ];
const SPACES_REGEXP = / +/g;
const parseTemplate = ({templates, expressions, tokens, index, template}) => { const templateString = template ?? templates.raw[index]; const templateTokens = templateString.split(SPACES_REGEXP).filter(Boolean); const newTokens = concatTokens( tokens, templateTokens, templateString.startsWith(' '), );
if (index === expressions.length) { return newTokens; }
const expression = expressions[index]; const expressionTokens = Array.isArray(expression) ? expression.map(expression => parseExpression(expression)) : [parseExpression(expression)]; return concatTokens( newTokens, expressionTokens, templateString.endsWith(' '), );};
export const parseTemplates = (templates, expressions) => { let tokens = [];
for (const [index, template] of templates.entries()) { tokens = parseTemplate({templates, expressions, tokens, index, template}); }
return tokens;};
modified:test/command.js+1 added-301 removed
1-import {inspect} from 'node:util';1 import test from 'ava';3-import {isStream} from 'is-stream';4-import {execa, execaSync, execaCommand, execaCommandSync, $} from '../index.js';2+import {execaCommand, execaCommandSync} from '../index.js';3 import {setFixtureDir} from './helpers/fixtures-dir.js';4 5 setFixtureDir();6 9-const command = async (t, expected, ...args) => {10- const {command: failCommand} = await t.throwsAsync(execa('fail.js', args));11- t.is(failCommand, `fail.js${expected}`);12-13- const {command} = await execa('noop.js', args);14- t.is(command, `noop.js${expected}`);15-};16-17-command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;18-19-test(command, ' foo bar', 'foo', 'bar');20-test(command, ' baz quz', 'baz', 'quz');21-test(command, '');22-23-const testEscapedCommand = async (t, expected, args) => {24- const {escapedCommand: failEscapedCommand} = await t.throwsAsync(execa('fail.js', args));25- t.is(failEscapedCommand, `fail.js ${expected}`);26-27- const {escapedCommand: failEscapedCommandSync} = t.throws(() => {28- execaSync('fail.js', args);29- });30- t.is(failEscapedCommandSync, `fail.js ${expected}`);31-32- const {escapedCommand} = await execa('noop.js', args);33- t.is(escapedCommand, `noop.js ${expected}`);34-35- const {escapedCommand: escapedCommandSync} = execaSync('noop.js', args);36- t.is(escapedCommandSync, `noop.js ${expected}`);37-};38-39-testEscapedCommand.title = (message, expected) => `escapedCommand is: ${JSON.stringify(expected)}`;40-41-test(testEscapedCommand, 'foo bar', ['foo', 'bar']);42-test(testEscapedCommand, '"foo bar"', ['foo bar']);43-test(testEscapedCommand, '"\\"foo\\""', ['"foo"']);44-test(testEscapedCommand, '"*"', ['*']);45-46-test('allow commands with spaces and no array arguments', async t => {47- const {stdout} = await execa('command with space.js');48- t.is(stdout, '');49-});50-51-test('allow commands with spaces and array arguments', async t => {52- const {stdout} = await execa('command with space.js', ['foo', 'bar']);53- t.is(stdout, 'foo\nbar');54-});55-7 test('execaCommand()', async t => {8 const {stdout} = await execaCommand('echo.js foo bar');9 t.is(stdout, 'foo\nbar');10 });26 unmodified lines37 test('execaCommandSync()', t => {38 const {stdout} = execaCommandSync('echo.js foo bar');39 t.is(stdout, 'foo\nbar');40 });90-91-test('$', async t => {92- const {stdout} = await $`echo.js foo bar`;93- t.is(stdout, 'foo\nbar');94-});95-96-test('$ accepts options', async t => {97- const {stdout} = await $({stripFinalNewline: true})`noop.js foo`;98- t.is(stdout, 'foo');99-});100-101-test('$ allows string interpolation', async t => {102- const {stdout} = await $`echo.js foo ${'bar'}`;103- t.is(stdout, 'foo\nbar');104-});105-106-test('$ allows number interpolation', async t => {107- const {stdout} = await $`echo.js 1 ${2}`;108- t.is(stdout, '1\n2');109-});110-111-test('$ allows array interpolation', async t => {112- const {stdout} = await $`echo.js ${['foo', 'bar']}`;113- t.is(stdout, 'foo\nbar');114-});115-116-test('$ allows empty array interpolation', async t => {117- const {stdout} = await $`echo.js foo ${[]} bar`;118- t.is(stdout, 'foo\nbar');119-});120-121-test('$ allows execa return value interpolation', async t => {122- const foo = await $`echo.js foo`;123- const {stdout} = await $`echo.js ${foo} bar`;124- t.is(stdout, 'foo\nbar');125-});126-127-test('$ allows execa return value array interpolation', async t => {128- const foo = await $`echo.js foo`;129- const {stdout} = await $`echo.js ${[foo, 'bar']}`;130- t.is(stdout, 'foo\nbar');131-});132-133-test('$ allows execa return value buffer interpolation', async t => {134- const foo = await $({encoding: 'buffer'})`echo.js foo`;135- const {stdout} = await $`echo.js ${foo} bar`;136- t.is(stdout, 'foo\nbar');137-});138-139-test('$ allows execa return value buffer array interpolation', async t => {140- const foo = await $({encoding: 'buffer'})`echo.js foo`;141- const {stdout} = await $`echo.js ${[foo, 'bar']}`;142- t.is(stdout, 'foo\nbar');143-});144-145-test('$ ignores consecutive spaces', async t => {146- const {stdout} = await $`echo.js foo bar`;147- t.is(stdout, 'foo\nbar');148-});149-150-test('$ allows escaping spaces with interpolation', async t => {151- const {stdout} = await $`echo.js ${'foo bar'}`;152- t.is(stdout, 'foo bar');153-});154-155-test('$ disallows escaping spaces with backslashes', async t => {156- const {stdout} = await $`echo.js foo\\ bar`;157- t.is(stdout, 'foo\\\nbar');158-});159-160-test('$ allows space escaped values in array interpolation', async t => {161- const {stdout} = await $`echo.js ${['foo', 'bar baz']}`;162- t.is(stdout, 'foo\nbar baz');163-});164-165-test('$ passes newline escape sequence as one argument', async t => {166- const {stdout} = await $`echo.js \n`;167- t.is(stdout, '\n');168-});169-170-test('$ passes newline escape sequence in interpolation as one argument', async t => {171- const {stdout} = await $`echo.js ${'\n'}`;172- t.is(stdout, '\n');173-});174-175-test('$ handles invalid escape sequence', async t => {176- const {stdout} = await $`echo.js \u`;177- t.is(stdout, '\\u');178-});179-180-test('$ can concatenate at the end of tokens', async t => {181- const {stdout} = await $`echo.js foo${'bar'}`;182- t.is(stdout, 'foobar');183-});184-185-test('$ does not concatenate at the end of tokens with a space', async t => {186- const {stdout} = await $`echo.js foo ${'bar'}`;187- t.is(stdout, 'foo\nbar');188-});189-190-test('$ can concatenate at the end of tokens followed by an array', async t => {191- const {stdout} = await $`echo.js foo${['bar', 'foo']}`;192- t.is(stdout, 'foobar\nfoo');193-});194-195-test('$ can concatenate at the start of tokens', async t => {196- const {stdout} = await $`echo.js ${'foo'}bar`;197- t.is(stdout, 'foobar');198-});199-200-test('$ does not concatenate at the start of tokens with a space', async t => {201- const {stdout} = await $`echo.js ${'foo'} bar`;202- t.is(stdout, 'foo\nbar');203-});204-205-test('$ can concatenate at the start of tokens followed by an array', async t => {206- const {stdout} = await $`echo.js ${['foo', 'bar']}foo`;207- t.is(stdout, 'foo\nbarfoo');208-});209-210-test('$ can concatenate at the start and end of tokens followed by an array', async t => {211- const {stdout} = await $`echo.js foo${['bar', 'foo']}bar`;212- t.is(stdout, 'foobar\nfoobar');213-});214-215-test('$ can concatenate multiple tokens', async t => {216- const {stdout} = await $`echo.js ${'foo'}bar${'foo'}`;217- t.is(stdout, 'foobarfoo');218-});219-220-test('$ allows escaping spaces in commands with interpolation', async t => {221- const {stdout} = await $`${'command with space.js'} foo bar`;222- t.is(stdout, 'foo\nbar');223-});224-225-test('$ escapes other whitespaces', async t => {226- const {stdout} = await $`echo.js foo\tbar`;227- t.is(stdout, 'foo\tbar');228-});229-230-test('$ trims', async t => {231- const {stdout} = await $` echo.js foo bar `;232- t.is(stdout, 'foo\nbar');233-});234-235-test('$.sync', t => {236- const {stdout} = $.sync`echo.js foo bar`;237- t.is(stdout, 'foo\nbar');238-});239-240-test('$.sync can be called $.s', t => {241- const {stdout} = $.s`echo.js foo bar`;242- t.is(stdout, 'foo\nbar');243-});244-245-test('$.sync accepts options', t => {246- const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;247- t.is(stdout, 'foo');248-});249-250-test('$.sync must be used after options binding, not before', t => {251- t.throws(() => $.sync({})`noop.js`, {message: /Please use/});252-});253-254-test('$.sync allows execa return value interpolation', t => {255- const foo = $.sync`echo.js foo`;256- const {stdout} = $.sync`echo.js ${foo} bar`;257- t.is(stdout, 'foo\nbar');258-});259-260-test('$.sync allows execa return value array interpolation', t => {261- const foo = $.sync`echo.js foo`;262- const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;263- t.is(stdout, 'foo\nbar');264-});265-266-test('$.sync allows execa return value buffer interpolation', t => {267- const foo = $({encoding: 'buffer'}).sync`echo.js foo`;268- const {stdout} = $.sync`echo.js ${foo} bar`;269- t.is(stdout, 'foo\nbar');270-});271-272-test('$.sync allows execa return value buffer array interpolation', t => {273- const foo = $({encoding: 'buffer'}).sync`echo.js foo`;274- const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;275- t.is(stdout, 'foo\nbar');276-});277-278-const invalidExpression = test.macro({279- async exec(t, input, expected) {280- await t.throwsAsync(281- async () => $`echo.js ${input}`,282- {instanceOf: TypeError, message: expected},283- );284-285- t.throws(286- () => $.sync`echo.js ${input}`,287- {instanceOf: TypeError, message: expected},288- );289- },290- title(prettyInput, input, expected) {291- return `$ APIs throw on invalid '${prettyInput ?? inspect(input)}' expression with '${expected}'`;292- },293-});294-295-test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');296-test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');297-298-test(invalidExpression, null, 'Unexpected "object" in template expression');299-test(invalidExpression, [null], 'Unexpected "object" in template expression');300-301-test(invalidExpression, true, 'Unexpected "boolean" in template expression');302-test(invalidExpression, [true], 'Unexpected "boolean" in template expression');303-304-test(invalidExpression, {}, 'Unexpected "object" in template expression');305-test(invalidExpression, [{}], 'Unexpected "object" in template expression');306-307-test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');308-test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');309-310-test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');311-test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');312-313-test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');314-test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');315-316-test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');317-test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');318-319-test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');320-test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');321-322-test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');323-test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');324-325-test('$({stdio: \'ignore\'}).sync`noop.js`', invalidExpression, $({stdio: 'ignore'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');326-test('[ $({stdio: \'ignore\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'ignore'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');327-328-test('$ stdin defaults to "inherit"', async t => {329- const {stdout} = await $({input: 'foo'})`stdin-script.js`;330- t.is(stdout, 'foo');331-});332-333-test('$.sync stdin defaults to "inherit"', t => {334- const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;335- t.is(stdout, 'foo');336-});337-338-test('$ stdin has no default value when stdio is set', t => {339- t.true(isStream($({stdio: 'pipe'})`noop.js`.stdin));340-});added:test/escape.js+52 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import test from 'ava';import {execa, execaSync} from '../index.js';import {setFixtureDir} from './helpers/fixtures-dir.js';
setFixtureDir();
const testResultCommand = 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}`);};
testResultCommand.title = (message, expected) => `result.command is: ${JSON.stringify(expected)}`;
test(testResultCommand, ' foo bar', 'foo', 'bar');test(testResultCommand, ' baz quz', 'baz', 'quz');test(testResultCommand, '');
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) => `result.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');});added:test/script.js+258 added
1+import {inspect} from 'node:util';2+import test from 'ava';3+import {isStream} from 'is-stream';4+import {$} from '../index.js';5+import {setFixtureDir} from './helpers/fixtures-dir.js';6+7+setFixtureDir();8+9+test('$', async t => {10+ const {stdout} = await $`echo.js foo bar`;11+ t.is(stdout, 'foo\nbar');12+});13+14+test('$ accepts options', async t => {15+ const {stdout} = await $({stripFinalNewline: true})`noop.js foo`;16+ t.is(stdout, 'foo');17+});18+19+test('$ allows string interpolation', async t => {20+ const {stdout} = await $`echo.js foo ${'bar'}`;21+ t.is(stdout, 'foo\nbar');22+});23+24+test('$ allows number interpolation', async t => {25+ const {stdout} = await $`echo.js 1 ${2}`;26+ t.is(stdout, '1\n2');27+});28+29+test('$ allows array interpolation', async t => {30+ const {stdout} = await $`echo.js ${['foo', 'bar']}`;31+ t.is(stdout, 'foo\nbar');32+});33+34+test('$ allows empty array interpolation', async t => {35+ const {stdout} = await $`echo.js foo ${[]} bar`;36+ t.is(stdout, 'foo\nbar');37+});38+39+test('$ allows execa return value interpolation', async t => {40+ const foo = await $`echo.js foo`;41+ const {stdout} = await $`echo.js ${foo} bar`;42+ t.is(stdout, 'foo\nbar');43+});44+45+test('$ allows execa return value array interpolation', async t => {46+ const foo = await $`echo.js foo`;47+ const {stdout} = await $`echo.js ${[foo, 'bar']}`;48+ t.is(stdout, 'foo\nbar');49+});50+51+test('$ allows execa return value buffer interpolation', async t => {52+ const foo = await $({encoding: 'buffer'})`echo.js foo`;53+ const {stdout} = await $`echo.js ${foo} bar`;54+ t.is(stdout, 'foo\nbar');55+});56+57+test('$ allows execa return value buffer array interpolation', async t => {58+ const foo = await $({encoding: 'buffer'})`echo.js foo`;59+ const {stdout} = await $`echo.js ${[foo, 'bar']}`;60+ t.is(stdout, 'foo\nbar');61+});62+63+test('$ ignores consecutive spaces', async t => {64+ const {stdout} = await $`echo.js foo bar`;65+ t.is(stdout, 'foo\nbar');66+});67+68+test('$ allows escaping spaces with interpolation', async t => {69+ const {stdout} = await $`echo.js ${'foo bar'}`;70+ t.is(stdout, 'foo bar');71+});72+73+test('$ disallows escaping spaces with backslashes', async t => {74+ const {stdout} = await $`echo.js foo\\ bar`;75+ t.is(stdout, 'foo\\\nbar');76+});77+78+test('$ allows space escaped values in array interpolation', async t => {79+ const {stdout} = await $`echo.js ${['foo', 'bar baz']}`;80+ t.is(stdout, 'foo\nbar baz');81+});82+83+test('$ passes newline escape sequence as one argument', async t => {84+ const {stdout} = await $`echo.js \n`;85+ t.is(stdout, '\n');86+});87+88+test('$ passes newline escape sequence in interpolation as one argument', async t => {89+ const {stdout} = await $`echo.js ${'\n'}`;90+ t.is(stdout, '\n');91+});92+93+test('$ handles invalid escape sequence', async t => {94+ const {stdout} = await $`echo.js \u`;95+ t.is(stdout, '\\u');96+});97+98+test('$ can concatenate at the end of tokens', async t => {99+ const {stdout} = await $`echo.js foo${'bar'}`;100+ t.is(stdout, 'foobar');101+});102+103+test('$ does not concatenate at the end of tokens with a space', async t => {104+ const {stdout} = await $`echo.js foo ${'bar'}`;105+ t.is(stdout, 'foo\nbar');106+});107+108+test('$ can concatenate at the end of tokens followed by an array', async t => {109+ const {stdout} = await $`echo.js foo${['bar', 'foo']}`;110+ t.is(stdout, 'foobar\nfoo');111+});112+113+test('$ can concatenate at the start of tokens', async t => {114+ const {stdout} = await $`echo.js ${'foo'}bar`;115+ t.is(stdout, 'foobar');116+});117+118+test('$ does not concatenate at the start of tokens with a space', async t => {119+ const {stdout} = await $`echo.js ${'foo'} bar`;120+ t.is(stdout, 'foo\nbar');121+});122+123+test('$ can concatenate at the start of tokens followed by an array', async t => {124+ const {stdout} = await $`echo.js ${['foo', 'bar']}foo`;125+ t.is(stdout, 'foo\nbarfoo');126+});127+128+test('$ can concatenate at the start and end of tokens followed by an array', async t => {129+ const {stdout} = await $`echo.js foo${['bar', 'foo']}bar`;130+ t.is(stdout, 'foobar\nfoobar');131+});132+133+test('$ can concatenate multiple tokens', async t => {134+ const {stdout} = await $`echo.js ${'foo'}bar${'foo'}`;135+ t.is(stdout, 'foobarfoo');136+});137+138+test('$ allows escaping spaces in commands with interpolation', async t => {139+ const {stdout} = await $`${'command with space.js'} foo bar`;140+ t.is(stdout, 'foo\nbar');141+});142+143+test('$ escapes other whitespaces', async t => {144+ const {stdout} = await $`echo.js foo\tbar`;145+ t.is(stdout, 'foo\tbar');146+});147+148+test('$ trims', async t => {149+ const {stdout} = await $` echo.js foo bar `;150+ t.is(stdout, 'foo\nbar');151+});152+153+test('$.sync', t => {154+ const {stdout} = $.sync`echo.js foo bar`;155+ t.is(stdout, 'foo\nbar');156+});157+158+test('$.sync can be called $.s', t => {159+ const {stdout} = $.s`echo.js foo bar`;160+ t.is(stdout, 'foo\nbar');161+});162+163+test('$.sync accepts options', t => {164+ const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;165+ t.is(stdout, 'foo');166+});167+168+test('$.sync must be used after options binding, not before', t => {169+ t.throws(() => $.sync({})`noop.js`, {message: /Please use/});170+});171+172+test('$.sync allows execa return value interpolation', t => {173+ const foo = $.sync`echo.js foo`;174+ const {stdout} = $.sync`echo.js ${foo} bar`;175+ t.is(stdout, 'foo\nbar');176+});177+178+test('$.sync allows execa return value array interpolation', t => {179+ const foo = $.sync`echo.js foo`;180+ const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;181+ t.is(stdout, 'foo\nbar');182+});183+184+test('$.sync allows execa return value buffer interpolation', t => {185+ const foo = $({encoding: 'buffer'}).sync`echo.js foo`;186+ const {stdout} = $.sync`echo.js ${foo} bar`;187+ t.is(stdout, 'foo\nbar');188+});189+190+test('$.sync allows execa return value buffer array interpolation', t => {191+ const foo = $({encoding: 'buffer'}).sync`echo.js foo`;192+ const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;193+ t.is(stdout, 'foo\nbar');194+});195+196+const invalidExpression = test.macro({197+ async exec(t, input, expected) {198+ await t.throwsAsync(199+ async () => $`echo.js ${input}`,200+ {instanceOf: TypeError, message: expected},201+ );202+203+ t.throws(204+ () => $.sync`echo.js ${input}`,205+ {instanceOf: TypeError, message: expected},206+ );207+ },208+ title(prettyInput, input, expected) {209+ return `$ APIs throw on invalid '${prettyInput ?? inspect(input)}' expression with '${expected}'`;210+ },211+});212+213+test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');214+test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');215+216+test(invalidExpression, null, 'Unexpected "object" in template expression');217+test(invalidExpression, [null], 'Unexpected "object" in template expression');218+219+test(invalidExpression, true, 'Unexpected "boolean" in template expression');220+test(invalidExpression, [true], 'Unexpected "boolean" in template expression');221+222+test(invalidExpression, {}, 'Unexpected "object" in template expression');223+test(invalidExpression, [{}], 'Unexpected "object" in template expression');224+225+test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');226+test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');227+228+test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');229+test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');230+231+test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');232+test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');233+234+test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');235+test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');236+237+test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');238+test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');239+240+test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');241+test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');242+243+test('$({stdio: \'ignore\'}).sync`noop.js`', invalidExpression, $({stdio: 'ignore'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');244+test('[ $({stdio: \'ignore\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'ignore'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');245+246+test('$ stdin defaults to "inherit"', async t => {247+ const {stdout} = await $({input: 'foo'})`stdin-script.js`;248+ t.is(stdout, 'foo');249+});250+251+test('$.sync stdin defaults to "inherit"', t => {252+ const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;253+ t.is(stdout, 'foo');254+});255+256+test('$ stdin has no default value when stdio is set', t => {257+ t.true(isStream($({stdio: 'pipe'})`noop.js`.stdin));258+});modified:index.js+3 added-1 removed
13 unmodified lines14 import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay, cleanupOnExit} from './lib/kill.js';15 import {pipeToProcess} from './lib/pipe.js';16 import {getSpawnedResult, makeAllStream} from './lib/stream.js';17 import {mergePromise} from './lib/promise.js';18-import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';18+import {joinCommand, getEscapedCommand} from './lib/escape.js';19+import {parseCommand} from './lib/command.js';20+import {parseTemplates} from './lib/script.js';21 import {logCommand, verboseDefault} from './lib/verbose.js';22 import {bufferToUint8Array} from './lib/stdio/utils.js';23 24 const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;341 unmodified lines