forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.test.ts
More file actions
260 lines (228 loc) · 13 KB
/
debugger.test.ts
File metadata and controls
260 lines (228 loc) · 13 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
import { assert, expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import { instance, mock } from 'ts-mockito';
import { ConfigurationTarget } from 'vscode';
import { CommandSource } from '../../client/common/constants';
import { createDeferred } from '../../client/common/utils/async';
import { ICondaService, IInterpreterService } from '../../client/interpreter/contracts';
import { InterpreterService } from '../../client/interpreter/interpreterService';
import { CondaService } from '../../client/pythonEnvironments/discovery/locators/services/condaService';
import { TestManagerRunner as NoseTestManagerRunner } from '../../client/testing//nosetest/runner';
import { TestManagerRunner as PytestManagerRunner } from '../../client/testing//pytest/runner';
import { TestManagerRunner as UnitTestTestManagerRunner } from '../../client/testing//unittest/runner';
import { ArgumentsHelper } from '../../client/testing/common/argumentsHelper';
import {
CANCELLATION_REASON,
NOSETEST_PROVIDER,
PYTEST_PROVIDER,
UNITTEST_PROVIDER,
} from '../../client/testing/common/constants';
import { TestRunner } from '../../client/testing/common/runner';
import {
IArgumentsHelper,
IArgumentsService,
ITestDebugLauncher,
ITestManagerFactory,
ITestManagerRunner,
ITestMessageService,
ITestRunner,
IUnitTestHelper,
IXUnitParser,
} from '../../client/testing/common/types';
import { XUnitParser } from '../../client/testing/common/xUnitParser';
import { ArgumentsService as NoseTestArgumentsService } from '../../client/testing/nosetest/services/argsService';
import { ArgumentsService as PyTestArgumentsService } from '../../client/testing/pytest/services/argsService';
import { TestMessageService } from '../../client/testing/pytest/services/testMessageService';
import { TestProvider } from '../../client/testing/types';
import { UnitTestHelper } from '../../client/testing/unittest/helper';
import { ArgumentsService as UnitTestArgumentsService } from '../../client/testing/unittest/services/argsService';
import { deleteDirectory, rootWorkspaceUri, updateSetting } from '../common';
import { initialize, initializeTest, IS_MULTI_ROOT_TEST, TEST_TIMEOUT } from './../initialize';
import { MockDebugLauncher } from './mocks';
import { UnitTestIocContainer } from './serviceRegistry';
use(chaiAsPromised);
const testFilesPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'testFiles', 'debuggerTest');
const defaultUnitTestArgs = ['-v', '-s', '.', '-p', '*test*.py'];
suite('Unit Tests - debugging', () => {
let ioc: UnitTestIocContainer;
const configTarget = IS_MULTI_ROOT_TEST ? ConfigurationTarget.WorkspaceFolder : ConfigurationTarget.Workspace;
suiteSetup(async function () {
this.timeout(TEST_TIMEOUT * 2);
// Test discovery is where the delay is, hence give 10 seconds (as we discover tests at least twice in each test).
await initialize();
await Promise.all([
updateSetting('testing.unittestArgs', defaultUnitTestArgs, rootWorkspaceUri, configTarget),
updateSetting('testing.nosetestArgs', [], rootWorkspaceUri, configTarget),
updateSetting('testing.pytestArgs', [], rootWorkspaceUri, configTarget),
]);
});
setup(async function () {
this.timeout(TEST_TIMEOUT * 2); // This hook requires more timeout as we're deleting files as well
await deleteDirectory(path.join(testFilesPath, '.cache'));
await initializeTest();
await initializeDI();
});
teardown(async function () {
// It's been observed that each call to `updateSetting` can take upto 20 seconds on Windows, hence increasing timeout.
this.timeout(TEST_TIMEOUT * 3);
await ioc.dispose();
await Promise.all([
updateSetting('testing.unittestArgs', defaultUnitTestArgs, rootWorkspaceUri, configTarget),
updateSetting('testing.nosetestArgs', [], rootWorkspaceUri, configTarget),
updateSetting('testing.pytestArgs', [], rootWorkspaceUri, configTarget),
]);
});
async function initializeDI() {
ioc = new UnitTestIocContainer();
ioc.registerCommonTypes();
ioc.registerProcessTypes();
ioc.registerVariableTypes();
ioc.registerTestParsers();
ioc.registerTestVisitors();
ioc.registerTestDiscoveryServices();
ioc.registerTestDiagnosticServices();
ioc.registerTestResultsHelper();
ioc.registerTestStorage();
ioc.registerTestsHelper();
ioc.registerTestManagers();
ioc.registerMockUnitTestSocketServer();
ioc.registerInterpreterStorageTypes();
await ioc.registerMockInterpreterTypes();
ioc.serviceManager.add<IArgumentsHelper>(IArgumentsHelper, ArgumentsHelper);
ioc.serviceManager.add<ITestRunner>(ITestRunner, TestRunner);
ioc.serviceManager.add<IXUnitParser>(IXUnitParser, XUnitParser);
ioc.serviceManager.add<IUnitTestHelper>(IUnitTestHelper, UnitTestHelper);
ioc.serviceManager.add<IArgumentsService>(IArgumentsService, NoseTestArgumentsService, NOSETEST_PROVIDER);
ioc.serviceManager.add<IArgumentsService>(IArgumentsService, PyTestArgumentsService, PYTEST_PROVIDER);
ioc.serviceManager.add<IArgumentsService>(IArgumentsService, UnitTestArgumentsService, UNITTEST_PROVIDER);
ioc.serviceManager.add<ITestManagerRunner>(ITestManagerRunner, PytestManagerRunner, PYTEST_PROVIDER);
ioc.serviceManager.add<ITestManagerRunner>(ITestManagerRunner, NoseTestManagerRunner, NOSETEST_PROVIDER);
ioc.serviceManager.add<ITestManagerRunner>(ITestManagerRunner, UnitTestTestManagerRunner, UNITTEST_PROVIDER);
ioc.serviceManager.addSingleton<ITestDebugLauncher>(ITestDebugLauncher, MockDebugLauncher);
ioc.serviceManager.addSingleton<ITestMessageService>(ITestMessageService, TestMessageService, PYTEST_PROVIDER);
ioc.serviceManager.rebindInstance<ICondaService>(ICondaService, instance(mock(CondaService)));
ioc.serviceManager.rebindInstance<IInterpreterService>(IInterpreterService, instance(mock(InterpreterService)));
}
async function testStartingDebugger(testProvider: TestProvider) {
const testManager = ioc.serviceContainer.get<ITestManagerFactory>(ITestManagerFactory)(
testProvider,
rootWorkspaceUri!,
testFilesPath,
);
const mockDebugLauncher = ioc.serviceContainer.get<MockDebugLauncher>(ITestDebugLauncher);
const tests = await testManager.discoverTests(CommandSource.commandPalette, true, true);
assert.equal(tests.testFiles.length, 2, 'Incorrect number of test files');
assert.equal(tests.testFunctions.length, 2, 'Incorrect number of test functions');
assert.equal(tests.testSuites.length, 2, 'Incorrect number of test suites');
const deferred = createDeferred<string>();
const testFunction = [tests.testFunctions[0].testFunction];
const runningPromise = testManager.runTest(CommandSource.commandPalette, { testFunction }, false, true);
// This promise should never resolve nor reject.
runningPromise
.then(() => deferred.reject("Debugger stopped when it shouldn't have"))
.catch((error) => deferred.reject(error));
mockDebugLauncher.launched
.then((launched) => {
if (launched) {
deferred.resolve('');
} else {
deferred.reject('Debugger not launched');
}
})
.catch((error) => deferred.reject(error));
await deferred.promise;
}
test('Debugger should start (unittest)', async () => {
await updateSetting('testing.unittestArgs', ['-s=./tests', '-p=test_*.py'], rootWorkspaceUri, configTarget);
await testStartingDebugger('unittest');
});
test('Debugger should start (pytest)', async () => {
await updateSetting('testing.pytestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
await testStartingDebugger('pytest');
});
test('Debugger should start (nosetest)', async () => {
await updateSetting('testing.nosetestArgs', ['-m', 'test'], rootWorkspaceUri, configTarget);
await testStartingDebugger('nosetest');
});
async function testStoppingDebugger(testProvider: TestProvider) {
const testManager = ioc.serviceContainer.get<ITestManagerFactory>(ITestManagerFactory)(
testProvider,
rootWorkspaceUri!,
testFilesPath,
);
const mockDebugLauncher = ioc.serviceContainer.get<MockDebugLauncher>(ITestDebugLauncher);
const tests = await testManager.discoverTests(CommandSource.commandPalette, true, true);
assert.equal(tests.testFiles.length, 2, 'Incorrect number of test files');
assert.equal(tests.testFunctions.length, 2, 'Incorrect number of test functions');
assert.equal(tests.testSuites.length, 2, 'Incorrect number of test suites');
const testFunction = [tests.testFunctions[0].testFunction];
const runningPromise = testManager.runTest(CommandSource.commandPalette, { testFunction }, false, true);
const launched = await mockDebugLauncher.launched;
assert.isTrue(launched, 'Debugger not launched');
const discoveryPromise = testManager.discoverTests(CommandSource.commandPalette, true, true, true);
await expect(runningPromise).to.be.rejectedWith(
CANCELLATION_REASON,
'Incorrect reason for ending the debugger',
);
await ioc.dispose(); // will cancel test discovery
await expect(discoveryPromise).to.be.rejectedWith(
CANCELLATION_REASON,
'Incorrect reason for ending the debugger',
);
}
test('Debugger should stop when user invokes a test discovery (unittest)', async () => {
await updateSetting('testing.unittestArgs', ['-s=./tests', '-p=test_*.py'], rootWorkspaceUri, configTarget);
await testStoppingDebugger('unittest');
});
test('Debugger should stop when user invokes a test discovery (pytest)', async () => {
await updateSetting('testing.pytestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
await testStoppingDebugger('pytest');
});
test('Debugger should stop when user invokes a test discovery (nosetest)', async () => {
await updateSetting('testing.nosetestArgs', ['-m', 'test'], rootWorkspaceUri, configTarget);
await testStoppingDebugger('nosetest');
});
async function testDebuggerWhenRediscoveringTests(testProvider: TestProvider) {
const testManager = ioc.serviceContainer.get<ITestManagerFactory>(ITestManagerFactory)(
testProvider,
rootWorkspaceUri!,
testFilesPath,
);
const mockDebugLauncher = ioc.serviceContainer.get<MockDebugLauncher>(ITestDebugLauncher);
const tests = await testManager.discoverTests(CommandSource.commandPalette, true, true);
assert.equal(tests.testFiles.length, 2, 'Incorrect number of test files');
assert.equal(tests.testFunctions.length, 2, 'Incorrect number of test functions');
assert.equal(tests.testSuites.length, 2, 'Incorrect number of test suites');
const testFunction = [tests.testFunctions[0].testFunction];
const runningPromise = testManager.runTest(CommandSource.commandPalette, { testFunction }, false, true);
const launched = await mockDebugLauncher.launched;
assert.isTrue(launched, 'Debugger not launched');
const discoveryPromise = testManager.discoverTests(CommandSource.commandPalette, false, true);
const deferred = createDeferred<string>();
discoveryPromise
.then(() => deferred.resolve(''))
.catch((ex) => deferred.reject(ex));
// This promise should never resolve nor reject.
runningPromise
.then(() => "Debugger stopped when it shouldn't have")
.catch(() => "Debugger crashed when it shouldn't have")
.then((error) => {
deferred.reject(error);
});
// Should complete without any errors
await deferred.promise;
}
test('Debugger should not stop when test discovery is invoked automatically by extension (unittest)', async () => {
await updateSetting('testing.unittestArgs', ['-s=./tests', '-p=test_*.py'], rootWorkspaceUri, configTarget);
await testDebuggerWhenRediscoveringTests('unittest');
});
test('Debugger should not stop when test discovery is invoked automatically by extension (pytest)', async () => {
await updateSetting('testing.pytestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
await testDebuggerWhenRediscoveringTests('pytest');
});
test('Debugger should not stop when test discovery is invoked automatically by extension (nosetest)', async () => {
await updateSetting('testing.nosetestArgs', ['-m', 'test'], rootWorkspaceUri, configTarget);
await testDebuggerWhenRediscoveringTests('nosetest');
});
});