Skip to content

Commit f84ca73

Browse files
committed
Adds tests for execShellScript
1 parent 7028b23 commit f84ca73

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

server/src/util/__tests__/sh.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,52 @@
1+
import * as ChildPorcess from 'child_process'
2+
// @ts-ignore
3+
ChildPorcess.spawn = jest.fn(ChildPorcess.spawn)
4+
15
/* eslint-disable no-useless-escape */
26
import * as sh from '../sh'
37

8+
describe('execShellScript', () => {
9+
it('resolves if childprocess sends close signal', async () => {
10+
// @ts-ignore
11+
ChildPorcess.spawn.mockReturnValueOnce({
12+
stdout: {
13+
on: (eventName: string, cb: (s: string) => {}) => {
14+
setImmediate(() => {
15+
cb('abc')
16+
})
17+
},
18+
},
19+
on: (eventName: string, cb: (n: number) => {}) => {
20+
setImmediate(() => {
21+
cb(0)
22+
})
23+
},
24+
})
25+
return expect(sh.execShellScript('something')).resolves.toBe('abc')
26+
})
27+
28+
it('rejects if childprocess sends error signal', async () => {
29+
// @ts-ignore
30+
ChildPorcess.spawn.mockReturnValueOnce({
31+
stdout: {
32+
on: (eventName: string, cb: (s: string) => {}) => {
33+
setImmediate(() => {
34+
cb('abc')
35+
})
36+
},
37+
},
38+
on: (eventName: string, cb: (err: Error) => {}) => {
39+
setImmediate(() => {
40+
cb(new Error('err'))
41+
})
42+
},
43+
})
44+
return expect(sh.execShellScript('something')).rejects.toBe(
45+
'Failed to execute something',
46+
)
47+
})
48+
})
49+
450
describe('getDocumentation', () => {
551
it('returns null for an unknown builtin', async () => {
652
const result = await sh.getShellDocumentation({ word: 'foobar' })

0 commit comments

Comments
 (0)