forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Virtualenvwrapper locator #13895
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
Merged
kimadeline
merged 13 commits into
microsoft:main
from
kimadeline:virtualenvwrapper-locator
Sep 16, 2020
Merged
Virtualenvwrapper locator #13895
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cb58136
virtualenvwrapper locator
kimadeline 99b3f31
skip os-specific tests
kimadeline 6fe3932
Test getDefaultVirtualenvwrapperDir
kimadeline dfd70e0
Merge branch 'main' into virtualenvwrapper-locator
kimadeline fbb3b81
Update src/client/pythonEnvironments/discovery/locators/services/virt…
kimadeline 38b41d9
add pathExists check to WORKON_HOME dir
kimadeline 26df20c
Change how the locator works + move util func out
kimadeline bd642d7
Fix functional tests
kimadeline 5f10e08
utils unit tests
kimadeline 74b168d
Update locator tests
kimadeline 83d06d8
Merge branch 'main' into virtualenvwrapper-locator
kimadeline 59015c2
Merge branch 'main' into virtualenvwrapper-locator
kimadeline 590341e
Stub getUserHomeDir instead of getDefaultetc
kimadeline File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
src/client/pythonEnvironments/common/virtualenvwrapperUtils.ts
This file contains hidden or 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,14 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
import * as path from 'path'; | ||
import { getOSType, getUserHomeDir, OSType } from '../../common/utils/platform'; | ||
|
||
export function getDefaultVirtualenvwrapperDir(): string { | ||
const homeDir = getUserHomeDir() || ''; | ||
|
||
// In Windows, the default path for WORKON_HOME is %USERPROFILE%\Envs. | ||
if (getOSType() === OSType.Windows) { | ||
return path.join(homeDir, 'Envs'); | ||
} | ||
return path.join(homeDir, '.virtualenvs'); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/client/pythonEnvironments/discovery/locators/services/virtualenvwrapperLocator.ts
This file contains hidden or 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,29 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as path from 'path'; | ||
import { | ||
getEnvironmentVariable, getOSType, OSType, | ||
} from '../../../../common/utils/platform'; | ||
import { pathExists } from '../../../common/externalDependencies'; | ||
import { getDefaultVirtualenvwrapperDir } from '../../../common/virtualenvwrapperUtils'; | ||
|
||
/** | ||
* Checks if the given interpreter belongs to a virtualenvWrapper based environment. | ||
* @param {string} interpreterPath: Absolute path to the python interpreter. | ||
* @returns {boolean}: Returns true if the interpreter belongs to a virtualenvWrapper environment. | ||
*/ | ||
export async function isVirtualenvwrapperEnvironment(interpreterPath:string): Promise<boolean> { | ||
// The WORKON_HOME variable contains the path to the root directory of all virtualenvwrapper environments. | ||
// If the interpreter path belongs to one of them then it is a virtualenvwrapper type of environment. | ||
const workonHomeDir = getEnvironmentVariable('WORKON_HOME') || getDefaultVirtualenvwrapperDir(); | ||
const environmentName = path.basename(path.dirname(path.dirname(interpreterPath))); | ||
|
||
let environmentDir = path.join(workonHomeDir, environmentName); | ||
|
||
if (getOSType() === OSType.Windows) { | ||
environmentDir = environmentDir.toUpperCase(); | ||
} | ||
|
||
return await pathExists(environmentDir) && interpreterPath.startsWith(`${environmentDir}${path.sep}`); | ||
} |
This file contains hidden or 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
Empty file.
Empty file.
Empty file.
43 changes: 43 additions & 0 deletions
43
src/test/pythonEnvironments/common/virtualenvwrapperUtils.unit.test.ts
This file contains hidden or 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,43 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as assert from 'assert'; | ||
import * as path from 'path'; | ||
import * as sinon from 'sinon'; | ||
import * as platformUtils from '../../../client/common/utils/platform'; | ||
import { getDefaultVirtualenvwrapperDir } from '../../../client/pythonEnvironments/common/virtualenvwrapperUtils'; | ||
|
||
suite('Virtualenvwrapper Utils tests', () => { | ||
const homeDir = path.join('path', 'to', 'home'); | ||
|
||
let getOsTypeStub: sinon.SinonStub; | ||
let getHomeDirStub: sinon.SinonStub; | ||
|
||
setup(() => { | ||
getOsTypeStub = sinon.stub(platformUtils, 'getOSType'); | ||
getHomeDirStub = sinon.stub(platformUtils, 'getUserHomeDir'); | ||
|
||
getHomeDirStub.returns(homeDir); | ||
}); | ||
|
||
teardown(() => { | ||
getOsTypeStub.restore(); | ||
getHomeDirStub.restore(); | ||
}); | ||
|
||
test('Default virtualenvwrapper directory on non-Windows should be ~/.virtualenvs', () => { | ||
getOsTypeStub.returns(platformUtils.OSType.Linux); | ||
|
||
const directory = getDefaultVirtualenvwrapperDir(); | ||
|
||
assert.deepStrictEqual(directory, path.join(homeDir, '.virtualenvs')); | ||
}); | ||
|
||
test('Default virtualenvwrapper directory on Windows should be %USERPROFILE%\\Envs', () => { | ||
getOsTypeStub.returns(platformUtils.OSType.Windows); | ||
|
||
const directory = getDefaultVirtualenvwrapperDir(); | ||
|
||
assert.deepStrictEqual(directory, path.join(homeDir, 'Envs')); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
src/test/pythonEnvironments/discovery/locators/virtualenvwrapperLocator.unit.test.ts
This file contains hidden or 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,63 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as assert from 'assert'; | ||
import * as path from 'path'; | ||
import * as sinon from 'sinon'; | ||
import * as platformUtils from '../../../../client/common/utils/platform'; | ||
import * as fileUtils from '../../../../client/pythonEnvironments/common/externalDependencies'; | ||
import * as virtualenvwrapperUtils from '../../../../client/pythonEnvironments/common/virtualenvwrapperUtils'; | ||
import { isVirtualenvwrapperEnvironment } from '../../../../client/pythonEnvironments/discovery/locators/services/virtualenvwrapperLocator'; | ||
|
||
suite('Virtualenvwrapper Locator Tests', () => { | ||
const envDirectory = 'myenv'; | ||
const homeDir = path.join('path', 'to', 'home'); | ||
|
||
let getEnvVariableStub: sinon.SinonStub; | ||
let pathExistsStub:sinon.SinonStub; | ||
let getDefaultDirStub:sinon.SinonStub; | ||
|
||
setup(() => { | ||
getEnvVariableStub = sinon.stub(platformUtils, 'getEnvironmentVariable'); | ||
pathExistsStub = sinon.stub(fileUtils, 'pathExists'); | ||
getDefaultDirStub = sinon.stub(virtualenvwrapperUtils, 'getDefaultVirtualenvwrapperDir'); | ||
|
||
pathExistsStub.withArgs(path.join(homeDir, envDirectory)).resolves(true); | ||
pathExistsStub.resolves(false); | ||
}); | ||
|
||
teardown(() => { | ||
getEnvVariableStub.restore(); | ||
pathExistsStub.restore(); | ||
getDefaultDirStub.restore(); | ||
}); | ||
|
||
test('WORKON_HOME is not set, and the interpreter is is in a subfolder', async () => { | ||
const interpreter = path.join(homeDir, envDirectory, 'bin', 'python'); | ||
|
||
getEnvVariableStub.withArgs('WORKON_HOME').returns(undefined); | ||
getDefaultDirStub.returns(homeDir); | ||
|
||
assert.ok(await isVirtualenvwrapperEnvironment(interpreter)); | ||
}); | ||
|
||
test('WORKON_HOME is set to a custom value, and the interpreter is is in a subfolder', async () => { | ||
const workonHomeDirectory = path.join('path', 'to', 'workonHome'); | ||
const interpreter = path.join(workonHomeDirectory, envDirectory, 'bin', 'python'); | ||
|
||
getEnvVariableStub.withArgs('WORKON_HOME').returns(workonHomeDirectory); | ||
pathExistsStub.withArgs(path.join(workonHomeDirectory, envDirectory)).resolves(true); | ||
|
||
assert.ok(await isVirtualenvwrapperEnvironment(interpreter)); | ||
}); | ||
|
||
test('The interpreter is not in a subfolder of WORKON_HOME', async () => { | ||
const workonHomeDirectory = path.join('path', 'to', 'workonHome'); | ||
const interpreter = path.join('some', 'path', envDirectory, 'bin', 'python'); | ||
|
||
getEnvVariableStub.withArgs('WORKON_HOME').returns(workonHomeDirectory); | ||
pathExistsStub.withArgs(path.join(workonHomeDirectory, envDirectory)).resolves(false); | ||
|
||
assert.deepStrictEqual(await isVirtualenvwrapperEnvironment(interpreter), false); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.