@@ -117,21 +117,23 @@ 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 ( ) ;
126125 } ) ;
127- 128- pythonshell . should . be . an . instanceOf ( PythonShell ) ;
129126 } ) ;
130127 it ( 'should be able to execute a string of python code using promises' , async function ( ) {
131128 let results = await PythonShell . runString ( 'print("hello");print("world")' ) ;
132129 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
133130 results . should . eql ( [ 'hello' , 'world' ] ) ;
134131 } ) ;
132+ it ( 'should be able to execute a string of python code async' , async function ( ) {
133+ let results = await PythonShell . runString ( 'print("hello");print("world")' ) ;
134+ results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
135+ results . should . eql ( [ 'hello' , 'world' ] ) ;
136+ } ) ;
135137 after ( ( ) => {
136138 PythonShell . defaultOptions = {
137139 // reset to match initial value
@@ -144,28 +146,27 @@ describe('PythonShell', function () {
144146 it ( 'should run the script and return output data using callbacks' , function ( done ) {
145147 PythonShell . run ( 'echo_args.py' , {
146148 args : [ 'hello' , 'world' ]
147- } , function ( err , results ) {
148- if ( err ) return done ( err ) ;
149+ } ) . then ( ( results ) => {
149150 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
150151 results . should . eql ( [ 'hello' , 'world' ] ) ;
151152 done ( ) ;
152153 } ) ;
153154 } ) ;
154- it ( 'should run the script and return output data using promise ' , async function ( ) {
155+ it ( 'should run the script and return output data async ' , async function ( ) {
155156 let results = await PythonShell . run ( 'echo_args.py' , {
156157 args : [ 'hello' , 'world' ]
157158 } ) ;
158159 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
159160 results . should . eql ( [ 'hello' , 'world' ] ) ;
160161 } ) ;
161162 it ( 'should try to run the script and fail appropriately' , function ( done ) {
162- PythonShell . run ( 'unknown_script.py' , null , function ( err , results ) {
163+ PythonShell . run ( 'unknown_script.py' , null ) . catch ( ( err ) => {
163164 err . should . be . an . Error ;
164165 err . exitCode . should . be . exactly ( 2 ) ;
165166 done ( ) ;
166167 } ) ;
167168 } ) ;
168- it ( 'should try to run the script and fail appropriately' , async function ( ) {
169+ it ( 'should try to run the script and fail appropriately - async ' , async function ( ) {
169170 try {
170171 let results = await PythonShell . run ( 'unknown_script.py' ) ;
171172 throw new Error ( `should not get here because the script should fail` + results ) ;
@@ -175,22 +176,24 @@ describe('PythonShell', function () {
175176 }
176177 } ) ;
177178 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 ( ) ;
179+ PythonShell . run ( 'echo_hi_then_error.py' , null ) . then ( ( results ) => {
180+ done ( "Error: This promise should never successfully resolve" ) ;
181+ } ) . catch ( ( err ) => {
182+ err . logs . should . eql ( [ 'hi' ] )
183+ err . should . be . an . Error
184+ done ( )
182185 } ) ;
183186 } ) ;
184187 it ( 'should run the script and fail with an extended stack trace' , function ( done ) {
185- PythonShell . run ( 'error.py' , null , function ( err , results ) {
188+ PythonShell . run ( 'error.py' , null ) . catch ( ( err ) => {
186189 err . should . be . an . Error ;
187190 err . exitCode . should . be . exactly ( 1 ) ;
188191 err . stack . should . containEql ( '----- Python Traceback -----' ) ;
189192 done ( ) ;
190193 } ) ;
191194 } ) ;
192195 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 ) {
196+ PythonShell . run ( 'error.py' , { mode : "binary" } ) . catch ( ( err ) => {
194197 err . should . be . an . Error ;
195198 err . exitCode . should . be . exactly ( 1 ) ;
196199 err . stack . should . containEql ( '----- Python Traceback -----' ) ;
@@ -210,7 +213,7 @@ describe('PythonShell', function () {
210213 }
211214 }
212215 function runSingleErrorScript ( callback ) {
213- PythonShell . run ( 'error.py' , null , function ( err , results ) {
216+ PythonShell . run ( 'error.py' , null ) . catch ( ( err ) => {
214217 err . should . be . an . Error ;
215218 err . exitCode . should . be . exactly ( 1 ) ;
216219 err . stack . should . containEql ( '----- Python Traceback -----' ) ;
@@ -234,8 +237,7 @@ describe('PythonShell', function () {
234237 function runSingleScript ( callback ) {
235238 PythonShell . run ( 'echo_args.py' , {
236239 args : [ 'hello' , 'world' ]
237- } , function ( err , results ) {
238- if ( err ) return done ( err ) ;
240+ } ) . then ( ( results ) => {
239241 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
240242 results . should . eql ( [ 'hello' , 'world' ] ) ;
241243 callback ( ) ;
@@ -249,14 +251,11 @@ describe('PythonShell', function () {
249251
250252 PythonShell . run ( '-m' , {
251253 args : [ 'timeit' , '-n 1' , `'x=5'` ]
252- } , function ( err , results ) {
253- 254+ } ) . then ( ( results ) => {
254255 PythonShell . defaultOptions = {
255256 // reset to match initial value
256257 scriptPath : pythonFolder
257258 } ;
258- 259- if ( err ) return done ( err ) ;
260259 results . should . be . an . Array ( ) ;
261260 results [ 0 ] . should . be . an . String ( ) ;
262261 results [ 0 ] . slice ( 0 , 6 ) . should . eql ( '1 loop' ) ;
@@ -522,17 +521,17 @@ describe('PythonShell', function () {
522521 let pyshell = new PythonShell ( 'error.py' ) ;
523522 pyshell . on ( 'pythonError' , function ( err ) {
524523 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' ) ;
524+ err . stack . should . containEql ( 'test' + sep + 'python' + sep + 'error.py", line 4' ) ;
525+ err . stack . should . containEql ( 'test' + sep + 'python' + sep + 'error.py", line 6' ) ;
527526 done ( ) ;
528527 } ) ;
529528 } ) ;
530529 it ( 'should work in json mode' , function ( done ) {
531530 let pyshell = new PythonShell ( 'error.py' , { mode : 'json' } ) ;
532531 pyshell . on ( 'pythonError' , function ( err ) {
533532 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' ) ;
533+ err . stack . should . containEql ( 'test' + sep + 'python' + sep + 'error.py", line 4' ) ;
534+ err . stack . should . containEql ( 'test' + sep + 'python' + sep + 'error.py", line 6' ) ;
536535 done ( ) ;
537536 } ) ;
538537 } ) ;
@@ -563,4 +562,4 @@ describe('PythonShell', function () {
563562 } , 500 ) ;
564563 } ) ;
565564 } ) ;
566- } ) ;
565+ } ) ;
0 commit comments