@@ -216,56 +216,53 @@ test('options.fatal = false and unknown command', t => {
216
216
// async
217
217
//
218
218
219
- test . cb ( 'no callback' , t => {
219
+ function execAsync ( ...execArgs ) {
220
+ return new Promise ( ( resolve ) => {
221
+ shell . exec ( ...execArgs , ( code , stdout , stderr ) => {
222
+ resolve ( { code, stdout, stderr } ) ;
223
+ } ) ;
224
+ } ) ;
225
+ }
226
+
227
+ test ( 'no callback' , t => {
220
228
const c = shell . exec ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(1234)"` , { async : true } ) ;
221
229
t . falsy ( shell . error ( ) ) ;
222
230
t . truthy ( 'stdout' in c , 'async exec returns child process object' ) ;
223
- t . end ( ) ;
224
231
} ) ;
225
232
226
- test . cb ( 'callback as 2nd argument' , t => {
227
- shell . exec ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(5678);"` , ( code , stdout , stderr ) => {
228
- t . is ( code , 0 ) ;
229
- t . is ( stdout , '5678\n' ) ;
230
- t . is ( stderr , '' ) ;
231
- t . end ( ) ;
232
- } ) ;
233
+ test ( 'callback as 2nd argument' , async t => {
234
+ const result = await execAsync ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(5678);"` ) ;
235
+ t . is ( result . code , 0 ) ;
236
+ t . is ( result . stdout , '5678\n' ) ;
237
+ t . is ( result . stderr , '' ) ;
233
238
} ) ;
234
239
235
- test . cb ( 'callback as end argument' , t => {
236
- shell . exec ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(5566);"` , { async : true } , ( code , stdout , stderr ) => {
237
- t . is ( code , 0 ) ;
238
- t . is ( stdout , '5566\n' ) ;
239
- t . is ( stderr , '' ) ;
240
- t . end ( ) ;
241
- } ) ;
240
+ test ( 'callback as end argument' , async t => {
241
+ const result = await execAsync ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(5566);"` , { async : true } ) ;
242
+ t . is ( result . code , 0 ) ;
243
+ t . is ( result . stdout , '5566\n' ) ;
244
+ t . is ( result . stderr , '' ) ;
242
245
} ) ;
243
246
244
- test . cb ( 'callback as 3rd argument (silent:true)' , t => {
245
- shell . exec ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(5678);"` , { silent : true } , ( code , stdout , stderr ) => {
246
- t . is ( code , 0 ) ;
247
- t . is ( stdout , '5678\n' ) ;
248
- t . is ( stderr , '' ) ;
249
- t . end ( ) ;
250
- } ) ;
247
+ test ( 'callback as 3rd argument (silent:true)' , async t => {
248
+ const result = await execAsync ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(5678);"` , { silent : true } ) ;
249
+ t . is ( result . code , 0 ) ;
250
+ t . is ( result . stdout , '5678\n' ) ;
251
+ t . is ( result . stderr , '' ) ;
251
252
} ) ;
252
253
253
- test . cb ( 'command that fails' , t => {
254
- shell . exec ( 'shx cp onlyOneCpArgument.txt' , { silent : true } , ( code , stdout , stderr ) => {
255
- t . is ( code , 1 ) ;
256
- t . is ( stdout , '' ) ;
257
- t . is ( stderr , 'cp: missing <source> and/or <dest>\n' ) ;
258
- t . end ( ) ;
259
- } ) ;
254
+ test ( 'command that fails' , async t => {
255
+ const result = await execAsync ( 'shx cp onlyOneCpArgument.txt' , { silent : true } ) ;
256
+ t . is ( result . code , 1 ) ;
257
+ t . is ( result . stdout , '' ) ;
258
+ t . is ( result . stderr , 'cp: missing <source> and/or <dest>\n' ) ;
260
259
} ) ;
261
260
262
- test . cb ( 'encoding option works with async' , t => {
263
- shell . exec ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(5566);"` , { async : true , encoding : 'buffer' } , ( code , stdout , stderr ) => {
264
- t . is ( code , 0 ) ;
265
- t . truthy ( Buffer . isBuffer ( stdout ) ) ;
266
- t . truthy ( Buffer . isBuffer ( stderr ) ) ;
267
- t . is ( stdout . toString ( ) , '5566\n' ) ;
268
- t . is ( stderr . toString ( ) , '' ) ;
269
- t . end ( ) ;
270
- } ) ;
261
+ test ( 'encoding option works with async' , async t => {
262
+ const result = await execAsync ( `${ JSON . stringify ( shell . config . execPath ) } -e "console.log(5566);"` , { async : true , encoding : 'buffer' } ) ;
263
+ t . is ( result . code , 0 ) ;
264
+ t . truthy ( Buffer . isBuffer ( result . stdout ) ) ;
265
+ t . truthy ( Buffer . isBuffer ( result . stderr ) ) ;
266
+ t . is ( result . stdout . toString ( ) , '5566\n' ) ;
267
+ t . is ( result . stderr . toString ( ) , '' ) ;
271
268
} ) ;
0 commit comments