diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 2b105b5..0364c94 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -4,7 +4,6 @@ about: Create a report to help us improve title: '' labels: 'bug' assignees: '' - --- **Describe the bug** @@ -23,9 +22,10 @@ A description of what you expected to happen. A description of what actually happened. **Other Information (please complete the following information):** - - OS: [e.g. Windows10] - - Python Version [e.g. 3.6] - - Node Version [e.g. 12] + +- OS: [e.g. Windows10] +- Python Version [e.g. 3.6] +- Node Version [e.g. 12] **Additional context** Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 36014cd..104f391 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -4,7 +4,6 @@ about: Suggest an idea for this project title: '' labels: 'enhancement' assignees: '' - --- **Is your feature request related to a problem? Please describe.** diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md index 96d933f..6e83ae1 100644 --- a/.github/ISSUE_TEMPLATE/question.md +++ b/.github/ISSUE_TEMPLATE/question.md @@ -4,14 +4,11 @@ about: A question about python-shell title: '' labels: 'question' assignees: '' - --- - - **The Question**: - **Any relevant python/javascript code:** diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1d296fb --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "semi": true, + "singleQuote": true, + "tabWidth": 2 +} diff --git a/CHANGELOG.md b/CHANGELOG.md index d19321e..ba12f43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,53 +1,74 @@ ## [5.0.0] - 2023-02-10 + ### BREAKING CHANGES + - run and runString now return a promise instead of a using a callback. - You will need to 1) check the return value, 2) remove the callback argument, and 3) change to a promise - see readme for usage examples ### Other notes + - I confirmed that python-shell works with python 3.11 and node v18. ## [4.0.0] - 2023-02-10 + ### Changed + - run and runString now return a promise instead of a using a callback. - This is somewhat backwards compatible with previous behavior ## [3.0.1] - 2021-10-09 + ### Fixed + - Previously when you called the kill method the terminated attribute was always set to true, regardless of whether the process was actually killed. Now the terminated boolean is set to true if kill succeeds, false otherwise. [#255](https://github.com/extrabacon/python-shell/issues/255) ## [3.0.0] - 2021-03-07 + ### Changed + - **BREAKING** Default python path changed back to `python` on Windows. [#237](https://github.com/extrabacon/python-shell/issues/237) - **BREAKING** `error` event renamed to `pythonError` event. [#118](https://github.com/extrabacon/python-shell/issues/118) - **BREAKING** `receive` methods removed in favor of `splitter` arguments in the constructor. This lets the default splitting logic reside in a reuseable stream transformer. Now if you have extra pipes you can reuse `newlineTransformer` to split incoming data into newline-seperated lines. ### Added + - `error` event that is fired upon failure to launch process, among other things. [#118](https://github.com/extrabacon/python-shell/issues/118) ## [1.0.8] + ### Fixed + - @joaoe fixed a bug with pythonshell not working with unset std streams - https://github.com/extrabacon/python-shell/milestone/9 ## [1.0.7] + ### Changed + - default python path updated to py on windows ## [1.0.4] + ### Added + - added getVersionSync ## [0.0.3] + ### Fixed + - fixed buffering in `PythonShell.receive`, fixing [#1](https://github.com/extrabacon/python-shell/issues/1) ## [0.0.2] + ### Changed + - improved documentation ## [0.0.1] + ### Added + - initial version - independent module moved from [extrabacon/pyspreadsheet](https://github.com/extrabacon/pyspreadsheet) - diff --git a/README.md b/README.md index a6baae4..96e1b69 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,19 @@ # [python-shell](https://www.npmjs.com/package/python-shell) [![Build status](https://ci.appveyor.com/api/projects/status/m8e3h53vvxg5wb2q/branch/master?svg=true)](https://ci.appveyor.com/project/Almenon/python-shell/branch/master) [![codecov](https://codecov.io/gh/extrabacon/python-shell/branch/master/graph/badge.svg)](https://codecov.io/gh/extrabacon/python-shell) + A simple way to run Python scripts from Node.js with basic but efficient inter-process communication and better error handling. ## Features -+ Reliably spawn Python scripts in a child process -+ Built-in text, JSON and binary modes -+ Custom parsers and formatters -+ Simple and efficient data transfers through stdin and stdout streams -+ Extended stack traces when an error is thrown +- Reliably spawn Python scripts in a child process +- Built-in text, JSON and binary modes +- Custom parsers and formatters +- Simple and efficient data transfers through stdin and stdout streams +- Extended stack traces when an error is thrown ## Requirements + First make sure you are able to run `python3` (Mac/Linux) or `python` (Windows) from the terminal. If you are not then you might need to add it to the PATH. If you want to use a version of python not in the PATH you should specify `options.pythonPath`. ## Installation @@ -25,9 +27,9 @@ npm install python-shell ### Running python code: ```typescript -import {PythonShell} from 'python-shell'; +import { PythonShell } from 'python-shell'; -PythonShell.runString('x=1+1;print(x)', null).then(messages=>{ +PythonShell.runString('x=1+1;print(x)', null).then((messages) => { console.log('finished'); }); ``` @@ -36,17 +38,18 @@ If the script exits with a non-zero code, an error will be thrown. Note the use of imports! If you're not using typescript ಠ_ಠ you can [still get imports to work with this guide](https://github.com/extrabacon/python-shell/issues/148#issuecomment-419120209). -Or you can use require like so: +Or you can use require like so: + ```javascript -let {PythonShell} = require('python-shell') +let { PythonShell } = require('python-shell'); ``` ### Running a Python script: ```typescript -import {PythonShell} from 'python-shell'; +import { PythonShell } from 'python-shell'; -PythonShell.run('my_script.py', null).then(messages=>{ +PythonShell.run('my_script.py', null).then((messages) => { console.log('finished'); }); ``` @@ -56,16 +59,16 @@ If the script exits with a non-zero code, an error will be thrown. ### Running a Python script with arguments and options: ```typescript -import {PythonShell} from 'python-shell'; +import { PythonShell } from 'python-shell'; let options = { mode: 'text', pythonPath: 'path/to/python', scriptPath: 'path/to/my/scripts', - args: ['value1', 'value2', 'value3'] + args: ['value1', 'value2', 'value3'], }; -PythonShell.run('my_script.py', options).then(messages=>{ +PythonShell.run('my_script.py', options).then((messages) => { // results is an array consisting of messages collected during execution console.log('results: %j', messages); }); @@ -74,7 +77,7 @@ PythonShell.run('my_script.py', options).then(messages=>{ ### Exchanging data between Node and Python: ```typescript -import {PythonShell} from 'python-shell'; +import { PythonShell } from 'python-shell'; let pyshell = new PythonShell('my_script.py'); // sends a message to the Python script via stdin @@ -86,7 +89,7 @@ pyshell.on('message', function (message) { }); // end the input stream and allow the process to exit -pyshell.end(function (err,code,signal) { +pyshell.end(function (err, code, signal) { if (err) throw err; console.log('The exit code was: ' + code); console.log('The exit signal was: ' + signal); @@ -98,9 +101,9 @@ Use `.send(message)` to send a message to the Python script. Attach the `message Use `options.mode` to quickly setup how data is sent and received between your Node and Python applications. - * use `text` mode for exchanging lines of text ending with a [newline character](http://hayne.net/MacDev/Notes/unixFAQ.html#endOfLine). - * use `json` mode for exchanging JSON fragments - * use `binary` mode for anything else (data is sent and received as-is) +- use `text` mode for exchanging lines of text ending with a [newline character](http://hayne.net/MacDev/Notes/unixFAQ.html#endOfLine). +- use `json` mode for exchanging JSON fragments +- use `binary` mode for anything else (data is sent and received as-is) Stderr always uses text mode. @@ -154,34 +157,35 @@ Error: ZeroDivisionError: integer division or modulo by zero Creates an instance of `PythonShell` and starts the Python process -* `script`: the path of the script to execute -* `options`: the execution options, consisting of: - * `mode`: Configures how data is exchanged when data flows through stdin and stdout. The possible values are: - * `text`: each line of data is emitted as a message (default) - * `json`: each line of data is parsed as JSON and emitted as a message - * `binary`: data is streamed as-is through `stdout` and `stdin` - * `formatter`: each message to send is transformed using this method, then appended with a newline - * `parser`: each line of data is parsed with this function and its result is emitted as a message - * `stderrParser`: each line of logs is parsed with this function and its result is emitted as a message - * `encoding`: the text encoding to apply on the child process streams (default: "utf8") - * `pythonPath`: The path where to locate the "python" executable. Default: "python3" ("python" for Windows) - * `pythonOptions`: Array of option switches to pass to "python" - * `scriptPath`: The default path where to look for scripts. Default is the current working directory. - * `args`: Array of arguments to pass to the script -* `stdoutSplitter`: splits stdout into chunks, defaulting to splitting into newline-seperated lines -* `stderrSplitter`: splits stderr into chunks, defaulting to splitting into newline-seperated lines +- `script`: the path of the script to execute +- `options`: the execution options, consisting of: + - `mode`: Configures how data is exchanged when data flows through stdin and stdout. The possible values are: + - `text`: each line of data is emitted as a message (default) + - `json`: each line of data is parsed as JSON and emitted as a message + - `binary`: data is streamed as-is through `stdout` and `stdin` + - `formatter`: each message to send is transformed using this method, then appended with a newline + - `parser`: each line of data is parsed with this function and its result is emitted as a message + - `stderrParser`: each line of logs is parsed with this function and its result is emitted as a message + - `encoding`: the text encoding to apply on the child process streams (default: "utf8") + - `pythonPath`: The path where to locate the "python" executable. Default: "python3" ("python" for Windows) + - `pythonOptions`: Array of option switches to pass to "python" + - `scriptPath`: The default path where to look for scripts. Default is the current working directory. + - `args`: Array of arguments to pass to the script +- `stdoutSplitter`: splits stdout into chunks, defaulting to splitting into newline-seperated lines +- `stderrSplitter`: splits stderr into chunks, defaulting to splitting into newline-seperated lines Other options are forwarded to `child_process.spawn`. PythonShell instances have the following properties: -* `script`: the path of the script to execute -* `command`: the full command arguments passed to the Python executable -* `stdin`: the Python stdin stream, used to send data to the child process -* `stdout`: the Python stdout stream, used for receiving data from the child process -* `stderr`: the Python stderr stream, used for communicating logs & errors -* `childProcess`: the process instance created via `child_process.spawn` -* `terminated`: boolean indicating whether the process has exited -* `exitCode`: the process exit code, available after the process has ended + +- `script`: the path of the script to execute +- `command`: the full command arguments passed to the Python executable +- `stdin`: the Python stdin stream, used to send data to the child process +- `stdout`: the Python stdout stream, used for receiving data from the child process +- `stderr`: the Python stderr stream, used for communicating logs & errors +- `childProcess`: the process instance created via `child_process.spawn` +- `terminated`: boolean indicating whether the process has exited +- `exitCode`: the process exit code, available after the process has ended Example: @@ -209,7 +213,7 @@ Example: ```typescript // run a simple script -PythonShell.run('script.py', null).then(results => { +PythonShell.run('script.py', null).then((results) => { // script finished }); ``` @@ -222,7 +226,7 @@ Example: ```typescript // run some simple code -PythonShell.runString('x=1;print(x)', null).then(messages=>{ +PythonShell.runString('x=1;print(x)', null).then((messages) => { // script finished }); ``` @@ -239,12 +243,12 @@ Promise is rejected if there is a syntax error. #### `#getVersion(pythonPath?:string)` -Returns the python version as a promise. Optional pythonPath param to get the version +Returns the python version as a promise. Optional pythonPath param to get the version of a specific python interpreter. #### `#getVersionSync(pythonPath?:string)` -Returns the python version. Optional pythonPath param to get the version +Returns the python version. Optional pythonPath param to get the version of a specific python interpreter. #### `.send(message)` @@ -255,12 +259,12 @@ Example: ```typescript // send a message in text mode -let shell = new PythonShell('script.py', { mode: 'text'}); +let shell = new PythonShell('script.py', { mode: 'text' }); shell.send('hello world!'); // send a message in JSON mode -let shell = new PythonShell('script.py', { mode: 'json'}); -shell.send({ command: "do_stuff", args: [1, 2, 3] }); +let shell = new PythonShell('script.py', { mode: 'json' }); +shell.send({ command: 'do_stuff', args: [1, 2, 3] }); ``` #### `.end(callback)` @@ -279,13 +283,13 @@ Example: ```typescript // receive a message in text mode -let shell = new PythonShell('script.py', { mode: 'text'}); +let shell = new PythonShell('script.py', { mode: 'text' }); shell.on('message', function (message) { // handle message (a line of text from stdout) }); // receive a message in JSON mode -let shell = new PythonShell('script.py', { mode: 'json'}); +let shell = new PythonShell('script.py', { mode: 'json' }); shell.on('message', function (message) { // handle message (a line of text from stdout, parsed as JSON) }); @@ -299,7 +303,7 @@ Example: ```typescript // receive a message in text mode -let shell = new PythonShell('script.py', { mode: 'text'}); +let shell = new PythonShell('script.py', { mode: 'text' }); shell.on('stderr', function (stderr) { // handle stderr (a line of text from stderr) }); @@ -316,9 +320,10 @@ Fires when the process terminates with a non-zero exit code. #### event: `error` Fires when: -* The process could not be spawned, or -* The process could not be killed, or -* Sending a message to the child process failed. + +- The process could not be spawned, or +- The process could not be killed, or +- Sending a message to the child process failed. If the process could not be spawned please double-check that python can be launched from the terminal. @@ -332,18 +337,17 @@ print('hello world', file=open(3, "w")) ``` ```typescript -import { PythonShell, NewlineTransformer, Options } from 'python-shell' +import { PythonShell, NewlineTransformer, Options } from 'python-shell'; const options: Options = { - 'stdio': - ['pipe', 'pipe', 'pipe', 'pipe'] // stdin, stdout, stderr, custom -} -const pyshell = new PythonShell('foo.py', options) + stdio: ['pipe', 'pipe', 'pipe', 'pipe'], // stdin, stdout, stderr, custom +}; +const pyshell = new PythonShell('foo.py', options); -const customPipe = pyshell.childProcess.stdio[3] +const customPipe = pyshell.childProcess.stdio[3]; customPipe.pipe(new NewlineTransformer()).on('data', (customResult: Buffer) => { - console.log(customResult.toString()) -}) + console.log(customResult.toString()); +}); ``` ## Used By: diff --git a/appveyor.yml b/appveyor.yml index f9ddaa9..7f5695e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -5,18 +5,17 @@ branches: - gh-pages image: -- Visual Studio 2022 -- Ubuntu2204 + - Visual Studio 2022 + - Ubuntu2204 environment: matrix: - - - nodejs_version: "18" + - nodejs_version: '18' PYTHON: "C:\\Python312-x64" - - nodejs_version: "20" + - nodejs_version: '20' PYTHON: "C:\\Python312-x64" - + install: - cmd: powershell Install-Product node $env:nodejs_version - sh: nvm install $nodejs_version @@ -40,7 +39,7 @@ notifications: to: - almenon214@gmail.com on_build_success: false - + skip_commits: files: - '**/*.md' diff --git a/index.ts b/index.ts index d226888..9569354 100644 --- a/index.ts +++ b/index.ts @@ -1,95 +1,101 @@ import { EventEmitter } from 'events'; -import { ChildProcess, spawn, SpawnOptions, exec, execSync } from 'child_process'; +import { + ChildProcess, + spawn, + SpawnOptions, + exec, + execSync, +} from 'child_process'; import { EOL as newline, tmpdir } from 'os'; -import { join, sep } from 'path' -import { Readable, Transform, TransformCallback, Writable } from 'stream' +import { join, sep } from 'path'; +import { Readable, Transform, TransformCallback, Writable } from 'stream'; import { writeFile, writeFileSync } from 'fs'; import { promisify } from 'util'; function toArray(source?: T | T[]): T[] { - if (typeof source === 'undefined' || source === null) { - return []; - } else if (!Array.isArray(source)) { - return [source]; - } - return source; + if (typeof source === 'undefined' || source === null) { + return []; + } else if (!Array.isArray(source)) { + return [source]; + } + return source; } /** * adds arguments as properties to obj */ function extend(obj: {}, ...args) { - Array.prototype.slice.call(arguments, 1).forEach(function (source) { - if (source) { - for (let key in source) { - obj[key] = source[key]; - } - } - }); - return obj; + Array.prototype.slice.call(arguments, 1).forEach(function (source) { + if (source) { + for (let key in source) { + obj[key] = source[key]; + } + } + }); + return obj; } /** * gets a random int from 0-10000000000 */ function getRandomInt() { - return Math.floor(Math.random() * 10000000000); + return Math.floor(Math.random() * 10000000000); } -const execPromise = promisify(exec) +const execPromise = promisify(exec); export interface Options extends SpawnOptions { - /** - * if binary is enabled message and stderr events will not be emitted - */ - mode?: 'text' | 'json' | 'binary' - formatter?: string | ((param: string) => any) - parser?: string | ((param: string) => any) - stderrParser?: string | ((param: string) => any) - encoding?: BufferEncoding - pythonPath?: string - /** - * see https://docs.python.org/3.7/using/cmdline.html - */ - pythonOptions?: string[] - /** - * overrides scriptPath passed into PythonShell constructor - */ - scriptPath?: string - /** - * arguments to your program - */ - args?: string[] + /** + * if binary is enabled message and stderr events will not be emitted + */ + mode?: 'text' | 'json' | 'binary'; + formatter?: string | ((param: string) => any); + parser?: string | ((param: string) => any); + stderrParser?: string | ((param: string) => any); + encoding?: BufferEncoding; + pythonPath?: string; + /** + * see https://docs.python.org/3.7/using/cmdline.html + */ + pythonOptions?: string[]; + /** + * overrides scriptPath passed into PythonShell constructor + */ + scriptPath?: string; + /** + * arguments to your program + */ + args?: string[]; } export class PythonShellError extends Error { - traceback: string | Buffer; - exitCode?: number; + traceback: string | Buffer; + exitCode?: number; } export class PythonShellErrorWithLogs extends PythonShellError { - logs: any[] + logs: any[]; } /** * Takes in a string stream and emits batches seperated by newlines */ export class NewlineTransformer extends Transform { - // NewlineTransformer: Megatron's little known once-removed cousin - private _lastLineData: string; - _transform(chunk: any, encoding: string, callback: TransformCallback){ - let data: string = chunk.toString() - if (this._lastLineData) data = this._lastLineData + data - const lines = data.split(newline) - this._lastLineData = lines.pop() - lines.forEach(this.push.bind(this)) - callback() - } - _flush(done: TransformCallback){ - if (this._lastLineData) this.push(this._lastLineData) - this._lastLineData = null; - done() - } + // NewlineTransformer: Megatron's little known once-removed cousin + private _lastLineData: string; + _transform(chunk: any, encoding: string, callback: TransformCallback) { + let data: string = chunk.toString(); + if (this._lastLineData) data = this._lastLineData + data; + const lines = data.split(newline); + this._lastLineData = lines.pop(); + lines.forEach(this.push.bind(this)); + callback(); + } + _flush(done: TransformCallback) { + if (this._lastLineData) this.push(this._lastLineData); + this._lastLineData = null; + done(); + } } /** @@ -101,376 +107,429 @@ export class NewlineTransformer extends Transform { * @constructor */ export class PythonShell extends EventEmitter { - scriptPath: string - command: string[] - mode: string - formatter: (param: string | Object) => any - parser: (param: string) => any - stderrParser: (param: string) => any - terminated: boolean - childProcess: ChildProcess - stdin: Writable; - stdout: Readable; - stderr: Readable; - exitSignal: string; - exitCode: number; - private stderrHasEnded: boolean; - private stdoutHasEnded: boolean; - private _remaining: string - private _endCallback: (err: PythonShellError, exitCode: number, exitSignal: string) => any - - // starting 2020 python2 is deprecated so we choose 3 as default - static defaultPythonPath = process.platform != "win32" ? "python3" : "python"; - - static defaultOptions: Options = {}; //allow global overrides for options + scriptPath: string; + command: string[]; + mode: string; + formatter: (param: string | Object) => any; + parser: (param: string) => any; + stderrParser: (param: string) => any; + terminated: boolean; + childProcess: ChildProcess; + stdin: Writable; + stdout: Readable; + stderr: Readable; + exitSignal: string; + exitCode: number; + private stderrHasEnded: boolean; + private stdoutHasEnded: boolean; + private _remaining: string; + private _endCallback: ( + err: PythonShellError, + exitCode: number, + exitSignal: string, + ) => any; + + // starting 2020 python2 is deprecated so we choose 3 as default + static defaultPythonPath = process.platform != 'win32' ? 'python3' : 'python'; + + static defaultOptions: Options = {}; //allow global overrides for options + + /** + * spawns a python process + * @param scriptPath path to script. Relative to current directory or options.scriptFolder if specified + * @param options + * @param stdoutSplitter Optional. Splits stdout into chunks, defaulting to splitting into newline-seperated lines + * @param stderrSplitter Optional. splits stderr into chunks, defaulting to splitting into newline-seperated lines + */ + constructor( + scriptPath: string, + options?: Options, + stdoutSplitter: Transform = null, + stderrSplitter: Transform = null, + ) { + super(); /** - * spawns a python process - * @param scriptPath path to script. Relative to current directory or options.scriptFolder if specified - * @param options - * @param stdoutSplitter Optional. Splits stdout into chunks, defaulting to splitting into newline-seperated lines - * @param stderrSplitter Optional. splits stderr into chunks, defaulting to splitting into newline-seperated lines + * returns either pythonshell func (if val string) or custom func (if val Function) */ - constructor(scriptPath: string, options?: Options, stdoutSplitter: Transform = null, stderrSplitter: Transform = null) { - super(); - - /** - * returns either pythonshell func (if val string) or custom func (if val Function) - */ - function resolve(type, val: string | Function) { - if (typeof val === 'string') { - // use a built-in function using its name - return PythonShell[type][val]; - } else if (typeof val === 'function') { - // use a custom function - return val; - } - } - - if (scriptPath.trim().length == 0) throw Error("scriptPath cannot be empty! You must give a script for python to run") - - let self = this; - let errorData = ''; - EventEmitter.call(this); - - options = extend({}, PythonShell.defaultOptions, options); - let pythonPath: string; - if (!options.pythonPath) { - pythonPath = PythonShell.defaultPythonPath; - } else pythonPath = options.pythonPath; - let pythonOptions = toArray(options.pythonOptions); - let scriptArgs = toArray(options.args); - - this.scriptPath = join(options.scriptPath || '', scriptPath); - this.command = pythonOptions.concat(this.scriptPath, scriptArgs); - this.mode = options.mode || 'text'; - this.formatter = resolve('format', options.formatter || this.mode); - this.parser = resolve('parse', options.parser || this.mode); - // We don't expect users to ever format stderr as JSON so we default to text mode - this.stderrParser = resolve('parse', options.stderrParser || 'text'); - this.terminated = false; - this.childProcess = spawn(pythonPath, this.command, options); - - ['stdout', 'stdin', 'stderr'].forEach(function (name) { - self[name] = self.childProcess[name]; - self.parser && self[name] && self[name].setEncoding(options.encoding || 'utf8'); - }); - - // Node buffers stdout&stderr in batches regardless of newline placement - // This is troublesome if you want to recieve distinct individual messages - // for example JSON parsing breaks if it recieves partial JSON - // so we use newlineTransformer to emit each batch seperated by newline - if (this.parser && this.stdout) { - if(!stdoutSplitter) stdoutSplitter = new NewlineTransformer() - // note that setting the encoding turns the chunk into a string - stdoutSplitter.setEncoding(options.encoding || 'utf8') - this.stdout.pipe(stdoutSplitter).on('data', (chunk: string) => { - this.emit('message', self.parser(chunk)); - }); - } - - // listen to stderr and emit errors for incoming data - if (this.stderrParser && this.stderr) { - if(!stderrSplitter) stderrSplitter = new NewlineTransformer() - // note that setting the encoding turns the chunk into a string - stderrSplitter.setEncoding(options.encoding || 'utf8') - this.stderr.pipe(stderrSplitter).on('data', (chunk: string) => { - this.emit('stderr', self.stderrParser(chunk)); - }); - } - - if (this.stderr) { - this.stderr.on('data', function (data) { - errorData += '' + data; - }); - this.stderr.on('end', function () { - self.stderrHasEnded = true; - terminateIfNeeded(); - }); - } else { - self.stderrHasEnded = true; - } - - if (this.stdout) { - this.stdout.on('end', function () { - self.stdoutHasEnded = true; - terminateIfNeeded(); - }); - } else { - self.stdoutHasEnded = true; - } - - this.childProcess.on('error', function (err: NodeJS.ErrnoException) { - self.emit('error', err); - }) - this.childProcess.on('exit', function (code, signal) { - self.exitCode = code; - self.exitSignal = signal; - terminateIfNeeded(); - }); - - function terminateIfNeeded() { - if (!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return; - - let err: PythonShellError; - if (self.exitCode && self.exitCode !== 0) { - if (errorData) { - err = self.parseError(errorData); - } else { - err = new PythonShellError('process exited with code ' + self.exitCode); - } - err = extend(err, { - executable: pythonPath, - options: pythonOptions.length ? pythonOptions : null, - script: self.scriptPath, - args: scriptArgs.length ? scriptArgs : null, - exitCode: self.exitCode - }); - // do not emit error if only a callback is used - if (self.listeners('pythonError').length || !self._endCallback) { - self.emit('pythonError', err); - } - } - - self.terminated = true; - self.emit('close'); - self._endCallback && self._endCallback(err, self.exitCode, self.exitSignal); - }; + function resolve(type, val: string | Function) { + if (typeof val === 'string') { + // use a built-in function using its name + return PythonShell[type][val]; + } else if (typeof val === 'function') { + // use a custom function + return val; + } } - // built-in formatters - static format = { - text: function toText(data): string { - if (!data) return ''; - else if (typeof data !== 'string') return data.toString(); - return data; - }, - json: function toJson(data) { - return JSON.stringify(data); - } - }; - - //built-in parsers - static parse = { - text: function asText(data): string { - return data; - }, - json: function asJson(data: string) { - return JSON.parse(data); - } - }; - - /** - * checks syntax without executing code - * @returns rejects promise w/ string error output if syntax failure - */ - static async checkSyntax(code: string) { - const randomInt = getRandomInt(); - const filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py` + if (scriptPath.trim().length == 0) + throw Error( + 'scriptPath cannot be empty! You must give a script for python to run', + ); + + let self = this; + let errorData = ''; + EventEmitter.call(this); + + options = extend({}, PythonShell.defaultOptions, options); + let pythonPath: string; + if (!options.pythonPath) { + pythonPath = PythonShell.defaultPythonPath; + } else pythonPath = options.pythonPath; + let pythonOptions = toArray(options.pythonOptions); + let scriptArgs = toArray(options.args); + + this.scriptPath = join(options.scriptPath || '', scriptPath); + this.command = pythonOptions.concat(this.scriptPath, scriptArgs); + this.mode = options.mode || 'text'; + this.formatter = resolve('format', options.formatter || this.mode); + this.parser = resolve('parse', options.parser || this.mode); + // We don't expect users to ever format stderr as JSON so we default to text mode + this.stderrParser = resolve('parse', options.stderrParser || 'text'); + this.terminated = false; + this.childProcess = spawn(pythonPath, this.command, options); + + ['stdout', 'stdin', 'stderr'].forEach(function (name) { + self[name] = self.childProcess[name]; + self.parser && + self[name] && + self[name].setEncoding(options.encoding || 'utf8'); + }); - const writeFilePromise = promisify(writeFile) - return writeFilePromise(filePath, code).then(() => { - return this.checkSyntaxFile(filePath) - }) + // Node buffers stdout&stderr in batches regardless of newline placement + // This is troublesome if you want to recieve distinct individual messages + // for example JSON parsing breaks if it recieves partial JSON + // so we use newlineTransformer to emit each batch seperated by newline + if (this.parser && this.stdout) { + if (!stdoutSplitter) stdoutSplitter = new NewlineTransformer(); + // note that setting the encoding turns the chunk into a string + stdoutSplitter.setEncoding(options.encoding || 'utf8'); + this.stdout.pipe(stdoutSplitter).on('data', (chunk: string) => { + this.emit('message', self.parser(chunk)); + }); } - static getPythonPath() { - return this.defaultOptions.pythonPath ? this.defaultOptions.pythonPath : this.defaultPythonPath; + // listen to stderr and emit errors for incoming data + if (this.stderrParser && this.stderr) { + if (!stderrSplitter) stderrSplitter = new NewlineTransformer(); + // note that setting the encoding turns the chunk into a string + stderrSplitter.setEncoding(options.encoding || 'utf8'); + this.stderr.pipe(stderrSplitter).on('data', (chunk: string) => { + this.emit('stderr', self.stderrParser(chunk)); + }); } - /** - * checks syntax without executing code - * @returns {Promise} rejects w/ stderr if syntax failure - */ - static async checkSyntaxFile(filePath: string) { - const pythonPath = this.getPythonPath() - let compileCommand = `${pythonPath} -m py_compile ${filePath}` - return execPromise(compileCommand) + if (this.stderr) { + this.stderr.on('data', function (data) { + errorData += '' + data; + }); + this.stderr.on('end', function () { + self.stderrHasEnded = true; + terminateIfNeeded(); + }); + } else { + self.stderrHasEnded = true; } - /** - * Runs a Python script and returns collected messages as a promise. - * If the promise is rejected, the err will probably be of type PythonShellErrorWithLogs - * @param scriptPath The path to the script to execute - * @param options The execution options - */ - static run(scriptPath: string, options?: Options): Promise { - return new Promise((resolve, reject) => { - let pyshell = new PythonShell(scriptPath, options); - let output = []; - - pyshell.on('message', function (message) { - output.push(message); - }).end(function (err) { - if(err){ - (err as PythonShellErrorWithLogs).logs = output - reject(err); - } - else resolve(output); - }); - }); - }; - - - - /** - * Runs the inputted string of python code and returns collected messages as a promise. DO NOT ALLOW UNTRUSTED USER INPUT HERE! - * @param code The python code to execute - * @param options The execution options - * @return a promise with the output from the python script - */ - static runString(code: string, options?: Options) { - - // put code in temp file - const randomInt = getRandomInt(); - const filePath = tmpdir + sep + `pythonShellFile${randomInt}.py` - writeFileSync(filePath, code); - - return PythonShell.run(filePath, options); - }; - - static getVersion(pythonPath?: string) { - if (!pythonPath) pythonPath = this.getPythonPath() - return execPromise(pythonPath + " --version"); + if (this.stdout) { + this.stdout.on('end', function () { + self.stdoutHasEnded = true; + terminateIfNeeded(); + }); + } else { + self.stdoutHasEnded = true; } - static getVersionSync(pythonPath?: string) { - if (!pythonPath) pythonPath = this.getPythonPath() - return execSync(pythonPath + " --version").toString() - } + this.childProcess.on('error', function (err: NodeJS.ErrnoException) { + self.emit('error', err); + }); + this.childProcess.on('exit', function (code, signal) { + self.exitCode = code; + self.exitSignal = signal; + terminateIfNeeded(); + }); - /** - * Parses an error thrown from the Python process through stderr - * @param {string|Buffer} data The stderr contents to parse - * @return {Error} The parsed error with extended stack trace when traceback is available - */ - private parseError(data: string | Buffer) { - let text = '' + data; - let error: PythonShellError; - - if (/^Traceback/.test(text)) { - // traceback data is available - let lines = text.trim().split(newline); - let exception = lines.pop(); - error = new PythonShellError(exception); - error.traceback = data; - // extend stack trace - error.stack += newline + ' ----- Python Traceback -----' + newline + ' '; - error.stack += lines.slice(1).join(newline + ' '); + function terminateIfNeeded() { + if ( + !self.stderrHasEnded || + !self.stdoutHasEnded || + (self.exitCode == null && self.exitSignal == null) + ) + return; + + let err: PythonShellError; + if (self.exitCode && self.exitCode !== 0) { + if (errorData) { + err = self.parseError(errorData); } else { - // otherwise, create a simpler error with stderr contents - error = new PythonShellError(text); + err = new PythonShellError( + 'process exited with code ' + self.exitCode, + ); } - - return error; - }; - - /** - * Sends a message to the Python shell through stdin - * Override this method to format data to be sent to the Python process - * @returns {PythonShell} The same instance for chaining calls - */ - send(message: string | Object) { - if (!this.stdin) throw new Error("stdin not open for writing"); - let data = this.formatter ? this.formatter(message) : message; - if (this.mode !== 'binary') data += newline; - this.stdin.write(data); - return this; - }; - - /** - * Closes the stdin stream. Unless python is listening for stdin in a loop - * this should cause the process to finish its work and close. - * @returns {PythonShell} The same instance for chaining calls - */ - end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any) { - if (this.childProcess.stdin) { - this.childProcess.stdin.end(); + err = extend(err, { + executable: pythonPath, + options: pythonOptions.length ? pythonOptions : null, + script: self.scriptPath, + args: scriptArgs.length ? scriptArgs : null, + exitCode: self.exitCode, + }); + // do not emit error if only a callback is used + if (self.listeners('pythonError').length || !self._endCallback) { + self.emit('pythonError', err); } - this._endCallback = callback; - return this; - }; + } - /** - * Sends a kill signal to the process - * @returns {PythonShell} The same instance for chaining calls - */ - kill(signal?: NodeJS.Signals) { - this.terminated = this.childProcess.kill(signal); - return this; - }; + self.terminated = true; + self.emit('close'); + self._endCallback && + self._endCallback(err, self.exitCode, self.exitSignal); + } + } + + // built-in formatters + static format = { + text: function toText(data): string { + if (!data) return ''; + else if (typeof data !== 'string') return data.toString(); + return data; + }, + json: function toJson(data) { + return JSON.stringify(data); + }, + }; + + //built-in parsers + static parse = { + text: function asText(data): string { + return data; + }, + json: function asJson(data: string) { + return JSON.parse(data); + }, + }; + + /** + * checks syntax without executing code + * @returns rejects promise w/ string error output if syntax failure + */ + static async checkSyntax(code: string) { + const randomInt = getRandomInt(); + const filePath = tmpdir() + sep + `pythonShellSyntaxCheck${randomInt}.py`; + + const writeFilePromise = promisify(writeFile); + return writeFilePromise(filePath, code).then(() => { + return this.checkSyntaxFile(filePath); + }); + } + + static getPythonPath() { + return this.defaultOptions.pythonPath + ? this.defaultOptions.pythonPath + : this.defaultPythonPath; + } + + /** + * checks syntax without executing code + * @returns {Promise} rejects w/ stderr if syntax failure + */ + static async checkSyntaxFile(filePath: string) { + const pythonPath = this.getPythonPath(); + let compileCommand = `${pythonPath} -m py_compile ${filePath}`; + return execPromise(compileCommand); + } + + /** + * Runs a Python script and returns collected messages as a promise. + * If the promise is rejected, the err will probably be of type PythonShellErrorWithLogs + * @param scriptPath The path to the script to execute + * @param options The execution options + */ + static run(scriptPath: string, options?: Options): Promise { + return new Promise((resolve, reject) => { + let pyshell = new PythonShell(scriptPath, options); + let output = []; + + pyshell + .on('message', function (message) { + output.push(message); + }) + .end(function (err) { + if (err) { + (err as PythonShellErrorWithLogs).logs = output; + reject(err); + } else resolve(output); + }); + }); + } + + /** + * Runs the inputted string of python code and returns collected messages as a promise. DO NOT ALLOW UNTRUSTED USER INPUT HERE! + * @param code The python code to execute + * @param options The execution options + * @return a promise with the output from the python script + */ + static runString(code: string, options?: Options) { + // put code in temp file + const randomInt = getRandomInt(); + const filePath = tmpdir + sep + `pythonShellFile${randomInt}.py`; + writeFileSync(filePath, code); + + return PythonShell.run(filePath, options); + } + + static getVersion(pythonPath?: string) { + if (!pythonPath) pythonPath = this.getPythonPath(); + return execPromise(pythonPath + ' --version'); + } + + static getVersionSync(pythonPath?: string) { + if (!pythonPath) pythonPath = this.getPythonPath(); + return execSync(pythonPath + ' --version').toString(); + } + + /** + * Parses an error thrown from the Python process through stderr + * @param {string|Buffer} data The stderr contents to parse + * @return {Error} The parsed error with extended stack trace when traceback is available + */ + private parseError(data: string | Buffer) { + let text = '' + data; + let error: PythonShellError; + + if (/^Traceback/.test(text)) { + // traceback data is available + let lines = text.trim().split(newline); + let exception = lines.pop(); + error = new PythonShellError(exception); + error.traceback = data; + // extend stack trace + error.stack += + newline + ' ----- Python Traceback -----' + newline + ' '; + error.stack += lines.slice(1).join(newline + ' '); + } else { + // otherwise, create a simpler error with stderr contents + error = new PythonShellError(text); + } - /** - * Alias for kill. - * @deprecated - */ - terminate(signal?: NodeJS.Signals) { - // todo: remove this next breaking release - return this.kill(signal) + return error; + } + + /** + * Sends a message to the Python shell through stdin + * Override this method to format data to be sent to the Python process + * @returns {PythonShell} The same instance for chaining calls + */ + send(message: string | Object) { + if (!this.stdin) throw new Error('stdin not open for writing'); + let data = this.formatter ? this.formatter(message) : message; + if (this.mode !== 'binary') data += newline; + this.stdin.write(data); + return this; + } + + /** + * Closes the stdin stream. Unless python is listening for stdin in a loop + * this should cause the process to finish its work and close. + * @returns {PythonShell} The same instance for chaining calls + */ + end( + callback: ( + err: PythonShellError, + exitCode: number, + exitSignal: string, + ) => any, + ) { + if (this.childProcess.stdin) { + this.childProcess.stdin.end(); } -}; + this._endCallback = callback; + return this; + } + + /** + * Sends a kill signal to the process + * @returns {PythonShell} The same instance for chaining calls + */ + kill(signal?: NodeJS.Signals) { + this.terminated = this.childProcess.kill(signal); + return this; + } + + /** + * Alias for kill. + * @deprecated + */ + terminate(signal?: NodeJS.Signals) { + // todo: remove this next breaking release + return this.kill(signal); + } +} // This interface is merged in with the above class definition export interface PythonShell { - addListener(event: string, listener: (...args: any[]) => void): this; - emit(event: string | symbol, ...args: any[]): boolean; - on(event: string, listener: (...args: any[]) => void): this; - once(event: string, listener: (...args: any[]) => void): this; - prependListener(event: string, listener: (...args: any[]) => void): this; - prependOnceListener(event: string, listener: (...args: any[]) => void): this; - - addListener(event: "message", listener: (parsedChunk: any) => void): this; - emit(event: "message", parsedChunk: any): boolean; - on(event: "message", listener: (parsedChunk: any) => void): this; - once(event: "message", listener: (parsedChunk: any) => void): this; - prependListener(event: "message", listener: (parsedChunk: any) => void): this; - prependOnceListener(event: "message", listener: (parsedChunk: any) => void): this; - - addListener(event: "stderr", listener: (parsedChunk: any) => void): this; - emit(event: "stderr", parsedChunk: any): boolean; - on(event: "stderr", listener: (parsedChunk: any) => void): this; - once(event: "stderr", listener: (parsedChunk: any) => void): this; - prependListener(event: "stderr", listener: (parsedChunk: any) => void): this; - prependOnceListener(event: "stderr", listener: (parsedChunk: any) => void): this; - - addListener(event: "close", listener: () => void): this; - emit(event: "close",): boolean; - on(event: "close", listener: () => void): this; - once(event: "close", listener: () => void): this; - prependListener(event: "close", listener: () => void): this; - prependOnceListener(event: "close", listener: () => void): this; - - addListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; - emit(event: "error", error: NodeJS.ErrnoException): boolean; - on(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; - once(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; - prependListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; - prependOnceListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this; - - addListener(event: "pythonError", listener: (error: PythonShellError) => void): this; - emit(event: "pythonError", error: PythonShellError): boolean; - on(event: "pythonError", listener: (error: PythonShellError) => void): this; - once(event: "pythonError", listener: (error: PythonShellError) => void): this; - prependListener(event: "pythonError", listener: (error: PythonShellError) => void): this; - prependOnceListener(event: "pythonError", listener: (error: PythonShellError) => void): this; + addListener(event: string, listener: (...args: any[]) => void): this; + emit(event: string | symbol, ...args: any[]): boolean; + on(event: string, listener: (...args: any[]) => void): this; + once(event: string, listener: (...args: any[]) => void): this; + prependListener(event: string, listener: (...args: any[]) => void): this; + prependOnceListener(event: string, listener: (...args: any[]) => void): this; + + addListener(event: 'message', listener: (parsedChunk: any) => void): this; + emit(event: 'message', parsedChunk: any): boolean; + on(event: 'message', listener: (parsedChunk: any) => void): this; + once(event: 'message', listener: (parsedChunk: any) => void): this; + prependListener(event: 'message', listener: (parsedChunk: any) => void): this; + prependOnceListener( + event: 'message', + listener: (parsedChunk: any) => void, + ): this; + + addListener(event: 'stderr', listener: (parsedChunk: any) => void): this; + emit(event: 'stderr', parsedChunk: any): boolean; + on(event: 'stderr', listener: (parsedChunk: any) => void): this; + once(event: 'stderr', listener: (parsedChunk: any) => void): this; + prependListener(event: 'stderr', listener: (parsedChunk: any) => void): this; + prependOnceListener( + event: 'stderr', + listener: (parsedChunk: any) => void, + ): this; + + addListener(event: 'close', listener: () => void): this; + emit(event: 'close'): boolean; + on(event: 'close', listener: () => void): this; + once(event: 'close', listener: () => void): this; + prependListener(event: 'close', listener: () => void): this; + prependOnceListener(event: 'close', listener: () => void): this; + + addListener( + event: 'error', + listener: (error: NodeJS.ErrnoException) => void, + ): this; + emit(event: 'error', error: NodeJS.ErrnoException): boolean; + on(event: 'error', listener: (error: NodeJS.ErrnoException) => void): this; + once(event: 'error', listener: (error: NodeJS.ErrnoException) => void): this; + prependListener( + event: 'error', + listener: (error: NodeJS.ErrnoException) => void, + ): this; + prependOnceListener( + event: 'error', + listener: (error: NodeJS.ErrnoException) => void, + ): this; + + addListener( + event: 'pythonError', + listener: (error: PythonShellError) => void, + ): this; + emit(event: 'pythonError', error: PythonShellError): boolean; + on(event: 'pythonError', listener: (error: PythonShellError) => void): this; + once(event: 'pythonError', listener: (error: PythonShellError) => void): this; + prependListener( + event: 'pythonError', + listener: (error: PythonShellError) => void, + ): this; + prependOnceListener( + event: 'pythonError', + listener: (error: PythonShellError) => void, + ): this; } diff --git a/package-lock.json b/package-lock.json index 261f96a..13e4c11 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@types/node": "^24.10.1", "@types/should": "^13.0.0", "mocha": "^11.7.5", + "prettier": "^3.6.2", "should": "^13.2.3", "ts-node": "^10.9.2", "typescript": "^5.9.3" @@ -120,6 +121,7 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.1.tgz", "integrity": "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ==", "dev": true, + "peer": true, "dependencies": { "undici-types": "~7.16.0" } @@ -778,6 +780,22 @@ "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", "dev": true }, + "node_modules/prettier": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.6.2.tgz", + "integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -1085,6 +1103,7 @@ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", "dev": true, + "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -1413,6 +1432,7 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.1.tgz", "integrity": "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ==", "dev": true, + "peer": true, "requires": { "undici-types": "~7.16.0" } @@ -1898,6 +1918,12 @@ "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", "dev": true }, + "prettier": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.6.2.tgz", + "integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==", + "dev": true + }, "randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -2128,7 +2154,8 @@ "version": "5.9.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", - "dev": true + "dev": true, + "peer": true }, "undici-types": { "version": "7.16.0", diff --git a/package.json b/package.json index e51b57b..48f2aa2 100644 --- a/package.json +++ b/package.json @@ -9,13 +9,15 @@ "test": "tsc -p ./ && mocha -r ts-node/register", "appveyorTest": "tsc -p ./ && nyc mocha test/*.js", "compile": "tsc -watch -p ./", - "compileOnce": "tsc -p ./" + "compileOnce": "tsc -p ./", + "format": "prettier --write ." }, "devDependencies": { "@types/mocha": "^10.0.1", "@types/node": "^24.10.1", "@types/should": "^13.0.0", "mocha": "^11.7.5", + "prettier": "^3.6.2", "should": "^13.2.3", "ts-node": "^10.9.2", "typescript": "^5.9.3" diff --git a/test/test-python-shell.ts b/test/test-python-shell.ts index 17bd8fe..3d5de81 100644 --- a/test/test-python-shell.ts +++ b/test/test-python-shell.ts @@ -1,565 +1,644 @@ import * as should from 'should'; -import { PythonShell } from '..' -import { sep, join } from 'path' -import { EOL as newline } from 'os' +import { PythonShell } from '..'; +import { sep, join } from 'path'; +import { EOL as newline } from 'os'; import { chdir, cwd } from 'process'; describe('PythonShell', function () { + const pythonFolder = 'test/python'; - const pythonFolder = 'test/python' + PythonShell.defaultOptions = { + scriptPath: pythonFolder, + }; - PythonShell.defaultOptions = { - scriptPath: pythonFolder - }; + describe('#ctor(script, options)', function () { + it('should spawn a Python process', function (done) { + // spawning python might take a while for the first time + // after this python should be in memory and not need extra time for startup + this.timeout(3000); - describe('#ctor(script, options)', function () { - it('should spawn a Python process', function (done) { - // spawning python might take a while for the first time - // after this python should be in memory and not need extra time for startup - this.timeout(3000) - - let pyshell = new PythonShell('exit-code.py'); - pyshell.command.should.eql(['test' + sep + 'python' + sep + 'exit-code.py']); - pyshell.terminated.should.be.false; - pyshell.end(function (err) { - if (err) return done(err); - pyshell.terminated.should.be.true; - done(); - }); - }); - it('should spawn a Python process even if scriptPath option is not specified', function (done) { - let originalDirectory = cwd() - PythonShell.defaultOptions = {}; - chdir(join(__dirname, "python")); + let pyshell = new PythonShell('exit-code.py'); + pyshell.command.should.eql([ + 'test' + sep + 'python' + sep + 'exit-code.py', + ]); + pyshell.terminated.should.be.false; + pyshell.end(function (err) { + if (err) return done(err); + pyshell.terminated.should.be.true; + done(); + }); + }); + it('should spawn a Python process even if scriptPath option is not specified', function (done) { + let originalDirectory = cwd(); + PythonShell.defaultOptions = {}; + chdir(join(__dirname, 'python')); - let pyshell = new PythonShell('exit-code.py'); - pyshell.command.should.eql(['exit-code.py']); - pyshell.terminated.should.be.false; - pyshell.end(function (err) { - if (err) return done(err); - pyshell.terminated.should.be.true; - done(); - }); + let pyshell = new PythonShell('exit-code.py'); + pyshell.command.should.eql(['exit-code.py']); + pyshell.terminated.should.be.false; + pyshell.end(function (err) { + if (err) return done(err); + pyshell.terminated.should.be.true; + done(); + }); - //reset values to intial status - PythonShell.defaultOptions = { - scriptPath: pythonFolder - }; - chdir(originalDirectory) - }); - // executing python-shell with a absolute path is tested in runString suite - it('should spawn a Python process with options', function (done) { - let pyshell = new PythonShell('exit-code.py', { - pythonOptions: ['-u'] - }); - pyshell.command.should.eql(['-u', 'test' + sep + 'python' + sep + 'exit-code.py']); - pyshell.end(done); - }); - it('should fail to spawn python with bad path', function (done) { - let pyshell = new PythonShell('exit-code.py', { - pythonPath: 'foeisjofseij' - }); - pyshell.on('error', (err) => { - err.code.should.eql('ENOENT') - done() - }) - }); - it('should spawn a Python process with script arguments', function (done) { - let pyshell = new PythonShell('echo_args.py', { - args: ['hello', 'world'] - }); - pyshell.command.should.eql(['test' + sep + 'python' + sep + 'echo_args.py', 'hello', 'world']); - pyshell.end(done); - }); + //reset values to intial status + PythonShell.defaultOptions = { + scriptPath: pythonFolder, + }; + chdir(originalDirectory); + }); + // executing python-shell with a absolute path is tested in runString suite + it('should spawn a Python process with options', function (done) { + let pyshell = new PythonShell('exit-code.py', { + pythonOptions: ['-u'], + }); + pyshell.command.should.eql([ + '-u', + 'test' + sep + 'python' + sep + 'exit-code.py', + ]); + pyshell.end(done); + }); + it('should fail to spawn python with bad path', function (done) { + let pyshell = new PythonShell('exit-code.py', { + pythonPath: 'foeisjofseij', + }); + pyshell.on('error', (err) => { + err.code.should.eql('ENOENT'); + done(); + }); }); + it('should spawn a Python process with script arguments', function (done) { + let pyshell = new PythonShell('echo_args.py', { + args: ['hello', 'world'], + }); + pyshell.command.should.eql([ + 'test' + sep + 'python' + sep + 'echo_args.py', + 'hello', + 'world', + ]); + pyshell.end(done); + }); + }); - describe('#checkSyntax(code:string)', function () { + describe('#checkSyntax(code:string)', function () { + // note checkSyntax is a wrapper around checkSyntaxFile + // so this tests checkSyntaxFile as well - // note checkSyntax is a wrapper around checkSyntaxFile - // so this tests checkSyntaxFile as well + it('should check syntax', function (done) { + PythonShell.checkSyntax('x=1').then(() => { + done(); + }); + }); - it('should check syntax', function (done) { - PythonShell.checkSyntax("x=1").then(() => { - done(); - }) - }) + it('should invalidate bad syntax', function (done) { + PythonShell.checkSyntax('x=').catch(() => { + done(); + }); + }); + }); - it('should invalidate bad syntax', function (done) { - PythonShell.checkSyntax("x=").catch(() => { - done(); - }) - }) - }) + // #158 these tests are failing on appveyor windows node 8/10 python 2/3 + // but they work locally on my windows machine ..... + // these methods are not that important so just commenting out tests untill someone fixes them + // describe("#getVersion", function(){ + // it('should return a string', function(done){ + // PythonShell.getVersion().then((out)=>{ + // const version = out.stdout + // version.should.be.a.String(); + // version.length.should.be.greaterThan(0) + // done() + // }) + // }) + // }) - // #158 these tests are failing on appveyor windows node 8/10 python 2/3 - // but they work locally on my windows machine ..... - // these methods are not that important so just commenting out tests untill someone fixes them - // describe("#getVersion", function(){ - // it('should return a string', function(done){ - // PythonShell.getVersion().then((out)=>{ - // const version = out.stdout - // version.should.be.a.String(); - // version.length.should.be.greaterThan(0) - // done() - // }) - // }) - // }) + // describe("#getVersionSync", function(){ + // it('should return a string', function(){ + // const version = PythonShell.getVersionSync() + // version.should.be.a.String(); + // version.length.should.be.greaterThan(0) + // }) + // }) - // describe("#getVersionSync", function(){ - // it('should return a string', function(){ - // const version = PythonShell.getVersionSync() - // version.should.be.a.String(); - // version.length.should.be.greaterThan(0) - // }) - // }) + describe('#runString(script, options)', function () { + before(() => { + PythonShell.defaultOptions = {}; + }); + it('should be able to execute a string of python code', function (done) { + PythonShell.runString('print("hello");print("world")', null).then( + (results) => { + results.should.be.an.Array().and.have.lengthOf(2); + results.should.eql(['hello', 'world']); + done(); + }, + ); + }); + it('should be able to execute a string of python code using promises', async function () { + let results = await PythonShell.runString( + 'print("hello");print("world")', + ); + results.should.be.an.Array().and.have.lengthOf(2); + results.should.eql(['hello', 'world']); + }); + it('should be able to execute a string of python code async', async function () { + let results = await PythonShell.runString( + 'print("hello");print("world")', + ); + results.should.be.an.Array().and.have.lengthOf(2); + results.should.eql(['hello', 'world']); + }); + after(() => { + PythonShell.defaultOptions = { + // reset to match initial value + scriptPath: pythonFolder, + }; + }); + }); - describe('#runString(script, options)', function () { - before(() => { - PythonShell.defaultOptions = {}; + describe('#run(script, options)', function () { + it('should run the script and return output data using callbacks', function (done) { + PythonShell.run('echo_args.py', { + args: ['hello', 'world'], + }).then((results) => { + results.should.be.an.Array().and.have.lengthOf(2); + results.should.eql(['hello', 'world']); + done(); + }); + }); + it('should run the script and return output data async', async function () { + let results = await PythonShell.run('echo_args.py', { + args: ['hello', 'world'], + }); + results.should.be.an.Array().and.have.lengthOf(2); + results.should.eql(['hello', 'world']); + }); + it('should try to run the script and fail appropriately', function (done) { + PythonShell.run('unknown_script.py', null).catch((err) => { + err.should.be.an.Error; + err.exitCode.should.be.exactly(2); + done(); + }); + }); + it('should try to run the script and fail appropriately - async', async function () { + try { + let results = await PythonShell.run('unknown_script.py'); + throw new Error( + `should not get here because the script should fail` + results, + ); + } catch (err) { + err.should.be.an.Error; + err.exitCode.should.be.exactly(2); + } + }); + it('should include both output and error', function (done) { + PythonShell.run('echo_hi_then_error.py', null) + .then((results) => { + done('Error: This promise should never successfully resolve'); }) - it('should be able to execute a string of python code', function (done) { - PythonShell.runString('print("hello");print("world")', null).then((results) => { - results.should.be.an.Array().and.have.lengthOf(2); - results.should.eql(['hello', 'world']); - done(); - }); + .catch((err) => { + err.logs.should.eql(['hi']); + err.should.be.an.Error; + done(); }); - it('should be able to execute a string of python code using promises', async function () { - let results = await PythonShell.runString('print("hello");print("world")'); - results.should.be.an.Array().and.have.lengthOf(2); - results.should.eql(['hello', 'world']); - }); - it('should be able to execute a string of python code async', async function () { - let results = await PythonShell.runString('print("hello");print("world")'); - results.should.be.an.Array().and.have.lengthOf(2); - results.should.eql(['hello', 'world']); - }); - after(() => { - PythonShell.defaultOptions = { - // reset to match initial value - scriptPath: pythonFolder - }; - }) + }); + it('should run the script and fail with an extended stack trace', function (done) { + PythonShell.run('error.py', null).catch((err) => { + err.should.be.an.Error; + err.exitCode.should.be.exactly(1); + err.stack.should.containEql('----- Python Traceback -----'); + done(); + }); + }); + it('should run the script and fail with an extended stack trace even when mode is binary', function (done) { + PythonShell.run('error.py', { mode: 'binary' }).catch((err) => { + err.should.be.an.Error; + err.exitCode.should.be.exactly(1); + err.stack.should.containEql('----- Python Traceback -----'); + done(); + }); + }); + it('should run multiple scripts and fail with an extended stack trace for each of them', function (done) { + let numberOfTimesToRun = 5; + for (let i = 0; i < numberOfTimesToRun; i++) { + runSingleErrorScript(end); + } + let count = 0; + function end() { + count++; + if (count === numberOfTimesToRun) { + done(); + } + } + function runSingleErrorScript(callback) { + PythonShell.run('error.py', null).catch((err) => { + err.should.be.an.Error; + err.exitCode.should.be.exactly(1); + err.stack.should.containEql('----- Python Traceback -----'); + callback(); + }); + } }); - describe('#run(script, options)', function () { - it('should run the script and return output data using callbacks', function (done) { - PythonShell.run('echo_args.py', { - args: ['hello', 'world'] - }).then((results) => { - results.should.be.an.Array().and.have.lengthOf(2); - results.should.eql(['hello', 'world']); - done(); - }); - }); - it('should run the script and return output data async', async function () { - let results = await PythonShell.run('echo_args.py', { - args: ['hello', 'world'] - }); - results.should.be.an.Array().and.have.lengthOf(2); - results.should.eql(['hello', 'world']); - }); - it('should try to run the script and fail appropriately', function (done) { - PythonShell.run('unknown_script.py', null).catch((err) => { - err.should.be.an.Error; - err.exitCode.should.be.exactly(2); - done(); - }); - }); - it('should try to run the script and fail appropriately - async', async function () { - try { - let results = await PythonShell.run('unknown_script.py'); - throw new Error(`should not get here because the script should fail` + results); - } catch (err) { - err.should.be.an.Error; - err.exitCode.should.be.exactly(2); - } - }); - it('should include both output and error', function (done) { - PythonShell.run('echo_hi_then_error.py', null).then((results) => { - done("Error: This promise should never successfully resolve"); - }).catch((err)=>{ - err.logs.should.eql(['hi']) - err.should.be.an.Error - done() - }); - }); - it('should run the script and fail with an extended stack trace', function (done) { - PythonShell.run('error.py', null).catch((err) => { - err.should.be.an.Error; - err.exitCode.should.be.exactly(1); - err.stack.should.containEql('----- Python Traceback -----'); - done(); - }); - }); - it('should run the script and fail with an extended stack trace even when mode is binary', function (done) { - PythonShell.run('error.py', { mode: "binary" }).catch((err) => { - err.should.be.an.Error; - err.exitCode.should.be.exactly(1); - err.stack.should.containEql('----- Python Traceback -----'); - done(); - }); - }); - it('should run multiple scripts and fail with an extended stack trace for each of them', function (done) { - let numberOfTimesToRun = 5; - for (let i = 0; i < numberOfTimesToRun; i++) { - runSingleErrorScript(end); - } - let count = 0; - function end() { - count++; - if (count === numberOfTimesToRun) { - done(); - } - } - function runSingleErrorScript(callback) { - PythonShell.run('error.py', null).catch((err) => { - err.should.be.an.Error; - err.exitCode.should.be.exactly(1); - err.stack.should.containEql('----- Python Traceback -----'); - callback(); - }); - } - }); - - it('should run multiple scripts and return output data for each of them', function (done) { - let numberOfTimesToRun = 5; - for (let i = 0; i < numberOfTimesToRun; i++) { - runSingleScript(end); - } - let count = 0; - function end() { - count++; - if (count === numberOfTimesToRun) { - done(); - } - } - function runSingleScript(callback) { - PythonShell.run('echo_args.py', { - args: ['hello', 'world'] - }).then((results)=> { - results.should.be.an.Array().and.have.lengthOf(2); - results.should.eql(['hello', 'world']); - callback(); - }); - } - - }); + it('should run multiple scripts and return output data for each of them', function (done) { + let numberOfTimesToRun = 5; + for (let i = 0; i < numberOfTimesToRun; i++) { + runSingleScript(end); + } + let count = 0; + function end() { + count++; + if (count === numberOfTimesToRun) { + done(); + } + } + function runSingleScript(callback) { + PythonShell.run('echo_args.py', { + args: ['hello', 'world'], + }).then((results) => { + results.should.be.an.Array().and.have.lengthOf(2); + results.should.eql(['hello', 'world']); + callback(); + }); + } + }); - it('should be able to run modules', function (done) { - PythonShell.defaultOptions = {}; + it('should be able to run modules', function (done) { + PythonShell.defaultOptions = {}; - PythonShell.run('-m', { - args: ['timeit', '-n 1', `'x=5'`] - }).then((results)=> { - PythonShell.defaultOptions = { - // reset to match initial value - scriptPath: pythonFolder - }; - results.should.be.an.Array(); - results[0].should.be.an.String(); - results[0].slice(0, 6).should.eql('1 loop'); - done(); - }); - }) + PythonShell.run('-m', { + args: ['timeit', '-n 1', `'x=5'`], + }).then((results) => { + PythonShell.defaultOptions = { + // reset to match initial value + scriptPath: pythonFolder, + }; + results.should.be.an.Array(); + results[0].should.be.an.String(); + results[0].slice(0, 6).should.eql('1 loop'); + done(); + }); + }); - after(() => { - // should be able to run modules test should theoretically reset this - // but we have this to in case something goes horribly wrong with the test - PythonShell.defaultOptions = { - // reset to match initial value - scriptPath: pythonFolder - }; - }) + after(() => { + // should be able to run modules test should theoretically reset this + // but we have this to in case something goes horribly wrong with the test + PythonShell.defaultOptions = { + // reset to match initial value + scriptPath: pythonFolder, + }; + }); - it('should run PythonShell normally without access to std streams', async function () { - var pyshell = await PythonShell.run('exit-code.py', { - // 3 different ways of assigning values to the std streams in child_process.spawn() - // * ignore - pipe to /dev/null - // * inherit - inherit fd from parent process; - // * process.stderr - pass output directly to that stream. - stdio: ['ignore', 'inherit', process.stderr], - // @ts-expect-error python-shell technically allows a non-array arg, - // although the user shouldn't be doing this. We are just testing for - // increased code coverage - args: "0" - }); + it('should run PythonShell normally without access to std streams', async function () { + var pyshell = await PythonShell.run('exit-code.py', { + // 3 different ways of assigning values to the std streams in child_process.spawn() + // * ignore - pipe to /dev/null + // * inherit - inherit fd from parent process; + // * process.stderr - pass output directly to that stream. + stdio: ['ignore', 'inherit', process.stderr], + // @ts-expect-error python-shell technically allows a non-array arg, + // although the user shouldn't be doing this. We are just testing for + // increased code coverage + args: '0', + }); - should(pyshell).be.eql([]); - - }); + should(pyshell).be.eql([]); }); + }); - describe('.send(message)', function () { - it('should send string messages when mode is "text"', function (done) { - let pyshell = new PythonShell('echo_text.py', { - mode: 'text' - }); - let output = ''; - pyshell.stdout.on('data', function (data) { - output += '' + data; - }); - pyshell.send('hello').send('world').end(function (err) { - if (err) return done(err); - output.should.be.exactly('hello' + newline + 'world' + newline); - done(); - }); + describe('.send(message)', function () { + it('should send string messages when mode is "text"', function (done) { + let pyshell = new PythonShell('echo_text.py', { + mode: 'text', + }); + let output = ''; + pyshell.stdout.on('data', function (data) { + output += '' + data; + }); + pyshell + .send('hello') + .send('world') + .end(function (err) { + if (err) return done(err); + output.should.be.exactly('hello' + newline + 'world' + newline); + done(); }); - it('should send JSON messages when mode is "json"', function (done) { - let pyshell = new PythonShell('echo_json.py', { - mode: 'json' - }); - let output = ''; - pyshell.stdout.on('data', function (data) { - output += '' + data; - }); - pyshell.send({ a: 'b' }).send(null).send([1, 2, 3]).end(function (err) { - if (err) return done(err); - output.should.be.exactly('{"a": "b"}' + newline + 'null' + newline + '[1, 2, 3]' + newline); - done(); - }); - }); - it('should use a custom formatter', function (done) { - let pyshell = new PythonShell('echo_text.py', { - formatter: function (message) { - return message.toUpperCase(); - } - }); - let output = ''; - pyshell.stdout.on('data', function (data) { - output += '' + data; - }); - pyshell.send('hello').send('world').end(function (err) { - if (err) return done(err); - output.should.be.exactly('HELLO' + newline + 'WORLD' + newline + ''); - done(); - }); + }); + it('should send JSON messages when mode is "json"', function (done) { + let pyshell = new PythonShell('echo_json.py', { + mode: 'json', + }); + let output = ''; + pyshell.stdout.on('data', function (data) { + output += '' + data; + }); + pyshell + .send({ a: 'b' }) + .send(null) + .send([1, 2, 3]) + .end(function (err) { + if (err) return done(err); + output.should.be.exactly( + '{"a": "b"}' + newline + 'null' + newline + '[1, 2, 3]' + newline, + ); + done(); }); - it('should write as-is when mode is "binary"', function (done) { - let pyshell = new PythonShell('echo_binary.py', { - mode: 'binary' - }); - let output = ''; - pyshell.stdout.on('data', function (data) { - output += '' + data; - }); - pyshell.send(Buffer.from('i am not a string')).end(function (err) { - if (err) return done(err); - output.should.be.exactly('i am not a string'); - done(); - }); + }); + it('should use a custom formatter', function (done) { + let pyshell = new PythonShell('echo_text.py', { + formatter: function (message) { + return message.toUpperCase(); + }, + }); + let output = ''; + pyshell.stdout.on('data', function (data) { + output += '' + data; + }); + pyshell + .send('hello') + .send('world') + .end(function (err) { + if (err) return done(err); + output.should.be.exactly('HELLO' + newline + 'WORLD' + newline + ''); + done(); }); }); + it('should write as-is when mode is "binary"', function (done) { + let pyshell = new PythonShell('echo_binary.py', { + mode: 'binary', + }); + let output = ''; + pyshell.stdout.on('data', function (data) { + output += '' + data; + }); + pyshell.send(Buffer.from('i am not a string')).end(function (err) { + if (err) return done(err); + output.should.be.exactly('i am not a string'); + done(); + }); + }); + }); - describe('stdout', function () { - it('should emit messages as strings when mode is "text"', function (done) { - let pyshell = new PythonShell('echo_text.py', { - mode: 'text' - }); - let count = 0; - pyshell.on('message', function (message) { - count === 0 && message.should.be.exactly('hello'); - count === 1 && message.should.be.exactly('world'); - count++; - }).on('close', function () { - count.should.be.exactly(2); - }).send('hello').send('world').end(done); - }); - it('should emit messages as JSON when mode is "json"', function (done) { - let pyshell = new PythonShell('echo_json.py', { - mode: 'json' - }); - let count = 0; - pyshell.send({ a: 'b' }).send(null).send([1, 2, 3, 4, 5]); - pyshell.on('message', function (message) { - count === 0 && message.should.eql({ a: 'b' }); - count === 1 && should(message).eql(null); - count === 2 && message.should.eql([1, 2, 3, 4, 5]); - count++; - }).on('close', function () { - count.should.be.exactly(3); - }).end(done); - }); - it('should properly buffer partial messages', function (done) { - // echo_text_with_newline_control echoes text with $'s replaced with newlines - let pyshell = new PythonShell('echo_text_with_newline_control.py', { - mode: 'text' - }); - pyshell.on('message', (message) => { - console.log(message) - let messageObject = JSON.parse(message) - messageObject.should.be.an.Object; - messageObject.should.eql({ a: true }); - }).send('{"a"').send(':').send('true}${').send('"a":true}$').end(() => { - done() - }); - }); - it('should not be invoked when mode is "binary"', function (done) { - let pyshell = new PythonShell('echo_args.py', { - args: ['hello', 'world'], - mode: 'binary' - }); - pyshell.on('message', () => { - done('should not emit messages in binary mode'); - return undefined - }); - pyshell.end(done); - }); - it('should use a custom parser function', function (done) { - let pyshell = new PythonShell('echo_text.py', { - mode: 'text', - parser: function (message) { - return message.toUpperCase(); - } - }); - let count = 0; - pyshell.on('message', function (message) { - count === 0 && message.should.be.exactly('HELLO'); - count === 1 && message.should.be.exactly('WORLD!'); - count++; - }).on('close', function () { - count.should.be.exactly(2); - }).send('hello').send('world!').end(done); + describe('stdout', function () { + it('should emit messages as strings when mode is "text"', function (done) { + let pyshell = new PythonShell('echo_text.py', { + mode: 'text', + }); + let count = 0; + pyshell + .on('message', function (message) { + count === 0 && message.should.be.exactly('hello'); + count === 1 && message.should.be.exactly('world'); + count++; + }) + .on('close', function () { + count.should.be.exactly(2); + }) + .send('hello') + .send('world') + .end(done); + }); + it('should emit messages as JSON when mode is "json"', function (done) { + let pyshell = new PythonShell('echo_json.py', { + mode: 'json', + }); + let count = 0; + pyshell.send({ a: 'b' }).send(null).send([1, 2, 3, 4, 5]); + pyshell + .on('message', function (message) { + count === 0 && message.should.eql({ a: 'b' }); + count === 1 && should(message).eql(null); + count === 2 && message.should.eql([1, 2, 3, 4, 5]); + count++; + }) + .on('close', function () { + count.should.be.exactly(3); + }) + .end(done); + }); + it('should properly buffer partial messages', function (done) { + // echo_text_with_newline_control echoes text with $'s replaced with newlines + let pyshell = new PythonShell('echo_text_with_newline_control.py', { + mode: 'text', + }); + pyshell + .on('message', (message) => { + console.log(message); + let messageObject = JSON.parse(message); + messageObject.should.be.an.Object; + messageObject.should.eql({ a: true }); + }) + .send('{"a"') + .send(':') + .send('true}${') + .send('"a":true}$') + .end(() => { + done(); }); }); + it('should not be invoked when mode is "binary"', function (done) { + let pyshell = new PythonShell('echo_args.py', { + args: ['hello', 'world'], + mode: 'binary', + }); + pyshell.on('message', () => { + done('should not emit messages in binary mode'); + return undefined; + }); + pyshell.end(done); + }); + it('should use a custom parser function', function (done) { + let pyshell = new PythonShell('echo_text.py', { + mode: 'text', + parser: function (message) { + return message.toUpperCase(); + }, + }); + let count = 0; + pyshell + .on('message', function (message) { + count === 0 && message.should.be.exactly('HELLO'); + count === 1 && message.should.be.exactly('WORLD!'); + count++; + }) + .on('close', function () { + count.should.be.exactly(2); + }) + .send('hello') + .send('world!') + .end(done); + }); + }); - describe('stderr', function () { - it('should emit stderr logs as strings when mode is "text"', function (done) { - let pyshell = new PythonShell('stderrLogging.py', { - mode: 'text' - }); - let count = 0; - pyshell.on('stderr', function (stderr) { - count === 0 && stderr.should.be.exactly('INFO:root:Jackdaws love my big sphinx of quartz.'); - count === 1 && stderr.should.be.exactly('DEBUG:log1:Quick zephyrs blow, vexing daft Jim.'); - count++; - }).on('close', function () { - count.should.be.exactly(5); - }).send('hello').send('world').end(done); - }); - it('should not be invoked when mode is "binary"', function (done) { - let pyshell = new PythonShell('stderrLogging.py', { - stderrParser: 'binary' - }); - pyshell.on('stderr', () => { - done('should not emit stderr in binary mode'); - }); - pyshell.end(() => { - done() - }); - }); - it('should use a custom parser function', function (done) { - let pyshell = new PythonShell('stderrLogging.py', { - mode: 'text', - stderrParser: function (stderr) { - return stderr.toUpperCase(); - } - }); - let count = 0; - pyshell.on('stderr', function (stderr) { - count === 0 && stderr.should.be.exactly('INFO:ROOT:JACKDAWS LOVE MY BIG SPHINX OF QUARTZ.'); - count === 1 && stderr.should.be.exactly('DEBUG:LOG1:QUICK ZEPHYRS BLOW, VEXING DAFT JIM.'); - count++; - }).on('close', function () { - count.should.be.exactly(5); - }).send('hello').send('world!').end(done); - }); + describe('stderr', function () { + it('should emit stderr logs as strings when mode is "text"', function (done) { + let pyshell = new PythonShell('stderrLogging.py', { + mode: 'text', + }); + let count = 0; + pyshell + .on('stderr', function (stderr) { + count === 0 && + stderr.should.be.exactly( + 'INFO:root:Jackdaws love my big sphinx of quartz.', + ); + count === 1 && + stderr.should.be.exactly( + 'DEBUG:log1:Quick zephyrs blow, vexing daft Jim.', + ); + count++; + }) + .on('close', function () { + count.should.be.exactly(5); + }) + .send('hello') + .send('world') + .end(done); + }); + it('should not be invoked when mode is "binary"', function (done) { + let pyshell = new PythonShell('stderrLogging.py', { + stderrParser: 'binary', + }); + pyshell.on('stderr', () => { + done('should not emit stderr in binary mode'); + }); + pyshell.end(() => { + done(); + }); + }); + it('should use a custom parser function', function (done) { + let pyshell = new PythonShell('stderrLogging.py', { + mode: 'text', + stderrParser: function (stderr) { + return stderr.toUpperCase(); + }, + }); + let count = 0; + pyshell + .on('stderr', function (stderr) { + count === 0 && + stderr.should.be.exactly( + 'INFO:ROOT:JACKDAWS LOVE MY BIG SPHINX OF QUARTZ.', + ); + count === 1 && + stderr.should.be.exactly( + 'DEBUG:LOG1:QUICK ZEPHYRS BLOW, VEXING DAFT JIM.', + ); + count++; + }) + .on('close', function () { + count.should.be.exactly(5); + }) + .send('hello') + .send('world!') + .end(done); }); + }); - describe('.end(callback)', function () { - it('should end normally when exit code is zero', function (done) { - let pyshell = new PythonShell('exit-code.py'); - pyshell.end(function (err, code, signal) { - if (err) return done(err); - code.should.be.exactly(0); - done(); - }); - }); - it('should emit error if exit code is not zero', function (done) { - let pyshell = new PythonShell('exit-code.py', { - args: ['3'] - }); - pyshell.on('pythonError', function (err) { - err.should.have.properties({ - message: 'process exited with code 3', - exitCode: 3 - }); - done(); - }); - }); - it('should emit error when the program exits because of an unhandled exception', function (done) { - let pyshell = new PythonShell('error.py'); - pyshell.on('pythonError', function (err) { - err.message.should.be.equalOneOf('ZeroDivisionError: integer division or modulo by zero', 'ZeroDivisionError: division by zero'); - err.should.have.property('traceback'); - err.traceback.should.containEql('Traceback (most recent call last)'); - done(); - }); - }); - it('should NOT emit error when logging is written to stderr', function (done) { - let pyshell = new PythonShell('stderrLogging.py'); - pyshell.on('pythonError', function (err) { - done(new Error("an error should not have been raised")); - }); - pyshell.on('close', function () { - done(); - }) - }); + describe('.end(callback)', function () { + it('should end normally when exit code is zero', function (done) { + let pyshell = new PythonShell('exit-code.py'); + pyshell.end(function (err, code, signal) { + if (err) return done(err); + code.should.be.exactly(0); + done(); + }); + }); + it('should emit error if exit code is not zero', function (done) { + let pyshell = new PythonShell('exit-code.py', { + args: ['3'], + }); + pyshell.on('pythonError', function (err) { + err.should.have.properties({ + message: 'process exited with code 3', + exitCode: 3, + }); + done(); + }); + }); + it('should emit error when the program exits because of an unhandled exception', function (done) { + let pyshell = new PythonShell('error.py'); + pyshell.on('pythonError', function (err) { + err.message.should.be.equalOneOf( + 'ZeroDivisionError: integer division or modulo by zero', + 'ZeroDivisionError: division by zero', + ); + err.should.have.property('traceback'); + err.traceback.should.containEql('Traceback (most recent call last)'); + done(); + }); }); + it('should NOT emit error when logging is written to stderr', function (done) { + let pyshell = new PythonShell('stderrLogging.py'); + pyshell.on('pythonError', function (err) { + done(new Error('an error should not have been raised')); + }); + pyshell.on('close', function () { + done(); + }); + }); + }); - describe('.parseError(data)', function () { - it('should extend error with context properties', function (done) { - let pyshell = new PythonShell('exit-code.py', { - args: ['1'] - }); - pyshell.on('pythonError', function (err) { - err.should.have.properties(['exitCode', 'script', 'options', 'args']); - done(); - }); - }); - it('should extend err.stack with traceback', function (done) { - let pyshell = new PythonShell('error.py'); - pyshell.on('pythonError', function (err) { - err.stack.should.containEql('----- Python Traceback -----'); - err.stack.should.containEql('test' + sep + 'python' + sep + 'error.py", line 4'); - err.stack.should.containEql('test' + sep + 'python' + sep + 'error.py", line 6'); - done(); - }); - }); - it('should work in json mode', function (done) { - let pyshell = new PythonShell('error.py', { mode: 'json' }); - pyshell.on('pythonError', function (err) { - err.stack.should.containEql('----- Python Traceback -----'); - err.stack.should.containEql('test' + sep + 'python' + sep + 'error.py", line 4'); - err.stack.should.containEql('test' + sep + 'python' + sep + 'error.py", line 6'); - done(); - }); - }); + describe('.parseError(data)', function () { + it('should extend error with context properties', function (done) { + let pyshell = new PythonShell('exit-code.py', { + args: ['1'], + }); + pyshell.on('pythonError', function (err) { + err.should.have.properties(['exitCode', 'script', 'options', 'args']); + done(); + }); + }); + it('should extend err.stack with traceback', function (done) { + let pyshell = new PythonShell('error.py'); + pyshell.on('pythonError', function (err) { + err.stack.should.containEql('----- Python Traceback -----'); + err.stack.should.containEql( + 'test' + sep + 'python' + sep + 'error.py", line 4', + ); + err.stack.should.containEql( + 'test' + sep + 'python' + sep + 'error.py", line 6', + ); + done(); + }); + }); + it('should work in json mode', function (done) { + let pyshell = new PythonShell('error.py', { mode: 'json' }); + pyshell.on('pythonError', function (err) { + err.stack.should.containEql('----- Python Traceback -----'); + err.stack.should.containEql( + 'test' + sep + 'python' + sep + 'error.py", line 4', + ); + err.stack.should.containEql( + 'test' + sep + 'python' + sep + 'error.py", line 6', + ); + done(); + }); }); + }); - describe('.kill()', function () { - it('set terminated to correct value', function (done) { - let pyshell = new PythonShell('infinite_loop.py'); - pyshell.kill(); - pyshell.terminated.should.be.true - done(); - }); - it('run the end callback if specified', function (done) { - let pyshell = new PythonShell('infinite_loop.py'); - pyshell.end(() => { - done(); - }) - pyshell.kill(); - }); - it('kill with correct signal', function (done) { - let pyshell = new PythonShell('infinite_loop.py'); - pyshell.terminated.should.be.false; - pyshell.kill('SIGKILL'); - pyshell.terminated.should.be.true; - setTimeout(() => { - pyshell.exitSignal.should.be.exactly('SIGKILL'); - done(); - }, 500); - }); + describe('.kill()', function () { + it('set terminated to correct value', function (done) { + let pyshell = new PythonShell('infinite_loop.py'); + pyshell.kill(); + pyshell.terminated.should.be.true; + done(); + }); + it('run the end callback if specified', function (done) { + let pyshell = new PythonShell('infinite_loop.py'); + pyshell.end(() => { + done(); + }); + pyshell.kill(); + }); + it('kill with correct signal', function (done) { + let pyshell = new PythonShell('infinite_loop.py'); + pyshell.terminated.should.be.false; + pyshell.kill('SIGKILL'); + pyshell.terminated.should.be.true; + setTimeout(() => { + pyshell.exitSignal.should.be.exactly('SIGKILL'); + done(); + }, 500); }); -}); \ No newline at end of file + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 7eb40ca..a8f1cba 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,17 +1,12 @@ { - "compilerOptions": { - "module": "commonjs", - "target": "es6", - "lib": [ - "es6" - ], - "sourceMap": true, - "rootDir": ".", - "declaration": true, - "forceConsistentCasingInFileNames": true - }, - "exclude": [ - "node_modules", - "personal" - ] -} \ No newline at end of file + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "lib": ["es6"], + "sourceMap": true, + "rootDir": ".", + "declaration": true, + "forceConsistentCasingInFileNames": true + }, + "exclude": ["node_modules", "personal"] +}