-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathtest.js
255 lines (211 loc) · 8.31 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import path from 'node:path';
import process from 'node:process';
import {fileURLToPath, pathToFileURL} from 'node:url';
import test from 'ava';
import isRunning from 'is-running';
import getNode from 'get-node';
import {execa, execaSync} from '../index.js';
import {setFixtureDir, PATH_KEY} from './helpers/fixtures-dir.js';
setFixtureDir();
process.env.FOO = 'foo';
const ENOENT_REGEXP = process.platform === 'win32' ? /failed with exit code 1/ : /spawn.* ENOENT/;
test('execa()', async t => {
const {stdout} = await execa('noop.js', ['foo']);
t.is(stdout, 'foo');
});
if (process.platform === 'win32') {
test('execa() - cmd file', async t => {
const {stdout} = await execa('hello.cmd');
t.is(stdout, 'Hello World');
});
test('execa() - run cmd command', async t => {
const {stdout} = await execa('cmd', ['/c', 'hello.cmd']);
t.is(stdout, 'Hello World');
});
}
test('execaSync()', t => {
const {stdout} = execaSync('noop.js', ['foo']);
t.is(stdout, 'foo');
});
test('execaSync() throws error if written to stderr', t => {
t.throws(() => {
execaSync('foo');
}, {message: ENOENT_REGEXP});
});
test('skip throwing when using reject option', async t => {
const {exitCode} = await execa('fail.js', {reject: false});
t.is(exitCode, 2);
});
test('skip throwing when using reject option in sync mode', t => {
const {exitCode} = execaSync('fail.js', {reject: false});
t.is(exitCode, 2);
});
test('stripFinalNewline: true', async t => {
const {stdout} = await execa('noop.js', ['foo']);
t.is(stdout, 'foo');
});
test('stripFinalNewline: false', async t => {
const {stdout} = await execa('noop.js', ['foo'], {stripFinalNewline: false});
t.is(stdout, 'foo\n');
});
test('stripFinalNewline on failure', async t => {
const {stderr} = await t.throwsAsync(execa('noop-throw.js', ['foo'], {stripFinalNewline: true}));
t.is(stderr, 'foo');
});
test('stripFinalNewline in sync mode', t => {
const {stdout} = execaSync('noop.js', ['foo'], {stripFinalNewline: true});
t.is(stdout, 'foo');
});
test('stripFinalNewline in sync mode on failure', t => {
const {stderr} = t.throws(() => {
execaSync('noop-throw.js', ['foo'], {stripFinalNewline: true});
});
t.is(stderr, 'foo');
});
const getPathWithoutLocalDir = () => {
const newPath = process.env[PATH_KEY].split(path.delimiter).filter(pathDir => !BIN_DIR_REGEXP.test(pathDir)).join(path.delimiter);
return {[PATH_KEY]: newPath};
};
const BIN_DIR_REGEXP = /node_modules[\\/]\.bin/;
test('preferLocal: true', async t => {
await t.notThrowsAsync(execa('ava', ['--version'], {preferLocal: true, env: getPathWithoutLocalDir()}));
});
test('preferLocal: false', async t => {
await t.throwsAsync(execa('ava', ['--version'], {preferLocal: false, env: getPathWithoutLocalDir()}), {message: ENOENT_REGEXP});
});
test('preferLocal: undefined', async t => {
await t.throwsAsync(execa('ava', ['--version'], {env: getPathWithoutLocalDir()}), {message: ENOENT_REGEXP});
});
test('localDir option', async t => {
const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';
const {stdout} = await execa(command, {shell: true, preferLocal: true, localDir: '/test'});
const envPaths = stdout.split(path.delimiter);
t.true(envPaths.some(envPath => envPath.endsWith('.bin')));
});
test('execPath option', async t => {
const {path: execPath} = await getNode('16.0.0');
const {stdout} = await execa('node', ['-p', 'process.env.Path || process.env.PATH'], {preferLocal: true, execPath});
t.true(stdout.includes('16.0.0'));
});
test('stdin errors are handled', async t => {
const subprocess = execa('noop.js');
subprocess.stdin.emit('error', new Error('test'));
await t.throwsAsync(subprocess, {message: /test/});
});
test('child process errors are handled', async t => {
const subprocess = execa('noop.js');
subprocess.emit('error', new Error('test'));
await t.throwsAsync(subprocess, {message: /test/});
});
test('child process errors rejects promise right away', async t => {
const subprocess = execa('noop.js');
subprocess.emit('error', new Error('test'));
await t.throwsAsync(subprocess, {message: /test/});
});
test('execa() returns a promise with pid', t => {
const {pid} = execa('noop.js', ['foo']);
t.is(typeof pid, 'number');
});
test('child_process.spawn() propagated errors have correct shape', t => {
const subprocess = execa('noop.js', {uid: -1});
t.notThrows(() => {
subprocess.catch(() => {});
subprocess.unref();
subprocess.on('error', () => {});
});
});
test('child_process.spawn() errors are propagated', async t => {
const {failed} = await t.throwsAsync(execa('noop.js', {uid: -1}));
t.true(failed);
});
test('child_process.spawnSync() errors are propagated with a correct shape', t => {
const {failed} = t.throws(() => {
execaSync('noop.js', {timeout: -1});
});
t.true(failed);
});
test('do not try to consume streams twice', async t => {
const subprocess = execa('noop.js', ['foo']);
const {stdout} = await subprocess;
const {stdout: stdout2} = await subprocess;
t.is(stdout, 'foo');
t.is(stdout2, 'foo');
});
test('use relative path with \'..\' chars', async t => {
const pathViaParentDir = path.join('..', path.basename(fileURLToPath(new URL('..', import.meta.url))), 'test', 'fixtures', 'noop.js');
const {stdout} = await execa(pathViaParentDir, ['foo']);
t.is(stdout, 'foo');
});
if (process.platform !== 'win32') {
test('execa() rejects if running non-executable', async t => {
const subprocess = execa('non-executable.js');
await t.throwsAsync(subprocess);
});
test('execa() rejects with correct error and doesn\'t throw if running non-executable with input', async t => {
await t.throwsAsync(execa('non-executable.js', {input: 'Hey!'}), {message: /EACCES/});
});
}
if (process.platform !== 'win32') {
test('write to fast-exit process', async t => {
// Try-catch here is necessary, because this test is not 100% accurate
// Sometimes process can manage to accept input before exiting
try {
await execa(`fast-exit-${process.platform}`, [], {input: 'data'});
t.pass();
} catch (error) {
t.is(error.code, 'EPIPE');
}
});
}
test('use environment variables by default', async t => {
const {stdout} = await execa('environment.js');
t.deepEqual(stdout.split('\n'), ['foo', 'undefined']);
});
test('extend environment variables by default', async t => {
const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}});
t.deepEqual(stdout.split('\n'), ['foo', 'bar']);
});
test('do not extend environment with `extendEnv: false`', async t => {
const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}, extendEnv: false});
t.deepEqual(stdout.split('\n'), ['undefined', 'bar']);
});
test('can use `options.cwd` as a string', async t => {
const cwd = '/';
const {stdout} = await execa('node', ['-p', 'process.cwd()'], {cwd});
t.is(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));
});
test('localDir option can be a URL', async t => {
const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';
const {stdout} = await execa(command, {shell: true, preferLocal: true, localDir: pathToFileURL('/test')});
const envPaths = stdout.split(path.delimiter);
t.true(envPaths.some(envPath => envPath.endsWith('.bin')));
});
test('can use `options.cwd` as a URL', async t => {
const cwd = '/';
const cwdUrl = pathToFileURL(cwd);
const {stdout} = await execa('node', ['-p', 'process.cwd()'], {cwd: cwdUrl});
t.is(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));
});
test('can use `options.shell: true`', async t => {
const {stdout} = await execa('node test/fixtures/noop.js foo', {shell: true});
t.is(stdout, 'foo');
});
test('can use `options.shell: string`', async t => {
const shell = process.platform === 'win32' ? 'cmd.exe' : '/bin/bash';
const {stdout} = await execa('node test/fixtures/noop.js foo', {shell});
t.is(stdout, 'foo');
});
test('use extend environment with `extendEnv: true` and `shell: true`', async t => {
process.env.TEST = 'test';
const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST';
const {stdout} = await execa(command, {shell: true, env: {}, extendEnv: true});
t.is(stdout, 'test');
delete process.env.TEST;
});
test('detach child process', async t => {
const {stdout} = await execa('detach.js');
const pid = Number(stdout);
t.true(Number.isInteger(pid));
t.true(isRunning(pid));
process.kill(pid, 'SIGKILL');
});