@@ -117,21 +117,23 @@ describe('PythonShell', function () {
117
117
before ( ( ) => {
118
118
PythonShell . defaultOptions = { } ;
119
119
} )
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 ) => {
123
122
results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
124
123
results . should . eql ( [ 'hello' , 'world' ] ) ;
125
124
done ( ) ;
126
125
} ) ;
127
-
128
- pythonshell . should . be . an . instanceOf ( PythonShell ) ;
129
126
} ) ;
130
127
it ( 'should be able to execute a string of python code using promises' , async function ( ) {
131
128
let results = await PythonShell . runString ( 'print("hello");print("world")' ) ;
132
129
results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
133
130
results . should . eql ( [ 'hello' , 'world' ] ) ;
134
131
} ) ;
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
+ } ) ;
135
137
after ( ( ) => {
136
138
PythonShell . defaultOptions = {
137
139
// reset to match initial value
@@ -144,28 +146,27 @@ describe('PythonShell', function () {
144
146
it ( 'should run the script and return output data using callbacks' , function ( done ) {
145
147
PythonShell . run ( 'echo_args.py' , {
146
148
args : [ 'hello' , 'world' ]
147
- } , function ( err , results ) {
148
- if ( err ) return done ( err ) ;
149
+ } ) . then ( ( results ) => {
149
150
results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
150
151
results . should . eql ( [ 'hello' , 'world' ] ) ;
151
152
done ( ) ;
152
153
} ) ;
153
154
} ) ;
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 ( ) {
155
156
let results = await PythonShell . run ( 'echo_args.py' , {
156
157
args : [ 'hello' , 'world' ]
157
158
} ) ;
158
159
results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
159
160
results . should . eql ( [ 'hello' , 'world' ] ) ;
160
161
} ) ;
161
162
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 ) => {
163
164
err . should . be . an . Error ;
164
165
err . exitCode . should . be . exactly ( 2 ) ;
165
166
done ( ) ;
166
167
} ) ;
167
168
} ) ;
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 ( ) {
169
170
try {
170
171
let results = await PythonShell . run ( 'unknown_script.py' ) ;
171
172
throw new Error ( `should not get here because the script should fail` + results ) ;
@@ -175,22 +176,24 @@ describe('PythonShell', function () {
175
176
}
176
177
} ) ;
177
178
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 ( )
182
185
} ) ;
183
186
} ) ;
184
187
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 ) => {
186
189
err . should . be . an . Error ;
187
190
err . exitCode . should . be . exactly ( 1 ) ;
188
191
err . stack . should . containEql ( '----- Python Traceback -----' ) ;
189
192
done ( ) ;
190
193
} ) ;
191
194
} ) ;
192
195
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 ) => {
194
197
err . should . be . an . Error ;
195
198
err . exitCode . should . be . exactly ( 1 ) ;
196
199
err . stack . should . containEql ( '----- Python Traceback -----' ) ;
@@ -210,7 +213,7 @@ describe('PythonShell', function () {
210
213
}
211
214
}
212
215
function runSingleErrorScript ( callback ) {
213
- PythonShell . run ( 'error.py' , null , function ( err , results ) {
216
+ PythonShell . run ( 'error.py' , null ) . catch ( ( err ) => {
214
217
err . should . be . an . Error ;
215
218
err . exitCode . should . be . exactly ( 1 ) ;
216
219
err . stack . should . containEql ( '----- Python Traceback -----' ) ;
@@ -234,8 +237,7 @@ describe('PythonShell', function () {
234
237
function runSingleScript ( callback ) {
235
238
PythonShell . run ( 'echo_args.py' , {
236
239
args : [ 'hello' , 'world' ]
237
- } , function ( err , results ) {
238
- if ( err ) return done ( err ) ;
240
+ } ) . then ( ( results ) => {
239
241
results . should . be . an . Array ( ) . and . have . lengthOf ( 2 ) ;
240
242
results . should . eql ( [ 'hello' , 'world' ] ) ;
241
243
callback ( ) ;
@@ -249,14 +251,11 @@ describe('PythonShell', function () {
249
251
250
252
PythonShell . run ( '-m' , {
251
253
args : [ 'timeit' , '-n 1' , `'x=5'` ]
252
- } , function ( err , results ) {
253
-
254
+ } ) . then ( ( results ) => {
254
255
PythonShell . defaultOptions = {
255
256
// reset to match initial value
256
257
scriptPath : pythonFolder
257
258
} ;
258
-
259
- if ( err ) return done ( err ) ;
260
259
results . should . be . an . Array ( ) ;
261
260
results [ 0 ] . should . be . an . String ( ) ;
262
261
results [ 0 ] . slice ( 0 , 6 ) . should . eql ( '1 loop' ) ;
@@ -522,17 +521,17 @@ describe('PythonShell', function () {
522
521
let pyshell = new PythonShell ( 'error.py' ) ;
523
522
pyshell . on ( 'pythonError' , function ( err ) {
524
523
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' ) ;
527
526
done ( ) ;
528
527
} ) ;
529
528
} ) ;
530
529
it ( 'should work in json mode' , function ( done ) {
531
530
let pyshell = new PythonShell ( 'error.py' , { mode : 'json' } ) ;
532
531
pyshell . on ( 'pythonError' , function ( err ) {
533
532
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' ) ;
536
535
done ( ) ;
537
536
} ) ;
538
537
} ) ;
@@ -563,4 +562,4 @@ describe('PythonShell', function () {
563
562
} , 500 ) ;
564
563
} ) ;
565
564
} ) ;
566
- } ) ;
565
+ } ) ;
0 commit comments