template.js
This file
Path
test/pipe/template.js
272 lines · 9.1 KiB · JavaScript
Last changed by
Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273import {spawn} from 'node:child_process';import {pathToFileURL} from 'node:url';import test from 'ava';import {$, execa} from '../../index.js';import {setFixtureDir, FIXTURES_DIR} from '../helpers/fixtures-dir.js';import {foobarString} from '../helpers/input.js';
setFixtureDir();
test('$.pipe(subprocess)', async t => { const {stdout} = await $`noop.js ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`); t.is(stdout, foobarString);});
test('execa.$.pipe(subprocess)', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe($({stdin: 'pipe'})`stdin.js`); t.is(stdout, foobarString);});
test('$.pipe.pipe(subprocess)', async t => { const {stdout} = await $`noop.js ${foobarString}` .pipe($({stdin: 'pipe'})`stdin.js`) .pipe($({stdin: 'pipe'})`stdin.js`); t.is(stdout, foobarString);});
test('$.pipe`command`', async t => { const {stdout} = await $`noop.js ${foobarString}`.pipe`stdin.js`; t.is(stdout, foobarString);});
test('execa.$.pipe`command`', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe`stdin.js`; t.is(stdout, foobarString);});
test('$.pipe.pipe`command`', async t => { const {stdout} = await $`noop.js ${foobarString}` .pipe`stdin.js` .pipe`stdin.js`; t.is(stdout, foobarString);});
test('$.pipe("file")', async t => { const {stdout} = await $`noop.js ${foobarString}`.pipe('stdin.js'); t.is(stdout, foobarString);});
test('execa.$.pipe("file")`', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe('stdin.js'); t.is(stdout, foobarString);});
test('$.pipe.pipe("file")', async t => { const {stdout} = await $`noop.js ${foobarString}` .pipe`stdin.js` .pipe('stdin.js'); t.is(stdout, foobarString);});
test('execa.$.pipe(fileUrl)`', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe(pathToFileURL(`${FIXTURES_DIR}/stdin.js`)); t.is(stdout, foobarString);});
test('$.pipe("file", args, options)', async t => { const {stdout} = await $`noop.js ${foobarString}`.pipe('node', ['stdin.js'], {cwd: FIXTURES_DIR}); t.is(stdout, foobarString);});
test('execa.$.pipe("file", args, options)`', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe('node', ['stdin.js'], {cwd: FIXTURES_DIR}); t.is(stdout, foobarString);});
test('$.pipe.pipe("file", args, options)', async t => { const {stdout} = await $`noop.js ${foobarString}` .pipe`stdin.js` .pipe('node', ['stdin.js'], {cwd: FIXTURES_DIR}); t.is(stdout, foobarString);});
test('$.pipe(subprocess, pipeOptions)', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'}); t.is(stdout, foobarString);});
test('execa.$.pipe(subprocess, pipeOptions)', async t => { const {stdout} = await execa('noop-fd.js', ['2', foobarString]).pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'}); t.is(stdout, foobarString);});
test('$.pipe.pipe(subprocess, pipeOptions)', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}` .pipe($({stdin: 'pipe'})`noop-stdin-fd.js 2`, {from: 'stderr'}) .pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'}); t.is(stdout, foobarString);});
test('$.pipe(pipeOptions)`command`', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe({from: 'stderr'})`stdin.js`; t.is(stdout, foobarString);});
test('execa.$.pipe(pipeOptions)`command`', async t => { const {stdout} = await execa('noop-fd.js', ['2', foobarString]).pipe({from: 'stderr'})`stdin.js`; t.is(stdout, foobarString);});
test('$.pipe.pipe(pipeOptions)`command`', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}` .pipe({from: 'stderr'})`noop-stdin-fd.js 2` .pipe({from: 'stderr'})`stdin.js`; t.is(stdout, foobarString);});
test('$.pipe("file", pipeOptions)', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe('stdin.js', {from: 'stderr'}); t.is(stdout, foobarString);});
test('execa.$.pipe("file", pipeOptions)', async t => { const {stdout} = await execa('noop-fd.js', ['2', foobarString]).pipe('stdin.js', {from: 'stderr'}); t.is(stdout, foobarString);});
test('$.pipe.pipe("file", pipeOptions)', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}` .pipe({from: 'stderr'})`noop-stdin-fd.js 2` .pipe('stdin.js', {from: 'stderr'}); t.is(stdout, foobarString);});
test('$.pipe(options)`command`', async t => { const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})`stdin.js`; t.is(stdout, `${foobarString}\n`);});
test('execa.$.pipe(options)`command`', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe({stripFinalNewline: false})`stdin.js`; t.is(stdout, `${foobarString}\n`);});
test('$.pipe.pipe(options)`command`', async t => { const {stdout} = await $`noop.js ${foobarString}` .pipe({})`stdin.js` .pipe({stripFinalNewline: false})`stdin.js`; t.is(stdout, `${foobarString}\n`);});
test('$.pipe("file", options)', async t => { const {stdout} = await $`noop.js ${foobarString}`.pipe('stdin.js', {stripFinalNewline: false}); t.is(stdout, `${foobarString}\n`);});
test('execa.$.pipe("file", options)', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe('stdin.js', {stripFinalNewline: false}); t.is(stdout, `${foobarString}\n`);});
test('$.pipe.pipe("file", options)', async t => { const {stdout} = await $`noop.js ${foobarString}` .pipe({})`stdin.js` .pipe('stdin.js', {stripFinalNewline: false}); t.is(stdout, `${foobarString}\n`);});
test('$.pipe(pipeAndSubprocessOptions)`command`', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`.pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`; t.is(stdout, `${foobarString}\n`);});
test('execa.$.pipe(pipeAndSubprocessOptions)`command`', async t => { const {stdout} = await execa('noop-fd.js', ['2', `${foobarString}\n`]).pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`; t.is(stdout, `${foobarString}\n`);});
test('$.pipe.pipe(pipeAndSubprocessOptions)`command`', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}\n` .pipe({from: 'stderr'})`noop-stdin-fd.js 2` .pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`; t.is(stdout, `${foobarString}\n`);});
test('$.pipe("file", pipeAndSubprocessOptions)', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`.pipe('stdin.js', {from: 'stderr', stripFinalNewline: false}); t.is(stdout, `${foobarString}\n`);});
test('execa.$.pipe("file", pipeAndSubprocessOptions)', async t => { const {stdout} = await execa('noop-fd.js', ['2', `${foobarString}\n`]).pipe('stdin.js', {from: 'stderr', stripFinalNewline: false}); t.is(stdout, `${foobarString}\n`);});
test('$.pipe.pipe("file", pipeAndSubprocessOptions)', async t => { const {stdout} = await $`noop-fd.js 2 ${foobarString}\n` .pipe({from: 'stderr'})`noop-stdin-fd.js 2` .pipe('stdin.js', {from: 'stderr', stripFinalNewline: false}); t.is(stdout, `${foobarString}\n`);});
test('$.pipe(options)(secondOptions)`command`', async t => { const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`; t.is(stdout, foobarString);});
test('execa.$.pipe(options)(secondOptions)`command`', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`; t.is(stdout, foobarString);});
test('$.pipe.pipe(options)(secondOptions)`command`', async t => { const {stdout} = await $`noop.js ${foobarString}` .pipe({})({})`stdin.js` .pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`; t.is(stdout, foobarString);});
test('$.pipe`command` forces "stdin: pipe"', async t => { const {stdout} = await $`noop.js ${foobarString}`.pipe({stdin: 'ignore'})`stdin.js`; t.is(stdout, foobarString);});
test('execa.pipe("file") forces "stdin: "pipe"', async t => { const {stdout} = await execa('noop.js', [foobarString]).pipe('stdin.js', {stdin: 'ignore'}); t.is(stdout, foobarString);});
test('execa.pipe(subprocess) does not force "stdin: pipe"', async t => { await t.throwsAsync( execa('noop.js', [foobarString]).pipe(execa('stdin.js', {stdin: 'ignore'})), {message: /"stdin: 'ignore'" option is incompatible/}, );});
test('$.pipe(options)(subprocess) fails', async t => { await t.throwsAsync( $`empty.js`.pipe({stdout: 'pipe'})($`empty.js`), {message: /Please use \.pipe/}, );});
test('execa.$.pipe(options)(subprocess) fails', async t => { await t.throwsAsync( execa('empty.js').pipe({stdout: 'pipe'})($`empty.js`), {message: /Please use \.pipe/}, );});
test('$.pipe(options)("file") fails', async t => { await t.throwsAsync( $`empty.js`.pipe({stdout: 'pipe'})('empty.js'), {message: /Please use \.pipe/}, );});
test('execa.$.pipe(options)("file") fails', async t => { await t.throwsAsync( execa('empty.js').pipe({stdout: 'pipe'})('empty.js'), {message: /Please use \.pipe/}, );});
const testInvalidPipe = async (t, ...args) => { await t.throwsAsync( $`empty.js`.pipe(...args), {message: /must be a template string/}, );};
test('$.pipe(nonExecaSubprocess) fails', testInvalidPipe, spawn('node', ['--version']));test('$.pipe(false) fails', testInvalidPipe, false);