forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpytestDiscoveryAdapter.unit.test.ts
More file actions
179 lines (168 loc) · 7.02 KB
/
Copy pathpytestDiscoveryAdapter.unit.test.ts
File metadata and controls
179 lines (168 loc) · 7.02 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
/* eslint-disable @typescript-eslint/no-explicit-any */
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as assert from 'assert';
import { Uri } from 'vscode';
import * as typeMoq from 'typemoq';
import * as path from 'path';
import { Observable } from 'rxjs/Observable';
import { IConfigurationService, ITestOutputChannel } from '../../../../client/common/types';
import { PytestTestDiscoveryAdapter } from '../../../../client/testing/testController/pytest/pytestDiscoveryAdapter';
import { ITestServer } from '../../../../client/testing/testController/common/types';
import {
IPythonExecutionFactory,
IPythonExecutionService,
SpawnOptions,
Output,
} from '../../../../client/common/process/types';
import { EXTENSION_ROOT_DIR } from '../../../../client/constants';
import { MockChildProcess } from '../../../mocks/mockChildProcess';
import { Deferred, createDeferred } from '../../../../client/common/utils/async';
suite('pytest test discovery adapter', () => {
let testServer: typeMoq.IMock<ITestServer>;
let configService: IConfigurationService;
let execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
let adapter: PytestTestDiscoveryAdapter;
let execService: typeMoq.IMock<IPythonExecutionService>;
let deferred: Deferred<void>;
let outputChannel: typeMoq.IMock<ITestOutputChannel>;
let portNum: number;
let uuid: string;
let expectedPath: string;
let uri: Uri;
let expectedExtraVariables: Record<string, string>;
let mockProc: MockChildProcess;
let deferred2: Deferred<void>;
setup(() => {
const mockExtensionRootDir = typeMoq.Mock.ofType<string>();
mockExtensionRootDir.setup((m) => m.toString()).returns(() => '/mocked/extension/root/dir');
// constants
portNum = 12345;
uuid = 'uuid123';
expectedPath = path.join('/', 'my', 'test', 'path');
uri = Uri.file(expectedPath);
const relativePathToPytest = 'pythonFiles';
const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest);
expectedExtraVariables = {
PYTHONPATH: fullPluginPath,
TEST_UUID: uuid,
TEST_PORT: portNum.toString(),
};
// set up test server
testServer = typeMoq.Mock.ofType<ITestServer>();
testServer.setup((t) => t.getPort()).returns(() => portNum);
testServer.setup((t) => t.createUUID(typeMoq.It.isAny())).returns(() => uuid);
testServer
.setup((t) => t.onDiscoveryDataReceived(typeMoq.It.isAny(), typeMoq.It.isAny()))
.returns(() => ({
dispose: () => {
/* no-body */
},
}));
// set up config service
configService = ({
getSettings: () => ({
testing: { pytestArgs: ['.'] },
}),
} as unknown) as IConfigurationService;
// 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);
outputChannel = typeMoq.Mock.ofType<ITestOutputChannel>();
const output = new Observable<Output<string>>(() => {
/* no op */
});
deferred2 = createDeferred();
execService
.setup((x) => x.execObservable(typeMoq.It.isAny(), typeMoq.It.isAny()))
.returns(() => {
deferred2.resolve();
return {
proc: mockProc,
out: output,
dispose: () => {
/* no-body */
},
};
});
});
test('Discovery should call exec with correct basic args', async () => {
// set up exec mock
deferred = createDeferred();
execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
deferred.resolve();
return Promise.resolve(execService.object);
});
adapter = new PytestTestDiscoveryAdapter(testServer.object, configService, outputChannel.object);
adapter.discoverTests(uri, execFactory.object);
// add in await and trigger
await deferred.promise;
await deferred2.promise;
mockProc.trigger('close');
// verification
execService.verify(
(x) =>
x.execObservable(
typeMoq.It.isAny(),
typeMoq.It.is<SpawnOptions>((options) => {
try {
assert.deepEqual(options.env, expectedExtraVariables);
assert.equal(options.cwd, expectedPath);
assert.equal(options.throwOnStdErr, true);
return true;
} catch (e) {
console.error(e);
throw e;
}
}),
),
typeMoq.Times.once(),
);
});
test('Test discovery correctly pulls pytest args from config service settings', async () => {
// set up a config service with different pytest args
const expectedPathNew = path.join('other', 'path');
const configServiceNew: IConfigurationService = ({
getSettings: () => ({
testing: {
pytestArgs: ['.', 'abc', 'xyz'],
cwd: expectedPathNew,
},
}),
} as unknown) as IConfigurationService;
// set up exec mock
deferred = createDeferred();
execFactory = typeMoq.Mock.ofType<IPythonExecutionFactory>();
execFactory
.setup((x) => x.createActivatedEnvironment(typeMoq.It.isAny()))
.returns(() => {
deferred.resolve();
return Promise.resolve(execService.object);
});
adapter = new PytestTestDiscoveryAdapter(testServer.object, configServiceNew, outputChannel.object);
adapter.discoverTests(uri, execFactory.object);
// add in await and trigger
await deferred.promise;
await deferred2.promise;
mockProc.trigger('close');
// verification
const expectedArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only', '.', 'abc', 'xyz'];
execService.verify(
(x) =>
x.execObservable(
expectedArgs,
typeMoq.It.is<SpawnOptions>((options) => {
assert.deepEqual(options.env, expectedExtraVariables);
assert.equal(options.cwd, expectedPathNew);
assert.equal(options.throwOnStdErr, true);
return true;
}),
),
typeMoq.Times.once(),
);
});
});