|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import * as assert from 'assert'; |
| 5 | +import * as sinon from 'sinon'; |
| 6 | +import { PythonEnvInfoCache } from '../../../client/pythonEnvironments/base/envsCache'; |
| 7 | +import { PythonEnvInfo, PythonEnvKind } from '../../../client/pythonEnvironments/base/info'; |
| 8 | +import * as externalDependencies from '../../../client/pythonEnvironments/common/externalDependencies'; |
| 9 | + |
| 10 | +suite('Environment Info cache', () => { |
| 11 | + let getGlobalPersistentStoreStub: sinon.SinonStub; |
| 12 | + let updatedValues: PythonEnvInfo[] | undefined; |
| 13 | + |
| 14 | + const allEnvsComplete = () => true; |
| 15 | + const envInfoArray = [ |
| 16 | + { |
| 17 | + kind: PythonEnvKind.Conda, executable: { filename: 'my-conda-env' }, |
| 18 | + }, |
| 19 | + { |
| 20 | + kind: PythonEnvKind.Venv, executable: { filename: 'my-venv-env' }, |
| 21 | + }, |
| 22 | + { |
| 23 | + kind: PythonEnvKind.Pyenv, executable: { filename: 'my-pyenv-env' }, |
| 24 | + }, |
| 25 | + ] as PythonEnvInfo[]; |
| 26 | + |
| 27 | + setup(() => { |
| 28 | + getGlobalPersistentStoreStub = sinon.stub(externalDependencies, 'getGlobalPersistentStore'); |
| 29 | + getGlobalPersistentStoreStub.returns({ |
| 30 | + get() { return envInfoArray; }, |
| 31 | + set(envs: PythonEnvInfo[]) { |
| 32 | + updatedValues = envs; |
| 33 | + return Promise.resolve(); |
| 34 | + }, |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + teardown(() => { |
| 39 | + getGlobalPersistentStoreStub.restore(); |
| 40 | + updatedValues = undefined; |
| 41 | + }); |
| 42 | + |
| 43 | + test('`initialize` reads from persistent storage', () => { |
| 44 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 45 | + |
| 46 | + envsCache.initialize(); |
| 47 | + |
| 48 | + assert.ok(getGlobalPersistentStoreStub.calledOnce); |
| 49 | + }); |
| 50 | + |
| 51 | + test('The in-memory env info array is undefined if there is no value in persistent storage when initializing the cache', () => { |
| 52 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 53 | + |
| 54 | + getGlobalPersistentStoreStub.returns({ get() { return undefined; } }); |
| 55 | + envsCache.initialize(); |
| 56 | + const result = envsCache.getAllEnvs(); |
| 57 | + |
| 58 | + assert.strictEqual(result, undefined); |
| 59 | + }); |
| 60 | + |
| 61 | + test('`getAllEnvs` should return a deep copy of the environments currently in memory', () => { |
| 62 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 63 | + |
| 64 | + envsCache.initialize(); |
| 65 | + const envs = envsCache.getAllEnvs()!; |
| 66 | + |
| 67 | + envs[0].name = 'some-other-name'; |
| 68 | + |
| 69 | + assert.ok(envs[0] !== envInfoArray[0]); |
| 70 | + }); |
| 71 | + |
| 72 | + test('`getAllEnvs` should return undefined if nothing has been set', () => { |
| 73 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 74 | + |
| 75 | + const envs = envsCache.getAllEnvs(); |
| 76 | + |
| 77 | + assert.deepStrictEqual(envs, undefined); |
| 78 | + }); |
| 79 | + |
| 80 | + test('`setAllEnvs` should clone the environment info array passed as a parameter', () => { |
| 81 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 82 | + |
| 83 | + envsCache.setAllEnvs(envInfoArray); |
| 84 | + const envs = envsCache.getAllEnvs(); |
| 85 | + |
| 86 | + assert.deepStrictEqual(envs, envInfoArray); |
| 87 | + assert.strictEqual(envs === envInfoArray, false); |
| 88 | + }); |
| 89 | + |
| 90 | + test('`filterEnvs` should return environments that match its argument using areSameEnvironmnet', () => { |
| 91 | + const env:PythonEnvInfo = { executable: { filename: 'my-venv-env' } } as unknown as PythonEnvInfo; |
| 92 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 93 | + |
| 94 | + envsCache.initialize(); |
| 95 | + |
| 96 | + const result = envsCache.filterEnvs(env); |
| 97 | + |
| 98 | + assert.deepStrictEqual(result, [{ |
| 99 | + kind: PythonEnvKind.Venv, executable: { filename: 'my-venv-env' }, |
| 100 | + }]); |
| 101 | + }); |
| 102 | + |
| 103 | + test('`filterEnvs` should return a deep copy of the matched environments', () => { |
| 104 | + const envToFind = { |
| 105 | + kind: PythonEnvKind.System, executable: { filename: 'my-system-env' }, |
| 106 | + } as unknown as PythonEnvInfo; |
| 107 | + const env:PythonEnvInfo = { executable: { filename: 'my-system-env' } } as unknown as PythonEnvInfo; |
| 108 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 109 | + |
| 110 | + envsCache.setAllEnvs([...envInfoArray, envToFind]); |
| 111 | + |
| 112 | + const result = envsCache.filterEnvs(env)!; |
| 113 | + result[0].name = 'some-other-name'; |
| 114 | + |
| 115 | + assert.notDeepStrictEqual(result[0], envToFind); |
| 116 | + }); |
| 117 | + |
| 118 | + test('`filterEnvs` should return an empty array if no environment matches the properties of its argument', () => { |
| 119 | + const env:PythonEnvInfo = { executable: { filename: 'my-nonexistent-env' } } as unknown as PythonEnvInfo; |
| 120 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 121 | + |
| 122 | + envsCache.initialize(); |
| 123 | + |
| 124 | + const result = envsCache.filterEnvs(env); |
| 125 | + |
| 126 | + assert.deepStrictEqual(result, []); |
| 127 | + }); |
| 128 | + |
| 129 | + test('`filterEnvs` should return undefined if the cache hasn\'t been initialized', () => { |
| 130 | + const env:PythonEnvInfo = { executable: { filename: 'my-nonexistent-env' } } as unknown as PythonEnvInfo; |
| 131 | + const envsCache = new PythonEnvInfoCache(allEnvsComplete); |
| 132 | + |
| 133 | + const result = envsCache.filterEnvs(env); |
| 134 | + |
| 135 | + assert.strictEqual(result, undefined); |
| 136 | + }); |
| 137 | + |
| 138 | + test('`flush` should write complete environment info objects to persistent storage', async () => { |
| 139 | + const otherEnv = { |
| 140 | + kind: PythonEnvKind.OtherGlobal, |
| 141 | + executable: { filename: 'my-other-env' }, |
| 142 | + defaultDisplayName: 'other-env', |
| 143 | + }; |
| 144 | + const updatedEnvInfoArray = [ |
| 145 | + otherEnv, { kind: PythonEnvKind.System, executable: { filename: 'my-system-env' } }, |
| 146 | + ] as PythonEnvInfo[]; |
| 147 | + const expected = [ |
| 148 | + otherEnv, |
| 149 | + ]; |
| 150 | + const envsCache = new PythonEnvInfoCache((env) => env.defaultDisplayName !== undefined); |
| 151 | + |
| 152 | + envsCache.initialize(); |
| 153 | + envsCache.setAllEnvs(updatedEnvInfoArray); |
| 154 | + await envsCache.flush(); |
| 155 | + |
| 156 | + assert.deepStrictEqual(updatedValues, expected); |
| 157 | + }); |
| 158 | + |
| 159 | + test('`flush` should not write to persistent storage if there are no environment info objects in-memory', async () => { |
| 160 | + const envsCache = new PythonEnvInfoCache((env) => env.kind === PythonEnvKind.MacDefault); |
| 161 | + |
| 162 | + await envsCache.flush(); |
| 163 | + |
| 164 | + assert.strictEqual(updatedValues, undefined); |
| 165 | + }); |
| 166 | + |
| 167 | + test('`flush` should not write to persistent storage if there are no complete environment info objects', async () => { |
| 168 | + const envsCache = new PythonEnvInfoCache((env) => env.kind === PythonEnvKind.MacDefault); |
| 169 | + |
| 170 | + envsCache.initialize(); |
| 171 | + await envsCache.flush(); |
| 172 | + |
| 173 | + assert.strictEqual(updatedValues, undefined); |
| 174 | + }); |
| 175 | +}); |
0 commit comments