Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cwd for debugging #21668

Merged
merged 7 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/client/testing/common/debugLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -223,8 +224,19 @@ 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) {
const additionalPythonPath = [pluginPath];
if (launchArgs.cwd) {
additionalPythonPath.push(launchArgs.cwd);
} else if (options.cwd) {
additionalPythonPath.push(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
Expand Down
37 changes: 37 additions & 0 deletions src/client/testing/common/helpers.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions src/test/testing/common/debugLauncher.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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',
Expand Down
48 changes: 48 additions & 0 deletions src/test/testing/common/helpers.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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);
});
});
Loading