@@ -117,9 +117,8 @@ describe('PythonShell', function () {
117117 before ( ( ) => {
118118 PythonShell . defaultOptions = { } ;
119119 } )
120- it ( 'should be able to execute a string of python code using callbacks' , function ( done ) {
121- let pythonshell = PythonShell . runString ( 'print("hello");print("world")' , null , function ( err , results ) {
122- if ( err ) return done ( err ) ;
120+ it ( 'should be able to execute a string of python code' , function ( done ) {
121+ PythonShell . runString ( 'print("hello");print("world")' , null ) . then ( ( results ) => {
123122 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
124123 results . should . eql ( [ 'hello' , 'world' ] ) ;
125124 done ( ) ;
@@ -132,6 +131,11 @@ describe('PythonShell', function () {
132131 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
133132 results . should . eql ( [ 'hello' , 'world' ] ) ;
134133 } ) ;
134+ it ( 'should be able to execute a string of python code async' , async function ( ) {
135+ let results = await PythonShell . runString ( 'print("hello");print("world")' ) ;
136+ results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
137+ results . should . eql ( [ 'hello' , 'world' ] ) ;
138+ } ) ;
135139 after ( ( ) => {
136140 PythonShell . defaultOptions = {
137141 // reset to match initial value
@@ -144,28 +148,27 @@ describe('PythonShell', function () {
144148 it ( 'should run the script and return output data using callbacks' , function ( done ) {
145149 PythonShell . run ( 'echo_args.py' , {
146150 args : [ 'hello' , 'world' ]
147- } , function ( err , results ) {
148- if ( err ) return done ( err ) ;
151+ } ) . then ( ( results ) => {
149152 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
150153 results . should . eql ( [ 'hello' , 'world' ] ) ;
151154 done ( ) ;
152155 } ) ;
153156 } ) ;
154- it ( 'should run the script and return output data using promise ' , async function ( ) {
157+ it ( 'should run the script and return output data async ' , async function ( ) {
155158 let results = await PythonShell . run ( 'echo_args.py' , {
156159 args : [ 'hello' , 'world' ]
157160 } ) ;
158161 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
159162 results . should . eql ( [ 'hello' , 'world' ] ) ;
160163 } ) ;
161164 it ( 'should try to run the script and fail appropriately' , function ( done ) {
162- PythonShell . run ( 'unknown_script.py' , null , function ( err , results ) {
165+ PythonShell . run ( 'unknown_script.py' , null ) . catch ( ( err ) => {
163166 err . should . be . an . Error ;
164167 err . exitCode . should . be . exactly ( 2 ) ;
165168 done ( ) ;
166169 } ) ;
167170 } ) ;
168- it ( 'should try to run the script and fail appropriately' , async function ( ) {
171+ it ( 'should try to run the script and fail appropriately - async ' , async function ( ) {
169172 try {
170173 let results = await PythonShell . run ( 'unknown_script.py' ) ;
171174 throw new Error ( `should not get here because the script should fail` + results ) ;
@@ -175,22 +178,24 @@ describe('PythonShell', function () {
175178 }
176179 } ) ;
177180 it ( 'should include both output and error' , function ( done ) {
178- PythonShell . run ( 'echo_hi_then_error.py' , null , function ( err , results ) {
179- err . should . be . an . Error ;
180- results . should . eql ( [ 'hi' ] )
181- done ( ) ;
181+ PythonShell . run ( 'echo_hi_then_error.py' , null ) . then ( ( results ) => {
182+ done ( "Error: This promise should never successfully resolve" ) ;
183+ } ) . catch ( ( err ) => {
184+ err . logs . should . eql ( [ 'hi' ] )
185+ err . should . be . an . Error
186+ done ( )
182187 } ) ;
183188 } ) ;
184189 it ( 'should run the script and fail with an extended stack trace' , function ( done ) {
185- PythonShell . run ( 'error.py' , null , function ( err , results ) {
190+ PythonShell . run ( 'error.py' , null ) . catch ( ( err ) => {
186191 err . should . be . an . Error ;
187192 err . exitCode . should . be . exactly ( 1 ) ;
188193 err . stack . should . containEql ( '----- Python Traceback -----' ) ;
189194 done ( ) ;
190195 } ) ;
191196 } ) ;
192197 it ( 'should run the script and fail with an extended stack trace even when mode is binary' , function ( done ) {
193- PythonShell . run ( 'error.py' , { mode : "binary" } , function ( err , results ) {
198+ PythonShell . run ( 'error.py' , { mode : "binary" } ) . catch ( ( err ) => {
194199 err . should . be . an . Error ;
195200 err . exitCode . should . be . exactly ( 1 ) ;
196201 err . stack . should . containEql ( '----- Python Traceback -----' ) ;
@@ -210,7 +215,7 @@ describe('PythonShell', function () {
210215 }
211216 }
212217 function runSingleErrorScript ( callback ) {
213- PythonShell . run ( 'error.py' , null , function ( err , results ) {
218+ PythonShell . run ( 'error.py' , null ) . catch ( ( err ) => {
214219 err . should . be . an . Error ;
215220 err . exitCode . should . be . exactly ( 1 ) ;
216221 err . stack . should . containEql ( '----- Python Traceback -----' ) ;
@@ -234,8 +239,7 @@ describe('PythonShell', function () {
234239 function runSingleScript ( callback ) {
235240 PythonShell . run ( 'echo_args.py' , {
236241 args : [ 'hello' , 'world' ]
237- } , function ( err , results ) {
238- if ( err ) return done ( err ) ;
242+ } ) . then ( ( results ) => {
239243 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
240244 results . should . eql ( [ 'hello' , 'world' ] ) ;
241245 callback ( ) ;
@@ -249,14 +253,11 @@ describe('PythonShell', function () {
249253
250254 PythonShell . run ( '-m' , {
251255 args : [ 'timeit' , '-n 1' , `'x=5'` ]
252- } , function ( err , results ) {
253-
256+ } ) . then ( ( results ) => {
254257 PythonShell . defaultOptions = {
255258 // reset to match initial value
256259 scriptPath : pythonFolder
257260 } ;
258-
259- if ( err ) return done ( err ) ;
260261 results . should . be . an . Array ( ) ;
261262 results [ 0 ] . should . be . an . String ( ) ;
262263 results [ 0 ] . slice ( 0 , 6 ) . should . eql ( '1 loop' ) ;
@@ -522,17 +523,17 @@ describe('PythonShell', function () {
522523 let pyshell = new PythonShell ( 'error.py' ) ;
523524 pyshell . on ( 'pythonError' , function ( err ) {
524525 err . stack . should . containEql ( '----- Python Traceback -----' ) ;
525- err . stack . should . containEql ( 'File " test' + sep + 'python' + sep + 'error.py", line 4' ) ;
526- err . stack . should . containEql ( 'File " test' + sep + 'python' + sep + 'error.py", line 6' ) ;
526+ err . stack . should . containEql ( 'test' + sep + 'python' + sep + 'error.py", line 4' ) ;
527+ err . stack . should . containEql ( 'test' + sep + 'python' + sep + 'error.py", line 6' ) ;
527528 done ( ) ;
528529 } ) ;
529530 } ) ;
530531 it ( 'should work in json mode' , function ( done ) {
531532 let pyshell = new PythonShell ( 'error.py' , { mode : 'json' } ) ;
532533 pyshell . on ( 'pythonError' , function ( err ) {
533534 err . stack . should . containEql ( '----- Python Traceback -----' ) ;
534- err . stack . should . containEql ( 'File " test' + sep + 'python' + sep + 'error.py", line 4' ) ;
535- err . stack . should . containEql ( 'File " test' + sep + 'python' + sep + 'error.py", line 6' ) ;
535+ err . stack . should . containEql ( 'test' + sep + 'python' + sep + 'error.py", line 4' ) ;
536+ err . stack . should . containEql ( 'test' + sep + 'python' + sep + 'error.py", line 6' ) ;
536537 done ( ) ;
537538 } ) ;
538539 } ) ;
@@ -563,4 +564,4 @@ describe('PythonShell', function () {
563564 } , 500 ) ;
564565 } ) ;
565566 } ) ;
566- } ) ;
567+ } ) ;
0 commit comments