Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 15 additions & 2 deletions src/managers/pyenv/pyenvUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ import {
} from '../common/nativePythonFinder';
import { shortVersion, sortEnvironments } from '../common/utils';

/**
* Returns the pyenv root directory from the pyenv executable path.
* On Windows, pyenv-win is at `~/.pyenv/pyenv-win/bin/pyenv.bat` (3 levels up).
* On POSIX, pyenv is at `~/.pyenv/bin/pyenv` (2 levels up).
*/
export function getPyenvDir(pyenv: string): string {
if (isWindows()) {
return path.dirname(path.dirname(path.dirname(pyenv)));
}
Comment thread
karthiknadig marked this conversation as resolved.
Outdated
Comment thread
karthiknadig marked this conversation as resolved.
return path.dirname(path.dirname(pyenv));
}

async function findPyenv(): Promise<string | undefined> {
try {
return await which('pyenv');
Expand Down Expand Up @@ -174,8 +186,9 @@ function nativeToPythonEnv(
return undefined;
}

const versionsPath = normalizePath(path.join(path.dirname(path.dirname(pyenv)), 'versions'));
const envsPaths = normalizePath(path.join(path.dirname(versionsPath), 'envs'));
const pyenvDir = getPyenvDir(pyenv);
const versionsPath = normalizePath(path.join(pyenvDir, 'versions'));
const envsPaths = normalizePath(path.join(pyenvDir, 'envs'));
let group = undefined;
const normPrefix = normalizePath(info.prefix);
if (normPrefix.startsWith(versionsPath)) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/managers/pyenv/pyenvUtils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import assert from 'node:assert';
import * as path from 'path';
import * as sinon from 'sinon';
import * as platformUtils from '../../../common/utils/platformUtils';
import { getPyenvDir } from '../../../managers/pyenv/pyenvUtils';

suite('pyenvUtils - getPyenvDir', () => {
let isWindowsStub: sinon.SinonStub;

setup(() => {
isWindowsStub = sinon.stub(platformUtils, 'isWindows');
});

teardown(() => {
sinon.restore();
});

test('should go up 2 levels on POSIX (bin/pyenv -> pyenv root)', () => {
isWindowsStub.returns(false);
// e.g. /home/user/.pyenv/bin/pyenv
const pyenvBin = path.join('home', 'user', '.pyenv', 'bin', 'pyenv');
const result = getPyenvDir(pyenvBin);
assert.strictEqual(result, path.join('home', 'user', '.pyenv'));
});
Comment thread
karthiknadig marked this conversation as resolved.
Outdated

test('should go up 3 levels on Windows (pyenv-win/bin/pyenv.bat -> pyenv root)', () => {
isWindowsStub.returns(true);
// e.g. C:\Users\user\.pyenv\pyenv-win\bin\pyenv.bat
const pyenvBin = path.join('C:', 'Users', 'user', '.pyenv', 'pyenv-win', 'bin', 'pyenv.bat');
const result = getPyenvDir(pyenvBin);
assert.strictEqual(result, path.join('C:', 'Users', 'user', '.pyenv'));
});
});
Loading