-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathx.test.ts
214 lines (175 loc) · 6.72 KB
/
x.test.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
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
import * as assert from 'node:assert'
import * as fs from 'node:fs'
import * as os from 'node:os'
import path from 'node:path'
import { describe, it } from 'node:test'
import { Stream } from 'node:stream'
import { getEventListeners } from 'node:events'
import { $ } from '../../main/ts/x.js'
const __dirname = new URL('.', import.meta.url).pathname
const fixtures = path.resolve(__dirname, '../fixtures')
const tempy = fs.mkdtempSync(path.join(os.tmpdir(), 'tempy-'))
const onStreamFinish = (stream: Stream) => new Promise((resolve) => stream.on('finish', resolve))
const throwError = (err: any = new Error('should have thrown')) => { throw err }
describe('$()', () => {
it('supports async flow', async () => {
const p = $`echo foo`
assert.equal((await p).toString(), 'foo')
assert.equal(await p.stdout, 'foo\n')
assert.equal(await p.stderr, '')
assert.equal(await p.status, 0)
try {
await $`exit 2`
} catch (error: unknown) {
console.error(error)
assert.ok((error as Error).message.includes('exit code: 2 (Misuse of shell builtins)'))
}
const err = await $`exit 2`.catch((error) => error)
assert.ok(err.message.includes('exit code: 2 (Misuse of shell builtins)'))
})
it('supports sync flow', () => {
const p = $({sync: true})`echo foo`
assert.equal(p.toString(), 'foo')
assert.equal(p.stdout, 'foo\n')
assert.equal(p.stderr, '')
assert.deepEqual(p.stdall, 'foo\n')
try {
$({sync: true})`exit 2`
} catch (error: unknown) {
assert.match((error as Error).message, /exit code: 2 \(Misuse of shell builtins\)/)
}
})
it('handles promises in cmd literal', async () => {
const example = $`echo example`
// eslint-disable-next-line sonarjs/no-nested-template-literals
assert.equal((await $`echo ${example} ${$`echo and`} ${await example}`)
.toString(), 'example and example')
})
it('supports stdin', async () => {
const input = '{"name": "foo"}'
const name = await $({input})`jq -r .name`
assert.equal(name.toString().trim(), 'foo')
const stdin = fs.createReadStream(path.resolve(fixtures, 'foo.json'))
const data = await $({stdin})`jq -r .data`
assert.equal(data.toString().trim(), 'foobar')
const p = $`echo "5\\n3\\n1\\n4\\n2"`
const sorted = $({input: p})`sort`
assert.equal((await sorted).toString(), '1\n2\n3\n4\n5')
})
it('handles custom stdio', async () => {
await $({stdio: ['inherit', 'inherit', 'inherit']})`ls`
await $({stdio: 'ignore'})`ls`
$({stdio: 'ignore', sync: true})`ls`
})
it('works without shell', async () => {
const o1 = await $({shell: true})`exit 2 | exit 0`
const o2 = await $({shell: false, nothrow: true})`exit 1 | exit 0`
assert.equal(o1.status, 0)
assert.equal(o2.status, -2)
})
it('supports presets', () => {
const $$ = $({sync: true, cmd: 'echo foo'})
const $$$ = $$({cmd: 'echo bar'})
const p1 = $$()
const p2 = $$$()
const p3 = $$`echo baz`
const p4 = $$$({cmd: 'echo qux'})()
const o1 = p1.stdout
const o2 = p2.stdout
const o3 = p3.stdout
const o4 = p4.stdout
assert.equal(o1.trim(), 'foo')
assert.equal(o2.trim(), 'bar')
assert.equal(o3.trim(), 'baz')
assert.equal(o4.trim(), 'qux')
})
})
describe('mixins', () => {
describe('kill', () => {
it('handles `kill`', async () => {
const p = $({nothrow: true})`sleep 10`
let killed
setTimeout(() => killed = p.kill(), 25)
const { error } = await p
const signal = await killed
assert.equal(signal, 'SIGTERM')
assert.ok(error.message.includes('signal: SIGTERM'))
})
it('handles `abort`', async () => {
const p = $({nothrow: true})`sleep 10`
const events: any[] = []
let c = 0
setTimeout(() => p.abort(), 25)
setTimeout(() => c = getEventListeners(p.ctx.signal, 'abort').length, 10)
p
.on('abort', () => events.push('abort'))
.on('end', () => events.push('end'))
const { error } = await p
assert.ok(getEventListeners(p.ctx.signal, 'abort').length < c)
assert.ok(error.message.startsWith('The operation was aborted'))
assert.match(error.message, /code: ABORT_ERR/)
assert.deepEqual(events, ['abort', 'end'])
})
})
describe('timeout', () => {
it('handles `timeout` as option', async () => {
const p = $({ timeout: 25, timeoutSignal: 'SIGALRM', nothrow: true })`sleep 10`
const { error } = await p
assert.ok(error.message.includes('signal: SIGALRM'))
})
it('handles `timeout` as promise setter', async () => {
const p = $`sleep 10`
p.timeoutSignal = 'SIGALRM'
p.timeout = 25
p.ctx.nothrow = true
const { error } = await p
assert.ok(error.message.includes('signal: SIGALRM'))
})
})
describe('pipe', () => {
it('supports async flow', async () => {
const result = $`echo "5\\n3\\n1\\n4\\n2"`
const expected = '1\n2\n3\n4\n5'
const writable1 = fs.createWriteStream(path.join(tempy, 'output1.txt'))
const writable2 = fs.createWriteStream(path.join(tempy, 'output2.txt'))
const w1 = onStreamFinish(writable1)
const w2 = onStreamFinish(writable1)
const piped0 = result.pipe`sort | cat`
const piped1 = result.pipe`sort`.pipe`cat`
const piped2 = result.pipe(writable2)
const piped3 = result.pipe($`sort`)
const piped4 = (await result).pipe`sort`
const piped5 = result.pipe($`sort`)
const piped6 = (await result.pipe`sort`).pipe(writable1)
assert.equal(piped6, writable1)
assert.equal(piped2, writable2)
assert.equal((await piped0).toString(), expected)
assert.equal((await piped1).toString(), expected)
assert.equal((await piped3).toString(), expected)
assert.equal((await piped4).toString(), expected)
assert.equal((await piped5).toString(), expected)
await w1
await w2
assert.equal(fs.readFileSync(path.join(tempy, 'output1.txt'), 'utf8').trim(), expected)
assert.equal(fs.readFileSync(path.join(tempy, 'output2.txt'), 'utf8').trim(), '5\n3\n1\n4\n2')
})
it('supports sync flow', async () => {
const result = $({sync: true})`echo "5\\n3\\n1\\n4\\n2"`
assert.equal(result.toString().trim(), '5\n3\n1\n4\n2')
const expected = '1\n2\n3\n4\n5'
const piped = result.pipe`sort`
assert.equal(piped.toString(), expected)
})
it('supports multipiping', async () => {
const result = $`echo 1; sleep 1; echo 2; sleep 1; echo 3`
const piped1 = result.pipe`cat`
let piped2: any
setTimeout(() => {
piped2 = result.pipe`cat`
}, 1500)
await piped1
assert.equal((await piped1).toString(), '1\n2\n3')
assert.equal((await piped2).toString(), '1\n2\n3')
})
})
})