forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.test.ts
More file actions
90 lines (81 loc) · 3.6 KB
/
common.test.ts
File metadata and controls
90 lines (81 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import * as assert from 'assert';
import { EOL } from 'os';
import * as vscode from 'vscode';
import { createDeferred } from '../../client/common/helpers';
import { execPythonFile, getInterpreterVersion } from '../../client/common/utils';
import { initialize } from './../initialize';
// Defines a Mocha test suite to group tests of similar kind together
suite('ChildProc', () => {
setup(initialize);
teardown(initialize);
test('Standard Response', done => {
execPythonFile(undefined, 'python', ['-c', 'print(1)'], __dirname, false).then(data => {
assert.ok(data === `1${EOL}`);
}).then(done).catch(done);
});
test('Error Response', done => {
// tslint:disable-next-line:no-any
const def = createDeferred<any>();
execPythonFile(undefined, 'python', ['-c', 'print(1'], __dirname, false).then(() => {
def.reject('Should have failed');
}).catch(() => {
def.resolve();
});
def.promise.then(done).catch(done);
});
test('Stream Stdout', done => {
const output: string[] = [];
function handleOutput(data: string) {
output.push(data);
}
execPythonFile(undefined, 'python', ['-c', 'print(1)'], __dirname, false, handleOutput).then(() => {
assert.equal(output.length, 1, 'Ouput length incorrect');
assert.equal(output[0], `1${EOL}`, 'Ouput value incorrect');
}).then(done).catch(done);
});
test('Stream Stdout (Unicode)', async () => {
const output: string[] = [];
function handleOutput(data: string) {
output.push(data);
}
await execPythonFile(undefined, 'python', ['-c', 'print(\'öä\')'], __dirname, false, handleOutput);
assert.equal(output.length, 1, 'Ouput length incorrect');
assert.equal(output[0], `öä${EOL}`, 'Ouput value incorrect');
});
test('Stream Stdout with Threads', function (done) {
// tslint:disable-next-line:no-invalid-this
this.timeout(6000);
const output: string[] = [];
function handleOutput(data: string) {
output.push(data);
}
execPythonFile(undefined, 'python', ['-c', 'import sys\nprint(1)\nsys.__stdout__.flush()\nimport time\ntime.sleep(5)\nprint(2)'], __dirname, false, handleOutput).then(() => {
assert.equal(output.length, 2, 'Ouput length incorrect');
assert.equal(output[0], `1${EOL}`, 'First Ouput value incorrect');
assert.equal(output[1], `2${EOL}`, 'Second Ouput value incorrect');
}).then(done).catch(done);
});
test('Kill', done => {
// tslint:disable-next-line:no-any
const def = createDeferred<any>();
const output: string[] = [];
function handleOutput(data: string) {
output.push(data);
}
const cancellation = new vscode.CancellationTokenSource();
execPythonFile(undefined, 'python', ['-c', 'import sys\nprint(1)\nsys.__stdout__.flush()\nimport time\ntime.sleep(5)\nprint(2)'], __dirname, false, handleOutput, cancellation.token).then(() => {
def.reject('Should not have completed');
}).catch(() => {
def.resolve();
});
setTimeout(() => {
cancellation.cancel();
}, 1000);
def.promise.then(done).catch(done);
});
test('Get Python display name', async () => {
const displayName = await getInterpreterVersion('python');
assert.equal(typeof displayName, 'string', 'Display name not returned');
assert.notEqual(displayName.length, 0, 'Display name cannot be empty');
});
});