Skip to content
Myr

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

7 files+415 added−403 removed

modified:lib/command.js-101 removed
added:lib/escape.js+21 added
added:lib/script.js+80 added
modified:test/command.js+1 added-301 removed
1import {inspect} from 'node:util';1import test from 'ava';3import {isStream} from 'is-stream';4import {execa, execaSync, execaCommand, execaCommandSync, $} from '../index.js';2import {execaCommand, execaCommandSync} from '../index.js';3import {setFixtureDir} from './helpers/fixtures-dir.js';45setFixtureDir();69const command = async (t, expected, ...args) => {10 const {command: failCommand} = await t.throwsAsync(execa('fail.js', args));11 t.is(failCommand, `fail.js${expected}`);1213 const {command} = await execa('noop.js', args);14 t.is(command, `noop.js${expected}`);15};1617command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;1819test(command, ' foo bar', 'foo', 'bar');20test(command, ' baz quz', 'baz', 'quz');21test(command, '');2223const testEscapedCommand = async (t, expected, args) => {24 const {escapedCommand: failEscapedCommand} = await t.throwsAsync(execa('fail.js', args));25 t.is(failEscapedCommand, `fail.js ${expected}`);2627 const {escapedCommand: failEscapedCommandSync} = t.throws(() => {28 execaSync('fail.js', args);29 });30 t.is(failEscapedCommandSync, `fail.js ${expected}`);3132 const {escapedCommand} = await execa('noop.js', args);33 t.is(escapedCommand, `noop.js ${expected}`);3435 const {escapedCommand: escapedCommandSync} = execaSync('noop.js', args);36 t.is(escapedCommandSync, `noop.js ${expected}`);37};3839testEscapedCommand.title = (message, expected) => `escapedCommand is: ${JSON.stringify(expected)}`;4041test(testEscapedCommand, 'foo bar', ['foo', 'bar']);42test(testEscapedCommand, '"foo bar"', ['foo bar']);43test(testEscapedCommand, '"\\"foo\\""', ['"foo"']);44test(testEscapedCommand, '"*"', ['*']);4546test('allow commands with spaces and no array arguments', async t => {47 const {stdout} = await execa('command with space.js');48 t.is(stdout, '');49});5051test('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});557test('execaCommand()', async t => {8 const {stdout} = await execaCommand('echo.js foo bar');9 t.is(stdout, 'foo\nbar');10});
26 unmodified lines
37test('execaCommandSync()', t => {38 const {stdout} = execaCommandSync('echo.js foo bar');39 t.is(stdout, 'foo\nbar');40});9091test('$', async t => {92 const {stdout} = await $`echo.js foo bar`;93 t.is(stdout, 'foo\nbar');94});9596test('$ accepts options', async t => {97 const {stdout} = await $({stripFinalNewline: true})`noop.js foo`;98 t.is(stdout, 'foo');99});100101test('$ allows string interpolation', async t => {102 const {stdout} = await $`echo.js foo ${'bar'}`;103 t.is(stdout, 'foo\nbar');104});105106test('$ allows number interpolation', async t => {107 const {stdout} = await $`echo.js 1 ${2}`;108 t.is(stdout, '1\n2');109});110111test('$ allows array interpolation', async t => {112 const {stdout} = await $`echo.js ${['foo', 'bar']}`;113 t.is(stdout, 'foo\nbar');114});115116test('$ allows empty array interpolation', async t => {117 const {stdout} = await $`echo.js foo ${[]} bar`;118 t.is(stdout, 'foo\nbar');119});120121test('$ 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});126127test('$ 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});132133test('$ 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});138139test('$ 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});144145test('$ ignores consecutive spaces', async t => {146 const {stdout} = await $`echo.js foo bar`;147 t.is(stdout, 'foo\nbar');148});149150test('$ allows escaping spaces with interpolation', async t => {151 const {stdout} = await $`echo.js ${'foo bar'}`;152 t.is(stdout, 'foo bar');153});154155test('$ disallows escaping spaces with backslashes', async t => {156 const {stdout} = await $`echo.js foo\\ bar`;157 t.is(stdout, 'foo\\\nbar');158});159160test('$ 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});164165test('$ passes newline escape sequence as one argument', async t => {166 const {stdout} = await $`echo.js \n`;167 t.is(stdout, '\n');168});169170test('$ passes newline escape sequence in interpolation as one argument', async t => {171 const {stdout} = await $`echo.js ${'\n'}`;172 t.is(stdout, '\n');173});174175test('$ handles invalid escape sequence', async t => {176 const {stdout} = await $`echo.js \u`;177 t.is(stdout, '\\u');178});179180test('$ can concatenate at the end of tokens', async t => {181 const {stdout} = await $`echo.js foo${'bar'}`;182 t.is(stdout, 'foobar');183});184185test('$ 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});189190test('$ 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});194195test('$ can concatenate at the start of tokens', async t => {196 const {stdout} = await $`echo.js ${'foo'}bar`;197 t.is(stdout, 'foobar');198});199200test('$ 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});204205test('$ 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});209210test('$ 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});214215test('$ can concatenate multiple tokens', async t => {216 const {stdout} = await $`echo.js ${'foo'}bar${'foo'}`;217 t.is(stdout, 'foobarfoo');218});219220test('$ 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});224225test('$ escapes other whitespaces', async t => {226 const {stdout} = await $`echo.js foo\tbar`;227 t.is(stdout, 'foo\tbar');228});229230test('$ trims', async t => {231 const {stdout} = await $` echo.js foo bar `;232 t.is(stdout, 'foo\nbar');233});234235test('$.sync', t => {236 const {stdout} = $.sync`echo.js foo bar`;237 t.is(stdout, 'foo\nbar');238});239240test('$.sync can be called $.s', t => {241 const {stdout} = $.s`echo.js foo bar`;242 t.is(stdout, 'foo\nbar');243});244245test('$.sync accepts options', t => {246 const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;247 t.is(stdout, 'foo');248});249250test('$.sync must be used after options binding, not before', t => {251 t.throws(() => $.sync({})`noop.js`, {message: /Please use/});252});253254test('$.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});259260test('$.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});265266test('$.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});271272test('$.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});277278const invalidExpression = test.macro({279 async exec(t, input, expected) {280 await t.throwsAsync(281 async () => $`echo.js ${input}`,282 {instanceOf: TypeError, message: expected},283 );284285 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});294295test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');296test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');297298test(invalidExpression, null, 'Unexpected "object" in template expression');299test(invalidExpression, [null], 'Unexpected "object" in template expression');300301test(invalidExpression, true, 'Unexpected "boolean" in template expression');302test(invalidExpression, [true], 'Unexpected "boolean" in template expression');303304test(invalidExpression, {}, 'Unexpected "object" in template expression');305test(invalidExpression, [{}], 'Unexpected "object" in template expression');306307test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');308test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');309310test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');311test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');312313test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');314test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');315316test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');317test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');318319test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');320test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');321322test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');323test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');324325test('$({stdio: \'ignore\'}).sync`noop.js`', invalidExpression, $({stdio: 'ignore'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');326test('[ $({stdio: \'ignore\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'ignore'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');327328test('$ stdin defaults to "inherit"', async t => {329 const {stdout} = await $({input: 'foo'})`stdin-script.js`;330 t.is(stdout, 'foo');331});332333test('$.sync stdin defaults to "inherit"', t => {334 const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;335 t.is(stdout, 'foo');336});337338test('$ 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
added:test/script.js+258 added
1import {inspect} from 'node:util';2import test from 'ava';3import {isStream} from 'is-stream';4import {$} from '../index.js';5import {setFixtureDir} from './helpers/fixtures-dir.js';67setFixtureDir();89test('$', async t => {10 const {stdout} = await $`echo.js foo bar`;11 t.is(stdout, 'foo\nbar');12});1314test('$ accepts options', async t => {15 const {stdout} = await $({stripFinalNewline: true})`noop.js foo`;16 t.is(stdout, 'foo');17});1819test('$ allows string interpolation', async t => {20 const {stdout} = await $`echo.js foo ${'bar'}`;21 t.is(stdout, 'foo\nbar');22});2324test('$ allows number interpolation', async t => {25 const {stdout} = await $`echo.js 1 ${2}`;26 t.is(stdout, '1\n2');27});2829test('$ allows array interpolation', async t => {30 const {stdout} = await $`echo.js ${['foo', 'bar']}`;31 t.is(stdout, 'foo\nbar');32});3334test('$ allows empty array interpolation', async t => {35 const {stdout} = await $`echo.js foo ${[]} bar`;36 t.is(stdout, 'foo\nbar');37});3839test('$ 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});4445test('$ 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});5051test('$ 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});5657test('$ 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});6263test('$ ignores consecutive spaces', async t => {64 const {stdout} = await $`echo.js foo bar`;65 t.is(stdout, 'foo\nbar');66});6768test('$ allows escaping spaces with interpolation', async t => {69 const {stdout} = await $`echo.js ${'foo bar'}`;70 t.is(stdout, 'foo bar');71});7273test('$ disallows escaping spaces with backslashes', async t => {74 const {stdout} = await $`echo.js foo\\ bar`;75 t.is(stdout, 'foo\\\nbar');76});7778test('$ 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});8283test('$ passes newline escape sequence as one argument', async t => {84 const {stdout} = await $`echo.js \n`;85 t.is(stdout, '\n');86});8788test('$ passes newline escape sequence in interpolation as one argument', async t => {89 const {stdout} = await $`echo.js ${'\n'}`;90 t.is(stdout, '\n');91});9293test('$ handles invalid escape sequence', async t => {94 const {stdout} = await $`echo.js \u`;95 t.is(stdout, '\\u');96});9798test('$ can concatenate at the end of tokens', async t => {99 const {stdout} = await $`echo.js foo${'bar'}`;100 t.is(stdout, 'foobar');101});102103test('$ 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});107108test('$ 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});112113test('$ can concatenate at the start of tokens', async t => {114 const {stdout} = await $`echo.js ${'foo'}bar`;115 t.is(stdout, 'foobar');116});117118test('$ 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});122123test('$ 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});127128test('$ 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});132133test('$ can concatenate multiple tokens', async t => {134 const {stdout} = await $`echo.js ${'foo'}bar${'foo'}`;135 t.is(stdout, 'foobarfoo');136});137138test('$ 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});142143test('$ escapes other whitespaces', async t => {144 const {stdout} = await $`echo.js foo\tbar`;145 t.is(stdout, 'foo\tbar');146});147148test('$ trims', async t => {149 const {stdout} = await $` echo.js foo bar `;150 t.is(stdout, 'foo\nbar');151});152153test('$.sync', t => {154 const {stdout} = $.sync`echo.js foo bar`;155 t.is(stdout, 'foo\nbar');156});157158test('$.sync can be called $.s', t => {159 const {stdout} = $.s`echo.js foo bar`;160 t.is(stdout, 'foo\nbar');161});162163test('$.sync accepts options', t => {164 const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;165 t.is(stdout, 'foo');166});167168test('$.sync must be used after options binding, not before', t => {169 t.throws(() => $.sync({})`noop.js`, {message: /Please use/});170});171172test('$.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});177178test('$.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});183184test('$.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});189190test('$.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});195196const invalidExpression = test.macro({197 async exec(t, input, expected) {198 await t.throwsAsync(199 async () => $`echo.js ${input}`,200 {instanceOf: TypeError, message: expected},201 );202203 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});212213test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');214test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');215216test(invalidExpression, null, 'Unexpected "object" in template expression');217test(invalidExpression, [null], 'Unexpected "object" in template expression');218219test(invalidExpression, true, 'Unexpected "boolean" in template expression');220test(invalidExpression, [true], 'Unexpected "boolean" in template expression');221222test(invalidExpression, {}, 'Unexpected "object" in template expression');223test(invalidExpression, [{}], 'Unexpected "object" in template expression');224225test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');226test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');227228test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');229test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');230231test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');232test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');233234test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');235test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');236237test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');238test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');239240test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');241test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');242243test('$({stdio: \'ignore\'}).sync`noop.js`', invalidExpression, $({stdio: 'ignore'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');244test('[ $({stdio: \'ignore\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'ignore'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');245246test('$ stdin defaults to "inherit"', async t => {247 const {stdout} = await $({input: 'foo'})`stdin-script.js`;248 t.is(stdout, 'foo');249});250251test('$.sync stdin defaults to "inherit"', t => {252 const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;253 t.is(stdout, 'foo');254});255256test('$ 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 lines
14import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay, cleanupOnExit} from './lib/kill.js';15import {pipeToProcess} from './lib/pipe.js';16import {getSpawnedResult, makeAllStream} from './lib/stream.js';17import {mergePromise} from './lib/promise.js';18import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';18import {joinCommand, getEscapedCommand} from './lib/escape.js';19import {parseCommand} from './lib/command.js';20import {parseTemplates} from './lib/script.js';21import {logCommand, verboseDefault} from './lib/verbose.js';22import {bufferToUint8Array} from './lib/stdio/utils.js';2324const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;
341 unmodified lines