@@ -117,13 +117,20 @@ describe('PythonShell', function () {
117117 before ( ( ) => {
118118 PythonShell . defaultOptions = { } ;
119119 } )
120- it ( 'should be able to execute a string of python code' , function ( done ) {
121- PythonShell . runString ( 'print("hello");print("world")' , null , function ( err , results ) {
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 ) {
122122 if ( err ) return done ( err ) ;
123123 results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
124124 results . should . eql ( [ 'hello' , 'world' ] ) ;
125125 done ( ) ;
126126 } ) ;
127+ 128+ pythonshell . should . be . an . instanceOf ( PythonShell ) ;
129+ } ) ;
130+ it ( 'should be able to execute a string of python code using promises' , async function ( ) {
131+ let results = await PythonShell . runString ( 'print("hello");print("world")' ) ;
132+ results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
133+ results . should . eql ( [ 'hello' , 'world' ] ) ;
127134 } ) ;
128135 after ( ( ) => {
129136 PythonShell . defaultOptions = {
@@ -134,7 +141,7 @@ describe('PythonShell', function () {
134141 } ) ;
135142
136143 describe ( '#run(script, options)' , function ( ) {
137- it ( 'should run the script and return output data' , function ( done ) {
144+ it ( 'should run the script and return output data using callbacks ' , function ( done ) {
138145 PythonShell . run ( 'echo_args.py' , {
139146 args : [ 'hello' , 'world' ]
140147 } , function ( err , results ) {
@@ -144,13 +151,29 @@ describe('PythonShell', function () {
144151 done ( ) ;
145152 } ) ;
146153 } ) ;
154+ it ( 'should run the script and return output data using promise' , async function ( ) {
155+ let results = await PythonShell . run ( 'echo_args.py' , {
156+ args : [ 'hello' , 'world' ]
157+ } ) ;
158+ results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
159+ results . should . eql ( [ 'hello' , 'world' ] ) ;
160+ } ) ;
147161 it ( 'should try to run the script and fail appropriately' , function ( done ) {
148162 PythonShell . run ( 'unknown_script.py' , null , function ( err , results ) {
149163 err . should . be . an . Error ;
150164 err . exitCode . should . be . exactly ( 2 ) ;
151165 done ( ) ;
152166 } ) ;
153167 } ) ;
168+ it ( 'should try to run the script and fail appropriately' , async function ( ) {
169+ try {
170+ let results = await PythonShell . run ( 'unknown_script.py' ) ;
171+ throw new Error ( `should not get here because the script should fail` + results ) ;
172+ } catch ( err ) {
173+ err . should . be . an . Error ;
174+ err . exitCode . should . be . exactly ( 2 ) ;
175+ }
176+ } ) ;
154177 it ( 'should include both output and error' , function ( done ) {
155178 PythonShell . run ( 'echo_hi_then_error.py' , null , function ( err , results ) {
156179 err . should . be . an . Error ;
@@ -250,8 +273,8 @@ describe('PythonShell', function () {
250273 } ;
251274 } )
252275
253- it ( 'should run PythonShell normally without access to std streams' , function ( done ) {
254- var pyshell = PythonShell . run ( 'exit-code.py' , {
276+ it ( 'should run PythonShell normally without access to std streams' , async function ( ) {
277+ var pyshell = await PythonShell . run ( 'exit-code.py' , {
255278 // 3 different ways of assigning values to the std streams in child_process.spawn()
256279 // * ignore - pipe to /dev/null
257280 // * inherit - inherit fd from parent process;
@@ -261,12 +284,10 @@ describe('PythonShell', function () {
261284 // although the user shouldn't be doing this. We are just testing for
262285 // increased code coverage
263286 args : "0"
264- } , done ) ;
287+ } ) ;
265288
266- should ( pyshell . stdin ) . be . eql ( null ) ;
267- should ( pyshell . stdout ) . be . eql ( null ) ;
268- should ( pyshell . stderr ) . be . eql ( null ) ;
269- should . throws ( ( ) => { pyshell . send ( "asd" ) } ) ;
289+ should ( pyshell ) . be . eql ( [ ] ) ;
290+ 270291 } ) ;
271292 } ) ;
272293
0 commit comments