template.js
This file
Path
test/methods/template.js
433 lines · 34.7 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
1import assert from 'node:assert/strict';2import process from 'node:process';3import test from 'node:test';4import {assertThrows} from '../helpers/assert.js';5import {$} from '../../index.js';6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';7import {majorNodeVersion} from '../helpers/node-version.js';8import {foobarString} from '../helpers/input.js';910setFixtureDirectory();1112const isWindows = process.platform === 'win32';1314// Workaround since some text editors or IDEs do not allow inputting \r directly15const escapedCall = string => {16 const templates = [string];17 templates.raw = [string];18 return $(templates);19};2021const testScriptStdout = async (getSubprocess, expectedStdout) => {22 const {stdout} = await getSubprocess();23 assert.equal(stdout, expectedStdout);24};2526test('$ allows number interpolation', () => testScriptStdout(() => $`echo.js 1 ${2}`, '1\n2'));27test('$ can concatenate multiple tokens', () => testScriptStdout(() => $`echo.js ${'foo'}bar${'foo'}`, 'foobarfoo'));28test('$ can use newlines and tab indentations', () => testScriptStdout(() => $`echo.js foo29 bar`, 'foo\nbar'));30test('$ can use newlines and space indentations', () => testScriptStdout(() => $`echo.js foo31 bar`, 'foo\nbar'));32test('$ can use escaped newlines and space indentations', () => testScriptStdout(() => $`echo.js foo\33 bar`, 'foo\nbar'));34test('$ can use escaped newlines and inline tab indentations', () => testScriptStdout(() => $`echo.js foo\35 bar`, 'foo\nbar'));36test('$ can use escaped newlines and character escaped tab indentations', () => testScriptStdout(() => $`echo.js foo\37\tbar`, 'foo\tbar'));38test('$ can use escaped newlines and character escaped newlines', () => testScriptStdout(() => $`echo.js foo\39\n\nbar`, 'foo\n\nbar'));40// An escaped newline is ignored, so it must not separate tokens, nor hide the whitespaces following it.41// This must behave the same whether the tokens around it are expressions or not.42test('$ can use escaped newlines between tokens', () => testScriptStdout(() => $`echo.js foo\43bar`, 'foobar'));44test('$ can use escaped newlines between expressions', () => testScriptStdout(() => $`echo.js ${'foo'}\45${'bar'}`, 'foobar'));46test('$ can use multiple escaped newlines between expressions', () => testScriptStdout(() => $`echo.js ${'foo'}\47\48${'bar'}`, 'foobar'));49test('$ can use escaped newlines and space indentations between expressions', () => testScriptStdout(() => $`echo.js ${'foo'}\50 ${'bar'}`, 'foo\nbar'));51test('$ can use escaped newlines and tab indentations between expressions', () => testScriptStdout(() => $`echo.js ${'foo'}\52 ${'bar'}`, 'foo\nbar'));53test('$ can use escaped newlines and space indentations after an expression', () => testScriptStdout(() => $`echo.js ${'foo'}\54 bar`, 'foo\nbar'));55test('$ can use escaped newlines and tab indentations after an expression', () => testScriptStdout(() => $`echo.js ${'foo'}\56 bar`, 'foo\nbar'));57test('$ can use escaped newlines before an expression', () => testScriptStdout(() => $`echo.js foo\58${'bar'}`, 'foobar'));59test('$ can use Windows newlines and tab indentations', () => testScriptStdout(() => escapedCall('echo.js foo\r\n\tbar'), 'foo\nbar'));60test('$ can use Windows newlines and space indentations', () => testScriptStdout(() => escapedCall('echo.js foo\r\n bar'), 'foo\nbar'));61test('$ does not ignore comments in expressions', () => testScriptStdout(() => $`echo.js foo62 ${/* This is a comment */''}63 bar64 ${/* This is another comment */''}65 baz66`, 'foo\n\nbar\n\nbaz'));67test('$ allows escaping spaces with interpolation', () => testScriptStdout(() => $`echo.js ${'foo bar'}`, 'foo bar'));68test('$ allows escaping spaces in commands with interpolation', () => testScriptStdout(() => $`${'command with space.js'} foo bar`, 'foo\nbar'));69test('$ trims', () => testScriptStdout(() => $` echo.js foo bar `, 'foo\nbar'));70test('$ allows array interpolation', () => testScriptStdout(() => $`echo.js ${['foo', 'bar']}`, 'foo\nbar'));71test('$ allows empty array interpolation', () => testScriptStdout(() => $`echo.js foo ${[]} bar`, 'foo\nbar'));72// An empty array must not remove the whitespace before it73test('$ allows empty array interpolation followed by a token', () => testScriptStdout(() => $`echo.js foo ${[]}bar`, 'foo\nbar'));74test('$ allows empty array interpolation followed by an expression', () => testScriptStdout(() => $`echo.js foo ${[]}${'bar'}`, 'foo\nbar'));75test('$ allows multiple empty array interpolations followed by a token', () => testScriptStdout(() => $`echo.js foo ${[]}${[]}bar`, 'foo\nbar'));76test('$ allows empty array interpolation after an expression and before a token', () => testScriptStdout(() => $`echo.js ${'foo'} ${[]}bar`, 'foo\nbar'));77test('$ allows empty array interpolation without whitespaces', () => testScriptStdout(() => $`echo.js foo${[]}bar`, 'foobar'));78test('$ allows empty array interpolation without whitespaces after it', () => testScriptStdout(() => $`echo.js foo${[]} bar`, 'foo\nbar'));79test('$ allows empty array interpolation between expressions without whitespaces', () => testScriptStdout(() => $`echo.js ${'foo'}${[]}${'bar'}`, 'foobar'));80test('$ allows empty array interpolation at the start of an argument', () => testScriptStdout(() => $`echo.js ${[]}foo bar`, 'foo\nbar'));81test('$ allows empty array interpolation at the end', () => testScriptStdout(() => $`echo.js foo ${[]}`, 'foo'));82test('$ allows empty array interpolation as the only argument', () => testScriptStdout(() => $`echo.js ${[]}`, ''));83test('$ allows empty array interpolation between tabs', () => testScriptStdout(() => $`echo.js foo ${[]} bar`, 'foo\nbar'));84test('$ allows empty array interpolation followed by an empty string', () => testScriptStdout(() => $`echo.js foo ${[]}${''} bar`, 'foo\n\nbar'));85test('$ allows empty array interpolation after escaped newlines', () => testScriptStdout(() => $`echo.js foo \86${[]}bar`, 'foo\nbar'));87test('$ allows space escaped values in array interpolation', () => testScriptStdout(() => $`echo.js ${['foo', 'bar baz']}`, 'foo\nbar baz'));88test('$ can concatenate at the end of tokens followed by an array', () => testScriptStdout(() => $`echo.js foo${['bar', 'foo']}`, 'foobar\nfoo'));89test('$ can concatenate at the start of tokens followed by an array', () => testScriptStdout(() => $`echo.js ${['foo', 'bar']}foo`, 'foo\nbarfoo'));90test('$ can concatenate at the start and end of tokens followed by an array', () => testScriptStdout(() => $`echo.js foo${['bar', 'foo']}bar`, 'foobar\nfoobar'));91test('$ handles escaped newlines', () => testScriptStdout(() => $`echo.js a\92b`, 'ab'));93test('$ handles backslashes at end of lines', () => testScriptStdout(() => $`echo.js a\\94 b`, 'a\\\nb'));95test('$ handles double backslashes at end of lines', () => testScriptStdout(() => $`echo.js a\\\\96 b`, 'a\\\\\nb'));97test('$ handles tokens - a', () => testScriptStdout(() => $`echo.js a`, 'a'));98test('$ handles expressions - a', () => testScriptStdout(() => $`echo.js ${'a'}`, 'a'));99test('$ handles tokens - abc', () => testScriptStdout(() => $`echo.js abc`, 'abc'));100test('$ handles expressions - abc', () => testScriptStdout(() => $`echo.js ${'abc'}`, 'abc'));101test('$ handles tokens - ""', () => testScriptStdout(() => $`echo.js`, ''));102test('$ handles expressions - ""', () => testScriptStdout(() => $`echo.js a ${''} b`, 'a\n\nb'));103test('$ splits tokens - ""', () => testScriptStdout(() => $`echo.js ab`, 'ab'));104test('$ splits expressions - ""', () => testScriptStdout(() => $`echo.js ${'a'}${'b'}`, 'ab'));105test('$ concatenates expressions - ""', () => testScriptStdout(() => $`echo.js a${'b'}c`, 'abc'));106test('$ handles tokens - " "', () => testScriptStdout(() => $`echo.js `, ''));107test('$ handles expressions - " "', () => testScriptStdout(() => $`echo.js ${' '}`, ' '));108test('$ splits tokens - " "', () => testScriptStdout(() => $`echo.js a b`, 'a\nb'));109test('$ splits expressions - " "', () => testScriptStdout(() => $`echo.js ${'a'} ${'b'}`, 'a\nb'));110test('$ concatenates tokens - " "', () => testScriptStdout(() => $`echo.js a `, 'a'));111test('$ concatenates expressions - " "', () => testScriptStdout(() => $`echo.js ${'a'} `, 'a'));112test('$ handles tokens - " " (2 spaces)', () => testScriptStdout(() => $`echo.js `, ''));113test('$ handles expressions - " " (2 spaces)', () => testScriptStdout(() => $`echo.js ${' '}`, ' '));114test('$ splits tokens - " " (2 spaces)', () => testScriptStdout(() => $`echo.js a b`, 'a\nb'));115test('$ splits expressions - " " (2 spaces)', () => testScriptStdout(() => $`echo.js ${'a'} ${'b'}`, 'a\nb'));116test('$ concatenates tokens - " " (2 spaces)', () => testScriptStdout(() => $`echo.js a `, 'a'));117test('$ concatenates expressions - " " (2 spaces)', () => testScriptStdout(() => $`echo.js ${'a'} `, 'a'));118test('$ handles tokens - " " (3 spaces)', () => testScriptStdout(() => $`echo.js `, ''));119// eslint-disable-next-line unicorn/prefer-string-repeat120test('$ handles expressions - " " (3 spaces)', () => testScriptStdout(() => $`echo.js ${' '}`, ' '));121test('$ splits tokens - " " (3 spaces)', () => testScriptStdout(() => $`echo.js a b`, 'a\nb'));122test('$ splits expressions - " " (3 spaces)', () => testScriptStdout(() => $`echo.js ${'a'} ${'b'}`, 'a\nb'));123test('$ concatenates tokens - " " (3 spaces)', () => testScriptStdout(() => $`echo.js a `, 'a'));124test('$ concatenates expressions - " " (3 spaces)', () => testScriptStdout(() => $`echo.js ${'a'} `, 'a'));125test('$ handles tokens - \\t (no escape)', () => testScriptStdout(() => $`echo.js `, ''));126test('$ handles expressions - \\t (no escape)', () => testScriptStdout(() => $`echo.js ${' '}`, '\t'));127test('$ splits tokens - \\t (no escape)', () => testScriptStdout(() => $`echo.js a b`, 'a\nb'));128test('$ splits expressions - \\t (no escape)', () => testScriptStdout(() => $`echo.js ${'a'} ${'b'}`, 'a\nb'));129test('$ concatenates tokens - \\t (no escape)', () => testScriptStdout(() => $`echo.js a b`, 'a\nb'));130test('$ concatenates expressions - \\t (no escape)', () => testScriptStdout(() => $`echo.js ${'a'} b`, 'a\nb'));131test('$ handles tokens - \\t (escape)', () => testScriptStdout(() => $`echo.js \t`, '\t'));132test('$ handles expressions - \\t (escape)', () => testScriptStdout(() => $`echo.js ${'\t'}`, '\t'));133test('$ splits tokens - \\t (escape)', () => testScriptStdout(() => $`echo.js a\tb`, 'a\tb'));134test('$ splits expressions - \\t (escape)', () => testScriptStdout(() => $`echo.js ${'a'}\t${'b'}`, 'a\tb'));135test('$ concatenates tokens - \\t (escape)', () => testScriptStdout(() => $`echo.js \ta\t b`, '\ta\t\nb'));136test('$ concatenates expressions - \\t (escape)', () => testScriptStdout(() => $`echo.js \t${'a'}\t b`, '\ta\t\nb'));137test('$ handles tokens - \\n (no escape)', () => testScriptStdout(() => $`echo.js138 `, ''));139test('$ handles expressions - \\n (no escape)', () => testScriptStdout(() => $`echo.js ${`140`} `, '\n'));141test('$ splits tokens - \\n (no escape)', () => testScriptStdout(() => $`echo.js a142 b`, 'a\nb'));143test('$ splits expressions - \\n (no escape)', () => testScriptStdout(() => $`echo.js ${'a'}144 ${'b'}`, 'a\nb'));145test('$ concatenates tokens - \\n (no escape)', () => testScriptStdout(() => $`echo.js146a147 b`, 'a\nb'));148test('$ concatenates expressions - \\n (no escape)', () => testScriptStdout(() => $`echo.js149${'a'}150 b`, 'a\nb'));151test('$ handles tokens - \\n (escape)', () => testScriptStdout(() => $`echo.js \n `, '\n'));152test('$ handles expressions - \\n (escape)', () => testScriptStdout(() => $`echo.js ${'\n'} `, '\n'));153test('$ splits tokens - \\n (escape)', () => testScriptStdout(() => $`echo.js a\n b`, 'a\n\nb'));154test('$ splits expressions - \\n (escape)', () => testScriptStdout(() => $`echo.js ${'a'}\n ${'b'}`, 'a\n\nb'));155test('$ concatenates tokens - \\n (escape)', () => testScriptStdout(() => $`echo.js \na\n b`, '\na\n\nb'));156test('$ concatenates expressions - \\n (escape)', () => testScriptStdout(() => $`echo.js \n${'a'}\n b`, '\na\n\nb'));157test('$ handles tokens - \\r (no escape)', () => testScriptStdout(() => escapedCall('echo.js \r '), ''));158test('$ splits tokens - \\r (no escape)', () => testScriptStdout(() => escapedCall('echo.js a\rb'), 'a\nb'));159test('$ splits expressions - \\r (no escape)', () => testScriptStdout(() => escapedCall('echo.js a\rb'), 'a\nb'));160test('$ concatenates tokens - \\r (no escape)', () => testScriptStdout(() => escapedCall('echo.js \ra\r b'), 'a\nb'));161test('$ concatenates expressions - \\r (no escape)', () => testScriptStdout(() => escapedCall('echo.js \ra\r b'), 'a\nb'));162test('$ splits tokens - \\r (escape)', () => testScriptStdout(() => $`echo.js a\r b`, 'a\r\nb'));163test('$ splits expressions - \\r (escape)', () => testScriptStdout(() => $`echo.js ${'a'}\r ${'b'}`, 'a\r\nb'));164test('$ concatenates tokens - \\r (escape)', () => testScriptStdout(() => $`echo.js \ra\r b`, '\ra\r\nb'));165test('$ concatenates expressions - \\r (escape)', () => testScriptStdout(() => $`echo.js \r${'a'}\r b`, '\ra\r\nb'));166test('$ handles tokens - \\r\\n (no escape)', () => testScriptStdout(() => escapedCall('echo.js \r\n '), ''));167test('$ splits tokens - \\r\\n (no escape)', () => testScriptStdout(() => escapedCall('echo.js a\r\nb'), 'a\nb'));168test('$ splits expressions - \\r\\n (no escape)', () => testScriptStdout(() => escapedCall('echo.js a\r\nb'), 'a\nb'));169test('$ concatenates tokens - \\r\\n (no escape)', () => testScriptStdout(() => escapedCall('echo.js \r\na\r\n b'), 'a\nb'));170test('$ concatenates expressions - \\r\\n (no escape)', () => testScriptStdout(() => escapedCall('echo.js \r\na\r\n b'), 'a\nb'));171test('$ handles tokens - \\r\\n (escape)', () => testScriptStdout(() => $`echo.js \r\n `, '\r\n'));172test('$ handles expressions - \\r\\n (escape)', () => testScriptStdout(() => $`echo.js ${'\r\n'} `, '\r\n'));173test('$ splits tokens - \\r\\n (escape)', () => testScriptStdout(() => $`echo.js a\r\n b`, 'a\r\n\nb'));174test('$ splits expressions - \\r\\n (escape)', () => testScriptStdout(() => $`echo.js ${'a'}\r\n ${'b'}`, 'a\r\n\nb'));175test('$ concatenates tokens - \\r\\n (escape)', () => testScriptStdout(() => $`echo.js \r\na\r\n b`, '\r\na\r\n\nb'));176test('$ concatenates expressions - \\r\\n (escape)', () => testScriptStdout(() => $`echo.js \r\n${'a'}\r\n b`, '\r\na\r\n\nb'));177/* eslint-disable no-irregular-whitespace -- intentionally testing a literal form feed character */178test('$ handles expressions - \\f (no escape)', () => testScriptStdout(() => $`echo.js ${''}`, '\f'));179test('$ splits tokens - \\f (no escape)', () => testScriptStdout(() => $`echo.js ab`, 'a\fb'));180test('$ splits expressions - \\f (no escape)', () => testScriptStdout(() => $`echo.js ${'a'}${'b'}`, 'a\fb'));181test('$ concatenates tokens - \\f (no escape)', () => testScriptStdout(() => $`echo.js a b`, '\fa\f\nb'));182test('$ concatenates expressions - \\f (no escape)', () => testScriptStdout(() => $`echo.js ${'a'} b`, '\fa\f\nb'));183/* eslint-enable no-irregular-whitespace */184test('$ handles tokens - \\f (escape)', () => testScriptStdout(() => $`echo.js \f`, '\f'));185test('$ handles expressions - \\f (escape)', () => testScriptStdout(() => $`echo.js ${'\f'}`, '\f'));186test('$ splits tokens - \\f (escape)', () => testScriptStdout(() => $`echo.js a\fb`, 'a\fb'));187test('$ splits expressions - \\f (escape)', () => testScriptStdout(() => $`echo.js ${'a'}\f${'b'}`, 'a\fb'));188test('$ concatenates tokens - \\f (escape)', () => testScriptStdout(() => $`echo.js \fa\f b`, '\fa\f\nb'));189test('$ concatenates expressions - \\f (escape)', () => testScriptStdout(() => $`echo.js \f${'a'}\f b`, '\fa\f\nb'));190test('$ handles tokens - \\', () => testScriptStdout(() => $`echo.js \\`, '\\'));191test('$ handles expressions - \\', () => testScriptStdout(() => $`echo.js ${'\\'}`, '\\'));192test('$ splits tokens - \\', () => testScriptStdout(() => $`echo.js a\\b`, 'a\\b'));193test('$ splits expressions - \\', () => testScriptStdout(() => $`echo.js ${'a'}\\${'b'}`, 'a\\b'));194test('$ concatenates tokens - \\', () => testScriptStdout(() => $`echo.js \\a\\ b`, '\\a\\\nb'));195test('$ concatenates expressions - \\', () => testScriptStdout(() => $`echo.js \\${'a'}\\ b`, '\\a\\\nb'));196test('$ handles tokens - \\\\', () => testScriptStdout(() => $`echo.js \\\\`, '\\\\'));197test('$ handles expressions - \\\\', () => testScriptStdout(() => $`echo.js ${'\\\\'}`, '\\\\'));198test('$ splits tokens - \\\\', () => testScriptStdout(() => $`echo.js a\\\\b`, 'a\\\\b'));199test('$ splits expressions - \\\\', () => testScriptStdout(() => $`echo.js ${'a'}\\\\${'b'}`, 'a\\\\b'));200test('$ concatenates tokens - \\\\', () => testScriptStdout(() => $`echo.js \\\\a\\\\ b`, '\\\\a\\\\\nb'));201test('$ concatenates expressions - \\\\', () => testScriptStdout(() => $`echo.js \\\\${'a'}\\\\ b`, '\\\\a\\\\\nb'));202test('$ handles tokens - `', () => testScriptStdout(() => $`echo.js \``, '`'));203test('$ handles expressions - `', () => testScriptStdout(() => $`echo.js ${'`'}`, '`'));204test('$ splits tokens - `', () => testScriptStdout(() => $`echo.js a\`b`, 'a`b'));205test('$ splits expressions - `', () => testScriptStdout(() => $`echo.js ${'a'}\`${'b'}`, 'a`b'));206test('$ concatenates tokens - `', () => testScriptStdout(() => $`echo.js \`a\` b`, '`a`\nb'));207test('$ concatenates expressions - `', () => testScriptStdout(() => $`echo.js \`${'a'}\` b`, '`a`\nb'));208test('$ handles tokens - \\v', () => testScriptStdout(() => $`echo.js \v`, '\v'));209test('$ handles expressions - \\v', () => testScriptStdout(() => $`echo.js ${'\v'}`, '\v'));210test('$ splits tokens - \\v', () => testScriptStdout(() => $`echo.js a\vb`, 'a\vb'));211test('$ splits expressions - \\v', () => testScriptStdout(() => $`echo.js ${'a'}\v${'b'}`, 'a\vb'));212test('$ concatenates tokens - \\v', () => testScriptStdout(() => $`echo.js \va\v b`, '\va\v\nb'));213test('$ concatenates expressions - \\v', () => testScriptStdout(() => $`echo.js \v${'a'}\v b`, '\va\v\nb'));214test('$ handles tokens - \\u2028', () => testScriptStdout(() => $`echo.js \u2028`, '\u{2028}'));215test('$ handles expressions - \\u2028', () => testScriptStdout(() => $`echo.js ${'\u{2028}'}`, '\u{2028}'));216test('$ splits tokens - \\u2028', () => testScriptStdout(() => $`echo.js a\u2028b`, 'a\u{2028}b'));217test('$ splits expressions - \\u2028', () => testScriptStdout(() => $`echo.js ${'a'}\u2028${'b'}`, 'a\u{2028}b'));218test('$ concatenates tokens - \\u2028', () => testScriptStdout(() => $`echo.js \u2028a\u2028 b`, '\u{2028}a\u{2028}\nb'));219test('$ concatenates expressions - \\u2028', () => testScriptStdout(() => $`echo.js \u2028${'a'}\u2028 b`, '\u{2028}a\u{2028}\nb'));220test('$ handles tokens - \\a', () => testScriptStdout(() => $`echo.js \a`, 'a'));221test('$ splits tokens - \\a', () => testScriptStdout(() => $`echo.js a\ab`, 'aab'));222test('$ splits expressions - \\a', () => testScriptStdout(() => $`echo.js ${'a'}\a${'b'}`, 'aab'));223test('$ concatenates tokens - \\a', () => testScriptStdout(() => $`echo.js \aa\a b`, 'aaa\nb'));224test('$ concatenates expressions - \\a', () => testScriptStdout(() => $`echo.js \a${'a'}\a b`, 'aaa\nb'));225test('$ handles tokens - \\cJ', () => testScriptStdout(() => $`echo.js \cJ`, 'cJ'));226test('$ splits tokens - \\cJ', () => testScriptStdout(() => $`echo.js a\cJb`, 'acJb'));227test('$ splits expressions - \\cJ', () => testScriptStdout(() => $`echo.js ${'a'}\cJ${'b'}`, 'acJb'));228test('$ concatenates tokens - \\cJ', () => testScriptStdout(() => $`echo.js \cJa\cJ b`, 'cJacJ\nb'));229test('$ concatenates expressions - \\cJ', () => testScriptStdout(() => $`echo.js \cJ${'a'}\cJ b`, 'cJacJ\nb'));230test('$ handles tokens - \\.', () => testScriptStdout(() => $`echo.js \.`, '.'));231test('$ splits tokens - \\.', () => testScriptStdout(() => $`echo.js a\.b`, 'a.b'));232test('$ splits expressions - \\.', () => testScriptStdout(() => $`echo.js ${'a'}\.${'b'}`, 'a.b'));233test('$ concatenates tokens - \\.', () => testScriptStdout(() => $`echo.js \.a\. b`, '.a.\nb'));234test('$ concatenates expressions - \\.', () => testScriptStdout(() => $`echo.js \.${'a'}\. b`, '.a.\nb'));235236test('$ handles tokens - \\x63', () => testScriptStdout(() => $`echo.js \x63`, 'c'));237test('$ splits tokens - \\x63', () => testScriptStdout(() => $`echo.js a\x63b`, 'acb'));238test('$ splits expressions - \\x63', () => testScriptStdout(() => $`echo.js ${'a'}\x63${'b'}`, 'acb'));239test('$ concatenates tokens - \\x63', () => testScriptStdout(() => $`echo.js \x63a\x63 b`, 'cac\nb'));240test('$ concatenates expressions - \\x63', () => testScriptStdout(() => $`echo.js \x63${'a'}\x63 b`, 'cac\nb'));241242test('$ handles tokens - \\u0063', () => testScriptStdout(() => $`echo.js \u0063`, 'c'));243test('$ splits tokens - \\u0063', () => testScriptStdout(() => $`echo.js a\u0063b`, 'acb'));244test('$ splits expressions - \\u0063', () => testScriptStdout(() => $`echo.js ${'a'}\u0063${'b'}`, 'acb'));245test('$ concatenates tokens - \\u0063', () => testScriptStdout(() => $`echo.js \u0063a\u0063 b`, 'cac\nb'));246test('$ concatenates expressions - \\u0063', () => testScriptStdout(() => $`echo.js \u0063${'a'}\u0063 b`, 'cac\nb'));247test('$ handles tokens - \\u{1}', () => testScriptStdout(() => $`echo.js \u{1}`, '\u{1}'));248test('$ splits tokens - \\u{1}', () => testScriptStdout(() => $`echo.js a\u{1}b`, 'a\u{1}b'));249test('$ splits expressions - \\u{1}', () => testScriptStdout(() => $`echo.js ${'a'}\u{1}${'b'}`, 'a\u{1}b'));250test('$ concatenates tokens - \\u{1}', () => testScriptStdout(() => $`echo.js \u{1}a\u{1} b`, '\u{1}a\u{1}\nb'));251test('$ concatenates expressions - \\u{1}', () => testScriptStdout(() => $`echo.js \u{1}${'a'}\u{1} b`, '\u{1}a\u{1}\nb'));252test('$ handles tokens - \\u{63}', () => testScriptStdout(() => $`echo.js \u{63}`, 'c'));253test('$ splits tokens - \\u{63}', () => testScriptStdout(() => $`echo.js a\u{63}b`, 'acb'));254test('$ splits expressions - \\u{63}', () => testScriptStdout(() => $`echo.js ${'a'}\u{63}${'b'}`, 'acb'));255test('$ concatenates tokens - \\u{63}', () => testScriptStdout(() => $`echo.js \u{63}a\u{63} b`, 'cac\nb'));256test('$ concatenates expressions - \\u{63}', () => testScriptStdout(() => $`echo.js \u{63}${'a'}\u{63} b`, 'cac\nb'));257test('$ handles tokens - \\u{063}', () => testScriptStdout(() => $`echo.js \u{063}`, 'c'));258test('$ splits tokens - \\u{063}', () => testScriptStdout(() => $`echo.js a\u{063}b`, 'acb'));259test('$ splits expressions - \\u{063}', () => testScriptStdout(() => $`echo.js ${'a'}\u{063}${'b'}`, 'acb'));260test('$ concatenates tokens - \\u{063}', () => testScriptStdout(() => $`echo.js \u{063}a\u{063} b`, 'cac\nb'));261test('$ concatenates expressions - \\u{063}', () => testScriptStdout(() => $`echo.js \u{063}${'a'}\u{063} b`, 'cac\nb'));262test('$ handles tokens - \\u{0063}', () => testScriptStdout(() => $`echo.js \u{0063}`, 'c'));263test('$ splits tokens - \\u{0063}', () => testScriptStdout(() => $`echo.js a\u{0063}b`, 'acb'));264test('$ splits expressions - \\u{0063}', () => testScriptStdout(() => $`echo.js ${'a'}\u{0063}${'b'}`, 'acb'));265test('$ concatenates tokens - \\u{0063}', () => testScriptStdout(() => $`echo.js \u{0063}a\u{0063} b`, 'cac\nb'));266test('$ concatenates expressions - \\u{0063}', () => testScriptStdout(() => $`echo.js \u{0063}${'a'}\u{0063} b`, 'cac\nb'));267test('$ handles tokens - \\u{00063}', () => testScriptStdout(() => $`echo.js \u{00063}`, 'c'));268test('$ splits tokens - \\u{00063}', () => testScriptStdout(() => $`echo.js a\u{00063}b`, 'acb'));269test('$ splits expressions - \\u{00063}', () => testScriptStdout(() => $`echo.js ${'a'}\u{00063}${'b'}`, 'acb'));270test('$ concatenates tokens - \\u{00063}', () => testScriptStdout(() => $`echo.js \u{00063}a\u{00063} b`, 'cac\nb'));271test('$ concatenates expressions - \\u{00063}', () => testScriptStdout(() => $`echo.js \u{00063}${'a'}\u{00063} b`, 'cac\nb'));272test('$ handles tokens - \\u{000063}', () => testScriptStdout(() => $`echo.js \u{000063}`, 'c'));273test('$ splits tokens - \\u{000063}', () => testScriptStdout(() => $`echo.js a\u{000063}b`, 'acb'));274test('$ splits expressions - \\u{000063}', () => testScriptStdout(() => $`echo.js ${'a'}\u{000063}${'b'}`, 'acb'));275test('$ concatenates tokens - \\u{000063}', () => testScriptStdout(() => $`echo.js \u{000063}a\u{000063} b`, 'cac\nb'));276test('$ concatenates expressions - \\u{000063}', () => testScriptStdout(() => $`echo.js \u{000063}${'a'}\u{000063} b`, 'cac\nb'));277test('$ handles tokens - \\u{0000063}', () => testScriptStdout(() => $`echo.js \u{0000063}`, 'c'));278test('$ splits tokens - \\u{0000063}', () => testScriptStdout(() => $`echo.js a\u{0000063}b`, 'acb'));279test('$ splits expressions - \\u{0000063}', () => testScriptStdout(() => $`echo.js ${'a'}\u{0000063}${'b'}`, 'acb'));280test('$ concatenates tokens - \\u{0000063}', () => testScriptStdout(() => $`echo.js \u{0000063}a\u{0000063} b`, 'cac\nb'));281test('$ concatenates expressions - \\u{0000063}', () => testScriptStdout(() => $`echo.js \u{0000063}${'a'}\u{0000063} b`, 'cac\nb'));282283// Code points above the BMP are a surrogate pair, i.e. two characters, in the template string but not in its raw version.284// Miscounting them shifts every following character, which splits tokens at the wrong place and breaks the surrogate pair.285test('$ handles tokens - \\u{1F600}', () => testScriptStdout(() => $`echo.js \u{1F600}`, '\u{1F600}'));286test('$ splits tokens - \\u{1F600}', () => testScriptStdout(() => $`echo.js a\u{1F600}b`, 'a\u{1F600}b'));287test('$ splits expressions - \\u{1F600}', () => testScriptStdout(() => $`echo.js ${'a'}\u{1F600}${'b'}`, 'a\u{1F600}b'));288test('$ concatenates tokens - \\u{1F600}', () => testScriptStdout(() => $`echo.js \u{1F600}a\u{1F600} b`, '\u{1F600}a\u{1F600}\nb'));289test('$ concatenates expressions - \\u{1F600}', () => testScriptStdout(() => $`echo.js \u{1F600}${'a'}\u{1F600} b`, '\u{1F600}a\u{1F600}\nb'));290test('$ handles multiple tokens - \\u{1F600}', () => testScriptStdout(() => $`echo.js \u{1F600}\u{1F600} b c`, '\u{1F600}\u{1F600}\nb\nc'));291// Node.js 22 aborts when spawning with that code point on Windows, due to a libuv bug fixed in Node.js 24292if (!isWindows || majorNodeVersion >= 24) {293 test('$ handles the highest code point - \\u{10FFFF}', () => testScriptStdout(() => $`echo.js \u{10FFFF} b`, '\u{10FFFF}\nb'));294}295296// The BMP boundary is the exact point where the character count changes297test('$ handles the last BMP code point - \\u{FFFF}', () => testScriptStdout(() => $`echo.js \u{FFFF} b`, '\u{FFFF}\nb'));298test('$ handles the first astral code point - \\u{10000}', () => testScriptStdout(() => $`echo.js \u{10000} b`, '\u{10000}\nb'));299test('$ handles tokens - \\u{0063}}', () => testScriptStdout(() => $`echo.js \u{0063}}`, 'c}'));300test('$ splits tokens - \\u{0063}}', () => testScriptStdout(() => $`echo.js a\u{0063}}b`, 'ac}b'));301test('$ splits expressions - \\u{0063}}', () => testScriptStdout(() => $`echo.js ${'a'}\u{0063}}${'b'}`, 'ac}b'));302test('$ concatenates tokens - \\u{0063}}', () => testScriptStdout(() => $`echo.js \u{0063}}a\u{0063}} b`, 'c}ac}\nb'));303test('$ concatenates expressions - \\u{0063}}', () => testScriptStdout(() => $`echo.js \u{0063}}${'a'}\u{0063}} b`, 'c}ac}\nb'));304305const testScriptErrorStdout = async getSubprocess => {306 assertThrows(getSubprocess, {message: /null bytes/});307};308309test('$ handles tokens - \\0', () => testScriptErrorStdout(() => $`echo.js \0`));310test('$ splits tokens - \\0', () => testScriptErrorStdout(() => $`echo.js a\0b`));311test('$ splits expressions - \\0', () => testScriptErrorStdout(() => $`echo.js ${'a'}\0${'b'}`));312test('$ concatenates tokens - \\0', () => testScriptErrorStdout(() => $`echo.js \0a\0 b`));313test('$ concatenates expressions - \\0', () => testScriptErrorStdout(() => $`echo.js \0${'a'}\0 b`));314315const testReturnInterpolate = async (getSubprocess, expectedStdout, options = {}) => {316 const foo = await $(options)`echo.js foo`;317 const {stdout} = await getSubprocess(foo);318 assert.equal(stdout, expectedStdout);319};320321test('$ allows execa return value interpolation', () => testReturnInterpolate(foo => $`echo.js ${foo} bar`, 'foo\nbar'));322test('$ allows execa return value buffer interpolation', () => testReturnInterpolate(foo => $`echo.js ${foo} bar`, 'foo\nbar', {encoding: 'buffer'}));323test('$ allows execa return value array interpolation', () => testReturnInterpolate(foo => $`echo.js ${[foo, 'bar']}`, 'foo\nbar'));324test('$ allows execa return value buffer array interpolation', () => testReturnInterpolate(foo => $`echo.js ${[foo, 'bar']}`, 'foo\nbar', {encoding: 'buffer'}));325326const testReturnInterpolateSync = (getSubprocess, expectedStdout, options = {}) => {327 const foo = $(options).sync`echo.js foo`;328 const {stdout} = getSubprocess(foo);329 assert.equal(stdout, expectedStdout);330};331332test('$.sync allows execa return value interpolation', () => testReturnInterpolateSync(foo => $.sync`echo.js ${foo} bar`, 'foo\nbar'));333test('$.sync allows execa return value buffer interpolation', () => testReturnInterpolateSync(foo => $.sync`echo.js ${foo} bar`, 'foo\nbar', {encoding: 'buffer'}));334test('$.sync allows execa return value array interpolation', () => testReturnInterpolateSync(foo => $.sync`echo.js ${[foo, 'bar']}`, 'foo\nbar'));335test('$.sync allows execa return value buffer array interpolation', () => testReturnInterpolateSync(foo => $.sync`echo.js ${[foo, 'bar']}`, 'foo\nbar', {encoding: 'buffer'}));336337// A failed result is an error instance, not a plain object338const testErrorInterpolate = async (getError, getSubprocess) => {339 const error = await getError();340 assert.equal(error.failed, true);341 const {stdout} = await getSubprocess(error);342 assert.equal(stdout, `${foobarString}\nbar`);343};344345test('$ allows execa error interpolation, reject: false', () => testErrorInterpolate(() => $({reject: false})`noop-fail.js 1 ${foobarString}`, error => $`echo.js ${error} bar`));346test('$ allows execa error interpolation, rejected', () => testErrorInterpolate(() => $`noop-fail.js 1 ${foobarString}`.catch(error => error), error => $`echo.js ${error} bar`));347test('$ allows execa error array interpolation', () => testErrorInterpolate(() => $({reject: false})`noop-fail.js 1 ${foobarString}`, error => $`echo.js ${[error, 'bar']}`));348test('$ allows execa error buffer interpolation', () => testErrorInterpolate(() => $({reject: false, encoding: 'buffer'})`noop-fail.js 1 ${foobarString}`, error => $`echo.js ${error} bar`));349test('$.sync allows execa error interpolation, reject: false', () => testErrorInterpolate(() => $({reject: false}).sync`noop-fail.js 1 ${foobarString}`, error => $.sync`echo.js ${error} bar`));350test('$.sync allows execa error interpolation, thrown', () => testErrorInterpolate(() => assertThrows(() => $.sync`noop-fail.js 1 ${foobarString}`), error => $.sync`echo.js ${error} bar`));351352const testInvalidSequence = getSubprocess => {353 assertThrows(getSubprocess, {message: /Invalid backslash sequence/});354};355356test('$ handles invalid escape sequence - \\1', () => testInvalidSequence(() => $`echo.js \1`));357test('$ handles invalid escape sequence - \\u', () => testInvalidSequence(() => $`echo.js \u`));358test('$ handles invalid escape sequence - \\u0', () => testInvalidSequence(() => $`echo.js \u0`));359test('$ handles invalid escape sequence - \\u00', () => testInvalidSequence(() => $`echo.js \u00`));360test('$ handles invalid escape sequence - \\u000', () => testInvalidSequence(() => $`echo.js \u000`));361test('$ handles invalid escape sequence - \\ug', () => testInvalidSequence(() => $`echo.js \ug`));362test('$ handles invalid escape sequence - \\u{', () => testInvalidSequence(() => $`echo.js \u{`));363test('$ handles invalid escape sequence - \\u{0000', () => testInvalidSequence(() => $`echo.js \u{0000`));364test('$ handles invalid escape sequence - \\u{g}', () => testInvalidSequence(() => $`echo.js \u{g}`));365366test('$ handles invalid escape sequence - \\x', () => testInvalidSequence(() => $`echo.js \x`));367test('$ handles invalid escape sequence - \\x0', () => testInvalidSequence(() => $`echo.js \x0`));368test('$ handles invalid escape sequence - \\xgg', () => testInvalidSequence(() => $`echo.js \xgg`));369370const testEmptyScript = getSubprocess => {371 assertThrows(getSubprocess, {message: /Template script must not be empty/});372};373374test('$``', () => testEmptyScript(() => $``));375test('$` `', () => testEmptyScript(() => $` `));376test('$` ` (2 spaces)', () => testEmptyScript(() => $` `));377test('$`\\t`', () => testEmptyScript(() => $` `));378test('$`\\n`', () => testEmptyScript(() => $`379`));380381const testInvalidExpression = invalidExpression => {382 assertThrows(383 () => $`echo.js ${invalidExpression}`,384 {message: /in template expression/},385 );386};387388test('$ throws on invalid expression - undefined', () => testInvalidExpression(undefined));389test('$ throws on invalid expression - null mentions null', () => {390 assertThrows(391 () => $`echo.js ${null}`,392 {message: 'Unexpected "null" in template expression'},393 );394});395test('$ throws on invalid expression - null', () => testInvalidExpression(null));396test('$ throws on invalid expression - true', () => testInvalidExpression(true));397test('$ throws on invalid expression - {}', () => testInvalidExpression({}));398test('$ throws on invalid expression - {foo: "bar"}', () => testInvalidExpression({foo: 'bar'}));399test('$ throws on invalid expression - {stdout: 1}', () => testInvalidExpression({stdout: 1}));400test('$ throws on invalid expression - [undefined]', () => testInvalidExpression([undefined]));401test('$ throws on invalid expression - [null]', () => testInvalidExpression([null]));402test('$ throws on invalid expression - [true]', () => testInvalidExpression([true]));403test('$ throws on invalid expression - [{}]', () => testInvalidExpression([{}]));404test('$ throws on invalid expression - [{foo: "bar"}]', () => testInvalidExpression([{foo: 'bar'}]));405test('$ throws on invalid expression - [{stdout: 1}]', () => testInvalidExpression([{stdout: 1}]));406test('$ throws on invalid expression - new Error()', () => testInvalidExpression(new Error(foobarString)));407test('$ throws on invalid expression - Object.assign(new Error(), {stdout})', () => testInvalidExpression(Object.assign(new Error(foobarString), {stdout: foobarString})));408409const testMissingOutput = invalidExpression => {410 assertThrows(411 () => $`echo.js ${invalidExpression()}`,412 {message: /Missing result.stdout/},413 );414};415416test('$ throws on invalid expression - {stdout: undefined}', () => testMissingOutput(() => ({stdout: undefined})));417test('$ throws on invalid expression - [{stdout: undefined}]', () => testMissingOutput(() => [{stdout: undefined}]));418test('$ throws on invalid expression - $(options).sync', () => testMissingOutput(() => $({stdio: 'ignore'}).sync`noop.js`));419test('$ throws on invalid expression - [$(options).sync]', () => testMissingOutput(() => [$({stdio: 'ignore'}).sync`noop.js`]));420421const testInvalidPromise = invalidExpression => {422 assertThrows(423 () => $`echo.js ${invalidExpression()}`,424 {message: /Please use \${await subprocess}/},425 );426};427428test('$ throws on invalid expression - Promise.resolve()', () => testInvalidPromise(async () => ({})));429test('$ throws on invalid expression - Promise.resolve({stdout: "foo"})', () => testInvalidPromise(async () => ({foo: 'bar'})));430test('$ throws on invalid expression - [Promise.resolve()]', () => testInvalidPromise(() => [Promise.resolve()]));431test('$ throws on invalid expression - [Promise.resolve({stdout: "foo"})]', () => testInvalidPromise(() => [Promise.resolve({stdout: 'foo'})]));432test('$ throws on invalid expression - $', () => testInvalidPromise(() => $`noop.js`));433test('$ throws on invalid expression - [$]', () => testInvalidPromise(() => [$`noop.js`]));