| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d5fc8e6 commit c4074f6
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,31 +73,48 @@ var PythonShell = function (script, options) { | |||
| 73 | 73 | errorData += ''+data; | |
| 74 | 74 | }); | |
| 75 | 75 | ||
| 76 | + this.stderr.on('end', function(){ | ||
| 77 | + self.stderrHasEnded = true | ||
| 78 | + terminateIfNeeded(); | ||
| 79 | + }) | ||
| 80 | + | ||
| 81 | + this.stdout.on('end', function(){ | ||
| 82 | + self.stdoutHasEnded = true | ||
| 83 | + terminateIfNeeded(); | ||
| 84 | + }) | ||
| 85 | + | ||
| 76 | 86 | this.childProcess.on('exit', function (code) { | |
| 87 | + self.exitCode = code; | ||
| 88 | + terminateIfNeeded(); | ||
| 89 | + }); | ||
| 90 | + | ||
| 91 | + function terminateIfNeeded() { | ||
| 92 | + if (!self.stderrHasEnded || !self.stdoutHasEnded || self.exitCode == null) { | ||
| 93 | + return; | ||
| 94 | + } | ||
| 77 | 95 | var err; | |
| 78 | - if (errorData || code !== 0) { | ||
| 96 | + if (errorData || self.exitCode !== 0) { | ||
| 79 | 97 | if (errorData) { | |
| 80 | 98 | err = self.parseError(errorData); | |
| 81 | 99 | } else { | |
| 82 | - err = new Error('process exited with code ' + code); | ||
| 100 | + err = new Error('process exited with code ' + self.exitCode); | ||
| 83 | 101 | } | |
| 84 | 102 | err = extend(err, { | |
| 85 | 103 | executable: pythonPath, | |
| 86 | 104 | options: pythonOptions.length ? pythonOptions : null, | |
| 87 | 105 | script: self.script, | |
| 88 | 106 | args: scriptArgs.length ? scriptArgs : null, | |
| 89 | - exitCode: code | ||
| 107 | + exitCode: self.exitCode | ||
| 90 | 108 | }); | |
| 91 | 109 | // do not emit error if only a callback is used | |
| 92 | 110 | if (self.listeners('error').length || !self._endCallback) { | |
| 93 | 111 | self.emit('error', err); | |
| 94 | 112 | } | |
| 95 | 113 | } | |
| 96 | - self.exitCode = code; | ||
| 97 | 114 | self.terminated = true; | |
| 98 | 115 | self.emit('close'); | |
| 99 | 116 | self._endCallback && self._endCallback(err); | |
| 100 | - }); | ||
| 117 | + } | ||
| 101 | 118 | }; | |
| 102 | 119 | util.inherits(PythonShell, EventEmitter); | |
| 103 | 120 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,6 +60,52 @@ describe('PythonShell', function () { | |||
| 60 | 60 | done(); | |
| 61 | 61 | }); | |
| 62 | 62 | }); | |
| 63 | + it('should run multiple scripts and fail with an extended stack trace for each of them', function (done) { | ||
| 64 | + var numberOfTimesToRun = 20; | ||
| 65 | + for (var i = 0; i < numberOfTimesToRun; i++) { | ||
| 66 | + runSingleErrorScript(end); | ||
| 67 | + } | ||
| 68 | + var count = 0; | ||
| 69 | + function end() { | ||
| 70 | + count++; | ||
| 71 | + if (count === numberOfTimesToRun) { | ||
| 72 | + done(); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + function runSingleErrorScript(callback) { | ||
| 76 | + PythonShell.run('error.py', function (err, results) { | ||
| 77 | + err.should.be.an.Error; | ||
| 78 | + err.exitCode.should.be.exactly(1); | ||
| 79 | + err.stack.should.containEql('----- Python Traceback -----'); | ||
| 80 | + callback(); | ||
| 81 | + }); | ||
| 82 | + } | ||
| 83 | + }); | ||
| 84 | + | ||
| 85 | + it('should run multiple scripts and return output data for each of them', function (done) { | ||
| 86 | + var numberOfTimesToRun = 20; | ||
| 87 | + for (var i = 0; i < numberOfTimesToRun; i++) { | ||
| 88 | + runSingleErrorScript(end); | ||
| 89 | + } | ||
| 90 | + var count = 0; | ||
| 91 | + function end() { | ||
| 92 | + count++; | ||
| 93 | + if (count === numberOfTimesToRun) { | ||
| 94 | + done(); | ||
| 95 | + } | ||
| 96 | + } | ||
| 97 | + function runSingleErrorScript(callback) { | ||
| 98 | + PythonShell.run('echo_args.py', { | ||
| 99 | + args: ['hello', 'world'] | ||
| 100 | + }, function (err, results) { | ||
| 101 | + if (err) return done(err); | ||
| 102 | + results.should.be.an.Array.and.have.lengthOf(2); | ||
| 103 | + results.should.eql(['hello', 'world']); | ||
| 104 | + callback(); | ||
| 105 | + }); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + }); | ||
| 63 | 109 | }); | |
| 64 | 110 | ||
| 65 | 111 | describe('.send(message)', function () { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments