Skip to content

Fix issue 241 (handle case where bash is not installed) #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions server/src/util/__tests__/sh.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
/* eslint-disable no-useless-escape */
import * as sh from '../sh'

describe('execShellScript', () => {
it('resolves if childprocess sends close signal', async () => {
return expect(sh.execShellScript('echo')).resolves
})

it('rejects if childprocess sends error signal', async () => {
// an error is sent if child_process cant spawn 'some-nonexistant-command'
return expect(
sh.execShellScript('something', 'some-nonexistant-command'),
).rejects.toBe('Failed to execute something')
})
})

describe('getDocumentation', () => {
it('returns null for an unknown builtin', async () => {
const result = await sh.getShellDocumentation({ word: 'foobar' })
Expand Down
17 changes: 10 additions & 7 deletions server/src/util/sh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ import * as ChildProcess from 'child_process'
/**
* Execute the following sh program.
*/
export function execShellScript(body: string): Promise<string> {
export function execShellScript(body: string, cmd = 'bash'): Promise<string> {
const args = ['-c', body]
const process = ChildProcess.spawn('bash', args)
const process = ChildProcess.spawn(cmd, args)

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

process.stdout.on('data', buffer => {
output += buffer
})

process.on('close', returnCode => {
const handleClose = (returnCode: number | Error) => {
if (returnCode === 0) {
resolve(output)
} else {
reject(`Failed to execute ${body}`)
}
}

process.stdout.on('data', buffer => {
output += buffer
})

process.on('close', handleClose)
process.on('error', handleClose)
})
}

Expand Down