Skip to content
Myr

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 file

Take it

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

line 1why, one step back
1import {inspect} from 'node:util';

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

line 2why, one step back
2import test from 'ava';

Fix default value of `stdin` with `$` (#550)ehmickybefore adoption6fe7e51

line 3why, one step back
3import {isStream} from 'is-stream';

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

line 4why, one step back
4import {execa, execaSync, execaCommand, execaCommandSync, $} from '../index.js';

Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2

line 5why, one step back
5import {setFixtureDir} from './helpers/fixtures-dir.js';

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

line 6why, one step back
6

Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2

line 7why, one step back
7setFixtureDir();

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

lines 8–9why, one step back
8
9const command = async (t, expected, ...args) => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 10–11why, one step back
10 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 back
12

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 13–14why, one step back
13 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 back
15};
1617command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;1819test(command, ' foo bar', 'foo', 'bar');20test(command, ' baz quz', 'baz', 'quz');21test(command, '');22

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 23why, one step back
23const testEscapedCommand = async (t, expected, args) => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 24–25why, one step back
24 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 back
26
27 const {escapedCommand: failEscapedCommandSync} = t.throws(() => {

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 28why, one step back
28 execaSync('fail.js', args);

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 29why, one step back
29 });

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 30why, one step back
30 t.is(failEscapedCommandSync, `fail.js ${expected}`);

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 31why, one step back
31

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 32–33why, one step back
32 const {escapedCommand} = await execa('noop.js', args);
33 t.is(escapedCommand, `noop.js ${expected}`);

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 34why, one step back
34

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 35–36why, one step back
35 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 back
37};
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, '"*"', ['*']);45

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

line 46why, one step back
46test('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 back
47 const {stdout} = await execa('command with space.js');

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

lines 48–51why, one step back
48 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 back
52 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 back
53 t.is(stdout, 'foo\nbar');
54});55

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 56why, one step back
56test('execaCommand()', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 57why, one step back
57 const {stdout} = await execaCommand('echo.js foo bar');

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

lines 58–60why, one step back
58 t.is(stdout, 'foo\nbar');
59});60

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 61why, one step back
61test('execaCommand() ignores consecutive spaces', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 62why, one step back
62 const {stdout} = await execaCommand('echo.js foo bar');

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

lines 63–65why, one step back
63 t.is(stdout, 'foo\nbar');
64});65

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 66–67why, one step back
66test('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 back
68 t.is(stdout, 'foo\nbar');
69});70

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 71why, one step back
71test('execaCommand() allows escaping spaces in arguments', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 72why, one step back
72 const {stdout} = await execaCommand('echo.js foo\\ bar');

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

lines 73–75why, one step back
73 t.is(stdout, 'foo bar');
74});75

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 76why, one step back
76test('execaCommand() escapes other whitespaces', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 77why, one step back
77 const {stdout} = await execaCommand('echo.js foo\tbar');

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

lines 78–80why, one step back
78 t.is(stdout, 'foo\tbar');
79});80

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 81why, one step back
81test('execaCommand() trims', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 82why, one step back
82 const {stdout} = await execaCommand(' echo.js foo bar ');

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

lines 83–85why, one step back
83 t.is(stdout, 'foo\nbar');
84});85

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 86why, one step back
86test('execaCommandSync()', t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 87why, one step back
87 const {stdout} = execaCommandSync('echo.js foo bar');

Separate command-related logic into its own file (#329)ehmickybefore adoption46daa7b

line 88why, one step back
88 t.is(stdout, 'foo\nbar');

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 89–91why, one step back
89});
9091test('$', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 92why, one step back
92 const {stdout} = await $`echo.js foo bar`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 93–101why, one step back
93 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 back
102 const {stdout} = await $`echo.js foo ${'bar'}`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 103–105why, one step back
103 t.is(stdout, 'foo\nbar');
104});105

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

line 106why, one step back
106test('$ allows number interpolation', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 107why, one step back
107 const {stdout} = await $`echo.js 1 ${2}`;

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

lines 108–110why, one step back
108 t.is(stdout, '1\n2');
109});110

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

line 111why, one step back
111test('$ allows array interpolation', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 112why, one step back
112 const {stdout} = await $`echo.js ${['foo', 'bar']}`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 113–115why, one step back
113 t.is(stdout, 'foo\nbar');
114});115

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

line 116why, one step back
116test('$ allows execa return value interpolation', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

lines 117–118why, one step back
117 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 back
119 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 back
123 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 back
125 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 back
129 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 back
131 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 back
135 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 back
137 t.is(stdout, 'foo\nbar');
138});139

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

line 140why, one step back
140test('$ ignores consecutive spaces', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 141why, one step back
141 const {stdout} = await $`echo.js foo bar`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 142–145why, one step back
142 t.is(stdout, 'foo\nbar');
143});144145test('$ allows escaping spaces with interpolation', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 146why, one step back
146 const {stdout} = await $`echo.js ${'foo bar'}`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 147–150why, one step back
147 t.is(stdout, 'foo bar');
148});149150test('$ disallows escaping spaces with backslashes', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 151why, one step back
151 const {stdout} = await $`echo.js foo\\ bar`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 152–155why, one step back
152 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 back
156 const {stdout} = await $`echo.js ${['foo', 'bar baz']}`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 157–160why, one step back
157 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 back
161 const {stdout} = await $`echo.js \n`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 162–165why, one step back
162 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 back
166 const {stdout} = await $`echo.js ${'\n'}`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 167–170why, one step back
167 t.is(stdout, '\n');
168});169170test('$ handles invalid escape sequence', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 171why, one step back
171 const {stdout} = await $`echo.js \u`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 172–180why, one step back
172 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 back
181 const {stdout} = await $`echo.js foo\tbar`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 182–185why, one step back
182 t.is(stdout, 'foo\tbar');
183});184185test('$ trims', async t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 186why, one step back
186 const {stdout} = await $` echo.js foo bar `;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 187–190why, one step back
187 t.is(stdout, 'foo\nbar');
188});189190test('$.sync', t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 191why, one step back
191 const {stdout} = $.sync`echo.js foo bar`;

Add `$` tagged templates API (#510)Aaron Casanovabefore adoptioneae327c

lines 192–197why, one step back
192 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 back
198});

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

line 199why, one step back
199

Improve validation of `$.sync` options binding (#551)ehmickybefore adoption881fbad

lines 200–203why, one step back
200test('$.sync must be used after options binding, not before', t => {
201 t.throws(() => $.sync({})`noop.js`, {message: /Please use/});202});203

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

line 204why, one step back
204test('$.sync allows execa return value interpolation', t => {

Simplify tests (#552)ehmickybefore adoption5fa61d8

lines 205–206why, one step back
205 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 back
207 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 back
211 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 back
213 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 back
217 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 back
219 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 back
223 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 back
225 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 back
231 async () => $`echo.js ${input}`,

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

lines 232–235why, one step back
232 {instanceOf: TypeError, message: expected},
233 );234235 t.throws(

Simplify tests (#552)ehmickybefore adoption5fa61d8

line 236why, one step back
236 () => $.sync`echo.js ${input}`,

Allow `execa` return values in `$` template expressions (#528)Aaron Casanovabefore adoption58ea251

lines 237–276why, one step back
237 {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 back
277
278test('$ 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});