diff --git a/src/client/pythonEnvironments/base/locators/lowLevel/poetryLocator.ts b/src/client/pythonEnvironments/base/locators/lowLevel/poetryLocator.ts index 5df4f366e86d..0b210c05b6a1 100644 --- a/src/client/pythonEnvironments/base/locators/lowLevel/poetryLocator.ts +++ b/src/client/pythonEnvironments/base/locators/lowLevel/poetryLocator.ts @@ -7,13 +7,13 @@ import * as path from 'path'; import { chain, iterable } from '../../../../common/utils/async'; import { PythonEnvKind } from '../../info'; import { BasicEnvInfo, IPythonEnvsIterator } from '../../locator'; -import { FSWatcherKind, FSWatchingLocator } from './fsWatchingLocator'; import { getInterpreterPathFromDir } from '../../../common/commonUtils'; import { pathExists } from '../../../common/externalDependencies'; import { isPoetryEnvironment, localPoetryEnvDirName, Poetry } from '../../../common/environmentManagers/poetry'; import '../../../../common/extensions'; import { asyncFilter } from '../../../../common/utils/arrayUtils'; import { traceError, traceVerbose } from '../../../../logging'; +import { LazyResourceBasedLocator } from '../common/resourceBasedLocator'; /** * Gets all default virtual environment locations to look for in a workspace. @@ -28,27 +28,6 @@ async function getVirtualEnvDirs(root: string): Promise { return asyncFilter(envDirs, pathExists); } -async function getRootVirtualEnvDir(root: string): Promise { - const rootDirs = []; - const poetry = await Poetry.getPoetry(root); - /** - * We can infer the directory in which the existing poetry environments are created to determine - * the root virtual env dir. If no virtual envs are created yet, then fetch the setting value to - * get the root directory instead. We prefer to use 'poetry env list' command first because the - * result of that command is already cached when getting poetry. - */ - const virtualenvs = await poetry?.getEnvList(); - if (virtualenvs?.length) { - rootDirs.push(path.dirname(virtualenvs[0])); - } else { - const setting = await poetry?.getVirtualenvsPathSetting(); - if (setting) { - rootDirs.push(setting); - } - } - return rootDirs; -} - async function getVirtualEnvKind(interpreterPath: string): Promise { if (await isPoetryEnvironment(interpreterPath)) { return PythonEnvKind.Poetry; @@ -60,16 +39,11 @@ async function getVirtualEnvKind(interpreterPath: string): Promise getRootVirtualEnvDir(root), - async () => PythonEnvKind.Poetry, - undefined, - FSWatcherKind.Workspace, - ); + super(); } protected doIterEnvs(): IPythonEnvsIterator { diff --git a/src/test/pythonEnvironments/base/locators/lowLevel/poetryLocator.testvirtualenvs.ts b/src/test/pythonEnvironments/base/locators/lowLevel/poetryLocator.testvirtualenvs.ts deleted file mode 100644 index d8472013db04..000000000000 --- a/src/test/pythonEnvironments/base/locators/lowLevel/poetryLocator.testvirtualenvs.ts +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -import { expect } from 'chai'; -import * as path from 'path'; -import * as sinon from 'sinon'; -import { ExecutionResult, ShellOptions } from '../../../../../client/common/process/types'; -import { PythonEnvKind } from '../../../../../client/pythonEnvironments/base/info'; -import { BasicEnvInfo, ILocator } from '../../../../../client/pythonEnvironments/base/locator'; -import { getEnvs } from '../../../../../client/pythonEnvironments/base/locatorUtils'; -import * as externalDependencies from '../../../../../client/pythonEnvironments/common/externalDependencies'; -import { PoetryLocator } from '../../../../../client/pythonEnvironments/base/locators/lowLevel/poetryLocator'; -import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../../../constants'; -import { TEST_LAYOUT_ROOT } from '../../../common/commonTestConstants'; -import { testLocatorWatcher } from './watcherTestUtils'; - -suite('Poetry Watcher', async () => { - let shellExecute: sinon.SinonStub; - const testPoetryDir = path.join(TEST_LAYOUT_ROOT, 'poetry'); - const project1 = path.join(testPoetryDir, 'project1'); - suiteSetup(async function () { - // Skipping these test see https://github.com/microsoft/vscode-python/issues/17087 - this.skip(); - - shellExecute = sinon.stub(externalDependencies, 'shellExecute'); - shellExecute.callsFake((command: string, options: ShellOptions) => { - // eslint-disable-next-line default-case - if (command === 'poetry env list --full-path') { - return Promise.resolve>({ stdout: '' }); - } - if (command === 'poetry config virtualenvs.path') { - if (options.cwd && externalDependencies.arePathsSame(options.cwd, project1)) { - return Promise.resolve>({ - stdout: `${testPoetryDir} \n`, - }); - } - } - return Promise.reject(new Error('Command failed')); - }); - }); - testLocatorWatcher(testPoetryDir, async () => new PoetryLocator(project1), { - kind: PythonEnvKind.Poetry, - doNotVerifyIfLocated: true, - }); - - suiteTeardown(() => sinon.restore()); -}); - -suite('Poetry Locator', async () => { - let locator: ILocator; - suiteSetup(async function () { - if (process.env.CI_PYTHON_VERSION && process.env.CI_PYTHON_VERSION.startsWith('2.')) { - // Poetry is soon to be deprecated for Python2.7, and tests do not pass - // as it is with pip installation of poetry, hence skip. - this.skip(); - } - locator = new PoetryLocator(EXTENSION_ROOT_DIR_FOR_TESTS); - }); - - test('Discovers existing poetry environments', async () => { - const items = await getEnvs(locator.iterEnvs()); - const isLocated = items.some( - (item) => item.kind === PythonEnvKind.Poetry && item.executablePath.includes('poetry-tutorial-project'), - ); - expect(isLocated).to.equal(true); - }); -});