Skip to content
Myr

test.js

This file

Path

test/test.js

305 lines · 11.4 KiB · JavaScript

Last changed by

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

History of this file

Take it

The walk stopped after 200 commits: lines older than e003f9c point at it.

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

lines 1–2why, one step back
1import path from 'node:path';
2import process from 'node:process';

Allow `cwd` and `localDir` options to be URLs (#492)ehmickybefore adoption93ab929

line 3why, one step back
3import {fileURLToPath, pathToFileURL} from 'node:url';

3.2.0ehmickybefore adoptione003f9c

lines 4–6why, one step back
4import test from 'ava';
5import isRunning from 'is-running';6import getNode from 'get-node';

Allow `shell` option to be a file URL (#635)ehmickybefore adoption189d486

line 7why, one step back
7import which from 'which';

Allow the first argument of `execa()` to be a file URL (#631)ehmickybefore adoption3bbd8ae

lines 8–9why, one step back
8import {execa, execaSync, execaNode, $} from '../index.js';
9import {setFixtureDir, PATH_KEY, FIXTURES_DIR_URL} from './helpers/fixtures-dir.js';

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 10–11why, one step back
10import {identity, fullStdio} from './helpers/stdio.js';
11import {stringGenerator} from './helpers/generator.js';

3.2.0ehmickybefore adoptione003f9c

line 12why, one step back
12

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

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

3.2.0ehmickybefore adoptione003f9c

lines 14–17why, one step back
14process.env.FOO = 'foo';
1516const ENOENT_REGEXP = process.platform === 'win32' ? /failed with exit code 1/ : /spawn.* ENOENT/;17

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 18–20why, one step back
18const testOutput = async (t, index, execaCommand) => {
19 const {stdout, stderr, stdio} = await execaCommand('noop-fd.js', [`${index}`, 'foobar'], fullStdio);20 t.is(stdio[index], 'foobar');

Allow the `execPath` option to be a file URL (#633)ehmickybefore adoptionb495146

line 21why, one step back
21

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 22–46why, one step back
22 if (index === 1) {
23 t.is(stdio[index], stdout);24 } else if (index === 2) {25 t.is(stdio[index], stderr);26 }27};2829test('can return stdout', testOutput, 1, execa);30test('can return stderr', testOutput, 2, execa);31test('can return output stdio[*]', testOutput, 3, execa);32test('can return stdout - sync', testOutput, 1, execaSync);33test('can return stderr - sync', testOutput, 2, execaSync);34test('can return output stdio[*] - sync', testOutput, 3, execaSync);3536const testNoStdin = async (t, execaCommand) => {37 const {stdio} = await execaCommand('noop.js', ['foobar']);38 t.is(stdio[0], undefined);39};4041test('cannot return stdin', testNoStdin, execa);42test('cannot return stdin - sync', testNoStdin, execaSync);4344test('cannot return input stdio[*]', async t => {45 const {stdio} = await execa('stdin-fd.js', ['3'], {stdio: ['pipe', 'pipe', 'pipe', stringGenerator()]});46 t.is(stdio[3], undefined);

3.2.0ehmickybefore adoptione003f9c

lines 47–60why, one step back
47});
4849if (process.platform === 'win32') {50 test('execa() - cmd file', async t => {51 const {stdout} = await execa('hello.cmd');52 t.is(stdout, 'Hello World');53 });5455 test('execa() - run cmd command', async t => {56 const {stdout} = await execa('cmd', ['/c', 'hello.cmd']);57 t.is(stdout, 'Hello World');58 });59}60

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 61why, one step back
61test('execaSync() throws error if ENOENT', t => {

3.2.0ehmickybefore adoptione003f9c

line 62why, one step back
62 t.throws(() => {

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

lines 63–64why, one step back
63 execaSync('foo');
64 }, {message: ENOENT_REGEXP});

3.2.0ehmickybefore adoptione003f9c

lines 65–67why, one step back
65});
6667test('skip throwing when using reject option', async t => {

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

line 68why, one step back
68 const {exitCode} = await execa('fail.js', {reject: false});

3.2.0ehmickybefore adoptione003f9c

lines 69–72why, one step back
69 t.is(exitCode, 2);
70});7172test('skip throwing when using reject option in sync mode', t => {

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

line 73why, one step back
73 const {exitCode} = execaSync('fail.js', {reject: false});

3.2.0ehmickybefore adoptione003f9c

lines 74–76why, one step back
74 t.is(exitCode, 2);
75});76

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 77–80why, one step back
77const testStripFinalNewline = async (t, index, stripFinalNewline, execaCommand) => {
78 const {stdio} = await execaCommand('noop-fd.js', [`${index}`, 'foobar\n'], {...fullStdio, stripFinalNewline});79 t.is(stdio[index], `foobar${stripFinalNewline ? '' : '\n'}`);80};

3.2.0ehmickybefore adoptione003f9c

line 81why, one step back
81

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 82–93why, one step back
82test('stripFinalNewline: true with stdout', testStripFinalNewline, 1, undefined, execa);
83test('stripFinalNewline: false with stdout', testStripFinalNewline, 1, false, execa);84test('stripFinalNewline: true with stderr', testStripFinalNewline, 2, undefined, execa);85test('stripFinalNewline: false with stderr', testStripFinalNewline, 2, false, execa);86test('stripFinalNewline: true with stdio[*]', testStripFinalNewline, 3, undefined, execa);87test('stripFinalNewline: false with stdio[*]', testStripFinalNewline, 3, false, execa);88test('stripFinalNewline: true with stdout - sync', testStripFinalNewline, 1, undefined, execaSync);89test('stripFinalNewline: false with stdout - sync', testStripFinalNewline, 1, false, execaSync);90test('stripFinalNewline: true with stderr - sync', testStripFinalNewline, 2, undefined, execaSync);91test('stripFinalNewline: false with stderr - sync', testStripFinalNewline, 2, false, execaSync);92test('stripFinalNewline: true with stdio[*] - sync', testStripFinalNewline, 3, undefined, execaSync);93test('stripFinalNewline: false with stdio[*] - sync', testStripFinalNewline, 3, false, execaSync);

3.2.0ehmickybefore adoptione003f9c

line 94why, one step back
94

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

lines 95–101why, one step back
95const getPathWithoutLocalDir = () => {
96 const newPath = process.env[PATH_KEY].split(path.delimiter).filter(pathDir => !BIN_DIR_REGEXP.test(pathDir)).join(path.delimiter);97 return {[PATH_KEY]: newPath};98};99100const BIN_DIR_REGEXP = /node_modules[\\/]\.bin/;101

3.2.0ehmickybefore adoptione003f9c

line 102why, one step back
102test('preferLocal: true', async t => {

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

line 103why, one step back
103 await t.notThrowsAsync(execa('ava', ['--version'], {preferLocal: true, env: getPathWithoutLocalDir()}));

3.2.0ehmickybefore adoptione003f9c

lines 104–106why, one step back
104});
105106test('preferLocal: false', async t => {

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

line 107why, one step back
107 await t.throwsAsync(execa('ava', ['--version'], {preferLocal: false, env: getPathWithoutLocalDir()}), {message: ENOENT_REGEXP});

3.2.0ehmickybefore adoptione003f9c

lines 108–110why, one step back
108});
109110test('preferLocal: undefined', async t => {

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

line 111why, one step back
111 await t.throwsAsync(execa('ava', ['--version'], {env: getPathWithoutLocalDir()}), {message: ENOENT_REGEXP});

Make `preferLocal` default to `true` with `$` (#529)ehmickybefore adoptiona6d1141

lines 112–115why, one step back
112});
113114test('preferLocal: undefined with $', async t => {115 await t.notThrowsAsync($({env: getPathWithoutLocalDir()})`ava --version`);

3.2.0ehmickybefore adoptione003f9c

lines 116–117why, one step back
116});
117

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

lines 118–121why, one step back
118test('preferLocal: undefined with $.sync', t => {
119 t.notThrows(() => $({env: getPathWithoutLocalDir()}).sync`ava --version`);120});121

3.2.0ehmickybefore adoptione003f9c

lines 122–128why, one step back
122test('localDir option', async t => {
123 const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';124 const {stdout} = await execa(command, {shell: true, preferLocal: true, localDir: '/test'});125 const envPaths = stdout.split(path.delimiter);126 t.true(envPaths.some(envPath => envPath.endsWith('.bin')));127});128

Allow the `execPath` option to be a file URL (#633)ehmickybefore adoptionb495146

lines 129–131why, one step back
129const testExecPath = async (t, mapPath) => {
130 const {path} = await getNode('16.0.0');131 const execPath = mapPath(path);

3.2.0ehmickybefore adoptione003f9c

line 132why, one step back
132 const {stdout} = await execa('node', ['-p', 'process.env.Path || process.env.PATH'], {preferLocal: true, execPath});

Meta tweaksSindre Sorhusbefore adoption09df476

line 133why, one step back
133 t.true(stdout.includes('16.0.0'));

Allow the `execPath` option to be a file URL (#633)ehmickybefore adoptionb495146

lines 134–137why, one step back
134};
135136test.serial('execPath option', testExecPath, identity);137test.serial('execPath option can be a file URL', testExecPath, pathToFileURL);

3.2.0ehmickybefore adoptione003f9c

lines 138–139why, one step back
138
139test('child process errors are handled', async t => {

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

line 140why, one step back
140 const subprocess = execa('noop.js');

3.2.0ehmickybefore adoptione003f9c

line 141why, one step back
141 subprocess.emit('error', new Error('test'));

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

line 142why, one step back
142 await t.throwsAsync(subprocess, {message: /test/});

3.2.0ehmickybefore adoptione003f9c

lines 143–144why, one step back
143});
144

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 145–148why, one step back
145test('execa() returns a promise with pid', async t => {
146 const subprocess = execa('noop.js', ['foo']);147 t.is(typeof subprocess.pid, 'number');148 await subprocess;

3.2.0ehmickybefore adoptione003f9c

lines 149–151why, one step back
149});
150151test('child_process.spawn() propagated errors have correct shape', t => {

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

line 152why, one step back
152 const subprocess = execa('noop.js', {uid: -1});

3.2.0ehmickybefore adoptione003f9c

lines 153–160why, one step back
153 t.notThrows(() => {
154 subprocess.catch(() => {});155 subprocess.unref();156 subprocess.on('error', () => {});157 });158});159160test('child_process.spawn() errors are propagated', async t => {

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

line 161why, one step back
161 const {failed} = await t.throwsAsync(execa('noop.js', {uid: -1}));

3.2.0ehmickybefore adoptione003f9c

lines 162–166why, one step back
162 t.true(failed);
163});164165test('child_process.spawnSync() errors are propagated with a correct shape', t => {166 const {failed} = t.throws(() => {

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

line 167why, one step back
167 execaSync('noop.js', {timeout: -1});

3.2.0ehmickybefore adoptione003f9c

lines 168–172why, one step back
168 });
169 t.true(failed);170});171172test('do not try to consume streams twice', async t => {

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

line 173why, one step back
173 const subprocess = execa('noop.js', ['foo']);

Meta tweaksSindre Sorhusbefore adoption5bac052

lines 174–177why, one step back
174 const {stdout} = await subprocess;
175 const {stdout: stdout2} = await subprocess;176 t.is(stdout, 'foo');177 t.is(stdout2, 'foo');

3.2.0ehmickybefore adoptione003f9c

lines 178–180why, one step back
178});
179180test('use relative path with \'..\' chars', async t => {

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

line 181why, one step back
181 const pathViaParentDir = path.join('..', path.basename(fileURLToPath(new URL('..', import.meta.url))), 'test', 'fixtures', 'noop.js');

3.2.0ehmickybefore adoptione003f9c

lines 182–187why, one step back
182 const {stdout} = await execa(pathViaParentDir, ['foo']);
183 t.is(stdout, 'foo');184});185186if (process.platform !== 'win32') {187 test('execa() rejects if running non-executable', async t => {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 188why, one step back
188 await t.throwsAsync(execa('non-executable.js'));

3.2.0ehmickybefore adoptione003f9c

lines 189–191why, one step back
189 });
190191 test('execa() rejects with correct error and doesn\'t throw if running non-executable with input', async t => {

Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0

line 192why, one step back
192 await t.throwsAsync(execa('non-executable.js', {input: 'Hey!'}), {message: /EACCES/});

3.2.0ehmickybefore adoptione003f9c

lines 193–209why, one step back
193 });
194}195196if (process.platform !== 'win32') {197 test('write to fast-exit process', async t => {198 // Try-catch here is necessary, because this test is not 100% accurate199 // Sometimes process can manage to accept input before exiting200 try {201 await execa(`fast-exit-${process.platform}`, [], {input: 'data'});202 t.pass();203 } catch (error) {204 t.is(error.code, 'EPIPE');205 }206 });207}208209test('use environment variables by default', async t => {

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

line 210why, one step back
210 const {stdout} = await execa('environment.js');

3.2.0ehmickybefore adoptione003f9c

lines 211–214why, one step back
211 t.deepEqual(stdout.split('\n'), ['foo', 'undefined']);
212});213214test('extend environment variables by default', async t => {

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

line 215why, one step back
215 const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}});

3.2.0ehmickybefore adoptione003f9c

lines 216–219why, one step back
216 t.deepEqual(stdout.split('\n'), ['foo', 'bar']);
217});218219test('do not extend environment with `extendEnv: false`', async t => {

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

line 220why, one step back
220 const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}, extendEnv: false});

3.2.0ehmickybefore adoptione003f9c

lines 221–222why, one step back
221 t.deepEqual(stdout.split('\n'), ['undefined', 'bar']);
222});

Allow `cwd` and `localDir` options to be URLs (#492)ehmickybefore adoption93ab929

lines 223–229why, one step back
223
224test('can use `options.cwd` as a string', async t => {225 const cwd = '/';226 const {stdout} = await execa('node', ['-p', 'process.cwd()'], {cwd});227 t.is(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));228});229

Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0

lines 230–235why, one step back
230test('localDir option can be a URL', async t => {
231 const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';232 const {stdout} = await execa(command, {shell: true, preferLocal: true, localDir: pathToFileURL('/test')});233 const envPaths = stdout.split(path.delimiter);234 t.true(envPaths.some(envPath => envPath.endsWith('.bin')));235});

Allow `cwd` and `localDir` options to be URLs (#492)ehmickybefore adoption93ab929

line 236why, one step back
236

Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0

lines 237–242why, one step back
237test('can use `options.cwd` as a URL', async t => {
238 const cwd = '/';239 const cwdUrl = pathToFileURL(cwd);240 const {stdout} = await execa('node', ['-p', 'process.cwd()'], {cwd: cwdUrl});241 t.is(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));242});

3.2.0ehmickybefore adoptione003f9c

lines 243–244why, one step back
243
244test('can use `options.shell: true`', async t => {

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

line 245why, one step back
245 const {stdout} = await execa('node test/fixtures/noop.js foo', {shell: true});

3.2.0ehmickybefore adoptione003f9c

lines 246–248why, one step back
246 t.is(stdout, 'foo');
247});248

Allow `shell` option to be a file URL (#635)ehmickybefore adoption189d486

lines 249–251why, one step back
249const testShellPath = async (t, mapPath) => {
250 const shellPath = process.platform === 'win32' ? 'cmd.exe' : 'bash';251 const shell = mapPath(await which(shellPath));

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

line 252why, one step back
252 const {stdout} = await execa('node test/fixtures/noop.js foo', {shell});

3.2.0ehmickybefore adoptione003f9c

line 253why, one step back
253 t.is(stdout, 'foo');

Allow `shell` option to be a file URL (#635)ehmickybefore adoption189d486

lines 254–257why, one step back
254};
255256test('can use `options.shell: string`', testShellPath, identity);257test('can use `options.shell: file URL`', testShellPath, pathToFileURL);

3.2.0ehmickybefore adoptione003f9c

lines 258–267why, one step back
258
259test('use extend environment with `extendEnv: true` and `shell: true`', async t => {260 process.env.TEST = 'test';261 const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST';262 const {stdout} = await execa(command, {shell: true, env: {}, extendEnv: true});263 t.is(stdout, 'test');264 delete process.env.TEST;265});266267test('detach child process', async t => {

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

line 268why, one step back
268 const {stdout} = await execa('detach.js');

3.2.0ehmickybefore adoptione003f9c

lines 269–274why, one step back
269 const pid = Number(stdout);
270 t.true(Number.isInteger(pid));271 t.true(isRunning(pid));272273 process.kill(pid, 'SIGKILL');274});

Allow the first argument of `execa()` to be a file URL (#631)ehmickybefore adoption3bbd8ae

lines 275–305why, one step back
275
276const testFileUrl = async (t, execaMethod) => {277 const command = new URL('noop.js', FIXTURES_DIR_URL);278 const {stdout} = await execaMethod(command, ['foobar']);279 t.is(stdout, 'foobar');280};281282test('execa()\'s command argument can be a file URL', testFileUrl, execa);283test('execaSync()\'s command argument can be a file URL', testFileUrl, execaSync);284test('execaNode()\'s command argument can be a file URL', testFileUrl, execaNode);285286const testInvalidFileUrl = async (t, execaMethod) => {287 const invalidUrl = new URL('https://invalid.com');288 t.throws(() => {289 execaMethod(invalidUrl);290 }, {code: 'ERR_INVALID_URL_SCHEME'});291};292293test('execa()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execa);294test('execaSync()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execaSync);295test('execaNode()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execaNode);296297const testInvalidCommand = async (t, execaMethod) => {298 t.throws(() => {299 execaMethod(['command', 'arg']);300 }, {message: /must be a string or a file URL/});301};302303test('execa()\'s command argument must be a string or file URL', testInvalidCommand, execa);304test('execaSync()\'s command argument must be a string or file URL', testInvalidCommand, execaSync);305test('execaNode()\'s command argument must be a string or file URL', testInvalidCommand, execaNode);