Skip to content

Commit

Permalink
add cwd for debugging (#21668)
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorjboyd committed Aug 2, 2023
1 parent ef16727 commit 84bbff9
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
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);
});
});

0 comments on commit 84bbff9

Please sign in to comment.