forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.unit.test.ts
More file actions
384 lines (355 loc) · 14.8 KB
/
Copy pathserver.unit.test.ts
File metadata and controls
384 lines (355 loc) · 14.8 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as assert from 'assert';
import * as net from 'net';
import * as sinon from 'sinon';
import * as crypto from 'crypto';
import { Observable } from 'rxjs';
import * as typeMoq from 'typemoq';
import { OutputChannel, Uri } from 'vscode';
import {
IPythonExecutionFactory,
IPythonExecutionService,
ObservableExecutionResult,
Output,
} from '../../../client/common/process/types';
import { PythonTestServer } from '../../../client/testing/testController/common/server';
import { ITestDebugLauncher, LaunchOptions } from '../../../client/testing/common/types';
import { Deferred, createDeferred } from '../../../client/common/utils/async';
import { MockChildProcess } from '../../mocks/mockChildProcess';
import {
PAYLOAD_MULTI_CHUNK,
PAYLOAD_SINGLE_CHUNK,
PAYLOAD_SPLIT_ACROSS_CHUNKS_ARRAY,
DataWithPayloadChunks,
} from './payloadTestCases';
import { traceLog } from '../../../client/logging';
const testCases = [
{
val: () => PAYLOAD_SINGLE_CHUNK('fake-uuid'),
},
{
val: () => PAYLOAD_MULTI_CHUNK('fake-uuid'),
},
{
val: () => PAYLOAD_SPLIT_ACROSS_CHUNKS_ARRAY('fake-uuid'),
},
];
suite('Python Test Server, DataWithPayloadChunks', () => {
const FAKE_UUID = 'fake-uuid';
let server: PythonTestServer;
let v4Stub: sinon.SinonStub;
let debugLauncher: ITestDebugLauncher;
let mockProc: MockChildProcess;
let execService: typeMoq.IMock<IPythonExecutionService>;
let deferred: Deferred<void>;
const sandbox = sinon.createSandbox();
setup(async () => {
// set up test command options
v4Stub = sandbox.stub(crypto, 'randomUUID');
v4Stub.returns(FAKE_UUID);
// set up exec service with child process
mockProc = new MockChildProcess('', ['']);
execService = typeMoq.Mock.ofType<IPythonExecutionService>();
const outputObservable = new Observable<Output<string>>(() => {
/* no op */
});
execService
.setup((x) => x.execObservable(typeMoq.It.isAny(), typeMoq.It.isAny()))
.returns(() => ({
proc: mockProc,
out: outputObservable,
dispose: () => {
/* no-body */
},
}));
execService.setup((p) => ((p as unknown) as any).then).returns(() => undefined);
});
teardown(() => {
sandbox.restore();
server.dispose();
});
testCases.forEach((testCase) => {
test(`run correctly`, async () => {
const testCaseDataObj: DataWithPayloadChunks = testCase.val();
let eventData = '';
const client = new net.Socket();
deferred = createDeferred();
mockProc = new MockChildProcess('', ['']);
const output2 = new Observable<Output<string>>(() => {
/* no op */
});
const stubExecutionService2 = ({
execObservable: () => {
client.connect(server.getPort());
return {
proc: mockProc,
out: output2,
dispose: () => {
/* no-body */
},
};
},
} as unknown) as IPythonExecutionService;
const stubExecutionFactory2 = ({
createActivatedEnvironment: () => Promise.resolve(stubExecutionService2),
} as unknown) as IPythonExecutionFactory;
server = new PythonTestServer(stubExecutionFactory2, debugLauncher);
const uuid = server.createUUID();
const options = {
command: { script: 'myscript', args: ['-foo', 'foo'] },
workspaceFolder: Uri.file('/foo/bar'),
cwd: '/foo/bar',
uuid,
};
const dataWithPayloadChunks = testCaseDataObj;
await server.serverReady();
let errorOccur = false;
let errorMessage = '';
server.onRunDataReceived(({ data }) => {
try {
const resultData = JSON.parse(data).result;
eventData = eventData + JSON.stringify(resultData);
} catch (e) {
errorOccur = true;
errorMessage = 'Error parsing data';
}
deferred.resolve();
});
client.on('connect', () => {
traceLog('Socket connected, local port:', client.localPort);
// since this test is a single payload as a single chunk there should be a single line in the payload.
for (const line of dataWithPayloadChunks.payloadArray) {
client.write(line);
}
client.end();
});
client.on('error', (error) => {
traceLog('Socket connection error:', error);
});
server.sendCommand(options, {});
await deferred.promise;
const expectedResult = dataWithPayloadChunks.data;
assert.deepStrictEqual(eventData, expectedResult);
assert.deepStrictEqual(errorOccur, false, errorMessage);
});
});
});
suite('Python Test Server, Send command etc', () => {
const FAKE_UUID = 'fake-uuid';
let server: PythonTestServer;
let v4Stub: sinon.SinonStub;
let debugLauncher: ITestDebugLauncher;
let mockProc: MockChildProcess;
let execService: typeMoq.IMock<IPythonExecutionService>;
let deferred: Deferred<void>;
const sandbox = sinon.createSandbox();
setup(async () => {
// set up test command options
v4Stub = sandbox.stub(crypto, 'randomUUID');
v4Stub.returns(FAKE_UUID);
// set up exec service with child process
mockProc = new MockChildProcess('', ['']);
execService = typeMoq.Mock.ofType<IPythonExecutionService>();
execService.setup((p) => ((p as unknown) as any).then).returns(() => undefined);
});
teardown(() => {
sandbox.restore();
server.dispose();
});
test('sendCommand should add the port to the command being sent and add the correct extra spawn variables', async () => {
const deferred2 = createDeferred();
const RUN_TEST_IDS_PORT_CONST = '5678';
let error = false;
let errorMessage = '';
execService
.setup((x) => x.execObservable(typeMoq.It.isAny(), typeMoq.It.isAny()))
.returns((_args, options2) => {
try {
assert.strictEqual(
options2.env.PYTHONPATH,
'/foo/bar',
'Expect python path to exist as extra variable and be set correctly',
);
assert.strictEqual(
options2.env.RUN_TEST_IDS_PORT,
RUN_TEST_IDS_PORT_CONST,
'Expect test id port to be in extra variables and set correctly',
);
assert.strictEqual(
options2.env.TEST_UUID,
FAKE_UUID,
'Expect test uuid to be in extra variables and set correctly',
);
assert.strictEqual(
options2.env.TEST_PORT,
'12345',
'Expect server port to be set correctly as a env var',
);
} catch (e) {
error = true;
errorMessage = `error occurred, assertion was incorrect, ${e}`;
}
return typeMoq.Mock.ofType<ObservableExecutionResult<string>>().object;
});
const execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
deferred2.resolve();
return Promise.resolve(execService.object);
});
server = new PythonTestServer(execFactory.object, debugLauncher);
sinon.stub(server, 'getPort').returns(12345);
// const portServer = server.getPort();
await server.serverReady();
const options = {
command: { script: 'myscript', args: ['-foo', 'foo'] },
workspaceFolder: Uri.file('/foo/bar'),
cwd: '/foo/bar',
uuid: FAKE_UUID,
};
try {
server.sendCommand(options, {}, RUN_TEST_IDS_PORT_CONST);
} catch (e) {
assert(false, `Error sending command, ${e}`);
}
// add in await and trigger
await deferred2.promise;
mockProc.trigger('close');
const expectedArgs = ['myscript', '-foo', 'foo'];
execService.verify((x) => x.execObservable(expectedArgs, typeMoq.It.isAny()), typeMoq.Times.once());
if (error) {
assert(false, errorMessage);
}
});
test('sendCommand should add right extra variables to command during debug', async () => {
const deferred2 = createDeferred();
const RUN_TEST_IDS_PORT_CONST = '5678';
const error = false;
const errorMessage = '';
const debugLauncherMock = typeMoq.Mock.ofType<ITestDebugLauncher>();
let actualLaunchOptions: LaunchOptions = {} as LaunchOptions;
const deferred4 = createDeferred();
debugLauncherMock
.setup((x) => x.launchDebugger(typeMoq.It.isAny(), typeMoq.It.isAny()))
.returns((options, _) => {
actualLaunchOptions = options;
deferred4.resolve();
return Promise.resolve();
});
execService
.setup((x) => x.execObservable(typeMoq.It.isAny(), typeMoq.It.isAny()))
.returns(() => typeMoq.Mock.ofType<ObservableExecutionResult<string>>().object);
const execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
deferred2.resolve();
return Promise.resolve(execService.object);
});
server = new PythonTestServer(execFactory.object, debugLauncherMock.object);
sinon.stub(server, 'getPort').returns(12345);
// const portServer = server.getPort();
await server.serverReady();
const options = {
command: { script: 'myscript', args: ['-foo', 'foo'] },
workspaceFolder: Uri.file('/foo/bar'),
cwd: '/foo/bar',
uuid: FAKE_UUID,
debugBool: true,
};
try {
server.sendCommand(options, {}, RUN_TEST_IDS_PORT_CONST);
} catch (e) {
assert(false, `Error sending command, ${e}`);
}
// add in await and trigger
await deferred2.promise;
await deferred4.promise;
mockProc.trigger('close');
assert.notDeepEqual(actualLaunchOptions, {}, 'launch options should be set');
assert.strictEqual(actualLaunchOptions.cwd, '/foo/bar');
assert.strictEqual(actualLaunchOptions.testProvider, 'unittest');
assert.strictEqual(actualLaunchOptions.pytestPort, '12345');
assert.strictEqual(actualLaunchOptions.pytestUUID, 'fake-uuid');
assert.strictEqual(actualLaunchOptions.runTestIdsPort, '5678');
debugLauncherMock.verify((x) => x.launchDebugger(typeMoq.It.isAny(), typeMoq.It.isAny()), typeMoq.Times.once());
if (error) {
assert(false, errorMessage);
}
});
test('sendCommand should write to an output channel if it is provided as an option', async () => {
const output2: string[] = [];
const outChannel = {
appendLine: (str: string) => {
output2.push(str);
},
} as OutputChannel;
const options = {
command: {
script: 'myscript',
args: ['-foo', 'foo'],
},
workspaceFolder: Uri.file('/foo/bar'),
cwd: '/foo/bar',
uuid: FAKE_UUID,
outChannel,
};
deferred = createDeferred();
const execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
deferred.resolve();
return Promise.resolve(execService.object);
});
server = new PythonTestServer(execFactory.object, debugLauncher);
await server.serverReady();
server.sendCommand(options, {});
// add in await and trigger
await deferred.promise;
mockProc.trigger('close');
const expected = ['python', 'myscript', '-foo', 'foo'].join(' ');
assert.equal(output2.length, 1);
assert.deepStrictEqual(output2, [expected]);
});
test('If script execution fails during sendCommand, an onDataReceived event should be fired with the "error" status', async () => {
let eventData: { status: string; errors: string[] } | undefined;
const deferred2 = createDeferred();
const deferred3 = createDeferred();
const stubExecutionService = typeMoq.Mock.ofType<IPythonExecutionService>();
stubExecutionService.setup((p) => ((p as unknown) as any).then).returns(() => undefined);
stubExecutionService
.setup((x) => x.execObservable(typeMoq.It.isAny(), typeMoq.It.isAny()))
.returns(() => {
deferred3.resolve();
throw new Error('Failed to execute');
});
const options = {
command: { script: 'myscript', args: ['-foo', 'foo'] },
workspaceFolder: Uri.file('/foo/bar'),
cwd: '/foo/bar',
uuid: FAKE_UUID,
};
const stubExecutionFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
stubExecutionFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
deferred2.resolve();
return Promise.resolve(stubExecutionService.object);
});
server = new PythonTestServer(stubExecutionFactory.object, debugLauncher);
await server.serverReady();
server.onDataReceived(({ data }) => {
eventData = JSON.parse(data);
});
server.sendCommand(options, {});
await deferred2.promise;
await deferred3.promise;
assert.notEqual(eventData, undefined);
assert.deepStrictEqual(eventData?.status, 'error');
assert.deepStrictEqual(eventData?.errors, ['Failed to execute']);
});
});