forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |