File tree Expand file tree Collapse file tree 2 files changed +23
-7
lines changed Expand file tree Collapse file tree 2 files changed +23
-7
lines changed Original file line number Diff line number Diff line change 1
1
/* eslint-disable no-useless-escape */
2
2
import * as sh from '../sh'
3
3
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
+
4
17
describe ( 'getDocumentation' , ( ) => {
5
18
it ( 'returns null for an unknown builtin' , async ( ) => {
6
19
const result = await sh . getShellDocumentation ( { word : 'foobar' } )
Original file line number Diff line number Diff line change @@ -3,24 +3,27 @@ import * as ChildProcess from 'child_process'
3
3
/**
4
4
* Execute the following sh program.
5
5
*/
6
- export function execShellScript ( body : string ) : Promise < string > {
6
+ export function execShellScript ( body : string , cmd = 'bash' ) : Promise < string > {
7
7
const args = [ '-c' , body ]
8
- const process = ChildProcess . spawn ( 'bash' , args )
8
+ const process = ChildProcess . spawn ( cmd , args )
9
9
10
10
return new Promise ( ( resolve , reject ) => {
11
11
let output = ''
12
12
13
- process . stdout . on ( 'data' , buffer => {
14
- output += buffer
15
- } )
16
-
17
- process . on ( 'close' , returnCode => {
13
+ const handleClose = ( returnCode : number | Error ) => {
18
14
if ( returnCode === 0 ) {
19
15
resolve ( output )
20
16
} else {
21
17
reject ( `Failed to execute ${ body } ` )
22
18
}
19
+ }
20
+
21
+ process . stdout . on ( 'data' , buffer => {
22
+ output += buffer
23
23
} )
24
+
25
+ process . on ( 'close' , handleClose )
26
+ process . on ( 'error' , handleClose )
24
27
} )
25
28
}
26
29
You can’t perform that action at this time.
0 commit comments