command.js
This file
Path
test/command.js
290 lines · 9.6 KiB · JavaScript
Last changed by
Fix default value of `stdin` with `$` (#550)ehmickybefore adoption6fe7e51
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});115Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
line 116why, one step back116test('$ allows execa return value interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 117–118why, one step back117 const foo = await $`echo.js foo`;118 const {stdout} = await $`echo.js ${foo} bar`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 119–122why, one step back119 t.is(stdout, 'foo\nbar');120});121122test('$ allows execa return value array interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 123–124why, one step back123 const foo = await $`echo.js foo`;124 const {stdout} = await $`echo.js ${[foo, 'bar']}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 125–128why, one step back125 t.is(stdout, 'foo\nbar');126});127128test('$ allows execa return value buffer interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 129–130why, one step back129 const foo = await $({encoding: null})`echo.js foo`;130 const {stdout} = await $`echo.js ${foo} bar`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 131–134why, one step back131 t.is(stdout, 'foo\nbar');132});133134test('$ allows execa return value buffer array interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 135–136why, one step back135 const foo = await $({encoding: null})`echo.js foo`;136 const {stdout} = await $`echo.js ${[foo, 'bar']}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 137–139why, one step back137 t.is(stdout, 'foo\nbar');138});139Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
line 140why, one step back140test('$ ignores consecutive spaces', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 141why, one step back141 const {stdout} = await $`echo.js foo bar`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 142–145why, one step back142 t.is(stdout, 'foo\nbar');143});144145test('$ allows escaping spaces with interpolation', 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 bar');148});149150test('$ disallows escaping spaces with backslashes', 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\\\nbar');153});154155test('$ allows space escaped values in array interpolation', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 156why, one step back156 const {stdout} = await $`echo.js ${['foo', 'bar baz']}`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 157–160why, one step back157 t.is(stdout, 'foo\nbar baz');158});159160test('$ passes newline escape sequence as one argument', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 161why, one step back161 const {stdout} = await $`echo.js \n`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 162–165why, one step back162 t.is(stdout, '\n');163});164165test('$ passes newline escape sequence in interpolation 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('$ handles invalid escape sequence', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 171why, one step back171 const {stdout} = await $`echo.js \u`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 172–180why, one step back172 t.is(stdout, '\\u');173});174175test('$ allows escaping spaces in commands with interpolation', async t => {176 const {stdout} = await $`${'command with space.js'} foo bar`;177 t.is(stdout, 'foo\nbar');178});179180test('$ escapes other whitespaces', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 181why, one step back181 const {stdout} = await $`echo.js foo\tbar`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 182–185why, one step back182 t.is(stdout, 'foo\tbar');183});184185test('$ trims', async t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 186why, one step back186 const {stdout} = await $` echo.js foo bar `;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 187–190why, one step back187 t.is(stdout, 'foo\nbar');188});189190test('$.sync', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
line 191why, one step back191 const {stdout} = $.sync`echo.js foo bar`;Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c
lines 192–197why, one step back192 t.is(stdout, 'foo\nbar');193});194195test('$.sync accepts options', t => {196 const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;197 t.is(stdout, 'foo');Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b
line 198why, one step back198});Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
line 199why, one step back199Improve validation of `$.sync` options binding (#551)ehmickybefore adoption881fbad
lines 200–203why, one step back200test('$.sync must be used after options binding, not before', t => {201 t.throws(() => $.sync({})`noop.js`, {message: /Please use/});202});203Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
line 204why, one step back204test('$.sync allows execa return value interpolation', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 205–206why, one step back205 const foo = $.sync`echo.js foo`;206 const {stdout} = $.sync`echo.js ${foo} bar`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 207–210why, one step back207 t.is(stdout, 'foo\nbar');208});209210test('$.sync allows execa return value array interpolation', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 211–212why, one step back211 const foo = $.sync`echo.js foo`;212 const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 213–216why, one step back213 t.is(stdout, 'foo\nbar');214});215216test('$.sync allows execa return value buffer interpolation', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 217–218why, one step back217 const foo = $({encoding: null}).sync`echo.js foo`;218 const {stdout} = $.sync`echo.js ${foo} bar`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 219–222why, one step back219 t.is(stdout, 'foo\nbar');220});221222test('$.sync allows execa return value buffer array interpolation', t => {Simplify tests (#552)ehmickybefore adoption5fa61d8
lines 223–224why, one step back223 const foo = $({encoding: null}).sync`echo.js foo`;224 const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 225–230why, one step back225 t.is(stdout, 'foo\nbar');226});227228const invalidExpression = test.macro({229 async exec(t, input, expected) {230 await t.throwsAsync(Simplify tests (#552)ehmickybefore adoption5fa61d8
line 231why, one step back231 async () => $`echo.js ${input}`,Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 232–235why, one step back232 {instanceOf: TypeError, message: expected},233 );234235 t.throws(Simplify tests (#552)ehmickybefore adoption5fa61d8
line 236why, one step back236 () => $.sync`echo.js ${input}`,Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251
lines 237–276why, one step back237 {instanceOf: TypeError, message: expected},238 );239 },240 title(prettyInput, input, expected) {241 return `$ APIs throw on invalid '${prettyInput ?? inspect(input)}' expression with '${expected}'`;242 },243});244245test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');246test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');247248test(invalidExpression, null, 'Unexpected "object" in template expression');249test(invalidExpression, [null], 'Unexpected "object" in template expression');250251test(invalidExpression, true, 'Unexpected "boolean" in template expression');252test(invalidExpression, [true], 'Unexpected "boolean" in template expression');253254test(invalidExpression, {}, 'Unexpected "object" in template expression');255test(invalidExpression, [{}], 'Unexpected "object" in template expression');256257test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');258test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');259260test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');261test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');262263test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');264test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');265266test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');267test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');268269test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');270test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');271272test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');273test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');274275test('$({stdio: \'inherit\'}).sync`noop.js`', invalidExpression, $({stdio: 'inherit'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');276test('[ $({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 277–290why, one step back277278test('$ stdin defaults to "inherit"', async t => {279 const {stdout} = await $({input: 'foo'})`stdin-script.js`;280 t.is(stdout, 'foo');281});282283test('$.sync stdin defaults to "inherit"', t => {284 const {stdout} = $({input: 'foo'}).sync`stdin-script.js`;285 t.is(stdout, 'foo');286});287288test('$ stdin has no default value when stdio is set', t => {289 t.true(isStream($({stdio: 'pipe'})`noop.js`.stdin));290});