-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
core.test.js
537 lines (472 loc) · 13.9 KB
/
core.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import assert from 'node:assert'
import { test, describe, beforeEach } from 'node:test'
import { inspect } from 'node:util'
import { Readable, Writable } from 'node:stream'
import { Socket } from 'node:net'
import { ProcessPromise, ProcessOutput } from '../build/index.js'
import '../build/globals.js'
describe('core', () => {
beforeEach(() => {
$.verbose = false
})
test('only stdout is used during command substitution', async () => {
let hello = await $`echo Error >&2; echo Hello`
let len = +(await $`echo ${hello} | wc -c`)
assert.equal(len, 6)
})
test('env vars works', async () => {
process.env.ZX_TEST_FOO = 'foo'
let foo = await $`echo $ZX_TEST_FOO`
assert.equal(foo.stdout, 'foo\n')
})
test('env vars is safe to pass', async () => {
process.env.ZX_TEST_BAR = 'hi; exit 1'
await $`echo $ZX_TEST_BAR`
})
test('arguments are quoted', async () => {
let bar = 'bar"";baz!$#^$\'&*~*%)({}||\\/'
assert.equal((await $`echo ${bar}`).stdout.trim(), bar)
})
test('undefined and empty string correctly quoted', async () => {
assert.equal((await $`echo -n ${undefined}`).toString(), 'undefined')
assert.equal((await $`echo -n ${''}`).toString(), '')
})
test('can create a dir with a space in the name', async () => {
let name = 'foo bar'
try {
await $`mkdir /tmp/${name}`
} catch {
assert.unreachable()
} finally {
await fs.rmdir('/tmp/' + name)
}
})
test('pipefail is on', async () => {
let p
try {
p = await $`cat /dev/not_found | sort`
} catch (e) {
p = e
}
assert.notEqual(p.exitCode, 0)
})
test('toString() is called on arguments', async () => {
let foo = 0
let p = await $`echo ${foo}`
assert.equal(p.stdout, '0\n')
})
test('can use array as an argument', async () => {
let args = ['-n', 'foo']
assert.equal((await $`echo ${args}`).toString(), 'foo')
})
test('quiet() mode is working', async () => {
let stdout = ''
let log = console.log
console.log = (...args) => {
stdout += args.join(' ')
}
await $`echo 'test'`.quiet()
console.log = log
assert.equal(stdout, '')
{
// Deprecated.
let stdout = ''
let log = console.log
console.log = (...args) => {
stdout += args.join(' ')
}
await quiet($`echo 'test'`)
console.log = log
assert.equal(stdout, '')
}
})
test('handles `input` option', async () => {
const p1 = $({ input: 'foo' })`cat`
const p2 = $({ input: Readable.from('bar') })`cat`
const p3 = $({ input: Buffer.from('baz') })`cat`
const p4 = $({ input: p3 })`cat`
const p5 = $({ input: await p3 })`cat`
assert.equal((await p1).stdout, 'foo')
assert.equal((await p2).stdout, 'bar')
assert.equal((await p3).stdout, 'baz')
assert.equal((await p4).stdout, 'baz')
assert.equal((await p5).stdout, 'baz')
})
test('`$.sync()` provides synchronous API', () => {
const o1 = $.sync`echo foo`
const o2 = $({ sync: true })`echo foo`
assert.equal(o1.stdout, 'foo\n')
assert.equal(o2.stdout, 'foo\n')
})
test('pipes are working', async () => {
let { stdout } = await $`echo "hello"`
.pipe($`awk '{print $1" world"}'`)
.pipe($`tr '[a-z]' '[A-Z]'`)
assert.equal(stdout, 'HELLO WORLD\n')
try {
await $`echo foo`.pipe(fs.createWriteStream('/tmp/output.txt'))
assert.equal((await fs.readFile('/tmp/output.txt')).toString(), 'foo\n')
let r = $`cat`
fs.createReadStream('/tmp/output.txt').pipe(r.stdin)
assert.equal((await r).stdout, 'foo\n')
} finally {
await fs.rm('/tmp/output.txt')
}
})
test('provides presets', async () => {
const $$ = $({ nothrow: true })
assert.equal((await $$`exit 1`).exitCode, 1)
})
test('ProcessPromise', async () => {
let contents = ''
let stream = new Writable({
write: function (chunk, encoding, next) {
contents += chunk.toString()
next()
},
})
let p = $`echo 'test'`.pipe(stream)
await p
assert.ok(p._piped)
assert.equal(contents, 'test\n')
assert.ok(p.stderr instanceof Socket)
let err
try {
$`echo 'test'`.pipe('str')
} catch (p) {
err = p
}
assert.equal(
err.message,
'The pipe() method does not take strings. Forgot $?'
)
})
test('ProcessPromise: inherits native Promise', async () => {
const p1 = $`echo 1`
const p2 = p1.then((v) => v)
const p3 = p2.then((v) => v)
const p4 = p3.catch((v) => v)
const p5 = p1.finally((v) => v)
assert(p1 instanceof Promise)
assert(p1 instanceof ProcessPromise)
assert(p2 instanceof ProcessPromise)
assert(p3 instanceof ProcessPromise)
assert(p4 instanceof ProcessPromise)
assert(p5 instanceof ProcessPromise)
assert.ok(p1 !== p2)
assert.ok(p2 !== p3)
assert.ok(p3 !== p4)
assert.ok(p5 !== p1)
})
test('ProcessPromise: implements toString()', async () => {
const p = $`echo foo`
assert.equal((await p).toString(), 'foo\n')
})
test('ProcessPromise: implements valueOf()', async () => {
const p = $`echo foo`
assert.equal((await p).valueOf(), 'foo')
assert.ok((await p) == 'foo')
})
test('cd() works with relative paths', async () => {
let cwd = process.cwd()
try {
fs.mkdirpSync('/tmp/zx-cd-test/one/two')
cd('/tmp/zx-cd-test/one/two')
let p1 = $`pwd`
assert.equal($.cwd, undefined)
assert.ok(process.cwd().endsWith('/two'))
cd('..')
let p2 = $`pwd`
assert.equal($.cwd, undefined)
assert.ok(process.cwd().endsWith('/one'))
cd('..')
let p3 = $`pwd`
assert.equal($.cwd, undefined)
assert.ok(process.cwd().endsWith('/tmp/zx-cd-test'))
const results = (await Promise.all([p1, p2, p3])).map((p) =>
path.basename(p.stdout.trim())
)
assert.deepEqual(results, ['two', 'one', 'zx-cd-test'])
} catch (e) {
assert.ok(!e, e)
} finally {
fs.rmSync('/tmp/zx-cd-test', { recursive: true })
cd(cwd)
}
})
test('cd() does affect parallel contexts', async () => {
const cwd = process.cwd()
try {
fs.mkdirpSync('/tmp/zx-cd-parallel/one/two')
await Promise.all([
within(async () => {
assert.equal(process.cwd(), cwd)
await sleep(1)
cd('/tmp/zx-cd-parallel/one')
assert.ok(process.cwd().endsWith('/tmp/zx-cd-parallel/one'))
}),
within(async () => {
assert.equal(process.cwd(), cwd)
await sleep(2)
assert.equal(process.cwd(), cwd)
}),
within(async () => {
assert.equal(process.cwd(), cwd)
await sleep(3)
$.cwd = '/tmp/zx-cd-parallel/one/two'
assert.equal(process.cwd(), cwd)
assert.ok(
(await $`pwd`).stdout
.toString()
.trim()
.endsWith('/tmp/zx-cd-parallel/one/two')
)
}),
])
} catch (e) {
assert.ok(!e, e)
} finally {
fs.rmSync('/tmp/zx-cd-parallel', { recursive: true })
cd(cwd)
}
})
test('cd() fails on entering not existing dir', async () => {
assert.throws(() => cd('/tmp/abra-kadabra'))
})
test('cd() accepts ProcessOutput in addition to string', async () => {
within(async () => {
const tmpDir = await $`mktemp -d`
cd(tmpDir)
assert.equal(process.cwd(), tmpDir.toString().trimEnd())
})
})
test('abort() method works', async () => {
const p = $`sleep 9999`
setTimeout(() => p.abort(), 100)
try {
await p
assert.unreachable('should have thrown')
} catch ({ message }) {
assert.match(message, /The operation was aborted/)
}
})
test('accepts optional AbortController', async () => {
const ac = new AbortController()
const p = $({ ac })`sleep 9999`
setTimeout(() => ac.abort(), 100)
try {
await p
assert.unreachable('should have thrown')
} catch ({ message }) {
assert.match(message, /The operation was aborted/)
}
})
test('kill() method works', async () => {
let p = $`sleep 9999`.nothrow()
setTimeout(() => {
p.kill()
}, 100)
await p
})
test('a signal is passed with kill() method', async () => {
let p = $`while true; do :; done`
setTimeout(() => p.kill('SIGKILL'), 100)
let signal
try {
await p
} catch (p) {
signal = p.signal
}
assert.equal(signal, 'SIGKILL')
})
test('within() works', async () => {
let resolve, reject
let promise = new Promise((...args) => ([resolve, reject] = args))
function yes() {
assert.equal($.verbose, true)
resolve()
}
$.verbose = false
assert.equal($.verbose, false)
within(() => {
$.verbose = true
})
assert.equal($.verbose, false)
within(async () => {
$.verbose = true
setTimeout(yes, 10)
})
assert.equal($.verbose, false)
await promise
})
test('within() restores previous cwd', async () => {
let resolve, reject
let promise = new Promise((...args) => ([resolve, reject] = args))
let pwd = await $`pwd`
within(async () => {
$.verbose = false
cd('/tmp')
setTimeout(async () => {
assert.ok((await $`pwd`).stdout.trim().endsWith('/tmp'))
resolve()
}, 1000)
})
assert.equal((await $`pwd`).stdout, pwd.stdout)
await promise
})
test(`within() isolates nested context and returns cb result`, async () => {
within(async () => {
const res = await within(async () => {
$.verbose = true
return within(async () => {
assert.equal($.verbose, true)
$.verbose = false
return within(async () => {
assert.equal($.verbose, false)
$.verbose = true
return 'foo'
})
})
})
assert.equal($.verbose, false)
assert.equal(res, 'foo')
})
})
test('stdio() works', async () => {
let p = $`printf foo`
await p
assert.throws(() => p.stdin)
assert.equal((await p).stdout, 'foo')
let b = $`read; printf $REPLY`
b.stdin.write('bar\n')
assert.equal((await b).stdout, 'bar')
})
test('snapshots works', async () => {
await within(async () => {
$.prefix += 'echo success;'
let p = $`:`
$.prefix += 'echo fail;'
let out = await p
assert.equal(out.stdout, 'success\n')
assert.doesNotMatch(out.stdout, /fail/)
})
})
test('timeout() works', async () => {
let exitCode, signal
try {
await $`sleep 9999`.timeout(10, 'SIGKILL')
} catch (p) {
exitCode = p.exitCode
signal = p.signal
}
assert.equal(exitCode, null)
assert.equal(signal, 'SIGKILL')
})
test('timeout() expiration works', async () => {
let exitCode, signal
try {
await $`sleep 1`.timeout(999)
} catch (p) {
exitCode = p.exitCode
signal = p.signal
}
assert.equal(exitCode, undefined)
assert.equal(signal, undefined)
})
test('$ thrown as error', async () => {
let err
try {
await $`wtf`
} catch (p) {
err = p
}
assert.ok(err.exitCode > 0)
assert.ok(err.stderr.includes('wtf: command not found'))
assert.ok(err[inspect.custom]().includes('Command not found'))
})
test('error event is handled', async () => {
await within(async () => {
$.cwd = 'wtf'
try {
await $`pwd`
assert.unreachable('should have thrown')
} catch (err) {
assert.ok(err instanceof ProcessOutput)
assert.match(err.message, /No such file or directory/)
}
})
})
test('pipe() throws if already resolved', async (t) => {
let ok = true
let p = $`echo "Hello"`
await p
try {
await p.pipe($`less`)
ok = false
} catch (err) {
assert.equal(
err.message,
`The pipe() method shouldn't be called after promise is already resolved!`
)
}
assert.ok(ok, 'Expected failure!')
})
test('await $`cmd`.exitCode does not throw', async () => {
assert.notEqual(await $`grep qwerty README.md`.exitCode, 0)
assert.equal(await $`[[ -f README.md ]]`.exitCode, 0)
})
test('nothrow() do not throw', async () => {
let { exitCode } = await $`exit 42`.nothrow()
assert.equal(exitCode, 42)
{
// Deprecated.
let { exitCode } = await nothrow($`exit 42`)
assert.equal(exitCode, 42)
}
})
test('malformed cmd error', async () => {
assert.throws(() => $`\033`, /malformed/i)
})
test('$ is a regular function', async () => {
const _$ = $.bind(null)
let foo = await _$`echo foo`
assert.equal(foo.stdout, 'foo\n')
assert.ok(typeof $.call === 'function')
assert.ok(typeof $.apply === 'function')
})
test('halt() works', async () => {
let filepath = `/tmp/${Math.random().toString()}`
let p = $`touch ${filepath}`.halt()
await sleep(1)
assert.ok(
!fs.existsSync(filepath),
'The cmd called, but it should not have been called'
)
await p.run()
assert.ok(fs.existsSync(filepath), 'The cmd should have been called')
})
test('await on halted throws', async () => {
let p = $`sleep 1`.halt()
let ok = true
try {
await p
ok = false
} catch (err) {
assert.equal(err.message, 'The process is halted!')
}
assert.ok(ok, 'Expected failure!')
})
})