Skip to content

Commit

Permalink
Ensure that we check .venv dir not .venv file (microsoft/vscode-p…
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig authored and wesm committed May 13, 2024
1 parent 4db2a8a commit 51fca42
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
12 changes: 10 additions & 2 deletions extensions/positron-python/python_files/create_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def parse_args(argv: Sequence[str]) -> argparse.Namespace:
default=None,
help="Install additional dependencies from sources like `pyproject.toml` into the virtual environment.",
)

parser.add_argument(
"--extras",
action="append",
Expand All @@ -49,6 +50,7 @@ def parse_args(argv: Sequence[str]) -> argparse.Namespace:
default=False,
help="Add .gitignore to the newly created virtual environment.",
)

parser.add_argument(
"--name",
default=VENV_NAME,
Expand All @@ -57,12 +59,14 @@ def parse_args(argv: Sequence[str]) -> argparse.Namespace:
metavar="NAME",
action="store",
)

parser.add_argument(
"--stdin",
action="store_true",
default=False,
help="Read arguments from stdin.",
)

return parser.parse_args(argv)


Expand All @@ -71,11 +75,15 @@ def is_installed(module: str) -> bool:


def file_exists(path: Union[str, pathlib.PurePath]) -> bool:
return os.path.exists(path)
return pathlib.Path(path).exists()


def venv_exists(name: str) -> bool:
return os.path.exists(CWD / name) and file_exists(get_venv_path(name))
return (
(CWD / name).exists()
and (CWD / name / "pyvenv.cfg").exists()
and file_exists(get_venv_path(name))
)


def run_process(args: Sequence[str], error_message: str) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function getVenvPath(workspaceFolder: WorkspaceFolder): string {
}

export async function hasVenv(workspaceFolder: WorkspaceFolder): Promise<boolean> {
return fs.pathExists(getVenvPath(workspaceFolder));
return fs.pathExists(path.join(getVenvPath(workspaceFolder), 'pyvenv.cfg'));
}

export function getVenvExecutable(workspaceFolder: WorkspaceFolder): string {
Expand Down
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 { expect } from 'chai';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as sinon from 'sinon';
import { Uri } from 'vscode';
import { hasVenv } from '../../../../client/pythonEnvironments/creation/common/commonUtils';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../../constants';

suite('CommonUtils', () => {
let fileExistsStub: sinon.SinonStub;
const workspace1 = {
uri: Uri.file(path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'testMultiRootWkspc', 'workspace1')),
name: 'workspace1',
index: 0,
};

setup(() => {
fileExistsStub = sinon.stub(fs, 'pathExists');
});

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

test('Venv exists test', async () => {
fileExistsStub.resolves(true);
const result = await hasVenv(workspace1);
expect(result).to.be.equal(true, 'Incorrect result');

fileExistsStub.calledOnceWith(path.join(workspace1.uri.fsPath, '.venv', 'pyvenv.cfg'));
});

test('Venv does not exist test', async () => {
fileExistsStub.resolves(false);
const result = await hasVenv(workspace1);
expect(result).to.be.equal(false, 'Incorrect result');

fileExistsStub.calledOnceWith(path.join(workspace1.uri.fsPath, '.venv', 'pyvenv.cfg'));
});
});

0 comments on commit 51fca42

Please sign in to comment.