command.js
This file
Path
test/command.js
335 lines · 11.0 KiB · JavaScript
Last changed by
Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
History of this fileTake it
Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
line 1why, one step back1import {inspect} from 'node:util';Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 2why, one step back2import test from 'ava';Fix default value of `stdin` with `$` (#550)ehmickybefore adoption6fe7e51
line 3why, one step back3import {isStream} from 'is-stream';Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
line 4why, one step back4import {execa, execaSync, execaCommand, execaCommandSync, $} from '../index.js';Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2
line 5why, one step back5import {setFixtureDir} from './helpers/fixtures-dir.js';Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 6why, one step back6Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2
line 7why, one step back7setFixtureDir();Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 8–9why, one step back89const command = async (t, expected, ...args) => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 10–11why, one step back10 const {command: failCommand} = await t.throwsAsync(execa('fail.js', args));11 t.is(failCommand, `fail.js${expected}`);Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 12why, one step back12Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 13–14why, one step back13 const {command} = await execa('noop.js', args);14 t.is(command, `noop.js${expected}`);Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 15–22why, one step back15};1617command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;1819test(command, ' foo bar', 'foo', 'bar');20test(command, ' baz quz', 'baz', 'quz');21test(command, '');22Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 23why, one step back23const testEscapedCommand = async (t, expected, args) => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 24–25why, one step back24 const {escapedCommand: failEscapedCommand} = await t.throwsAsync(execa('fail.js', args));25 t.is(failEscapedCommand, `fail.js ${expected}`);Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 26–27why, one step back2627 const {escapedCommand: failEscapedCommandSync} = t.throws(() => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 28why, one step back28 execaSync('fail.js', args);Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 29why, one step back29 });Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 30why, one step back30 t.is(failEscapedCommandSync, `fail.js ${expected}`);Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 31why, one step back31Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 32–33why, one step back32 const {escapedCommand} = await execa('noop.js', args);33 t.is(escapedCommand, `noop.js ${expected}`);Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 34why, one step back34Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 35–36why, one step back35 const {escapedCommand: escapedCommandSync} = execaSync('noop.js', args);36 t.is(escapedCommandSync, `noop.js ${expected}`);Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
lines 37–45why, one step back37};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, '"*"', ['*']);45Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 46why, one step back46test('allow commands with spaces and no array arguments', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 47why, one step back47 const {stdout} = await execa('command with space.js');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 48–51why, one step back48 t.is(stdout, '');49});5051test('allow commands with spaces and array arguments', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 52why, one step back52 const {stdout} = await execa('command with space.js', ['foo', 'bar']);Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 53–55why, one step back53 t.is(stdout, 'foo\nbar');54});55Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 56why, one step back56test('execaCommand()', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 57why, one step back57 const {stdout} = await execaCommand('echo.js foo bar');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 58–60why, one step back58 t.is(stdout, 'foo\nbar');59});60Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 61why, one step back61test('execaCommand() ignores consecutive spaces', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 62why, one step back62 const {stdout} = await execaCommand('echo.js foo bar');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 63–65why, one step back63 t.is(stdout, 'foo\nbar');64});65Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 66–67why, one step back66test('execaCommand() allows escaping spaces in commands', async t => {67 const {stdout} = await execaCommand('command\\ with\\ space.js foo bar');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 68–70why, one step back68 t.is(stdout, 'foo\nbar');69});70Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 71why, one step back71test('execaCommand() allows escaping spaces in arguments', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 72why, one step back72 const {stdout} = await execaCommand('echo.js foo\\ bar');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 73–75why, one step back73 t.is(stdout, 'foo bar');74});75Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 76why, one step back76test('execaCommand() escapes other whitespaces', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 77why, one step back77 const {stdout} = await execaCommand('echo.js foo\tbar');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 78–80why, one step back78 t.is(stdout, 'foo\tbar');79});80Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 81why, one step back81test('execaCommand() trims', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 82why, one step back82 const {stdout} = await execaCommand(' echo.js foo bar ');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
lines 83–85why, one step back83 t.is(stdout, 'foo\nbar');84});85Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 86why, one step back86test('execaCommandSync()', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 87why, one step back87 const {stdout} = execaCommandSync('echo.js foo bar');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 88why, one step back88 t.is(stdout, 'foo\nbar');Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 89–91why, one step back89});9091test('$', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 92why, one step back92 const {stdout} = await $`echo.js foo bar`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 93–101why, one step back93 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 => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 102why, one step back102 const {stdout} = await $`echo.js foo ${'bar'}`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 103–105why, one step back103 t.is(stdout, 'foo\nbar');104});105Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
line 106why, one step back106test('$ allows number interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 107why, one step back107 const {stdout} = await $`echo.js 1 ${2}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 108–110why, one step back108 t.is(stdout, '1\n2');109});110Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
line 111why, one step back111test('$ allows array interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 112why, one step back112 const {stdout} = await $`echo.js ${['foo', 'bar']}`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 113–115why, one step back113 t.is(stdout, 'foo\nbar');114});115Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 116–120why, one step back116test('$ allows empty array interpolation', async t => {117 const {stdout} = await $`echo.js foo ${[]} bar`;118 t.is(stdout, 'foo\nbar');119});120Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
line 121why, one step back121test('$ allows execa return value interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 122–123why, one step back122 const foo = await $`echo.js foo`;123 const {stdout} = await $`echo.js ${foo} bar`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 124–127why, one step back124 t.is(stdout, 'foo\nbar');125});126127test('$ allows execa return value array interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 128–129why, one step back128 const foo = await $`echo.js foo`;129 const {stdout} = await $`echo.js ${[foo, 'bar']}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 130–133why, one step back130 t.is(stdout, 'foo\nbar');131});132133test('$ allows execa return value buffer interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 134–135why, one step back134 const foo = await $({encoding: null})`echo.js foo`;135 const {stdout} = await $`echo.js ${foo} bar`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 136–139why, one step back136 t.is(stdout, 'foo\nbar');137});138139test('$ allows execa return value buffer array interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 140–141why, one step back140 const foo = await $({encoding: null})`echo.js foo`;141 const {stdout} = await $`echo.js ${[foo, 'bar']}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 142–144why, one step back142 t.is(stdout, 'foo\nbar');143});144Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
line 145why, one step back145test('$ ignores consecutive spaces', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 146why, one step back146 const {stdout} = await $`echo.js foo bar`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 147–150why, one step back147 t.is(stdout, 'foo\nbar');148});149150test('$ allows escaping spaces with interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 151why, one step back151 const {stdout} = await $`echo.js ${'foo bar'}`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 152–155why, one step back152 t.is(stdout, 'foo bar');153});154155test('$ disallows escaping spaces with backslashes', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 156why, one step back156 const {stdout} = await $`echo.js foo\\ bar`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 157–160why, one step back157 t.is(stdout, 'foo\\\nbar');158});159160test('$ allows space escaped values in array interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 161why, one step back161 const {stdout} = await $`echo.js ${['foo', 'bar baz']}`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 162–165why, one step back162 t.is(stdout, 'foo\nbar baz');163});164165test('$ passes newline escape sequence as one argument', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 166why, one step back166 const {stdout} = await $`echo.js \n`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 167–170why, one step back167 t.is(stdout, '\n');168});169170test('$ passes newline escape sequence in interpolation as one argument', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 171why, one step back171 const {stdout} = await $`echo.js ${'\n'}`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 172–175why, one step back172 t.is(stdout, '\n');173});174175test('$ handles invalid escape sequence', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 176why, one step back176 const {stdout} = await $`echo.js \u`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
line 177why, one step back177 t.is(stdout, '\\u');Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0
lines 178–217why, one step back178});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');Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 218–225why, one step back218});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 => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 226why, one step back226 const {stdout} = await $`echo.js foo\tbar`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 227–230why, one step back227 t.is(stdout, 'foo\tbar');228});229230test('$ trims', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 231why, one step back231 const {stdout} = await $` echo.js foo bar `;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 232–235why, one step back232 t.is(stdout, 'foo\nbar');233});234235test('$.sync', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 236why, one step back236 const {stdout} = $.sync`echo.js foo bar`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 237–242why, one step back237 t.is(stdout, 'foo\nbar');238});239240test('$.sync accepts options', t => {241 const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;242 t.is(stdout, 'foo');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 243why, one step back243});Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
line 244why, one step back244Improve validation of `$.sync` options binding (#551)ehmickybefore adoption881fbad
lines 245–248why, one step back245test('$.sync must be used after options binding, not before', t => {246 t.throws(() => $.sync({})`noop.js`, {message: /Please use/});247});248Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
line 249why, one step back249test('$.sync allows execa return value interpolation', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 250–251why, one step back250 const foo = $.sync`echo.js foo`;251 const {stdout} = $.sync`echo.js ${foo} bar`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 252–255why, one step back252 t.is(stdout, 'foo\nbar');253});254255test('$.sync allows execa return value array interpolation', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 256–257why, one step back256 const foo = $.sync`echo.js foo`;257 const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 258–261why, one step back258 t.is(stdout, 'foo\nbar');259});260261test('$.sync allows execa return value buffer interpolation', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 262–263why, one step back262 const foo = $({encoding: null}).sync`echo.js foo`;263 const {stdout} = $.sync`echo.js ${foo} bar`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 264–267why, one step back264 t.is(stdout, 'foo\nbar');265});266267test('$.sync allows execa return value buffer array interpolation', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 268–269why, one step back268 const foo = $({encoding: null}).sync`echo.js foo`;269 const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 270–275why, one step back270 t.is(stdout, 'foo\nbar');271});272273const invalidExpression = test.macro({274 async exec(t, input, expected) {275 await t.throwsAsync(Simplify tests (#552)ehmickybefore adoption5fa61d8
line 276why, one step back276 async () => $`echo.js ${input}`,Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 277–280why, one step back277 {instanceOf: TypeError, message: expected},278 );279280 t.throws(Simplify tests (#552)ehmickybefore adoption5fa61d8
line 281why, one step back281 () => $.sync`echo.js ${input}`,Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 282–321why, one step back282 {instanceOf: TypeError, message: expected},283 );284 },285 title(prettyInput, input, expected) {286 return `$ APIs throw on invalid '${prettyInput ?? inspect(input)}' expression with '${expected}'`;287 },288});289290test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');291test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');292293test(invalidExpression, null, 'Unexpected "object" in template expression');294test(invalidExpression, [null], 'Unexpected "object" in template expression');295296test(invalidExpression, true, 'Unexpected "boolean" in template expression');297test(invalidExpression, [true], 'Unexpected "boolean" in template expression');298299test(invalidExpression, {}, 'Unexpected "object" in template expression');300test(invalidExpression, [{}], 'Unexpected "object" in template expression');301302test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');303test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');304305test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');306test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');307308test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');309test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');310311test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');312test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');313314test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');315test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');316317test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');318test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');319320test('$({stdio: \'inherit\'}).sync`noop.js`', invalidExpression, $({stdio: 'inherit'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');321test('[ $({stdio: \'inherit\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'inherit'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');Fix default value of `stdin` with `$` (#550)ehmickybefore adoption6fe7e51
lines 322–335why, one step back322323test('$ stdin defaults to "inherit"', async t => {324 const {stdout} = await $({input: 'foo'})`stdin-script.js`;325 t.is(stdout, 'foo');326});327328test('$.sync stdin defaults to "inherit"', t => {329 const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;330 t.is(stdout, 'foo');331});332333test('$ stdin has no default value when stdio is set', t => {334 t.true(isStream($({stdio: 'pipe'})`noop.js`.stdin));335});