forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.test.ts
More file actions
202 lines (177 loc) · 10.5 KB
/
debugger.test.ts
File metadata and controls
202 lines (177 loc) · 10.5 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
import { assert, expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import { ConfigurationTarget } from 'vscode';
import { createDeferred } from '../../client/common/helpers';
import { BaseTestManager } from '../../client/unittests/common/baseTestManager';
import { CANCELLATION_REASON, CommandSource } from '../../client/unittests/common/constants';
import { TestCollectionStorageService } from '../../client/unittests/common/storageService';
import { TestResultsService } from '../../client/unittests/common/testResultsService';
import { TestsHelper } from '../../client/unittests/common/testUtils';
import { ITestCollectionStorageService, ITestResultsService, ITestsHelper } from '../../client/unittests/common/types';
import { TestResultDisplay } from '../../client/unittests/display/main';
import { TestManager as NosetestManager } from '../../client/unittests/nosetest/main';
import { TestManager as PytestManager } from '../../client/unittests/pytest/main';
import { TestManager as UnitTestManager } from '../../client/unittests/unittest/main';
import { deleteDirectory, rootWorkspaceUri, updateSetting } from '../common';
import { initialize, initializeTest, IS_MULTI_ROOT_TEST } from './../initialize';
import { MockOutputChannel } from './../mockClasses';
import { MockDebugLauncher } from './mocks';
use(chaiAsPromised);
const testFilesPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'testFiles', 'debuggerTest');
const defaultUnitTestArgs = [
'-v',
'-s',
'.',
'-p',
'*test*.py'
];
// tslint:disable-next-line:max-func-body-length
suite('Unit Tests Debugging', () => {
let testManager: BaseTestManager;
let testResultDisplay: TestResultDisplay;
let outChannel: MockOutputChannel;
let storageService: ITestCollectionStorageService;
let resultsService: ITestResultsService;
let mockDebugLauncher: MockDebugLauncher;
let testsHelper: ITestsHelper;
const configTarget = IS_MULTI_ROOT_TEST ? ConfigurationTarget.WorkspaceFolder : ConfigurationTarget.Workspace;
suiteSetup(async function () {
// Test disvovery is where the delay is, hence give 10 seconds (as we discover tests at least twice in each test).
// tslint:disable-next-line:no-invalid-this
this.timeout(10000);
await initialize();
await updateSetting('unitTest.unittestArgs', defaultUnitTestArgs, rootWorkspaceUri, configTarget);
await updateSetting('unitTest.nosetestArgs', [], rootWorkspaceUri, configTarget);
await updateSetting('unitTest.pyTestArgs', [], rootWorkspaceUri, configTarget);
});
setup(async () => {
outChannel = new MockOutputChannel('Python Test Log');
testResultDisplay = new TestResultDisplay(outChannel);
await deleteDirectory(path.join(testFilesPath, '.cache'));
await initializeTest();
});
teardown(async () => {
await updateSetting('unitTest.unittestArgs', defaultUnitTestArgs, rootWorkspaceUri, configTarget);
await updateSetting('unitTest.nosetestArgs', [], rootWorkspaceUri, configTarget);
await updateSetting('unitTest.pyTestArgs', [], rootWorkspaceUri, configTarget);
outChannel.dispose();
mockDebugLauncher.dispose();
if (testManager) {
testManager.dispose();
}
testResultDisplay.dispose();
});
function createTestManagerDepedencies() {
storageService = new TestCollectionStorageService();
resultsService = new TestResultsService();
testsHelper = new TestsHelper();
mockDebugLauncher = new MockDebugLauncher();
}
async function testStartingDebugger() {
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];
// tslint:disable-next-line:no-floating-promises
testManager.runTest(CommandSource.commandPalette, { testFunction }, false, true);
const launched = await mockDebugLauncher.launched;
assert.isTrue(launched, 'Debugger not launched');
}
test('Debugger should start (unittest)', async () => {
await updateSetting('unitTest.unittestArgs', ['-s=./tests', '-p=test_*.py'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new UnitTestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testStartingDebugger();
});
test('Debugger should start (pytest)', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new PytestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testStartingDebugger();
});
test('Debugger should start (nosetest)', async () => {
await updateSetting('unitTest.nosetestArgs', ['-m', 'test'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new NosetestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testStartingDebugger();
});
async function testStoppingDebugger() {
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');
// tslint:disable-next-line:no-floating-promises
testManager.discoverTests(CommandSource.commandPalette, true, true, true);
await expect(runningPromise).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('unitTest.unittestArgs', ['-s=./tests', '-p=test_*.py'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new UnitTestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testStoppingDebugger();
});
test('Debugger should stop when user invokes a test discovery (pytest)', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new PytestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testStoppingDebugger();
});
test('Debugger should stop when user invokes a test discovery (nosetest)', async () => {
await updateSetting('unitTest.nosetestArgs', ['-m', 'test'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new NosetestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testStoppingDebugger();
});
async function testDebuggerWhenRediscoveringTests() {
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>();
// tslint:disable-next-line:no-floating-promises
discoveryPromise
// tslint:disable-next-line:no-unsafe-any
.then(() => deferred.resolve(''))
// tslint:disable-next-line:no-unsafe-any
.catch(ex => deferred.reject(ex));
// This promise should never resolve nor reject.
// tslint:disable-next-line:no-floating-promises
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('unitTest.unittestArgs', ['-s=./tests', '-p=test_*.py'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new UnitTestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testDebuggerWhenRediscoveringTests();
});
test('Debugger should not stop when test discovery is invoked automatically by extension (pytest)', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new PytestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testDebuggerWhenRediscoveringTests();
});
test('Debugger should not stop when test discovery is invoked automatically by extension (nosetest)', async () => {
await updateSetting('unitTest.nosetestArgs', ['-m', 'test'], rootWorkspaceUri, configTarget);
createTestManagerDepedencies();
testManager = new NosetestManager(testFilesPath, outChannel, storageService, resultsService, testsHelper, mockDebugLauncher);
await testDebuggerWhenRediscoveringTests();
});
});