Skip to content
Myr

script.js

This file

Path

test/script.js

258 lines · 8.4 KiB · JavaScript

Last changed by

Split files (#791)ehmickybefore adoption932fb26

History of this file

Take it

Split files (#791)ehmickybefore adoption932fb26

lines 1–258why, one step back
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});