Skip to content
Myr

command.js

This file

Path

test/command.js

271 lines · 9.8 KiB · JavaScript

Last changed by

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

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';

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

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

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

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

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

line 5why, one step back
5

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

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

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

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

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

lines 9–10why, one step back
9 const {command: failCommand} = await t.throwsAsync(execa('fail.js', args));
10 t.is(failCommand, `fail.js${expected}`);

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

line 11why, one step back
11

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

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

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

lines 14–21why, one step back
14};
1516command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;1718test(command, ' foo bar', 'foo', 'bar');19test(command, ' baz quz', 'baz', 'quz');20test(command, '');21

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

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

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

lines 23–24why, one step back
23 const {escapedCommand: failEscapedCommand} = await t.throwsAsync(execa('fail.js', args));
24 t.is(failEscapedCommand, `fail.js ${expected}`);

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

lines 25–26why, one step back
25
26 const {escapedCommand: failEscapedCommandSync} = t.throws(() => {

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

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

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

line 28why, one step back
28 });

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

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

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

line 30why, one step back
30

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

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

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

line 33why, one step back
33

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

lines 34–35why, one step back
34 const {escapedCommand: escapedCommandSync} = execaSync('noop.js', args);
35 t.is(escapedCommandSync, `noop.js ${expected}`);

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

lines 36–44why, one step back
36};
3738testEscapedCommand.title = (message, expected) => `escapedCommand is: ${JSON.stringify(expected)}`;3940test(testEscapedCommand, 'foo bar', ['foo', 'bar']);41test(testEscapedCommand, '"foo bar"', ['foo bar']);42test(testEscapedCommand, '"\\"foo\\""', ['"foo"']);43test(testEscapedCommand, '"*"', ['*']);44

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

line 45why, one step back
45test('allow commands with spaces and no array arguments', async t => {

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

line 46why, one step back
46 const {stdout} = await execa('command with space.js');

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

lines 47–50why, one step back
47 t.is(stdout, '');
48});4950test('allow commands with spaces and array arguments', async t => {

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

line 51why, one step back
51 const {stdout} = await execa('command with space.js', ['foo', 'bar']);

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

lines 52–54why, one step back
52 t.is(stdout, 'foo\nbar');
53});54

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

