forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Pyenv locator #13996
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
Merged
Pyenv locator #13996
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b2b6002
Pyenv locator
kimadeline 9bb444b
Skip tests per platform
kimadeline 24a9522
Wrong pyenv path order in ternary
kimadeline 557249b
Add description
kimadeline 5c8c4ab
Autoformat venv locator
kimadeline a672989
Revert "Autoformat venv locator"
kimadeline df4d528
Add links
kimadeline ed883aa
Windows-specific fixes
kimadeline 4a27b41
Typo
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
46 changes: 46 additions & 0 deletions
46
src/client/pythonEnvironments/discovery/locators/services/pyenvLocator.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,46 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
import * as path from 'path'; | ||
import { | ||
getEnvironmentVariable, getOSType, getUserHomeDir, OSType, | ||
} from '../../../../common/utils/platform'; | ||
import { pathExists } from '../../../common/externalDependencies'; | ||
|
||
/** | ||
* Checks if the given interpreter belongs to a pyenv based environment. | ||
* @param {string} interpreterPath: Absolute path to the python interpreter. | ||
* @returns {boolean}: Returns true if the interpreter belongs to a pyenv environment. | ||
*/ | ||
export async function isPyenvEnvironment(interpreterPath:string): Promise<boolean> { | ||
// Check if the pyenv environment variables exist: PYENV on Windows, PYENV_ROOT on Unix. | ||
// They contain the path to pyenv's installation folder. | ||
// If they don't exist, use the default path: ~/.pyenv/pyenv-win on Windows, ~/.pyenv on Unix. | ||
// If the interpreter path starts with the path to the pyenv folder, then it is a pyenv environment. | ||
// See https://github.com/pyenv/pyenv#locating-the-python-installation for general usage, | ||
// And https://github.com/pyenv-win/pyenv-win for Windows specifics. | ||
const isWindows = getOSType() === OSType.Windows; | ||
const envVariable = isWindows ? 'PYENV' : 'PYENV_ROOT'; | ||
|
||
let pyenvDir = getEnvironmentVariable(envVariable); | ||
let pathToCheck = interpreterPath; | ||
|
||
if (!pyenvDir) { | ||
const homeDir = getUserHomeDir() || ''; | ||
pyenvDir = isWindows ? path.join(homeDir, '.pyenv', 'pyenv-win') : path.join(homeDir, '.pyenv'); | ||
} | ||
|
||
if (!await pathExists(pyenvDir)) { | ||
return false; | ||
} | ||
|
||
if (!pyenvDir.endsWith(path.sep)) { | ||
pyenvDir += path.sep; | ||
} | ||
|
||
if (getOSType() === OSType.Windows) { | ||
pyenvDir = pyenvDir.toUpperCase(); | ||
pathToCheck = pathToCheck.toUpperCase(); | ||
} | ||
|
||
return pathToCheck.startsWith(pyenvDir); | ||
} |
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.
Empty file.
106 changes: 106 additions & 0 deletions
106
src/test/pythonEnvironments/discovery/locators/pyenvLocator.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,106 @@ | ||
// 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 { isPyenvEnvironment } from '../../../../client/pythonEnvironments/discovery/locators/services/pyenvLocator'; | ||
|
||
suite('Pyenv Locator Tests', () => { | ||
const home = platformUtils.getUserHomeDir() || ''; | ||
let getEnvVariableStub: sinon.SinonStub; | ||
let pathExistsStub:sinon.SinonStub; | ||
let getOsTypeStub: sinon.SinonStub; | ||
|
||
setup(() => { | ||
getEnvVariableStub = sinon.stub(platformUtils, 'getEnvironmentVariable'); | ||
getOsTypeStub = sinon.stub(platformUtils, 'getOSType'); | ||
pathExistsStub = sinon.stub(fileUtils, 'pathExists'); | ||
}); | ||
|
||
teardown(() => { | ||
getEnvVariableStub.restore(); | ||
pathExistsStub.restore(); | ||
getOsTypeStub.restore(); | ||
}); | ||
|
||
type PyenvUnitTestData = { | ||
testTitle: string, | ||
interpreterPath: string, | ||
pyenvEnvVar?: string, | ||
osType: platformUtils.OSType, | ||
}; | ||
|
||
const testData: PyenvUnitTestData[] = [ | ||
{ | ||
testTitle: 'undefined', | ||
interpreterPath: path.join(home, '.pyenv', 'versions', '3.8.0', 'bin', 'python'), | ||
osType: platformUtils.OSType.Linux, | ||
}, | ||
{ | ||
testTitle: 'undefined', | ||
interpreterPath: path.join(home, '.pyenv', 'pyenv-win', 'versions', '3.8.0', 'bin', 'python'), | ||
osType: platformUtils.OSType.Windows, | ||
}, | ||
{ | ||
testTitle: 'its default value', | ||
interpreterPath: path.join(home, '.pyenv', 'versions', '3.8.0', 'bin', 'python'), | ||
pyenvEnvVar: path.join(home, '.pyenv'), | ||
osType: platformUtils.OSType.Linux, | ||
}, | ||
{ | ||
testTitle: 'its default value', | ||
interpreterPath: path.join(home, '.pyenv', 'pyenv-win', 'versions', '3.8.0', 'bin', 'python'), | ||
pyenvEnvVar: path.join(home, '.pyenv', 'pyenv-win'), | ||
osType: platformUtils.OSType.Windows, | ||
}, | ||
{ | ||
testTitle: 'a custom value', | ||
interpreterPath: path.join('path', 'to', 'mypyenv', 'versions', '3.8.0', 'bin', 'python'), | ||
pyenvEnvVar: path.join('path', 'to', 'mypyenv'), | ||
osType: platformUtils.OSType.Linux, | ||
}, | ||
{ | ||
testTitle: 'a custom value', | ||
interpreterPath: path.join('path', 'to', 'mypyenv', 'pyenv-win', 'versions', '3.8.0', 'bin', 'python'), | ||
pyenvEnvVar: path.join('path', 'to', 'mypyenv', 'pyenv-win'), | ||
osType: platformUtils.OSType.Windows, | ||
}, | ||
]; | ||
|
||
testData.forEach(({ | ||
testTitle, interpreterPath, pyenvEnvVar, osType, | ||
}) => { | ||
test(`The environment variable is set to ${testTitle} on ${osType}, and the interpreter path is in a subfolder of the pyenv folder`, async () => { | ||
getEnvVariableStub.withArgs('PYENV_ROOT').returns(pyenvEnvVar); | ||
getEnvVariableStub.withArgs('PYENV').returns(pyenvEnvVar); | ||
getOsTypeStub.returns(osType); | ||
pathExistsStub.resolves(true); | ||
|
||
const result = await isPyenvEnvironment(interpreterPath); | ||
|
||
assert.strictEqual(result, true); | ||
}); | ||
}); | ||
|
||
test('The pyenv directory does not exist', async () => { | ||
const interpreterPath = path.join('path', 'to', 'python'); | ||
|
||
pathExistsStub.resolves(false); | ||
|
||
const result = await isPyenvEnvironment(interpreterPath); | ||
|
||
assert.strictEqual(result, false); | ||
}); | ||
|
||
test('The interpreter path is not in a subfolder of the pyenv folder', async () => { | ||
const interpreterPath = path.join('path', 'to', 'python'); | ||
|
||
pathExistsStub.resolves(true); | ||
|
||
const result = await isPyenvEnvironment(interpreterPath); | ||
|
||
assert.strictEqual(result, 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.