Skip to content

Commit 59ae034

Browse files
committed
converting unit tests to typescript #134
1 parent 593f095 commit 59ae034

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface Options extends SpawnOptions{
4040

4141
class PythonShellError extends Error{
4242
traceback: string | Buffer;
43+
exitCode?:number;
4344
}
4445

4546
/**
@@ -59,10 +60,10 @@ export class PythonShell extends EventEmitter{
5960
stdin: Writable;
6061
stdout: Readable;
6162
stderr: Readable;
63+
exitSignal:string;
64+
exitCode:number;
6265
private stderrHasEnded:boolean;
6366
private stdoutHasEnded:boolean;
64-
private exitCode:number;
65-
private exitSignal:string;
6667
private _remaining:string
6768
private _endCallback:(err:PythonShellError, exitCode:number, exitSignal:string)=>any
6869

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff 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'
55

66
describe('PythonShell', function () {
77

@@ -11,8 +11,8 @@ describe('PythonShell', function () {
1111

1212
describe('#ctor(script, options)', function () {
1313
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']);
1616
pyshell.terminated.should.be.false;
1717
pyshell.end(function (err) {
1818
if (err) return done(err);
@@ -21,17 +21,17 @@ describe('PythonShell', function () {
2121
});
2222
});
2323
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']
2626
});
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']);
2828
pyshell.end(done);
2929
});
3030
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', {
3232
args: ['hello', 'world']
3333
});
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']);
3535
pyshell.end(done);
3636
});
3737
});
@@ -48,34 +48,34 @@ describe('PythonShell', function () {
4848
});
4949
});
5050
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) {
5252
err.should.be.an.Error;
5353
err.exitCode.should.be.exactly(2);
5454
done();
5555
});
5656
});
5757
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) {
5959
err.should.be.an.Error;
6060
err.exitCode.should.be.exactly(1);
6161
err.stack.should.containEql('----- Python Traceback -----');
6262
done();
6363
});
6464
});
6565
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++) {
6868
runSingleErrorScript(end);
6969
}
70-
var count = 0;
70+
let count = 0;
7171
function end() {
7272
count++;
7373
if (count === numberOfTimesToRun) {
7474
done();
7575
}
7676
}
7777
function runSingleErrorScript(callback) {
78-
PythonShell.run('error.py', function (err, results) {
78+
PythonShell.run('error.py', null, function (err, results) {
7979
err.should.be.an.Error;
8080
err.exitCode.should.be.exactly(1);
8181
err.stack.should.containEql('----- Python Traceback -----');
@@ -85,11 +85,11 @@ describe('PythonShell', function () {
8585
});
8686

8787
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++) {
9090
runSingleScript(end);
9191
}
92-
var count = 0;
92+
let count = 0;
9393
function end() {
9494
count++;
9595
if (count === numberOfTimesToRun) {
@@ -112,10 +112,10 @@ describe('PythonShell', function () {
112112

113113
describe('.send(message)', function () {
114114
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', {
116116
mode: 'text'
117117
});
118-
var output = '';
118+
let output = '';
119119
pyshell.stdout.on('data', function (data) {
120120
output += ''+data;
121121
});
@@ -126,10 +126,10 @@ describe('PythonShell', function () {
126126
});
127127
});
128128
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', {
130130
mode: 'json'
131131
});
132-
var output = '';
132+
let output = '';
133133
pyshell.stdout.on('data', function (data) {
134134
output += ''+data;
135135
});
@@ -140,12 +140,12 @@ describe('PythonShell', function () {
140140
});
141141
});
142142
it('should use a custom formatter', function (done) {
143-
var pyshell = new PythonShell('echo_text.py', {
143+
let pyshell = new PythonShell('echo_text.py', {
144144
formatter: function (message) {
145145
return message.toUpperCase();
146146
}
147147
});
148-
var output = '';
148+
let output = '';
149149
pyshell.stdout.on('data', function (data) {
150150
output += ''+data;
151151
});
@@ -156,10 +156,10 @@ describe('PythonShell', function () {
156156
});
157157
});
158158
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', {
160160
mode: 'binary'
161161
});
162-
var output = '';
162+
let output = '';
163163
pyshell.stdout.on('data', function (data) {
164164
output += ''+data;
165165
});
@@ -173,10 +173,10 @@ describe('PythonShell', function () {
173173

174174
describe('.receive(data)', function () {
175175
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', {
177177
mode: 'text'
178178
});
179-
var count = 0;
179+
let count = 0;
180180
pyshell.on('message', function (message) {
181181
count === 0 && message.should.be.exactly('hello');
182182
count === 1 && message.should.be.exactly('world');
@@ -186,10 +186,10 @@ describe('PythonShell', function () {
186186
}).send('hello').send('world').end(done);
187187
});
188188
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', {
190190
mode: 'json'
191191
});
192-
var count = 0;
192+
let count = 0;
193193
pyshell.send({ a: 'b' }).send(null).send([1, 2, 3, 4, 5]);
194194
pyshell.on('message', function (message) {
195195
count === 0 && message.should.eql({ a: 'b' });
@@ -201,7 +201,7 @@ describe('PythonShell', function () {
201201
}).end(done);
202202
});
203203
it('should properly buffer partial messages', function (done) {
204-
var pyshell = new PythonShell('echo_json.py', {
204+
let pyshell = new PythonShell('echo_json.py', {
205205
mode: 'json'
206206
});
207207
pyshell.on('message', function (message) {
@@ -210,7 +210,7 @@ describe('PythonShell', function () {
210210
}).receive('{"a"').receive(':').receive('true}'+newline+'').end(done);
211211
});
212212
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', {
214214
args: ['hello', 'world'],
215215
mode: 'binary'
216216
});
@@ -220,13 +220,13 @@ describe('PythonShell', function () {
220220
pyshell.end(done);
221221
});
222222
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', {
224224
mode: 'text',
225225
parser: function (message) {
226226
return message.toUpperCase();
227227
}
228228
});
229-
var count = 0;
229+
let count = 0;
230230
pyshell.on('message', function (message) {
231231
count === 0 && message.should.be.exactly('HELLO');
232232
count === 1 && message.should.be.exactly('WORLD!');
@@ -239,16 +239,16 @@ describe('PythonShell', function () {
239239

240240
describe('.end(callback)', function () {
241241
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');
243243
pyshell.end(function (err,code,signal) {
244244
if (err) return done(err);
245245
code.should.be.exactly(0);
246246
done();
247247
});
248248
});
249249
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']
252252
});
253253
pyshell.on('error', function (err) {
254254
err.should.have.properties({
@@ -258,8 +258,8 @@ describe('PythonShell', function () {
258258
done();
259259
});
260260
});
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');
263263
pyshell.on('error', function (err) {
264264
err.message.should.be.equalOneOf('ZeroDivisionError: integer division or modulo by zero','ZeroDivisionError: division by zero');
265265
err.should.have.property('traceback');
@@ -271,35 +271,35 @@ describe('PythonShell', function () {
271271

272272
describe('.parseError(data)', function () {
273273
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']
276276
});
277277
pyshell.on('error', function (err) {
278278
err.should.have.properties(['exitCode', 'script', 'options', 'args']);
279279
done();
280280
});
281281
});
282282
it('should extend err.stack with traceback', function (done) {
283-
var pyshell = new PythonShell('error.py');
283+
let pyshell = new PythonShell('error.py');
284284
pyshell.on('error', function (err) {
285285
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');
288288
done();
289289
});
290290
});
291291
});
292292

293293
describe('.terminate()', function () {
294294
it('set terminated to true', function (done) {
295-
var pyshell = new PythonShell('infinite_loop.py');
295+
let pyshell = new PythonShell('infinite_loop.py');
296296
pyshell.terminate();
297297
pyshell.terminated.should.be.true
298298
done();
299299
});
300300
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;
303303
pyshell.end(()=>{
304304
endCalled = true;
305305
})
@@ -308,8 +308,8 @@ describe('PythonShell', function () {
308308
done();
309309
});
310310
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;
313313
pyshell.end(()=>{
314314
endCalled = true;
315315
})

0 commit comments

Comments
 (0)