lines 55–56why, one step back
55test('execaCommand()', async t => {
56 const {stdout} = await execaCommand('node test/fixtures/echo.js foo bar');

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

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

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

lines 60–61why, one step back
60test('execaCommand() ignores consecutive spaces', async t => {
61 const {stdout} = await execaCommand('node test/fixtures/echo.js foo bar');

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

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

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

lines 65–66why, one step back
65test('execaCommand() allows escaping spaces in commands', async t => {
66 const {stdout} = await execaCommand('command\\ with\\ space.js foo bar');

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

lines 67–69why, one step back
67 t.is(stdout, 'foo\nbar');
68});69

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

lines 70–71why, one step back
70test('execaCommand() allows escaping spaces in arguments', async t => {
71 const {stdout} = await execaCommand('node test/fixtures/echo.js foo\\ bar');

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

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

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

lines 75–76why, one step back
75test('execaCommand() escapes other whitespaces', async t => {
76 const {stdout} = await execaCommand('node test/fixtures/echo.js foo\tbar');

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

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

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

lines 80–81why, one step back
80test('execaCommand() trims', async t => {
81 const {stdout} = await execaCommand(' node test/fixtures/echo.js foo bar ');

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

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

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

lines 85–86why, one step back
85test('execaCommandSync()', t => {
86 const {stdout} = execaCommandSync('node test/fixtures/echo.js foo bar');

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

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

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

lines 88–104why, one step back
88});
8990test('$', async t => {91 const {stdout} = await $`node test/fixtures/echo.js foo bar`;92 t.is(stdout, 'foo\nbar');93});9495test('$ accepts options', async t => {96 const {stdout} = await $({stripFinalNewline: true})`noop.js foo`;97 t.is(stdout, 'foo');98});99100test('$ allows string interpolation', async t => {101 const {stdout} = await $`node test/fixtures/echo.js foo ${'bar'}`;102 t.is(stdout, 'foo\nbar');103});104

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

lines 105–109why, one step back
105test('$ allows number interpolation', async t => {
106 const {stdout} = await $`node test/fixtures/echo.js 1 ${2}`;107 t.is(stdout, '1\n2');108});109

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

lines 110–114why, one step back
110test('$ allows array interpolation', async t => {
111 const {stdout} = await $`node test/fixtures/echo.js ${['foo', 'bar']}`;112 t.is(stdout, 'foo\nbar');113});114

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

lines 115–138why, one step back
115test('$ allows execa return value interpolation', async t => {
116 const foo = await $`node test/fixtures/echo.js foo`;117 const {stdout} = await $`node test/fixtures/echo.js ${foo} bar`;118 t.is(stdout, 'foo\nbar');119});120121test('$ allows execa return value array interpolation', async t => {122 const foo = await $`node test/fixtures/echo.js foo`;123 const {stdout} = await $`node test/fixtures/echo.js ${[foo, 'bar']}`;124 t.is(stdout, 'foo\nbar');125});126127test('$ allows execa return value buffer interpolation', async t => {128 const foo = await $({encoding: null})`node test/fixtures/echo.js foo`;129 const {stdout} = await $`node test/fixtures/echo.js ${foo} bar`;130 t.is(stdout, 'foo\nbar');131});132133test('$ allows execa return value buffer array interpolation', async t => {134 const foo = await $({encoding: null})`node test/fixtures/echo.js foo`;135 const {stdout} = await $`node test/fixtures/echo.js ${[foo, 'bar']}`;136 t.is(stdout, 'foo\nbar');137});138

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

lines 139–196why, one step back
139test('$ ignores consecutive spaces', async t => {
140 const {stdout} = await $`node test/fixtures/echo.js foo bar`;141 t.is(stdout, 'foo\nbar');142});143144test('$ allows escaping spaces with interpolation', async t => {145 const {stdout} = await $`node test/fixtures/echo.js ${'foo bar'}`;146 t.is(stdout, 'foo bar');147});148149test('$ disallows escaping spaces with backslashes', async t => {150 const {stdout} = await $`node test/fixtures/echo.js foo\\ bar`;151 t.is(stdout, 'foo\\\nbar');152});153154test('$ allows space escaped values in array interpolation', async t => {155 const {stdout} = await $`node test/fixtures/echo.js ${['foo', 'bar baz']}`;156 t.is(stdout, 'foo\nbar baz');157});158159test('$ passes newline escape sequence as one argument', async t => {160 const {stdout} = await $`node test/fixtures/echo.js \n`;161 t.is(stdout, '\n');162});163164test('$ passes newline escape sequence in interpolation as one argument', async t => {165 const {stdout} = await $`node test/fixtures/echo.js ${'\n'}`;166 t.is(stdout, '\n');167});168169test('$ handles invalid escape sequence', async t => {170 const {stdout} = await $`node test/fixtures/echo.js \u`;171 t.is(stdout, '\\u');172});173174test('$ allows escaping spaces in commands with interpolation', async t => {175 const {stdout} = await $`${'command with space.js'} foo bar`;176 t.is(stdout, 'foo\nbar');177});178179test('$ escapes other whitespaces', async t => {180 const {stdout} = await $`node test/fixtures/echo.js foo\tbar`;181 t.is(stdout, 'foo\tbar');182});183184test('$ trims', async t => {185 const {stdout} = await $` node test/fixtures/echo.js foo bar `;186 t.is(stdout, 'foo\nbar');187});188189test('$.sync', t => {190 const {stdout} = $.sync`node test/fixtures/echo.js foo bar`;191 t.is(stdout, 'foo\nbar');192});193194test('$.sync accepts options', t => {195 const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;196 t.is(stdout, 'foo');

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

line 197why, one step back
197});

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

lines 198–271why, one step back
198
199test('$.sync allows execa return value interpolation', t => {200 const foo = $.sync`node test/fixtures/echo.js foo`;201 const {stdout} = $.sync`node test/fixtures/echo.js ${foo} bar`;202 t.is(stdout, 'foo\nbar');203});204205test('$.sync allows execa return value array interpolation', t => {206 const foo = $.sync`node test/fixtures/echo.js foo`;207 const {stdout} = $.sync`node test/fixtures/echo.js ${[foo, 'bar']}`;208 t.is(stdout, 'foo\nbar');209});210211test('$.sync allows execa return value buffer interpolation', t => {212 const foo = $({encoding: null}).sync`node test/fixtures/echo.js foo`;213 const {stdout} = $.sync`node test/fixtures/echo.js ${foo} bar`;214 t.is(stdout, 'foo\nbar');215});216217test('$.sync allows execa return value buffer array interpolation', t => {218 const foo = $({encoding: null}).sync`node test/fixtures/echo.js foo`;219 const {stdout} = $.sync`node test/fixtures/echo.js ${[foo, 'bar']}`;220 t.is(stdout, 'foo\nbar');221});222223const invalidExpression = test.macro({224 async exec(t, input, expected) {225 await t.throwsAsync(226 async () => $`node test/fixtures/echo.js ${input}`,227 {instanceOf: TypeError, message: expected},228 );229230 t.throws(231 () => $.sync`node test/fixtures/echo.js ${input}`,232 {instanceOf: TypeError, message: expected},233 );234 },235 title(prettyInput, input, expected) {236 return `$ APIs throw on invalid '${prettyInput ?? inspect(input)}' expression with '${expected}'`;237 },238});239240test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');241test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');242243test(invalidExpression, null, 'Unexpected "object" in template expression');244test(invalidExpression, [null], 'Unexpected "object" in template expression');245246test(invalidExpression, true, 'Unexpected "boolean" in template expression');247test(invalidExpression, [true], 'Unexpected "boolean" in template expression');248249test(invalidExpression, {}, 'Unexpected "object" in template expression');250test(invalidExpression, [{}], 'Unexpected "object" in template expression');251252test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');253test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');254255test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');256test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');257258test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');259test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');260261test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');262test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');263264test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');265test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');266267test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');268test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');269270test('$({stdio: \'inherit\'}).sync`noop.js`', invalidExpression, $({stdio: 'inherit'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');271test('[ $({stdio: \'inherit\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'inherit'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');