From d00c04e911fe8de4d9022d037710ad394dce54de Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Tue, 10 Oct 2017 21:12:28 -0700 Subject: [PATCH 1/3] fix #1298 remove vscode.startDebug command --- src/client/unittests/nosetest/runner.ts | 9 ++++++++- src/client/unittests/pytest/runner.ts | 11 +++++++++-- src/client/unittests/unittest/runner.ts | 11 +++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/client/unittests/nosetest/runner.ts b/src/client/unittests/nosetest/runner.ts index 14c0d3251300..7dbfde3f6f16 100644 --- a/src/client/unittests/nosetest/runner.ts +++ b/src/client/unittests/nosetest/runner.ts @@ -100,7 +100,14 @@ export function runTest(rootDirectory: string, tests: Tests, args: string[], tes }); launchDef.promise.then(() => { - return vscode.commands.executeCommand('vscode.startDebug', { + if (!Array.isArray(vscode.workspace.workspaceFolders) || vscode.workspace.workspaceFolders.length === 0) { + throw new Error('Please open a workspace'); + } + let workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(rootDirectory)); + if (!workspaceFolder) { + workspaceFolder = vscode.workspace.workspaceFolders[0]; + } + return vscode.debug.startDebugging(workspaceFolder, { "name": "Debug Unit Test", "type": "python", "request": "attach", diff --git a/src/client/unittests/pytest/runner.ts b/src/client/unittests/pytest/runner.ts index 40c5137653d0..7209ca896877 100644 --- a/src/client/unittests/pytest/runner.ts +++ b/src/client/unittests/pytest/runner.ts @@ -78,7 +78,14 @@ export function runTest(rootDirectory: string, tests: Tests, args: string[], tes }); launchDef.promise.then(() => { - return vscode.commands.executeCommand('vscode.startDebug', { + if (!Array.isArray(vscode.workspace.workspaceFolders) || vscode.workspace.workspaceFolders.length === 0) { + throw new Error('Please open a workspace'); + } + let workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(rootDirectory)); + if (!workspaceFolder) { + workspaceFolder = vscode.workspace.workspaceFolders[0]; + } + return vscode.debug.startDebugging(workspaceFolder, { "name": "Debug Unit Test", "type": "python", "request": "attach", @@ -115,4 +122,4 @@ export function updateResultsFromLogFiles(tests: Tests, outputXmlFile: string): updateResults(tests); return tests; }); -} \ No newline at end of file +} diff --git a/src/client/unittests/unittest/runner.ts b/src/client/unittests/unittest/runner.ts index b451af48db23..b16a5af59ce9 100644 --- a/src/client/unittests/unittest/runner.ts +++ b/src/client/unittests/unittest/runner.ts @@ -129,7 +129,14 @@ export function runTest(testManager: BaseTestManager, rootDirectory: string, tes launchDef.promise .then(() => { - return vscode.commands.executeCommand('vscode.startDebug', { + if (!Array.isArray(vscode.workspace.workspaceFolders) || vscode.workspace.workspaceFolders.length === 0) { + throw new Error('Please open a workspace'); + } + let workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(rootDirectory)); + if (!workspaceFolder) { + workspaceFolder = vscode.workspace.workspaceFolders[0]; + } + return vscode.debug.startDebugging(workspaceFolder, { "name": "Debug Unit Test", "type": "python", "request": "attach", @@ -253,4 +260,4 @@ function getIdsOfTestsToRun(tests: Tests, testsToRun: TestsToRun): string[] { testIds.push(...testsToRun.testFunction.map(f => f.nameToRun)); } return testIds; -} \ No newline at end of file +} From 828661db2890e8468dea6fa53e5866582deec1b1 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Wed, 11 Oct 2017 14:13:27 -0700 Subject: [PATCH 2/3] fix code review comments --- src/client/unittests/common/debugLauncher.ts | 63 +++++++++++++++++++ src/client/unittests/nosetest/runner.ts | 64 +------------------- src/client/unittests/pytest/runner.ts | 64 +------------------- src/client/unittests/unittest/runner.ts | 64 +------------------- 4 files changed, 71 insertions(+), 184 deletions(-) create mode 100644 src/client/unittests/common/debugLauncher.ts diff --git a/src/client/unittests/common/debugLauncher.ts b/src/client/unittests/common/debugLauncher.ts new file mode 100644 index 000000000000..4314eb47a76c --- /dev/null +++ b/src/client/unittests/common/debugLauncher.ts @@ -0,0 +1,63 @@ +import * as os from 'os'; +import { CancellationToken, debug, OutputChannel, workspace, Uri } from 'vscode'; +import { PythonSettings } from '../../common/configSettings'; +import { execPythonFile } from './../../common/utils'; +import { createDeferred } from './../../common/helpers'; + +const pythonSettings = PythonSettings.getInstance(); +export function launchDebugger(rootDirectory: string, testArgs: string[], token?: CancellationToken, outChannel?: OutputChannel) { + const def = createDeferred(); + const launchDef = createDeferred(); + let outputChannelShown = false; + execPythonFile(pythonSettings.pythonPath, testArgs, rootDirectory, true, (data: string) => { + if (data.startsWith('READY' + os.EOL)) { + // debug socket server has started + launchDef.resolve(); + data = data.substring(('READY' + os.EOL).length); + } + + if (!outputChannelShown) { + outputChannelShown = true; + outChannel.show(); + } + outChannel.append(data); + }, token).catch(reason => { + if (!def.rejected && !def.resolved) { + def.reject(reason); + } + }).then(() => { + if (!def.rejected && !def.resolved) { + def.resolve(); + } + }).catch(reason => { + if (!def.rejected && !def.resolved) { + def.reject(reason); + } + }); + + launchDef.promise.then(() => { + if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length === 0) { + throw new Error('Please open a workspace'); + } + let workspaceFolder = workspace.getWorkspaceFolder(Uri.file(rootDirectory)); + if (!workspaceFolder) { + workspaceFolder = workspace.workspaceFolders[0]; + } + return debug.startDebugging(workspaceFolder, { + "name": "Debug Unit Test", + "type": "python", + "request": "attach", + "localRoot": rootDirectory, + "remoteRoot": rootDirectory, + "port": pythonSettings.unitTest.debugPort, + "secret": "my_secret", + "host": "localhost" + }); + }).catch(reason => { + if (!def.rejected && !def.resolved) { + def.reject(reason); + } + }); + + return def.promise; +} \ No newline at end of file diff --git a/src/client/unittests/nosetest/runner.ts b/src/client/unittests/nosetest/runner.ts index 7dbfde3f6f16..fdaba3ff8254 100644 --- a/src/client/unittests/nosetest/runner.ts +++ b/src/client/unittests/nosetest/runner.ts @@ -6,11 +6,8 @@ import { updateResults } from '../common/testUtils'; import { updateResultsFromXmlLogFile, PassCalculationFormulae } from '../common/xUnitParser'; import { run } from '../common/runner'; import { PythonSettings } from '../../common/configSettings'; -import * as vscode from 'vscode'; -import { execPythonFile } from './../../common/utils'; -import { createDeferred } from './../../common/helpers'; -import * as os from 'os'; import * as path from 'path'; +import { launchDebugger } from '../common/debugLauncher'; const pythonSettings = PythonSettings.getInstance(); const WITH_XUNIT = '--with-xunit'; @@ -65,65 +62,10 @@ export function runTest(rootDirectory: string, tests: Tests, args: string[], tes return promiseToGetXmlLogFile.then(() => { if (debug === true) { - const def = createDeferred(); - const launchDef = createDeferred(); const testLauncherFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'PythonTools', 'testlauncher.py'); - - // start the debug adapter only once we have started the debug process - // pytestlauncherargs const nosetestlauncherargs = [rootDirectory, 'my_secret', pythonSettings.unitTest.debugPort.toString(), 'nose']; - let outputChannelShown = false; - execPythonFile(pythonSettings.pythonPath, [testLauncherFile].concat(nosetestlauncherargs).concat(noseTestArgs.concat(testPaths)), rootDirectory, true, (data: string) => { - if (data.startsWith('READY' + os.EOL)) { - // debug socket server has started - launchDef.resolve(); - data = data.substring(('READY' + os.EOL).length); - } - - if (!outputChannelShown) { - outputChannelShown = true; - outChannel.show(); - } - outChannel.append(data); - }, token).catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }).then(() => { - if (!def.rejected && !def.resolved) { - def.resolve(); - } - }).catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }); - - launchDef.promise.then(() => { - if (!Array.isArray(vscode.workspace.workspaceFolders) || vscode.workspace.workspaceFolders.length === 0) { - throw new Error('Please open a workspace'); - } - let workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(rootDirectory)); - if (!workspaceFolder) { - workspaceFolder = vscode.workspace.workspaceFolders[0]; - } - return vscode.debug.startDebugging(workspaceFolder, { - "name": "Debug Unit Test", - "type": "python", - "request": "attach", - "localRoot": rootDirectory, - "remoteRoot": rootDirectory, - "port": pythonSettings.unitTest.debugPort, - "secret": "my_secret", - "host": "localhost" - }); - }).catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }); - - return def.promise; + const args = [testLauncherFile].concat(nosetestlauncherargs).concat(noseTestArgs.concat(testPaths)); + return launchDebugger(rootDirectory, args, token, outChannel); } else { return run(pythonSettings.unitTest.nosetestPath, noseTestArgs.concat(testPaths), rootDirectory, token, outChannel); diff --git a/src/client/unittests/pytest/runner.ts b/src/client/unittests/pytest/runner.ts index 7209ca896877..eb564b57c015 100644 --- a/src/client/unittests/pytest/runner.ts +++ b/src/client/unittests/pytest/runner.ts @@ -8,11 +8,8 @@ import { CancellationToken, OutputChannel } from 'vscode'; import { updateResultsFromXmlLogFile, PassCalculationFormulae } from '../common/xUnitParser'; import { run } from '../common/runner'; import { PythonSettings } from '../../common/configSettings'; -import * as vscode from 'vscode'; -import { execPythonFile } from './../../common/utils'; -import { createDeferred } from './../../common/helpers'; -import * as os from 'os'; import * as path from 'path'; +import { launchDebugger } from '../common/debugLauncher'; const pythonSettings = PythonSettings.getInstance(); @@ -43,65 +40,10 @@ export function runTest(rootDirectory: string, tests: Tests, args: string[], tes } const testArgs = testPaths.concat(args, [`--junitxml=${xmlLogFile}`]); if (debug) { - const def = createDeferred(); - const launchDef = createDeferred(); const testLauncherFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'PythonTools', 'testlauncher.py'); - - // start the debug adapter only once we have started the debug process - // pytestlauncherargs const pytestlauncherargs = [rootDirectory, 'my_secret', pythonSettings.unitTest.debugPort.toString(), 'pytest']; - let outputChannelShown = false; - execPythonFile(pythonSettings.pythonPath, [testLauncherFile].concat(pytestlauncherargs).concat(testArgs), rootDirectory, true, (data: string) => { - if (data.startsWith('READY' + os.EOL)) { - // debug socket server has started - launchDef.resolve(); - data = data.substring(('READY' + os.EOL).length); - } - - if (!outputChannelShown) { - outputChannelShown = true; - outChannel.show(); - } - outChannel.append(data); - }, token).catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }).then(() => { - if (!def.rejected && !def.resolved) { - def.resolve(); - } - }).catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }); - - launchDef.promise.then(() => { - if (!Array.isArray(vscode.workspace.workspaceFolders) || vscode.workspace.workspaceFolders.length === 0) { - throw new Error('Please open a workspace'); - } - let workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(rootDirectory)); - if (!workspaceFolder) { - workspaceFolder = vscode.workspace.workspaceFolders[0]; - } - return vscode.debug.startDebugging(workspaceFolder, { - "name": "Debug Unit Test", - "type": "python", - "request": "attach", - "localRoot": rootDirectory, - "remoteRoot": rootDirectory, - "port": pythonSettings.unitTest.debugPort, - "secret": "my_secret", - "host": "localhost" - }); - }).catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }); - - return def.promise; + const args = [testLauncherFile].concat(pytestlauncherargs).concat(testArgs); + return launchDebugger(rootDirectory, args, token, outChannel); } else { return run(pythonSettings.unitTest.pyTestPath, testArgs, rootDirectory, token, outChannel); diff --git a/src/client/unittests/unittest/runner.ts b/src/client/unittests/unittest/runner.ts index b16a5af59ce9..1435dc8b3062 100644 --- a/src/client/unittests/unittest/runner.ts +++ b/src/client/unittests/unittest/runner.ts @@ -9,10 +9,7 @@ import { CancellationToken, OutputChannel } from 'vscode'; import { run } from '../common/runner'; import { Server } from './socketServer'; import { PythonSettings } from '../../common/configSettings'; -import * as vscode from 'vscode'; -import { execPythonFile } from './../../common/utils'; -import { createDeferred } from './../../common/helpers'; -import * as os from 'os'; +import { launchDebugger } from '../common/debugLauncher'; const settings = PythonSettings.getInstance(); interface TestStatusMap { @@ -96,64 +93,7 @@ export function runTest(testManager: BaseTestManager, rootDirectory: string, tes testArgs.push(`--testFile=${testFile}`); } if (debug === true) { - const def = createDeferred(); - const launchDef = createDeferred(); - let outputChannelShown = false; - - // start the debug adapter only once we have started the debug process - execPythonFile(settings.pythonPath, [testLauncherFile].concat(testArgs), rootDirectory, true, (data: string) => { - if (data.startsWith('READY' + os.EOL)) { - // debug socket server has started - launchDef.resolve(); - data = data.substring(('READY' + os.EOL).length); - } - - if (!outputChannelShown) { - outputChannelShown = true; - outChannel.show(); - } - outChannel.append(data); - }, token).catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }).then(() => { - if (!def.rejected && !def.resolved) { - def.resolve(); - } - }).catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }); - - launchDef.promise - .then(() => { - if (!Array.isArray(vscode.workspace.workspaceFolders) || vscode.workspace.workspaceFolders.length === 0) { - throw new Error('Please open a workspace'); - } - let workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(rootDirectory)); - if (!workspaceFolder) { - workspaceFolder = vscode.workspace.workspaceFolders[0]; - } - return vscode.debug.startDebugging(workspaceFolder, { - "name": "Debug Unit Test", - "type": "python", - "request": "attach", - "localRoot": rootDirectory, - "remoteRoot": rootDirectory, - "port": settings.unitTest.debugPort, - "secret": "my_secret", - "host": "localhost" - }); - }) - .catch(reason => { - if (!def.rejected && !def.resolved) { - def.reject(reason); - } - }); - - return def.promise; + return launchDebugger(rootDirectory, [testLauncherFile].concat(testArgs), token, outChannel); } else { return run(settings.pythonPath, [testLauncherFile].concat(testArgs), rootDirectory, token, outChannel); From 5263c5e19fed6997cda25a4822a735f56fc1c93d Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Wed, 11 Oct 2017 16:41:33 -0700 Subject: [PATCH 3/3] added period as per code review comments --- src/client/unittests/common/debugLauncher.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/unittests/common/debugLauncher.ts b/src/client/unittests/common/debugLauncher.ts index 4314eb47a76c..099bfaa5b377 100644 --- a/src/client/unittests/common/debugLauncher.ts +++ b/src/client/unittests/common/debugLauncher.ts @@ -11,7 +11,7 @@ export function launchDebugger(rootDirectory: string, testArgs: string[], token? let outputChannelShown = false; execPythonFile(pythonSettings.pythonPath, testArgs, rootDirectory, true, (data: string) => { if (data.startsWith('READY' + os.EOL)) { - // debug socket server has started + // debug socket server has started. launchDef.resolve(); data = data.substring(('READY' + os.EOL).length); } @@ -60,4 +60,4 @@ export function launchDebugger(rootDirectory: string, testArgs: string[], token? }); return def.promise; -} \ No newline at end of file +}