|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { assert } from 'chai'; |
| 5 | +import * as fs from 'fs-extra'; |
| 6 | +import * as path from 'path'; |
| 7 | +import { traceWarning } from '../../../../client/common/logger'; |
| 8 | +import { FileChangeType } from '../../../../client/common/platform/fileSystemWatcher'; |
| 9 | +import { createDeferred, Deferred, sleep } from '../../../../client/common/utils/async'; |
| 10 | +import { PythonEnvKind } from '../../../../client/pythonEnvironments/base/info'; |
| 11 | +import { getEnvs } from '../../../../client/pythonEnvironments/base/locatorUtils'; |
| 12 | +import { PythonEnvsChangedEvent } from '../../../../client/pythonEnvironments/base/watcher'; |
| 13 | +import { arePathsSame } from '../../../../client/pythonEnvironments/common/externalDependencies'; |
| 14 | +import { WindowsStoreLocator } from '../../../../client/pythonEnvironments/discovery/locators/services/windowsStoreLocator'; |
| 15 | +import { TEST_TIMEOUT } from '../../../constants'; |
| 16 | +import { TEST_LAYOUT_ROOT } from '../../common/commonTestConstants'; |
| 17 | + |
| 18 | +class WindowsStoreEnvs { |
| 19 | + private executables: string[] = []; |
| 20 | + |
| 21 | + constructor(private readonly storeAppRoot: string) {} |
| 22 | + |
| 23 | + public async create(basename: string): Promise<string> { |
| 24 | + const filename = path.join(this.storeAppRoot, basename); |
| 25 | + try { |
| 26 | + await fs.createFile(filename); |
| 27 | + } catch (err) { |
| 28 | + throw new Error(`Failed to create Windows Apps executable ${filename}, Error: ${err}`); |
| 29 | + } |
| 30 | + this.executables.push(filename); |
| 31 | + return filename; |
| 32 | + } |
| 33 | + |
| 34 | + public async update(basename: string): Promise<void> { |
| 35 | + const filename = path.join(this.storeAppRoot, basename); |
| 36 | + try { |
| 37 | + await fs.writeFile(filename, 'Environment has been updated'); |
| 38 | + } catch (err) { |
| 39 | + throw new Error(`Failed to update Windows Apps executable ${filename}, Error: ${err}`); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public async cleanUp() { |
| 44 | + await Promise.all( |
| 45 | + this.executables.map(async (filename: string) => { |
| 46 | + try { |
| 47 | + await fs.remove(filename); |
| 48 | + } catch (err) { |
| 49 | + traceWarning(`Failed to clean up ${filename}`); |
| 50 | + } |
| 51 | + }), |
| 52 | + ); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +suite('Windows Store Locator', async () => { |
| 57 | + const testLocalAppData = path.join(TEST_LAYOUT_ROOT, 'storeApps'); |
| 58 | + const testStoreAppRoot = path.join(testLocalAppData, 'Microsoft', 'WindowsApps'); |
| 59 | + const windowsStoreEnvs = new WindowsStoreEnvs(testStoreAppRoot); |
| 60 | + let locator: WindowsStoreLocator; |
| 61 | + const localAppDataOldValue = process.env.LOCALAPPDATA; |
| 62 | + |
| 63 | + async function waitForChangeToBeDetected(deferred: Deferred<void>) { |
| 64 | + const timeout = setTimeout( |
| 65 | + () => { |
| 66 | + clearTimeout(timeout); |
| 67 | + deferred.reject(new Error('Environment not detected')); |
| 68 | + }, |
| 69 | + TEST_TIMEOUT, |
| 70 | + ); |
| 71 | + await deferred.promise; |
| 72 | + } |
| 73 | + |
| 74 | + async function isLocated(executable: string): Promise<boolean> { |
| 75 | + const items = await getEnvs(locator.iterEnvs()); |
| 76 | + return items.some((item) => arePathsSame(item.executable.filename, executable)); |
| 77 | + } |
| 78 | + |
| 79 | + suiteSetup(async () => { |
| 80 | + process.env.LOCALAPPDATA = testLocalAppData; |
| 81 | + await windowsStoreEnvs.cleanUp(); |
| 82 | + }); |
| 83 | + |
| 84 | + async function setupLocator(onChanged: (e: PythonEnvsChangedEvent) => Promise<void>) { |
| 85 | + locator = new WindowsStoreLocator(); |
| 86 | + locator.initialize(); |
| 87 | + // Wait for watchers to get ready |
| 88 | + await sleep(1000); |
| 89 | + locator.onChanged(onChanged); |
| 90 | + } |
| 91 | + |
| 92 | + teardown(() => windowsStoreEnvs.cleanUp()); |
| 93 | + suiteTeardown(async () => { |
| 94 | + process.env.LOCALAPPDATA = localAppDataOldValue; |
| 95 | + }); |
| 96 | + |
| 97 | + test('Detect a new environment', async () => { |
| 98 | + let actualEvent: PythonEnvsChangedEvent; |
| 99 | + const deferred = createDeferred<void>(); |
| 100 | + const expectedEvent = { |
| 101 | + kind: PythonEnvKind.WindowsStore, |
| 102 | + type: FileChangeType.Created, |
| 103 | + }; |
| 104 | + await setupLocator(async (e) => { |
| 105 | + actualEvent = e; |
| 106 | + deferred.resolve(); |
| 107 | + }); |
| 108 | + |
| 109 | + const executable = await windowsStoreEnvs.create('python3.4.exe'); |
| 110 | + await waitForChangeToBeDetected(deferred); |
| 111 | + const isFound = await isLocated(executable); |
| 112 | + |
| 113 | + assert.ok(isFound); |
| 114 | + assert.deepEqual(actualEvent!, expectedEvent, 'Wrong event emitted'); |
| 115 | + }); |
| 116 | + |
| 117 | + test('Detect when an environment has been deleted', async () => { |
| 118 | + let actualEvent: PythonEnvsChangedEvent; |
| 119 | + const deferred = createDeferred<void>(); |
| 120 | + const expectedEvent = { |
| 121 | + kind: PythonEnvKind.WindowsStore, |
| 122 | + type: FileChangeType.Deleted, |
| 123 | + }; |
| 124 | + const executable = await windowsStoreEnvs.create('python3.4.exe'); |
| 125 | + // Wait before the change event has been sent. If both operations occur almost simultaneously no event is sent. |
| 126 | + await sleep(100); |
| 127 | + await setupLocator(async (e) => { |
| 128 | + actualEvent = e; |
| 129 | + deferred.resolve(); |
| 130 | + }); |
| 131 | + |
| 132 | + await windowsStoreEnvs.cleanUp(); |
| 133 | + await waitForChangeToBeDetected(deferred); |
| 134 | + const isFound = await isLocated(executable); |
| 135 | + |
| 136 | + assert.notOk(isFound); |
| 137 | + assert.deepEqual(actualEvent!, expectedEvent, 'Wrong event emitted'); |
| 138 | + }); |
| 139 | + |
| 140 | + test('Detect when an environment has been updated', async () => { |
| 141 | + let actualEvent: PythonEnvsChangedEvent; |
| 142 | + const deferred = createDeferred<void>(); |
| 143 | + const expectedEvent = { |
| 144 | + kind: PythonEnvKind.WindowsStore, |
| 145 | + type: FileChangeType.Changed, |
| 146 | + }; |
| 147 | + const executable = await windowsStoreEnvs.create('python3.4.exe'); |
| 148 | + // Wait before the change event has been sent. If both operations occur almost simultaneously no event is sent. |
| 149 | + await sleep(100); |
| 150 | + await setupLocator(async (e) => { |
| 151 | + actualEvent = e; |
| 152 | + deferred.resolve(); |
| 153 | + }); |
| 154 | + |
| 155 | + await windowsStoreEnvs.update('python3.4.exe'); |
| 156 | + await waitForChangeToBeDetected(deferred); |
| 157 | + const isFound = await isLocated(executable); |
| 158 | + |
| 159 | + assert.ok(isFound); |
| 160 | + assert.deepEqual(actualEvent!, expectedEvent, 'Wrong event emitted'); |
| 161 | + }); |
| 162 | +}); |
0 commit comments