From b4cf74d97f1990d3c9233e9ada3a0b3dcd9e2a44 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 19 Jul 2023 15:16:52 -0700 Subject: [PATCH 1/7] add cwd for debugging --- src/client/testing/common/debugLauncher.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/client/testing/common/debugLauncher.ts b/src/client/testing/common/debugLauncher.ts index dcf23c0478db..a54470abefd5 100644 --- a/src/client/testing/common/debugLauncher.ts +++ b/src/client/testing/common/debugLauncher.ts @@ -226,6 +226,11 @@ export class DebugLauncher implements ITestDebugLauncher { const pluginPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); launchArgs.env.PYTHONPATH = pluginPath; } + if (launchArgs.env && launchArgs.env.PYTHONPATH) { + launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${options.cwd}`; + } else if (launchArgs.env) { + launchArgs.env.PYTHONPATH = options.cwd; + } // Clear out purpose so we can detect if the configuration was used to // run via F5 style debugging. From 55e74e5e58cb263dec652732246e892877154914 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Thu, 20 Jul 2023 08:38:04 -0700 Subject: [PATCH 2/7] add in check for plugin path and always append --- src/client/testing/common/debugLauncher.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/client/testing/common/debugLauncher.ts b/src/client/testing/common/debugLauncher.ts index a54470abefd5..b12aad88f21c 100644 --- a/src/client/testing/common/debugLauncher.ts +++ b/src/client/testing/common/debugLauncher.ts @@ -223,13 +223,20 @@ export class DebugLauncher implements ITestDebugLauncher { `Missing value for debug setup, both port and uuid need to be defined. port: "${options.pytestPort}" uuid: "${options.pytestUUID}"`, ); } - const pluginPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); - launchArgs.env.PYTHONPATH = pluginPath; } + const pluginPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); + // check if PYTHONPATH is already set in the environment variables if (launchArgs.env && launchArgs.env.PYTHONPATH) { - launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${options.cwd}`; + // add the plugin path or cwd to PYTHONPATH if it is not already there + if (!launchArgs.env.PYTHONPATH.includes(pluginPath)) { + launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${pluginPath}`; + } + if (!launchArgs.env.PYTHONPATH.includes(options.cwd)) { + launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${options.cwd}`; + } } else if (launchArgs.env) { - launchArgs.env.PYTHONPATH = options.cwd; + // if PYTHONPATH is not set in the environment variables, set it to the plugin path and cwd + launchArgs.env.PYTHONPATH = `${pluginPath}${path.delimiter}${options.cwd}`; } // Clear out purpose so we can detect if the configuration was used to From efbfbbaf5935c30b01a28321261f5df24523ded3 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Fri, 21 Jul 2023 11:30:36 -0700 Subject: [PATCH 3/7] fix launch debugger tests --- src/client/testing/common/debugLauncher.ts | 6 +++++- src/test/testing/common/debugLauncher.unit.test.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/client/testing/common/debugLauncher.ts b/src/client/testing/common/debugLauncher.ts index b12aad88f21c..aa1107c592a1 100644 --- a/src/client/testing/common/debugLauncher.ts +++ b/src/client/testing/common/debugLauncher.ts @@ -231,7 +231,11 @@ export class DebugLauncher implements ITestDebugLauncher { if (!launchArgs.env.PYTHONPATH.includes(pluginPath)) { launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${pluginPath}`; } - if (!launchArgs.env.PYTHONPATH.includes(options.cwd)) { + if (launchArgs.cwd) { + if (!launchArgs.env.PYTHONPATH.includes(launchArgs.cwd)) { + launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${launchArgs.cwd}`; + } + } else if (!launchArgs.env.PYTHONPATH.includes(options.cwd)) { launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${options.cwd}`; } } else if (launchArgs.env) { diff --git a/src/test/testing/common/debugLauncher.unit.test.ts b/src/test/testing/common/debugLauncher.unit.test.ts index 4712c9b6136a..c6a46e7fff46 100644 --- a/src/test/testing/common/debugLauncher.unit.test.ts +++ b/src/test/testing/common/debugLauncher.unit.test.ts @@ -215,6 +215,9 @@ suite('Unit Tests - Debug Launcher', () => { if (!expected.cwd) { expected.cwd = workspaceFolders[0].uri.fsPath; } + const pluginPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); + const pythonPath = `${pluginPath}${path.delimiter}${expected.cwd}`; + expected.env.PYTHONPATH = pythonPath; // added by LaunchConfigurationResolver: if (!expected.python) { @@ -342,6 +345,10 @@ suite('Unit Tests - Debug Launcher', () => { }; const expected = getDefaultDebugConfig(); expected.cwd = 'path/to/settings/cwd'; + const pluginPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); + const pythonPath = `${pluginPath}${path.delimiter}${expected.cwd}`; + expected.env.PYTHONPATH = pythonPath + setupSuccess(options, 'unittest', expected); await debugLauncher.launchDebugger(options); @@ -366,6 +373,7 @@ suite('Unit Tests - Debug Launcher', () => { console: 'integratedTerminal', cwd: 'some/dir', env: { + PYTHONPATH: 'one/two/three' SPAM: 'EGGS', }, envFile: 'some/dir/.env', From eacca74a69cdece1aaededd5fffe13bcfba6d613 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Fri, 21 Jul 2023 11:37:24 -0700 Subject: [PATCH 4/7] missed semicolon --- src/test/testing/common/debugLauncher.unit.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/testing/common/debugLauncher.unit.test.ts b/src/test/testing/common/debugLauncher.unit.test.ts index c6a46e7fff46..bbb65f0b2e2a 100644 --- a/src/test/testing/common/debugLauncher.unit.test.ts +++ b/src/test/testing/common/debugLauncher.unit.test.ts @@ -347,7 +347,7 @@ suite('Unit Tests - Debug Launcher', () => { expected.cwd = 'path/to/settings/cwd'; const pluginPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); const pythonPath = `${pluginPath}${path.delimiter}${expected.cwd}`; - expected.env.PYTHONPATH = pythonPath + expected.env.PYTHONPATH = pythonPath; setupSuccess(options, 'unittest', expected); await debugLauncher.launchDebugger(options); @@ -373,7 +373,7 @@ suite('Unit Tests - Debug Launcher', () => { console: 'integratedTerminal', cwd: 'some/dir', env: { - PYTHONPATH: 'one/two/three' + PYTHONPATH: 'one/two/three', SPAM: 'EGGS', }, envFile: 'some/dir/.env', From c8dff04546416ad907235ca4ed42ac28879af7af Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 31 Jul 2023 12:04:19 -0700 Subject: [PATCH 5/7] initial --- .vscode/launch.json | 2 +- .vscode/settings.json | 2 +- src/client/testing/common/debugLauncher.ts | 22 +++++------- src/client/testing/common/helpers.ts | 37 +++++++++++++++++++ src/test/testing/common/helpers.unit.test.ts | 38 ++++++++++++++++++++ 5 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 src/client/testing/common/helpers.ts create mode 100644 src/test/testing/common/helpers.unit.test.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index 82981a93305d..af1917dfaaf4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -160,7 +160,7 @@ "--ui=tdd", "--recursive", "--colors", - //"--grep", "", + "--grep", "Unit Tests - Test Helpers", "--timeout=300000" ], "outFiles": ["${workspaceFolder}/out/**/*.js", "!${workspaceFolder}/**/node_modules**/*"], diff --git a/.vscode/settings.json b/.vscode/settings.json index 174a850c901e..ec30e8dbf044 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -29,7 +29,7 @@ } }, "[typescript]": { - "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.defaultFormatter": "vscode.typescript-language-features", "editor.formatOnSave": true }, "[javascript]": { diff --git a/src/client/testing/common/debugLauncher.ts b/src/client/testing/common/debugLauncher.ts index aa1107c592a1..63e2a4543beb 100644 --- a/src/client/testing/common/debugLauncher.ts +++ b/src/client/testing/common/debugLauncher.ts @@ -17,6 +17,7 @@ import { getWorkspaceFolder, getWorkspaceFolders } from '../../common/vscodeApis import { showErrorMessage } from '../../common/vscodeApis/windowApis'; import { createDeferred } from '../../common/utils/async'; import { pythonTestAdapterRewriteEnabled } from '../testController/common/utils'; +import { addPathToPythonpath } from './helpers'; @injectable() export class DebugLauncher implements ITestDebugLauncher { @@ -226,21 +227,16 @@ export class DebugLauncher implements ITestDebugLauncher { } const pluginPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); // check if PYTHONPATH is already set in the environment variables - if (launchArgs.env && launchArgs.env.PYTHONPATH) { - // add the plugin path or cwd to PYTHONPATH if it is not already there - if (!launchArgs.env.PYTHONPATH.includes(pluginPath)) { - launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${pluginPath}`; - } + if (launchArgs.env) { + const additionalPythonPath = [pluginPath]; if (launchArgs.cwd) { - if (!launchArgs.env.PYTHONPATH.includes(launchArgs.cwd)) { - launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${launchArgs.cwd}`; - } - } else if (!launchArgs.env.PYTHONPATH.includes(options.cwd)) { - launchArgs.env.PYTHONPATH = `${launchArgs.env.PYTHONPATH}${path.delimiter}${options.cwd}`; + additionalPythonPath.push(launchArgs.cwd); + } else if (options.cwd) { + additionalPythonPath.push(options.cwd); } - } else if (launchArgs.env) { - // if PYTHONPATH is not set in the environment variables, set it to the plugin path and cwd - launchArgs.env.PYTHONPATH = `${pluginPath}${path.delimiter}${options.cwd}`; + // add the plugin path or cwd to PYTHONPATH if it is not already there using the following function + // this function will handle if PYTHONPATH is undefined + addPathToPythonpath(additionalPythonPath, launchArgs.env.PYTHONPATH); } // Clear out purpose so we can detect if the configuration was used to diff --git a/src/client/testing/common/helpers.ts b/src/client/testing/common/helpers.ts new file mode 100644 index 000000000000..021849277b33 --- /dev/null +++ b/src/client/testing/common/helpers.ts @@ -0,0 +1,37 @@ +import * as path from 'path'; + +/** + * This function normalizes the provided paths and the existing paths in PYTHONPATH, + * adds the provided paths to PYTHONPATH if they're not already present, + * and then returns the updated PYTHONPATH. + * + * @param newPaths - An array of paths to be added to PYTHONPATH + * @param launchPythonPath - The initial PYTHONPATH + * @returns The updated PYTHONPATH + */ +export function addPathToPythonpath(newPaths: string[], launchPythonPath: string | undefined): string { + // Split PYTHONPATH into array of paths if it exists + let paths: string[]; + if (!launchPythonPath) { + paths = []; + } else { + paths = launchPythonPath.split(path.delimiter); + } + + // Normalize each path in the existing PYTHONPATH + paths = paths.map((p) => path.normalize(p)); + + // Normalize each new path and add it to PYTHONPATH if it's not already present + newPaths.forEach((newPath) => { + const normalizedNewPath: string = path.normalize(newPath); + + if (!paths.includes(normalizedNewPath)) { + paths.push(normalizedNewPath); + } + }); + + // Join the paths with ':' to create the updated PYTHONPATH + const updatedPythonPath: string = paths.join(path.delimiter); + + return updatedPythonPath; +} diff --git a/src/test/testing/common/helpers.unit.test.ts b/src/test/testing/common/helpers.unit.test.ts new file mode 100644 index 000000000000..18332b715789 --- /dev/null +++ b/src/test/testing/common/helpers.unit.test.ts @@ -0,0 +1,38 @@ +import * as path from 'path'; +import * as assert from 'assert'; +import { addPathToPythonpath } from '../../../client/testing/common/helpers'; + + +suite('Unit Tests - Test Helpers', () => { + const newPaths = [path.join("path", "to", "new")] + test('addPathToPythonpath handles undefined path', async () => { + const launchPythonPath = undefined; + const actualPath = addPathToPythonpath(newPaths, launchPythonPath); + assert.equal(actualPath, path.join("path", "to", "new")); + }); + test('addPathToPythonpath adds path if it does not exist in the python path', async () => { + const launchPythonPath = path.join("random", "existing", "pythonpath"); + const actualPath = addPathToPythonpath(newPaths, launchPythonPath); + const expectedPath = path.join("random", "existing", "pythonpath") + path.delimiter + path.join("path", "to", "new"); + assert.equal(actualPath, expectedPath); + }); + test('addPathToPythonpath does not add to python path if the given python path already contains the path', async () => { + const launchPythonPath = path.join("path", "to", "new"); + const actualPath = addPathToPythonpath(newPaths, launchPythonPath); + const expectedPath = path.join("path", "to", "new"); + assert.equal(actualPath, expectedPath); + }); + test('addPathToPythonpath correctly normalizes both existing and new paths', async () => { + const newerPaths = [path.join("path", "to", "/", "new")]; + const launchPythonPath = path.join("path", "to", "..", "old"); + const actualPath = addPathToPythonpath(newerPaths, launchPythonPath); + const expectedPath = path.join("path", "old") + path.delimiter + path.join("path", "to", "new"); + assert.equal(actualPath, expectedPath); + }); + test('addPathToPythonpath splits pythonpath then rejoins it', async () => { + const launchPythonPath = path.join("path", "to", "new") + path.delimiter + path.join("path", "to", "old") + path.delimiter + path.join("path", "to", "random"); + const actualPath = addPathToPythonpath(newPaths, launchPythonPath); + const expectedPath = path.join("path", "to", "new") + path.delimiter + path.join("path", "to", "old") + path.delimiter + path.join("path", "to", "random"); + assert.equal(actualPath, expectedPath); + }); +}); From ecd95b4e3f3afed618b1aea0a0ae631a3c6cde35 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 31 Jul 2023 12:06:17 -0700 Subject: [PATCH 6/7] prettier --- src/test/testing/common/helpers.unit.test.ts | 34 +++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/test/testing/common/helpers.unit.test.ts b/src/test/testing/common/helpers.unit.test.ts index 18332b715789..441b257d4d0e 100644 --- a/src/test/testing/common/helpers.unit.test.ts +++ b/src/test/testing/common/helpers.unit.test.ts @@ -2,37 +2,47 @@ import * as path from 'path'; import * as assert from 'assert'; import { addPathToPythonpath } from '../../../client/testing/common/helpers'; - suite('Unit Tests - Test Helpers', () => { - const newPaths = [path.join("path", "to", "new")] + const newPaths = [path.join('path', 'to', 'new')]; test('addPathToPythonpath handles undefined path', async () => { const launchPythonPath = undefined; const actualPath = addPathToPythonpath(newPaths, launchPythonPath); - assert.equal(actualPath, path.join("path", "to", "new")); + assert.equal(actualPath, path.join('path', 'to', 'new')); }); test('addPathToPythonpath adds path if it does not exist in the python path', async () => { - const launchPythonPath = path.join("random", "existing", "pythonpath"); + const launchPythonPath = path.join('random', 'existing', 'pythonpath'); const actualPath = addPathToPythonpath(newPaths, launchPythonPath); - const expectedPath = path.join("random", "existing", "pythonpath") + path.delimiter + path.join("path", "to", "new"); + const expectedPath = + path.join('random', 'existing', 'pythonpath') + path.delimiter + path.join('path', 'to', 'new'); assert.equal(actualPath, expectedPath); }); test('addPathToPythonpath does not add to python path if the given python path already contains the path', async () => { - const launchPythonPath = path.join("path", "to", "new"); + const launchPythonPath = path.join('path', 'to', 'new'); const actualPath = addPathToPythonpath(newPaths, launchPythonPath); - const expectedPath = path.join("path", "to", "new"); + const expectedPath = path.join('path', 'to', 'new'); assert.equal(actualPath, expectedPath); }); test('addPathToPythonpath correctly normalizes both existing and new paths', async () => { - const newerPaths = [path.join("path", "to", "/", "new")]; - const launchPythonPath = path.join("path", "to", "..", "old"); + const newerPaths = [path.join('path', 'to', '/', 'new')]; + const launchPythonPath = path.join('path', 'to', '..', 'old'); const actualPath = addPathToPythonpath(newerPaths, launchPythonPath); - const expectedPath = path.join("path", "old") + path.delimiter + path.join("path", "to", "new"); + const expectedPath = path.join('path', 'old') + path.delimiter + path.join('path', 'to', 'new'); assert.equal(actualPath, expectedPath); }); test('addPathToPythonpath splits pythonpath then rejoins it', async () => { - const launchPythonPath = path.join("path", "to", "new") + path.delimiter + path.join("path", "to", "old") + path.delimiter + path.join("path", "to", "random"); + const launchPythonPath = + path.join('path', 'to', 'new') + + path.delimiter + + path.join('path', 'to', 'old') + + path.delimiter + + path.join('path', 'to', 'random'); const actualPath = addPathToPythonpath(newPaths, launchPythonPath); - const expectedPath = path.join("path", "to", "new") + path.delimiter + path.join("path", "to", "old") + path.delimiter + path.join("path", "to", "random"); + const expectedPath = + path.join('path', 'to', 'new') + + path.delimiter + + path.join('path', 'to', 'old') + + path.delimiter + + path.join('path', 'to', 'random'); assert.equal(actualPath, expectedPath); }); }); From 584ec47b0c52fd1c695d507dd33aa289cf357ebe Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Mon, 31 Jul 2023 13:23:29 -0700 Subject: [PATCH 7/7] fix unrealized merge items --- .vscode/launch.json | 2 +- .vscode/settings.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index af1917dfaaf4..82981a93305d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -160,7 +160,7 @@ "--ui=tdd", "--recursive", "--colors", - "--grep", "Unit Tests - Test Helpers", + //"--grep", "", "--timeout=300000" ], "outFiles": ["${workspaceFolder}/out/**/*.js", "!${workspaceFolder}/**/node_modules**/*"], diff --git a/.vscode/settings.json b/.vscode/settings.json index ec30e8dbf044..174a850c901e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -29,7 +29,7 @@ } }, "[typescript]": { - "editor.defaultFormatter": "vscode.typescript-language-features", + "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true }, "[javascript]": {