-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.spec.ts
98 lines (88 loc) · 2.01 KB
/
run.spec.ts
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
import assert from 'node:assert/strict'
import os from 'node:os'
import { describe, it, mock } from 'node:test'
import run from './run.js'
void describe('run()', () => {
void it('should run a command and return the output', async () => {
const res = await run({
command: 'echo',
args: ['-n', 'test'],
})
assert.equal(res, 'test')
})
void it('should pass input', async () => {
const res = await run({
command: '/bin/bash',
args: ['-c', 'read input && echo $input'],
input: `test${os.EOL}`,
})
assert.equal(res, `test${os.EOL}`)
})
void it('should log the executed command', async () => {
const debug = mock.fn()
await run({
command: 'echo',
args: ['-n', 'test'],
log: {
debug,
},
})
assert.equal(debug.mock.calls[0]?.arguments[0], 'echo -n test')
})
void it('should reject if the command does not exist', async () =>
assert.rejects(
async () =>
run({
command: 'foo',
}),
/spawn foo ENOENT/,
))
void it('should reject on non-zero exit code', async () =>
assert.rejects(
async () =>
run({
command: '/bin/bash',
args: ['-c', 'exit 42'],
}),
/\(42\)/,
))
void it('should log the errors', async () => {
const error = mock.fn()
await run({
command: 'npx',
args: ['tsx', 'src/test/stderr.ts'],
log: {
stderr: error,
},
})
assert.equal(error.mock.calls[0]?.arguments[0].toString(), 'error')
})
void it('should log the output', async () => {
const log = mock.fn()
await run({
command: 'echo',
args: ['-n', 'test'],
log: {
stdout: log,
},
})
assert.equal(log.mock.calls[0]?.arguments[0].toString(), 'test')
})
void it('can be launched in a different working directory', async () => {
await run({
command: 'npx',
args: ['tsx', 'stderr.ts'],
cwd: 'src/test',
})
})
void it('can be passed environment variables', async () => {
const res = await run({
command: '/bin/bash',
args: ['-c', 'echo $FOO'],
env: {
FOO: 'bar',
},
})
assert.equal(res, `bar${os.EOL}`)
})
})