Skip to content

Commit 72ca9de

Browse files
authored
Merge pull request #242 from nikita-skobov/fix-issue-241
Fix issue 241
2 parents 016291a + 689decb commit 72ca9de

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
/* eslint-disable no-useless-escape */
22
import * as sh from '../sh'
33

4+
describe('execShellScript', () => {
5+
it('resolves if childprocess sends close signal', async () => {
6+
return expect(sh.execShellScript('echo')).resolves
7+
})
8+
9+
it('rejects if childprocess sends error signal', async () => {
10+
// an error is sent if child_process cant spawn 'some-nonexistant-command'
11+
return expect(
12+
sh.execShellScript('something', 'some-nonexistant-command'),
13+
).rejects.toBe('Failed to execute something')
14+
})
15+
})
16+
417
describe('getDocumentation', () => {
518
it('returns null for an unknown builtin', async () => {
619
const result = await sh.getShellDocumentation({ word: 'foobar' })

server/src/util/sh.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@ import * as ChildProcess from 'child_process'
33
/**
44
* Execute the following sh program.
55
*/
6-
export function execShellScript(body: string): Promise<string> {
6+
export function execShellScript(body: string, cmd = 'bash'): Promise<string> {
77
const args = ['-c', body]
8-
const process = ChildProcess.spawn('bash', args)
8+
const process = ChildProcess.spawn(cmd, args)
99

1010
return new Promise((resolve, reject) => {
1111
let output = ''
1212

13-
process.stdout.on('data', buffer => {
14-
output += buffer
15-
})
16-
17-
process.on('close', returnCode => {
13+
const handleClose = (returnCode: number | Error) => {
1814
if (returnCode === 0) {
1915
resolve(output)
2016
} else {
2117
reject(`Failed to execute ${body}`)
2218
}
19+
}
20+
21+
process.stdout.on('data', buffer => {
22+
output += buffer
2323
})
24+
25+
process.on('close', handleClose)
26+
process.on('error', handleClose)
2427
})
2528
}
2629

0 commit comments

Comments
 (0)