script.js
This file
Path
test/script.js
367 lines · 12.1 KiB · JavaScript
Last changed by
Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
History of this fileTake it
`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
line 1why, one step back1import {spawn} from 'node:child_process';Split files (#791)ehmickybefore adoption932fb26
lines 2–6why, one step back2import {inspect} from 'node:util';3import test from 'ava';4import {isStream} from 'is-stream';5import {$} from '../index.js';6import {setFixtureDir} from './helpers/fixtures-dir.js';`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
line 7why, one step back7import {foobarString} from './helpers/input.js';Split files (#791)ehmickybefore adoption932fb26
lines 8–154why, one step back89setFixtureDir();1011test('$', async t => {12 const {stdout} = await $`echo.js foo bar`;13 t.is(stdout, 'foo\nbar');14});1516test('$ accepts options', async t => {17 const {stdout} = await $({stripFinalNewline: true})`noop.js foo`;18 t.is(stdout, 'foo');19});2021test('$ allows string interpolation', async t => {22 const {stdout} = await $`echo.js foo ${'bar'}`;23 t.is(stdout, 'foo\nbar');24});2526test('$ allows number interpolation', async t => {27 const {stdout} = await $`echo.js 1 ${2}`;28 t.is(stdout, '1\n2');29});3031test('$ allows array interpolation', async t => {32 const {stdout} = await $`echo.js ${['foo', 'bar']}`;33 t.is(stdout, 'foo\nbar');34});3536test('$ allows empty array interpolation', async t => {37 const {stdout} = await $`echo.js foo ${[]} bar`;38 t.is(stdout, 'foo\nbar');39});4041test('$ allows execa return value interpolation', async t => {42 const foo = await $`echo.js foo`;43 const {stdout} = await $`echo.js ${foo} bar`;44 t.is(stdout, 'foo\nbar');45});4647test('$ allows execa return value array interpolation', async t => {48 const foo = await $`echo.js foo`;49 const {stdout} = await $`echo.js ${[foo, 'bar']}`;50 t.is(stdout, 'foo\nbar');51});5253test('$ allows execa return value buffer interpolation', async t => {54 const foo = await $({encoding: 'buffer'})`echo.js foo`;55 const {stdout} = await $`echo.js ${foo} bar`;56 t.is(stdout, 'foo\nbar');57});5859test('$ allows execa return value buffer array interpolation', async t => {60 const foo = await $({encoding: 'buffer'})`echo.js foo`;61 const {stdout} = await $`echo.js ${[foo, 'bar']}`;62 t.is(stdout, 'foo\nbar');63});6465test('$ ignores consecutive spaces', async t => {66 const {stdout} = await $`echo.js foo bar`;67 t.is(stdout, 'foo\nbar');68});6970test('$ allows escaping spaces with interpolation', async t => {71 const {stdout} = await $`echo.js ${'foo bar'}`;72 t.is(stdout, 'foo bar');73});7475test('$ disallows escaping spaces with backslashes', async t => {76 const {stdout} = await $`echo.js foo\\ bar`;77 t.is(stdout, 'foo\\\nbar');78});7980test('$ allows space escaped values in array interpolation', async t => {81 const {stdout} = await $`echo.js ${['foo', 'bar baz']}`;82 t.is(stdout, 'foo\nbar baz');83});8485test('$ passes newline escape sequence as one argument', async t => {86 const {stdout} = await $`echo.js \n`;87 t.is(stdout, '\n');88});8990test('$ passes newline escape sequence in interpolation as one argument', async t => {91 const {stdout} = await $`echo.js ${'\n'}`;92 t.is(stdout, '\n');93});9495test('$ handles invalid escape sequence', async t => {96 const {stdout} = await $`echo.js \u`;97 t.is(stdout, '\\u');98});99100test('$ can concatenate at the end of tokens', async t => {101 const {stdout} = await $`echo.js foo${'bar'}`;102 t.is(stdout, 'foobar');103});104105test('$ does not concatenate at the end of tokens with a space', async t => {106 const {stdout} = await $`echo.js foo ${'bar'}`;107 t.is(stdout, 'foo\nbar');108});109110test('$ can concatenate at the end of tokens followed by an array', async t => {111 const {stdout} = await $`echo.js foo${['bar', 'foo']}`;112 t.is(stdout, 'foobar\nfoo');113});114115test('$ can concatenate at the start of tokens', async t => {116 const {stdout} = await $`echo.js ${'foo'}bar`;117 t.is(stdout, 'foobar');118});119120test('$ does not concatenate at the start of tokens with a space', async t => {121 const {stdout} = await $`echo.js ${'foo'} bar`;122 t.is(stdout, 'foo\nbar');123});124125test('$ can concatenate at the start of tokens followed by an array', async t => {126 const {stdout} = await $`echo.js ${['foo', 'bar']}foo`;127 t.is(stdout, 'foo\nbarfoo');128});129130test('$ can concatenate at the start and end of tokens followed by an array', async t => {131 const {stdout} = await $`echo.js foo${['bar', 'foo']}bar`;132 t.is(stdout, 'foobar\nfoobar');133});134135test('$ can concatenate multiple tokens', async t => {136 const {stdout} = await $`echo.js ${'foo'}bar${'foo'}`;137 t.is(stdout, 'foobarfoo');138});139140test('$ allows escaping spaces in commands with interpolation', async t => {141 const {stdout} = await $`${'command with space.js'} foo bar`;142 t.is(stdout, 'foo\nbar');143});144145test('$ escapes other whitespaces', async t => {146 const {stdout} = await $`echo.js foo\tbar`;147 t.is(stdout, 'foo\tbar');148});149150test('$ trims', async t => {151 const {stdout} = await $` echo.js foo bar `;152 t.is(stdout, 'foo\nbar');153});154Improve input validation (#838)ehmickybefore adoption8ee137c
lines 155–158why, one step back155test('$ must only use options or templates', t => {156 t.throws(() => $(true)`noop.js`, {message: /Please use either/});157});158Split files (#791)ehmickybefore adoption932fb26
lines 159–199why, one step back159test('$.sync', t => {160 const {stdout} = $.sync`echo.js foo bar`;161 t.is(stdout, 'foo\nbar');162});163164test('$.sync can be called $.s', t => {165 const {stdout} = $.s`echo.js foo bar`;166 t.is(stdout, 'foo\nbar');167});168169test('$.sync accepts options', t => {170 const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;171 t.is(stdout, 'foo');172});173174test('$.sync must be used after options binding, not before', t => {175 t.throws(() => $.sync({})`noop.js`, {message: /Please use/});176});177178test('$.sync allows execa return value 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 array interpolation', t => {185 const foo = $.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 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});195196test('$.sync allows execa return value buffer array interpolation', t => {197 const foo = $({encoding: 'buffer'}).sync`echo.js foo`;198 const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;199 t.is(stdout, 'foo\nbar');Improve input validation (#838)ehmickybefore adoption8ee137c
lines 200–203why, one step back200});201202test('$.sync must only templates', t => {203 t.throws(() => $.sync(true)`noop.js`, {message: /A template string must be used/});Split files (#791)ehmickybefore adoption932fb26
lines 204–268why, one step back204});205206const invalidExpression = test.macro({207 async exec(t, input, expected) {208 await t.throwsAsync(209 async () => $`echo.js ${input}`,210 {instanceOf: TypeError, message: expected},211 );212213 t.throws(214 () => $.sync`echo.js ${input}`,215 {instanceOf: TypeError, message: expected},216 );217 },218 title(prettyInput, input, expected) {219 return `$ APIs throw on invalid '${prettyInput ?? inspect(input)}' expression with '${expected}'`;220 },221});222223test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');224test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');225226test(invalidExpression, null, 'Unexpected "object" in template expression');227test(invalidExpression, [null], 'Unexpected "object" in template expression');228229test(invalidExpression, true, 'Unexpected "boolean" in template expression');230test(invalidExpression, [true], 'Unexpected "boolean" in template expression');231232test(invalidExpression, {}, 'Unexpected "object" in template expression');233test(invalidExpression, [{}], 'Unexpected "object" in template expression');234235test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');236test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');237238test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');239test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');240241test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');242test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');243244test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');245test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');246247test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');248test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');249250test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');251test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');252253test('$({stdio: \'ignore\'}).sync`noop.js`', invalidExpression, $({stdio: 'ignore'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');254test('[ $({stdio: \'ignore\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'ignore'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');255256test('$ stdin defaults to "inherit"', async t => {257 const {stdout} = await $({input: 'foo'})`stdin-script.js`;258 t.is(stdout, 'foo');259});260261test('$.sync stdin defaults to "inherit"', t => {262 const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;263 t.is(stdout, 'foo');264});265266test('$ stdin has no default value when stdio is set', t => {267 t.true(isStream($({stdio: 'pipe'})`noop.js`.stdin));268});`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 269–274why, one step back269270test('$.pipe(childProcess)', async t => {271 const {stdout} = await $`noop.js ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`);272 t.is(stdout, foobarString);273});274Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
lines 275–281why, one step back275test('$.pipe.pipe(childProcess)', async t => {276 const {stdout} = await $`noop.js ${foobarString}`277 .pipe($({stdin: 'pipe'})`stdin.js`)278 .pipe($({stdin: 'pipe'})`stdin.js`);279 t.is(stdout, foobarString);280});281`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 282–286why, one step back282test('$.pipe`command`', async t => {283 const {stdout} = await $`noop.js ${foobarString}`.pipe`stdin.js`;284 t.is(stdout, foobarString);285});286Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
lines 287–293why, one step back287test('$.pipe.pipe`command`', async t => {288 const {stdout} = await $`noop.js ${foobarString}`289 .pipe`stdin.js`290 .pipe`stdin.js`;291 t.is(stdout, foobarString);292});293`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 294–298why, one step back294test('$.pipe(childProcess, pipeOptions)', async t => {295 const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'});296 t.is(stdout, foobarString);297});298Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
lines 299–305why, one step back299test('$.pipe.pipe(childProcess, pipeOptions)', async t => {300 const {stdout} = await $`noop-fd.js 2 ${foobarString}`301 .pipe($({stdin: 'pipe'})`noop-stdin-fd.js 2`, {from: 'stderr'})302 .pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'});303 t.is(stdout, foobarString);304});305`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 306–310why, one step back306test('$.pipe(pipeOptions)`command`', async t => {307 const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe({from: 'stderr'})`stdin.js`;308 t.is(stdout, foobarString);309});310Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
lines 311–317why, one step back311test('$.pipe.pipe(pipeOptions)`command`', async t => {312 const {stdout} = await $`noop-fd.js 2 ${foobarString}`313 .pipe({from: 'stderr'})`noop-stdin-fd.js 2`314 .pipe({from: 'stderr'})`stdin.js`;315 t.is(stdout, foobarString);316});317`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 318–322why, one step back318test('$.pipe(options)`command`', async t => {319 const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})`stdin.js`;320 t.is(stdout, `${foobarString}\n`);321});322Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
lines 323–329why, one step back323test('$.pipe.pipe(options)`command`', async t => {324 const {stdout} = await $`noop.js ${foobarString}`325 .pipe({})`stdin.js`326 .pipe({stripFinalNewline: false})`stdin.js`;327 t.is(stdout, `${foobarString}\n`);328});329`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 330–334why, one step back330test('$.pipe(pipeAndProcessOptions)`command`', async t => {331 const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`.pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`;332 t.is(stdout, `${foobarString}\n`);333});334Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
lines 335–341why, one step back335test('$.pipe.pipe(pipeAndProcessOptions)`command`', async t => {336 const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`337 .pipe({from: 'stderr'})`noop-stdin-fd.js 2`338 .pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`;339 t.is(stdout, `${foobarString}\n`);340});341`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 342–343why, one step back342test('$.pipe(options)(secondOptions)`command`', async t => {343 const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`;Fix calling script `.pipe` multiple times (#846)ehmickybefore adoptionbd547e4
lines 344–350why, one step back344 t.is(stdout, foobarString);345});346347test('$.pipe.pipe(options)(secondOptions)`command`', async t => {348 const {stdout} = await $`noop.js ${foobarString}`349 .pipe({})({})`stdin.js`350 .pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`;`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
lines 351–367why, one step back351 t.is(stdout, foobarString);352});353354test('$.pipe(options)(childProcess) fails', t => {355 t.throws(() => {356 $`empty.js`.pipe({stdout: 'pipe'})($`empty.js`);357 }, {message: /Please use \.pipe/});358});359360const testInvalidPipe = (t, ...args) => {361 t.throws(() => {362 $`empty.js`.pipe(...args);363 }, {message: /must be a template string/});364};365366test('$.pipe(nonExecaChildProcess) fails', testInvalidPipe, spawn('node', ['--version']));367test('$.pipe(false) fails', testInvalidPipe, false);