| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 593f095 commit 59ae034
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,6 +40,7 @@ export interface Options extends SpawnOptions{ | |||
| 40 | 40 | ||
| 41 | 41 | class PythonShellError extends Error{ | |
| 42 | 42 | traceback: string | Buffer; | |
| 43 | + exitCode?:number; | ||
| 43 | 44 | } | |
| 44 | 45 | ||
| 45 | 46 | /** | |
@@ -59,10 +60,10 @@ export class PythonShell extends EventEmitter{ | |||
| 59 | 60 | stdin: Writable; | |
| 60 | 61 | stdout: Readable; | |
| 61 | 62 | stderr: Readable; | |
| 63 | + exitSignal:string; | ||
| 64 | + exitCode:number; | ||
| 62 | 65 | private stderrHasEnded:boolean; | |
| 63 | 66 | private stdoutHasEnded:boolean; | |
| 64 | - private exitCode:number; | ||
| 65 | - private exitSignal:string; | ||
| 66 | 67 | private _remaining:string | |
| 67 | 68 | private _endCallback:(err:PythonShellError, exitCode:number, exitSignal:string)=>any | |
| 68 | 69 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | - var should = require('should'); | ||
| 2 | - var PythonShell = require('..'); | ||
| 3 | - var path = require('path') | ||
| 4 | - const newline = require('os').EOL | ||
| 1 | + import * as should from 'should'; | ||
| 2 | + import {PythonShell} from '..' | ||
| 3 | + import {sep} from 'path' | ||
| 4 | + import {EOL as newline} from 'os' | ||
| 5 | 5 | ||
| 6 | 6 | describe('PythonShell', function () { | |
| 7 | 7 | ||
@@ -11,8 +11,8 @@ describe('PythonShell', function () { | |||
| 11 | 11 | ||
| 12 | 12 | describe('#ctor(script, options)', function () { | |
| 13 | 13 | it('should spawn a Python process', function (done) { | |
| 14 | - var pyshell = new PythonShell('exit-code.py'); | ||
| 15 | - pyshell.command.should.eql(['test' + path.sep + 'python' + path.sep + 'exit-code.py']); | ||
| 14 | + let pyshell = new PythonShell('exit-code.py'); | ||
| 15 | + pyshell.command.should.eql(['test' + sep + 'python' + sep + 'exit-code.py']); | ||
| 16 | 16 | pyshell.terminated.should.be.false; | |
| 17 | 17 | pyshell.end(function (err) { | |
| 18 | 18 | if (err) return done(err); | |
@@ -21,17 +21,17 @@ describe('PythonShell', function () { | |||
| 21 | 21 | }); | |
| 22 | 22 | }); | |
| 23 | 23 | it('should spawn a Python process with options', function (done) { | |
| 24 | - var pyshell = new PythonShell('exit-code.py', { | ||
| 25 | - pythonOptions: '-u' | ||
| 24 | + let pyshell = new PythonShell('exit-code.py', { | ||
| 25 | + pythonOptions: ['-u'] | ||
| 26 | 26 | }); | |
| 27 | - pyshell.command.should.eql(['-u', 'test' + path.sep + 'python' + path.sep + 'exit-code.py']); | ||
| 27 | + pyshell.command.should.eql(['-u', 'test' + sep + 'python' + sep + 'exit-code.py']); | ||
| 28 | 28 | pyshell.end(done); | |
| 29 | 29 | }); | |
| 30 | 30 | it('should spawn a Python process with script arguments', function (done) { | |
| 31 | - var pyshell = new PythonShell('echo_args.py', { | ||
| 31 | + let pyshell = new PythonShell('echo_args.py', { | ||
| 32 | 32 | args: ['hello', 'world'] | |
| 33 | 33 | }); | |
| 34 | - pyshell.command.should.eql(['test' + path.sep + 'python' + path.sep + 'echo_args.py', 'hello', 'world']); | ||
| 34 | + pyshell.command.should.eql(['test' + sep + 'python' + sep + 'echo_args.py', 'hello', 'world']); | ||
| 35 | 35 | pyshell.end(done); | |
| 36 | 36 | }); | |
| 37 | 37 | }); | |
@@ -48,34 +48,34 @@ describe('PythonShell', function () { | |||
| 48 | 48 | }); | |
| 49 | 49 | }); | |
| 50 | 50 | it('should try to run the script and fail appropriately', function (done) { | |
| 51 | - PythonShell.run('unknown_script.py', function (err, results) { | ||
| 51 | + PythonShell.run('unknown_script.py', null, function (err, results) { | ||
| 52 | 52 | err.should.be.an.Error; | |
| 53 | 53 | err.exitCode.should.be.exactly(2); | |
| 54 | 54 | done(); | |
| 55 | 55 | }); | |
| 56 | 56 | }); | |
| 57 | 57 | it('should run the script and fail with an extended stack trace', function (done) { | |
| 58 | - PythonShell.run('error.py', function (err, results) { | ||
| 58 | + PythonShell.run('error.py', null, function (err, results) { | ||
| 59 | 59 | err.should.be.an.Error; | |
| 60 | 60 | err.exitCode.should.be.exactly(1); | |
| 61 | 61 | err.stack.should.containEql('----- Python Traceback -----'); | |
| 62 | 62 | done(); | |
| 63 | 63 | }); | |
| 64 | 64 | }); | |
| 65 | 65 | it('should run multiple scripts and fail with an extended stack trace for each of them', function (done) { | |
| 66 | - var numberOfTimesToRun = 20; | ||
| 67 | - for (var i = 0; i < numberOfTimesToRun; i++) { | ||
| 66 | + let numberOfTimesToRun = 20; | ||
| 67 | + for (let i = 0; i < numberOfTimesToRun; i++) { | ||
| 68 | 68 | runSingleErrorScript(end); | |
| 69 | 69 | } | |
| 70 | - var count = 0; | ||
| 70 | + let count = 0; | ||
| 71 | 71 | function end() { | |
| 72 | 72 | count++; | |
| 73 | 73 | if (count === numberOfTimesToRun) { | |
| 74 | 74 | done(); | |
| 75 | 75 | } | |
| 76 | 76 | } | |
| 77 | 77 | function runSingleErrorScript(callback) { | |
| 78 | - PythonShell.run('error.py', function (err, results) { | ||
| 78 | + PythonShell.run('error.py', null, function (err, results) { | ||
| 79 | 79 | err.should.be.an.Error; | |
| 80 | 80 | err.exitCode.should.be.exactly(1); | |
| 81 | 81 | err.stack.should.containEql('----- Python Traceback -----'); | |
@@ -85,11 +85,11 @@ describe('PythonShell', function () { | |||
| 85 | 85 | }); | |
| 86 | 86 | ||
| 87 | 87 | it('should run multiple scripts and return output data for each of them', function (done) { | |
| 88 | - var numberOfTimesToRun = 20; | ||
| 89 | - for (var i = 0; i < numberOfTimesToRun; i++) { | ||
| 88 | + let numberOfTimesToRun = 20; | ||
| 89 | + for (let i = 0; i < numberOfTimesToRun; i++) { | ||
| 90 | 90 | runSingleScript(end); | |
| 91 | 91 | } | |
| 92 | - var count = 0; | ||
| 92 | + let count = 0; | ||
| 93 | 93 | function end() { | |
| 94 | 94 | count++; | |
| 95 | 95 | if (count === numberOfTimesToRun) { | |
@@ -112,10 +112,10 @@ describe('PythonShell', function () { | |||
| 112 | 112 | ||
| 113 | 113 | describe('.send(message)', function () { | |
| 114 | 114 | it('should send string messages when mode is "text"', function (done) { | |
| 115 | - var pyshell = new PythonShell('echo_text.py', { | ||
| 115 | + let pyshell = new PythonShell('echo_text.py', { | ||
| 116 | 116 | mode: 'text' | |
| 117 | 117 | }); | |
| 118 | - var output = ''; | ||
| 118 | + let output = ''; | ||
| 119 | 119 | pyshell.stdout.on('data', function (data) { | |
| 120 | 120 | output += ''+data; | |
| 121 | 121 | }); | |
@@ -126,10 +126,10 @@ describe('PythonShell', function () { | |||
| 126 | 126 | }); | |
| 127 | 127 | }); | |
| 128 | 128 | it('should send JSON messages when mode is "json"', function (done) { | |
| 129 | - var pyshell = new PythonShell('echo_json.py', { | ||
| 129 | + let pyshell = new PythonShell('echo_json.py', { | ||
| 130 | 130 | mode: 'json' | |
| 131 | 131 | }); | |
| 132 | - var output = ''; | ||
| 132 | + let output = ''; | ||
| 133 | 133 | pyshell.stdout.on('data', function (data) { | |
| 134 | 134 | output += ''+data; | |
| 135 | 135 | }); | |
@@ -140,12 +140,12 @@ describe('PythonShell', function () { | |||
| 140 | 140 | }); | |
| 141 | 141 | }); | |
| 142 | 142 | it('should use a custom formatter', function (done) { | |
| 143 | - var pyshell = new PythonShell('echo_text.py', { | ||
| 143 | + let pyshell = new PythonShell('echo_text.py', { | ||
| 144 | 144 | formatter: function (message) { | |
| 145 | 145 | return message.toUpperCase(); | |
| 146 | 146 | } | |
| 147 | 147 | }); | |
| 148 | - var output = ''; | ||
| 148 | + let output = ''; | ||
| 149 | 149 | pyshell.stdout.on('data', function (data) { | |
| 150 | 150 | output += ''+data; | |
| 151 | 151 | }); | |
@@ -156,10 +156,10 @@ describe('PythonShell', function () { | |||
| 156 | 156 | }); | |
| 157 | 157 | }); | |
| 158 | 158 | it('should write as-is when mode is "binary"', function (done) { | |
| 159 | - var pyshell = new PythonShell('echo_binary.py', { | ||
| 159 | + let pyshell = new PythonShell('echo_binary.py', { | ||
| 160 | 160 | mode: 'binary' | |
| 161 | 161 | }); | |
| 162 | - var output = ''; | ||
| 162 | + let output = ''; | ||
| 163 | 163 | pyshell.stdout.on('data', function (data) { | |
| 164 | 164 | output += ''+data; | |
| 165 | 165 | }); | |
@@ -173,10 +173,10 @@ describe('PythonShell', function () { | |||
| 173 | 173 | ||
| 174 | 174 | describe('.receive(data)', function () { | |
| 175 | 175 | it('should emit messages as strings when mode is "text"', function (done) { | |
| 176 | - var pyshell = new PythonShell('echo_text.py', { | ||
| 176 | + let pyshell = new PythonShell('echo_text.py', { | ||
| 177 | 177 | mode: 'text' | |
| 178 | 178 | }); | |
| 179 | - var count = 0; | ||
| 179 | + let count = 0; | ||
| 180 | 180 | pyshell.on('message', function (message) { | |
| 181 | 181 | count === 0 && message.should.be.exactly('hello'); | |
| 182 | 182 | count === 1 && message.should.be.exactly('world'); | |
@@ -186,10 +186,10 @@ describe('PythonShell', function () { | |||
| 186 | 186 | }).send('hello').send('world').end(done); | |
| 187 | 187 | }); | |
| 188 | 188 | it('should emit messages as JSON when mode is "json"', function (done) { | |
| 189 | - var pyshell = new PythonShell('echo_json.py', { | ||
| 189 | + let pyshell = new PythonShell('echo_json.py', { | ||
| 190 | 190 | mode: 'json' | |
| 191 | 191 | }); | |
| 192 | - var count = 0; | ||
| 192 | + let count = 0; | ||
| 193 | 193 | pyshell.send({ a: 'b' }).send(null).send([1, 2, 3, 4, 5]); | |
| 194 | 194 | pyshell.on('message', function (message) { | |
| 195 | 195 | count === 0 && message.should.eql({ a: 'b' }); | |
@@ -201,7 +201,7 @@ describe('PythonShell', function () { | |||
| 201 | 201 | }).end(done); | |
| 202 | 202 | }); | |
| 203 | 203 | it('should properly buffer partial messages', function (done) { | |
| 204 | - var pyshell = new PythonShell('echo_json.py', { | ||
| 204 | + let pyshell = new PythonShell('echo_json.py', { | ||
| 205 | 205 | mode: 'json' | |
| 206 | 206 | }); | |
| 207 | 207 | pyshell.on('message', function (message) { | |
@@ -210,7 +210,7 @@ describe('PythonShell', function () { | |||
| 210 | 210 | }).receive('{"a"').receive(':').receive('true}'+newline+'').end(done); | |
| 211 | 211 | }); | |
| 212 | 212 | it('should not be invoked when mode is "binary"', function (done) { | |
| 213 | - var pyshell = new PythonShell('echo_args.py', { | ||
| 213 | + let pyshell = new PythonShell('echo_args.py', { | ||
| 214 | 214 | args: ['hello', 'world'], | |
| 215 | 215 | mode: 'binary' | |
| 216 | 216 | }); | |
@@ -220,13 +220,13 @@ describe('PythonShell', function () { | |||
| 220 | 220 | pyshell.end(done); | |
| 221 | 221 | }); | |
| 222 | 222 | it('should use a custom parser function', function (done) { | |
| 223 | - var pyshell = new PythonShell('echo_text.py', { | ||
| 223 | + let pyshell = new PythonShell('echo_text.py', { | ||
| 224 | 224 | mode: 'text', | |
| 225 | 225 | parser: function (message) { | |
| 226 | 226 | return message.toUpperCase(); | |
| 227 | 227 | } | |
| 228 | 228 | }); | |
| 229 | - var count = 0; | ||
| 229 | + let count = 0; | ||
| 230 | 230 | pyshell.on('message', function (message) { | |
| 231 | 231 | count === 0 && message.should.be.exactly('HELLO'); | |
| 232 | 232 | count === 1 && message.should.be.exactly('WORLD!'); | |
@@ -239,16 +239,16 @@ describe('PythonShell', function () { | |||
| 239 | 239 | ||
| 240 | 240 | describe('.end(callback)', function () { | |
| 241 | 241 | it('should end normally when exit code is zero', function (done) { | |
| 242 | - var pyshell = new PythonShell('exit-code.py'); | ||
| 242 | + let pyshell = new PythonShell('exit-code.py'); | ||
| 243 | 243 | pyshell.end(function (err,code,signal) { | |
| 244 | 244 | if (err) return done(err); | |
| 245 | 245 | code.should.be.exactly(0); | |
| 246 | 246 | done(); | |
| 247 | 247 | }); | |
| 248 | 248 | }); | |
| 249 | 249 | it('should emit error if exit code is not zero', function (done) { | |
| 250 | - var pyshell = new PythonShell('exit-code.py', { | ||
| 251 | - args: 3 | ||
| 250 | + let pyshell = new PythonShell('exit-code.py', { | ||
| 251 | + args: ['3'] | ||
| 252 | 252 | }); | |
| 253 | 253 | pyshell.on('error', function (err) { | |
| 254 | 254 | err.should.have.properties({ | |
@@ -258,8 +258,8 @@ describe('PythonShell', function () { | |||
| 258 | 258 | done(); | |
| 259 | 259 | }); | |
| 260 | 260 | }); | |
| 261 | - it('should emit error when data is written to stderr', function (done) { | ||
| 262 | - var pyshell = new PythonShell('error.py'); | ||
| 261 | + it('should emit error when error is written to stderr', function (done) { | ||
| 262 | + let pyshell = new PythonShell('error.py'); | ||
| 263 | 263 | pyshell.on('error', function (err) { | |
| 264 | 264 | err.message.should.be.equalOneOf('ZeroDivisionError: integer division or modulo by zero','ZeroDivisionError: division by zero'); | |
| 265 | 265 | err.should.have.property('traceback'); | |
@@ -271,35 +271,35 @@ describe('PythonShell', function () { | |||
| 271 | 271 | ||
| 272 | 272 | describe('.parseError(data)', function () { | |
| 273 | 273 | it('should extend error with context properties', function (done) { | |
| 274 | - var pyshell = new PythonShell('exit-code.py', { | ||
| 275 | - args: 1 | ||
| 274 | + let pyshell = new PythonShell('exit-code.py', { | ||
| 275 | + args: ['1'] | ||
| 276 | 276 | }); | |
| 277 | 277 | pyshell.on('error', function (err) { | |
| 278 | 278 | err.should.have.properties(['exitCode', 'script', 'options', 'args']); | |
| 279 | 279 | done(); | |
| 280 | 280 | }); | |
| 281 | 281 | }); | |
| 282 | 282 | it('should extend err.stack with traceback', function (done) { | |
| 283 | - var pyshell = new PythonShell('error.py'); | ||
| 283 | + let pyshell = new PythonShell('error.py'); | ||
| 284 | 284 | pyshell.on('error', function (err) { | |
| 285 | 285 | err.stack.should.containEql('----- Python Traceback -----'); | |
| 286 | - err.stack.should.containEql('File "test' + path.sep + 'python' + path.sep + 'error.py", line 4'); | ||
| 287 | - err.stack.should.containEql('File "test' + path.sep + 'python' + path.sep + 'error.py", line 6'); | ||
| 286 | + err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 4'); | ||
| 287 | + err.stack.should.containEql('File "test' + sep + 'python' + sep + 'error.py", line 6'); | ||
| 288 | 288 | done(); | |
| 289 | 289 | }); | |
| 290 | 290 | }); | |
| 291 | 291 | }); | |
| 292 | 292 | ||
| 293 | 293 | describe('.terminate()', function () { | |
| 294 | 294 | it('set terminated to true', function (done) { | |
| 295 | - var pyshell = new PythonShell('infinite_loop.py'); | ||
| 295 | + let pyshell = new PythonShell('infinite_loop.py'); | ||
| 296 | 296 | pyshell.terminate(); | |
| 297 | 297 | pyshell.terminated.should.be.true | |
| 298 | 298 | done(); | |
| 299 | 299 | }); | |
| 300 | 300 | it('run the end callback if specified', function (done) { | |
| 301 | - var pyshell = new PythonShell('infinite_loop.py'); | ||
| 302 | - var endCalled = false; | ||
| 301 | + let pyshell = new PythonShell('infinite_loop.py'); | ||
| 302 | + let endCalled = false; | ||
| 303 | 303 | pyshell.end(()=>{ | |
| 304 | 304 | endCalled = true; | |
| 305 | 305 | }) | |
@@ -308,8 +308,8 @@ describe('PythonShell', function () { | |||
| 308 | 308 | done(); | |
| 309 | 309 | }); | |
| 310 | 310 | it('terminate with correct kill signal', function (done) { | |
| 311 | - var pyshell = new PythonShell('infinite_loop.py'); | ||
| 312 | - var endCalled = false; | ||
| 311 | + let pyshell = new PythonShell('infinite_loop.py'); | ||
| 312 | + let endCalled = false; | ||
| 313 | 313 | pyshell.end(()=>{ | |
| 314 | 314 | endCalled = true; | |
| 315 | 315 | }) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments