Skip to content

Commit 2ec8cab

Browse files
Kartik Rajkarthiknadig
andcommitted
Do not run poetry env info -p to get the type for local poetry environment (#15898)
* Do not run poetry env info -p to get the type for local poetry environment, get best effort type instead * Update src/test/pythonEnvironments/common/envlayouts/poetry/project4/pyproject.toml Co-authored-by: Karthik Nadig <[email protected]> Co-authored-by: Karthik Nadig <[email protected]>
1 parent 84d798f commit 2ec8cab

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

src/client/pythonEnvironments/discovery/locators/services/poetry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ async function isLocalPoetryEnvironment(interpreterPath: string): Promise<boolea
6969
// - 'pyproject.toml' has a poetry section which contains the necessary fields
7070
// - Poetry configuration allows local virtual environments
7171
// ... possibly more
72-
// Or we can simply try running poetry to find the related environment instead. We do the latter for simplicity and reliability.
73-
// It should not be much expensive as we have already narrowed down this possibility through various file checks.
74-
return isPoetryEnvironmentRelatedToFolder(interpreterPath, project);
72+
// Or we can try running poetry to find the related environment instead. Launching poetry binaries although
73+
// reliable, can be expensive. So report the best effort type instead, i.e this is likely a poetry env.
74+
return true;
7575
}
7676

7777
/**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[tool.poetrzzzzy]
2+
name = "poetry-tutorial-project"
3+
version = "0.1.0"
4+
description = ""
5+
6+
[tool.poetry.dependencies]
7+
python = "^3.5"
8+
9+
[tool.poetry.dev-dependencies]
10+
11+
[build-system]
12+
requires = ["poetry-core>=1.0.0"]
13+
build-backend = "poetry.core.masonry.api"

src/test/pythonEnvironments/discovery/locators/lowLevel/poetry.unit.test.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { TEST_LAYOUT_ROOT } from '../../../common/commonTestConstants';
1515

1616
const testPoetryDir = path.join(TEST_LAYOUT_ROOT, 'poetry');
1717
const project1 = path.join(testPoetryDir, 'project1');
18-
const project2 = path.join(testPoetryDir, 'project2');
18+
const project4 = path.join(testPoetryDir, 'project4');
1919
const project3 = path.join(testPoetryDir, 'project3');
2020

2121
suite('isPoetryEnvironment Tests', () => {
@@ -54,17 +54,9 @@ suite('isPoetryEnvironment Tests', () => {
5454
shellExecute = sinon.stub(externalDependencies, 'shellExecute');
5555
getPythonSetting = sinon.stub(externalDependencies, 'getPythonSetting');
5656
getPythonSetting.returns('poetry');
57-
shellExecute.callsFake((command: string, options: ShellOptions) => {
58-
// eslint-disable-next-line default-case
59-
switch (command) {
60-
case 'poetry env list --full-path':
61-
return Promise.resolve<ExecutionResult<string>>({ stdout: '' });
62-
case 'poetry env info -p':
63-
if (options.cwd && externalDependencies.arePathsSame(options.cwd, project1)) {
64-
return Promise.resolve<ExecutionResult<string>>({
65-
stdout: `${path.join(project1, '.venv')} \n`,
66-
});
67-
}
57+
shellExecute.callsFake((command: string, _options: ShellOptions) => {
58+
if (command === 'poetry env list --full-path') {
59+
return Promise.resolve<ExecutionResult<string>>({ stdout: '' });
6860
}
6961
return Promise.reject(new Error('Command failed'));
7062
});
@@ -84,8 +76,8 @@ suite('isPoetryEnvironment Tests', () => {
8476
expect(result).to.equal(false);
8577
});
8678

87-
test(`Return false if running poetry for project dir as cwd fails`, async () => {
88-
const result = await isPoetryEnvironment(path.join(project2, '.venv', 'bin', 'python'));
79+
test(`Return false if running poetry for project dir as cwd fails (pyproject.toml file is invalid)`, async () => {
80+
const result = await isPoetryEnvironment(path.join(project4, '.venv', 'bin', 'python'));
8981
expect(result).to.equal(false);
9082
});
9183
});

src/test/pythonEnvironments/discovery/locators/poetryLocator.unit.test.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,7 @@ suite('Poetry Locator', () => {
7575
locator = new PoetryLocator(project1);
7676
getOSTypeStub.returns(platformUtils.OSType.Windows);
7777
shellExecute.callsFake((command: string, options: ShellOptions) => {
78-
if (command === 'poetry env info -p') {
79-
if (options.cwd && externalDependencies.arePathsSame(options.cwd, project1)) {
80-
return Promise.resolve<ExecutionResult<string>>({
81-
stdout: `${path.join(project1, '.venv')} \n`,
82-
});
83-
}
84-
} else if (command === 'poetry env list --full-path') {
78+
if (command === 'poetry env list --full-path') {
8579
if (options.cwd && externalDependencies.arePathsSame(options.cwd, project1)) {
8680
return Promise.resolve<ExecutionResult<string>>({
8781
stdout: `${path.join(testPoetryDir, 'poetry-tutorial-project-6hnqYwvD-py3.8')} \n
@@ -214,14 +208,7 @@ suite('Poetry Locator', () => {
214208
locator = new PoetryLocator(project2);
215209
getOSTypeStub.returns(platformUtils.OSType.Linux);
216210
shellExecute.callsFake((command: string, options: ShellOptions) => {
217-
// eslint-disable-next-line default-case
218-
if (command === 'poetry env info -p') {
219-
if (options.cwd && externalDependencies.arePathsSame(options.cwd, project2)) {
220-
return Promise.resolve<ExecutionResult<string>>({
221-
stdout: `${path.join(project2, '.venv')} \n`,
222-
});
223-
}
224-
} else if (command === 'poetry env list --full-path') {
211+
if (command === 'poetry env list --full-path') {
225212
if (options.cwd && externalDependencies.arePathsSame(options.cwd, project2)) {
226213
return Promise.resolve<ExecutionResult<string>>({
227214
stdout: `${path.join(testPoetryDir, 'posix1project-9hvDnqYw-py3.4')} (Activated)\n

0 commit comments

Comments
 (0)