Skip to content

Commit

Permalink
Unit tests for debugging module (#1297)
Browse files Browse the repository at this point in the history
Fixes #1188
  • Loading branch information
DonJayamanne authored Apr 5, 2018
1 parent 3e40ce2 commit 2fd7b69
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"coverage": true
},
"typescript.tsdk": "./node_modules/typescript/lib", // we want to use the TS server from our node_modules folder to control its version
"tslint.enable": true, // We will run our own linting in gulp (& git commit hooks), else tslint extension just complains about unmodified files
"tslint.enable": true,
"python.linting.enabled": false,
"python.unitTest.promptToConfigure": false,
"python.workspaceSymbols.enabled": false,
"python.formatting.provider": "none"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@
{
"name": "Python Experimental: Attach",
"type": "pythonExperimental",
"request": "pythonExperimental",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}",
"port": 3000,
Expand Down
8 changes: 4 additions & 4 deletions src/client/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export interface Deferred<T> {
}

class DeferredImpl<T> implements Deferred<T> {
private _resolve: (value?: T | PromiseLike<T>) => void;
private _resolve!: (value?: T | PromiseLike<T>) => void;
// tslint:disable-next-line:no-any
private _reject: (reason?: any) => void;
private _reject!: (reason?: any) => void;
private _resolved: boolean = false;
private _rejected: boolean = false;
private _promise: Promise<T>;
Expand Down Expand Up @@ -70,14 +70,14 @@ export function createDeferred<T>(scope: any = null): Deferred<T> {
return new DeferredImpl<T>(scope);
}

export function createTemporaryFile(extension: string, temporaryDirectory?: string): Promise<{ filePath: string, cleanupCallback: Function }> {
export function createTemporaryFile(extension: string, temporaryDirectory?: string): Promise<{ filePath: string; cleanupCallback: Function }> {
// tslint:disable-next-line:no-any
const options: any = { postfix: extension };
if (temporaryDirectory) {
options.dir = temporaryDirectory;
}

return new Promise<{ filePath: string, cleanupCallback: Function }>((resolve, reject) => {
return new Promise<{ filePath: string; cleanupCallback: Function }>((resolve, reject) => {
tmp.file(options, (err, tmpFile, fd, cleanupCallback) => {
if (err) {
return reject(err);
Expand Down
9 changes: 9 additions & 0 deletions src/client/debugger/Common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import * as path from 'path';
import { EXTENSION_ROOT_DIR } from '../../common/constants';

export const PTVSD_PATH = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'ptvsd');
5 changes: 2 additions & 3 deletions src/client/debugger/DebugClients/LocalDebugClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { ChildProcess, spawn } from 'child_process';
import * as path from 'path';
import { DebugSession, OutputEvent } from 'vscode-debugadapter';
import { DebugProtocol } from 'vscode-debugprotocol';
import { EXTENSION_ROOT_DIR } from '../../common/constants';
import { open } from '../../common/open';
import { PathUtils } from '../../common/platform/pathUtils';
import { CurrentProcess } from '../../common/process/currentProcess';
import { EnvironmentVariablesService } from '../../common/variables/environment';
import { IServiceContainer } from '../../ioc/types';
import { PTVSD_PATH } from '../Common/constants';
import { DebugOptions, IDebugServer, IPythonProcess, LaunchRequestArguments } from '../Common/Contracts';
import { IS_WINDOWS } from '../Common/Utils';
import { BaseDebugServer } from '../DebugServers/BaseDebugServer';
Expand Down Expand Up @@ -88,8 +88,7 @@ export class LocalDebugClient extends DebugClient<LaunchRequestArguments> {
const environmentVariables = await helper.getEnvironmentVariables(this.args);
if (this.args.type === 'pythonExperimental') {
// Import the PTVSD debugger, allowing users to use their own latest copies.
const experimentalPTVSDPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'ptvsd');
environmentVariablesService.appendPythonPath(environmentVariables, experimentalPTVSDPath);
environmentVariablesService.appendPythonPath(environmentVariables, PTVSD_PATH);
}
// tslint:disable-next-line:max-func-body-length cyclomatic-complexity no-any
return new Promise<any>((resolve, reject) => {
Expand Down
4 changes: 2 additions & 2 deletions src/test/debugger/attach.ptvsd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import * as path from 'path';
import { DebugClient } from 'vscode-debugadapter-testsupport';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import '../../client/common/extensions';
import { PTVSD_PATH } from '../../client/debugger/Common/constants';
import { DebugOptions } from '../../client/debugger/Common/Contracts';
import { sleep } from '../common';
import { initialize, IS_APPVEYOR, IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
import { continueDebugging, createDebugAdapter } from './utils';

const fileToDebug = path.join(EXTENSION_ROOT_DIR, 'src', 'testMultiRootWkspc', 'workspace5', 'remoteDebugger-start-with-ptvsd.py');
const ptvsdPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'ptvsd');

suite('Attach Debugger - Experimental', () => {
let debugClient: DebugClient;
Expand Down Expand Up @@ -55,7 +55,7 @@ suite('Attach Debugger - Experimental', () => {

// Set the path for PTVSD to be picked up.
// tslint:disable-next-line:no-string-literal
customEnv['PYTHONPATH'] = ptvsdPath;
customEnv['PYTHONPATH'] = PTVSD_PATH;
const pythonArgs = ['-m', 'ptvsd', '--server', '--port', `${port}`, '--file', fileToDebug.fileToCommandArgument()];
procToKill = spawn('python', pythonArgs, { env: customEnv, cwd: path.dirname(fileToDebug) });

Expand Down
5 changes: 2 additions & 3 deletions src/test/debugger/capabilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { expect } from 'chai';
import { ChildProcess, spawn } from 'child_process';
import * as getFreePort from 'get-port';
import { connect, Socket } from 'net';
import * as path from 'path';
import { PassThrough } from 'stream';
import { Message } from 'vscode-debugadapter/lib/messages';
import { DebugProtocol } from 'vscode-debugprotocol';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import { createDeferred } from '../../client/common/helpers';
import { PTVSD_PATH } from '../../client/debugger/Common/constants';
import { ProtocolParser } from '../../client/debugger/Common/protocolParser';
import { ProtocolMessageWriter } from '../../client/debugger/Common/protocolWriter';
import { PythonDebugger } from '../../client/debugger/mainV2';
Expand Down Expand Up @@ -75,7 +74,7 @@ suite('Debugging - Capabilities', () => {
const host = 'localhost';
const port = await getFreePort({ host });
const env = { ...process.env };
env.PYTHONPATH = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'ptvsd');
env.PYTHONPATH = PTVSD_PATH;
proc = spawn('python', ['-m', 'ptvsd', '--server', '--port', `${port}`, '--file', 'someFile.py'], { cwd: __dirname, env });
// Wait for the socket server to start.
// Keep trying till we timeout.
Expand Down
9 changes: 3 additions & 6 deletions src/test/debugger/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { noop } from '../../client/common/core.utils';
import { IS_WINDOWS } from '../../client/common/platform/constants';
import { FileSystem } from '../../client/common/platform/fileSystem';
import { PlatformService } from '../../client/common/platform/platformService';
import { PTVSD_PATH } from '../../client/debugger/Common/constants';
import { DebugOptions, LaunchRequestArguments } from '../../client/debugger/Common/Contracts';
import { sleep } from '../common';
import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
Expand Down Expand Up @@ -69,8 +70,9 @@ let testCounter = 0;
const env = {};
if (debuggerType === 'pythonExperimental') {
// tslint:disable-next-line:no-string-literal
env['PYTHONPATH'] = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'experimental', 'ptvsd');
env['PYTHONPATH'] = PTVSD_PATH;
}
// tslint:disable-next-line:no-unnecessary-local-variable
const options: LaunchRequestArguments = {
program: path.join(debugFilesPath, pythonFile),
cwd: debugFilesPath,
Expand All @@ -84,11 +86,6 @@ let testCounter = 0;
type: debuggerType
};

// Custom experimental debugger options (filled in by DebugConfigurationProvider).
if (debuggerType === 'pythonExperimental') {
(options as any).redirectOutput = true;
}

return options;
}

Expand Down
70 changes: 70 additions & 0 deletions src/test/debugger/module.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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

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 { PTVSD_PATH } from '../../client/debugger/Common/constants';
import { DebugOptions, LaunchRequestArguments } from '../../client/debugger/Common/Contracts';
import { sleep } from '../common';
import { IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
import { createDebugAdapter } from './utils';

const workspaceDirectory = path.join(EXTENSION_ROOT_DIR, 'src', 'testMultiRootWkspc', 'workspace5');
const debuggerType = 'pythonExperimental';
suite(`Module Debugging - Misc tests: ${debuggerType}`, () => {
let debugClient: DebugClient;
setup(async function () {
if (!IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) {
this.skip();
}
const coverageDirectory = path.join(EXTENSION_ROOT_DIR, 'debug_coverage_module');
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 buildLauncArgs(): LaunchRequestArguments {
const env = {};
// tslint:disable-next-line:no-string-literal
env['PYTHONPATH'] = `.${path.delimiter}${PTVSD_PATH}`;

// tslint:disable-next-line:no-unnecessary-local-variable
const options: LaunchRequestArguments = {
module: 'mymod',
program: '',
cwd: workspaceDirectory,
debugOptions: [DebugOptions.RedirectOutput],
pythonPath: 'python',
args: [],
env,
envFile: '',
logToFile: false,
type: debuggerType
};

return options;
}

test('Test stdout output', async () => {
await Promise.all([
debugClient.configurationSequence(),
debugClient.launch(buildLauncArgs()),
debugClient.waitForEvent('initialized'),
debugClient.assertOutput('stdout', 'Hello world!'),
debugClient.waitForEvent('exited'),
debugClient.waitForEvent('terminated')
]);
});
});
1 change: 1 addition & 0 deletions src/test/pythonFiles/debugging/stdErrOutput.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import time
sys.stderr.write('error output')
sys.stderr.flush()
1 change: 1 addition & 0 deletions src/test/pythonFiles/debugging/stdOutOutput.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import time
sys.stdout.write('normal output')
sys.stdout.flush()
Empty file.
1 change: 1 addition & 0 deletions src/testMultiRootWkspc/workspace5/mymod/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello world!")

0 comments on commit 2fd7b69

Please sign in to comment.