forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.framework.test.ts
More file actions
174 lines (149 loc) · 7.11 KB
/
Copy pathweb.framework.test.ts
File metadata and controls
174 lines (149 loc) · 7.11 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:no-suspicious-comment max-func-body-length no-invalid-this no-var-requires no-require-imports no-any no-http-string no-string-literal no-console
import { expect } from 'chai';
import * as getFreePort from 'get-port';
import * as path from 'path';
import { DebugClient } from 'vscode-debugadapter-testsupport';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import { noop } from '../../client/common/core.utils';
import { StopWatch } from '../../client/common/stopWatch';
import { DebugOptions, LaunchRequestArguments } from '../../client/debugger/Common/Contracts';
import { PYTHON_PATH, sleep } from '../common';
import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
import { DEBUGGER_TIMEOUT } from './common/constants';
import { continueDebugging, createDebugAdapter, ExpectedVariable, hitHttpBreakpoint, makeHttpRequest, validateVariablesInFrame } from './utils';
let testCounter = 0;
const debuggerType = 'pythonExperimental';
suite(`Django and Flask Debugging: ${debuggerType}`, () => {
let debugClient: DebugClient;
setup(async function () {
if (!IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) {
this.skip();
}
this.timeout(5 * DEBUGGER_TIMEOUT);
const coverageDirectory = path.join(EXTENSION_ROOT_DIR, `debug_coverage_django_flask${testCounter += 1}`);
debugClient = await createDebugAdapter(coverageDirectory);
});
teardown(async () => {
// Wait for a second before starting another test (sometimes, sockets take a while to get closed).
await sleep(1000);
try {
await debugClient.stop().catch(noop);
// tslint:disable-next-line:no-empty
} catch (ex) { }
await sleep(1000);
});
function buildLaunchArgs(workspaceDirectory: string): LaunchRequestArguments {
const env = {};
// tslint:disable-next-line:no-string-literal
env['PYTHONPATH'] = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'ptvsd');
// tslint:disable-next-line:no-unnecessary-local-variable
const options: LaunchRequestArguments = {
cwd: workspaceDirectory,
program: '',
debugOptions: [DebugOptions.RedirectOutput],
pythonPath: PYTHON_PATH,
args: [],
env,
envFile: '',
logToFile: false,
type: debuggerType
};
return options;
}
async function buildFlaskLaunchArgs(workspaceDirectory: string) {
const port = await getFreePort({ host: 'localhost' });
const options = buildLaunchArgs(workspaceDirectory);
options.env!['FLASK_APP'] = 'run.py';
options.module = 'flask';
options.debugOptions = [DebugOptions.RedirectOutput, DebugOptions.Jinja];
options.args = [
'run',
'--no-debugger',
'--no-reload',
'--without-threads',
'--port',
`${port}`
];
return { options, port };
}
async function buildDjangoLaunchArgs(workspaceDirectory: string) {
const port = await getFreePort({ host: 'localhost' });
const options = buildLaunchArgs(workspaceDirectory);
options.program = path.join(workspaceDirectory, 'manage.py');
options.debugOptions = [DebugOptions.RedirectOutput, DebugOptions.Django];
options.args = [
'runserver',
'--noreload',
'--nothreading',
`${port}`
];
return { options, port };
}
async function waitForWebServerToStart(url: string): Promise<any> {
return new Promise<any>((resolve, reject) => {
let maxTries = 50;
const timeout = 10_000;
const start = new StopWatch();
const fn = () => {
makeHttpRequest(url)
.then(resolve)
.catch(ex => {
maxTries -= 1;
if (maxTries === 0 || start.elapsedTime >= timeout) {
reject(ex);
} else {
setTimeout(fn, 100);
}
});
};
fn();
});
}
async function testTemplateDebugging(launchArgs: LaunchRequestArguments, port: number, viewFile: string, viewLine: number, templateFile: string, templateLine: number) {
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(launchArgs),
debugClient.waitForEvent('initialized'),
debugClient.waitForEvent('process'),
debugClient.waitForEvent('thread')
]);
const url = `http://localhost:${port}`;
await waitForWebServerToStart(url);
const httpResult = await makeHttpRequest(url);
expect(httpResult).to.contain('Hello this_is_a_value_from_server');
expect(httpResult).to.contain('Hello this_is_another_value_from_server');
await hitHttpBreakpoint(debugClient, url, viewFile, viewLine);
await continueDebugging(debugClient);
await debugClient.setBreakpointsRequest({ breakpoints: [], lines: [], source: { path: viewFile } });
// Template debugging.
const [stackTrace, htmlResultPromise] = await hitHttpBreakpoint(debugClient, url, templateFile, templateLine);
// Wait for breakpoint to hit
const expectedVariables: ExpectedVariable[] = [
{ name: 'value_from_server', type: 'str', value: '\'this_is_a_value_from_server\'' },
{ name: 'another_value_from_server', type: 'str', value: '\'this_is_another_value_from_server\'' }
];
await validateVariablesInFrame(debugClient, stackTrace, expectedVariables, 1);
await debugClient.setBreakpointsRequest({ breakpoints: [], lines: [], source: { path: templateFile } });
await continueDebugging(debugClient);
const htmlResult = await htmlResultPromise;
expect(htmlResult).to.contain('Hello this_is_a_value_from_server');
expect(htmlResult).to.contain('Hello this_is_another_value_from_server');
}
test('Test Flask Route and Template debugging', async () => {
const workspaceDirectory = path.join(EXTENSION_ROOT_DIR, 'src', 'testMultiRootWkspc', 'workspace5', 'flaskApp');
const { options, port } = await buildFlaskLaunchArgs(workspaceDirectory);
await testTemplateDebugging(options, port,
path.join(workspaceDirectory, 'run.py'), 7,
path.join(workspaceDirectory, 'templates', 'index.html'), 6);
});
test('Test Django Route and Template debugging', async () => {
const workspaceDirectory = path.join(EXTENSION_ROOT_DIR, 'src', 'testMultiRootWkspc', 'workspace5', 'djangoApp');
const { options, port } = await buildDjangoLaunchArgs(workspaceDirectory);
await testTemplateDebugging(options, port,
path.join(workspaceDirectory, 'home', 'views.py'), 10,
path.join(workspaceDirectory, 'home', 'templates', 'index.html'), 6);
});
});