|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// eslint-disable-next-line max-classes-per-file |
| 5 | +import { assert } from 'chai'; |
| 6 | +import * as fs from 'fs-extra'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { traceWarning } from '../../../../../client/common/logger'; |
| 9 | +import { FileChangeType } from '../../../../../client/common/platform/fileSystemWatcher'; |
| 10 | +import { createDeferred, Deferred, sleep } from '../../../../../client/common/utils/async'; |
| 11 | +import { getOSType, OSType } from '../../../../../client/common/utils/platform'; |
| 12 | +import { IDisposableLocator } from '../../../../../client/pythonEnvironments/base/locator'; |
| 13 | +import { createWorkspaceVirtualEnvLocator } from '../../../../../client/pythonEnvironments/base/locators/lowLevel/workspaceVirtualEnvLocator'; |
| 14 | +import { getEnvs } from '../../../../../client/pythonEnvironments/base/locatorUtils'; |
| 15 | +import { PythonEnvsChangedEvent } from '../../../../../client/pythonEnvironments/base/watcher'; |
| 16 | +import { getInterpreterPathFromDir } from '../../../../../client/pythonEnvironments/common/commonUtils'; |
| 17 | +import { arePathsSame } from '../../../../../client/pythonEnvironments/common/externalDependencies'; |
| 18 | +import { deleteFiles, PYTHON_PATH } from '../../../../common'; |
| 19 | +import { TEST_TIMEOUT } from '../../../../constants'; |
| 20 | +import { TEST_LAYOUT_ROOT } from '../../../common/commonTestConstants'; |
| 21 | +import { run } from '../../../discovery/locators/envTestUtils'; |
| 22 | + |
| 23 | +class WorkspaceVenvs { |
| 24 | + constructor(private readonly root: string, private readonly prefix = '.virtual') { } |
| 25 | + |
| 26 | + public async create(name: string): Promise<string> { |
| 27 | + const envName = this.resolve(name); |
| 28 | + const argv = [PYTHON_PATH.fileToCommandArgument(), '-m', 'virtualenv', envName]; |
| 29 | + try { |
| 30 | + await run(argv, { cwd: this.root }); |
| 31 | + } catch (err) { |
| 32 | + throw new Error(`Failed to create Env ${path.basename(envName)} Error: ${err}`); |
| 33 | + } |
| 34 | + const dirToLookInto = path.join(this.root, envName); |
| 35 | + const filename = await getInterpreterPathFromDir(dirToLookInto); |
| 36 | + if (!filename) { |
| 37 | + throw new Error(`No environment to update exists in ${dirToLookInto}`); |
| 38 | + } |
| 39 | + return filename; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a dummy environment by creating a fake executable. |
| 44 | + * @param name environment suffix name to create |
| 45 | + */ |
| 46 | + public async createDummyEnv(name: string): Promise<string> { |
| 47 | + const envName = this.resolve(name); |
| 48 | + const filepath = path.join(this.root, envName, getOSType() === OSType.Windows ? 'python.exe' : 'python'); |
| 49 | + try { |
| 50 | + await fs.createFile(filepath); |
| 51 | + } catch (err) { |
| 52 | + throw new Error(`Failed to create python executable ${filepath}, Error: ${err}`); |
| 53 | + } |
| 54 | + return filepath; |
| 55 | + } |
| 56 | + |
| 57 | + // eslint-disable-next-line class-methods-use-this |
| 58 | + public async update(filename: string): Promise<void> { |
| 59 | + try { |
| 60 | + await fs.writeFile(filename, 'Environment has been updated'); |
| 61 | + } catch (err) { |
| 62 | + throw new Error(`Failed to update Workspace virtualenv executable ${filename}, Error: ${err}`); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // eslint-disable-next-line class-methods-use-this |
| 67 | + public async delete(filename: string): Promise<void> { |
| 68 | + try { |
| 69 | + await fs.remove(filename); |
| 70 | + } catch (err) { |
| 71 | + traceWarning(`Failed to clean up ${filename}`); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public async cleanUp() { |
| 76 | + const globPattern = path.join(this.root, `${this.prefix}*`); |
| 77 | + await deleteFiles(globPattern); |
| 78 | + } |
| 79 | + |
| 80 | + private resolve(name: string): string { |
| 81 | + // Ensure env is random to avoid conflicts in tests (corrupting test data) |
| 82 | + const now = new Date().getTime().toString().substr(-8); |
| 83 | + return `${this.prefix}${name}${now}`; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +suite('WorkspaceVirtualEnvironment Locator', async () => { |
| 88 | + const testWorkspaceFolder = path.join(TEST_LAYOUT_ROOT, 'workspace', 'folder1'); |
| 89 | + const workspaceVenvs = new WorkspaceVenvs(testWorkspaceFolder); |
| 90 | + let locator: IDisposableLocator; |
| 91 | + |
| 92 | + async function waitForChangeToBeDetected(deferred: Deferred<void>) { |
| 93 | + const timeout = setTimeout( |
| 94 | + () => { |
| 95 | + clearTimeout(timeout); |
| 96 | + deferred.reject(new Error('Environment not detected')); |
| 97 | + }, |
| 98 | + TEST_TIMEOUT, |
| 99 | + ); |
| 100 | + await deferred.promise; |
| 101 | + } |
| 102 | + |
| 103 | + async function isLocated(executable: string): Promise<boolean> { |
| 104 | + const items = await getEnvs(locator.iterEnvs()); |
| 105 | + return items.some((item) => arePathsSame(item.executable.filename, executable)); |
| 106 | + } |
| 107 | + |
| 108 | + suiteSetup(async () => workspaceVenvs.cleanUp()); |
| 109 | + |
| 110 | + async function setupLocator(onChanged: (e: PythonEnvsChangedEvent) => Promise<void>) { |
| 111 | + locator = await createWorkspaceVirtualEnvLocator(testWorkspaceFolder); |
| 112 | + // Wait for watchers to get ready |
| 113 | + await sleep(1000); |
| 114 | + locator.onChanged(onChanged); |
| 115 | + } |
| 116 | + |
| 117 | + teardown(async () => { |
| 118 | + await workspaceVenvs.cleanUp(); |
| 119 | + locator.dispose(); |
| 120 | + }); |
| 121 | + |
| 122 | + test('Detect a new environment', async () => { |
| 123 | + let actualEvent: PythonEnvsChangedEvent; |
| 124 | + const deferred = createDeferred<void>(); |
| 125 | + await setupLocator(async (e) => { |
| 126 | + actualEvent = e; |
| 127 | + deferred.resolve(); |
| 128 | + }); |
| 129 | + |
| 130 | + const executable = await workspaceVenvs.create('one'); |
| 131 | + await waitForChangeToBeDetected(deferred); |
| 132 | + const isFound = await isLocated(executable); |
| 133 | + |
| 134 | + assert.ok(isFound); |
| 135 | + // Detecting kind of virtual env depends on the file structure around the executable, so we need to wait before |
| 136 | + // attempting to verify it. Omitting that check as we can never deterministically say when it's ready to check. |
| 137 | + assert.deepEqual(actualEvent!.type, FileChangeType.Created, 'Wrong event emitted'); |
| 138 | + }); |
| 139 | + |
| 140 | + test('Detect when an environment has been deleted', async () => { |
| 141 | + let actualEvent: PythonEnvsChangedEvent; |
| 142 | + const deferred = createDeferred<void>(); |
| 143 | + const executable = await workspaceVenvs.create('one'); |
| 144 | + // Wait before the change event has been sent. If both operations occur almost simultaneously no event is sent. |
| 145 | + await sleep(100); |
| 146 | + await setupLocator(async (e) => { |
| 147 | + actualEvent = e; |
| 148 | + deferred.resolve(); |
| 149 | + }); |
| 150 | + |
| 151 | + // VSCode API has a limitation where it fails to fire event when environment folder is deleted directly: |
| 152 | + // https://github.com/microsoft/vscode/issues/110923 |
| 153 | + // Using chokidar directly in tests work, but it has permission issues on Windows that you cannot delete a |
| 154 | + // folder if it has a subfolder that is being watched inside: https://github.com/paulmillr/chokidar/issues/422 |
| 155 | + // Hence we test directly deleting the executable, and not the whole folder using `workspaceVenvs.cleanUp()`. |
| 156 | + await workspaceVenvs.delete(executable); |
| 157 | + await waitForChangeToBeDetected(deferred); |
| 158 | + const isFound = await isLocated(executable); |
| 159 | + |
| 160 | + assert.notOk(isFound); |
| 161 | + assert.deepEqual(actualEvent!.type, FileChangeType.Deleted, 'Wrong event emitted'); |
| 162 | + }); |
| 163 | + |
| 164 | + test('Detect when an environment has been updated', async () => { |
| 165 | + let actualEvent: PythonEnvsChangedEvent; |
| 166 | + const deferred = createDeferred<void>(); |
| 167 | + // Create a dummy environment so we can update its executable later. We can't choose a real environment here. |
| 168 | + // Executables inside real environments can be symlinks, so writing on them can result in the real executable |
| 169 | + // being updated instead of the symlink. |
| 170 | + const executable = await workspaceVenvs.createDummyEnv('one'); |
| 171 | + // Wait before the change event has been sent. If both operations occur almost simultaneously no event is sent. |
| 172 | + await sleep(100); |
| 173 | + await setupLocator(async (e) => { |
| 174 | + actualEvent = e; |
| 175 | + deferred.resolve(); |
| 176 | + }); |
| 177 | + |
| 178 | + await workspaceVenvs.update(executable); |
| 179 | + await waitForChangeToBeDetected(deferred); |
| 180 | + const isFound = await isLocated(executable); |
| 181 | + |
| 182 | + assert.ok(isFound); |
| 183 | + // Detecting kind of virtual env depends on the file structure around the executable, so we need to wait before |
| 184 | + // attempting to verify it. Omitting that check as we can never deterministically say when it's ready to check. |
| 185 | + assert.deepEqual(actualEvent!.type, FileChangeType.Changed, 'Wrong event emitted'); |
| 186 | + }); |
| 187 | +}); |
0 commit comments