Skip to content

Commit 9e438bc

Browse files
committed
getEnv -> filterEnvs
1 parent fab5344 commit 9e438bc

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

src/client/pythonEnvironments/base/envsCache.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import { cloneDeep } from 'lodash';
55
import { getGlobalPersistentStore, IPersistentStore } from '../common/externalDependencies';
6-
import { areSameEnvironment, PythonEnvInfo } from './info';
6+
import { PythonEnvInfo } from './info';
7+
import { areSameEnvironment } from './info/env';
78

89
/**
910
* Represents the environment info cache to be used by the cache locator.
@@ -29,15 +30,17 @@ export interface IEnvsCache {
2930
setAllEnvs(envs: PythonEnvInfo[]): void;
3031

3132
/**
32-
* Return a specific environmnent info object.
33+
* If the cache has been initialized, return environmnent info objects that match a query object.
34+
* If none of the environments in the cache match the query data, return an empty array.
35+
* If the in-memory cache has not been initialized prior to calling `filterEnvs`, return `undefined`.
3336
*
3437
* @param env The environment info data that will be used to look for
35-
* an environment info object in the cache, or a unique environment key.
38+
* environment info objects in the cache, or a unique environment key.
3639
* If passing an environment info object, it may contain incomplete environment info.
37-
* @return The environment info object that matches all non-undefined keys from the `env` param,
38-
* `undefined` otherwise.
40+
* @return The environment info objects matching the `env` param,
41+
* or `undefined` if the in-memory cache is not initialized.
3942
*/
40-
getEnv(env: PythonEnvInfo | string): PythonEnvInfo | undefined;
43+
filterEnvs(env: PythonEnvInfo | string): PythonEnvInfo[] | undefined;
4144

4245
/**
4346
* Writes the content of the in-memory cache to persistent storage.
@@ -77,8 +80,8 @@ export class PythonEnvInfoCache implements IEnvsCache {
7780
this.envsList = cloneDeep(envs);
7881
}
7982

80-
public getEnv(env: PythonEnvInfo | string): PythonEnvInfo | undefined {
81-
const result = this.envsList?.find((info) => areSameEnvironment(info, env));
83+
public filterEnvs(env: PythonEnvInfo | string): PythonEnvInfo[] | undefined {
84+
const result = this.envsList?.filter((info) => areSameEnvironment(info, env));
8285

8386
if (result) {
8487
return cloneDeep(result);

src/test/pythonEnvironments/base/envsCache.unit.test.ts

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,38 @@
44
import * as assert from 'assert';
55
import * as sinon from 'sinon';
66
import { PythonEnvInfoCache } from '../../../client/pythonEnvironments/base/envsCache';
7-
import * as envInfo from '../../../client/pythonEnvironments/base/info';
7+
import { PythonEnvInfo, PythonEnvKind } from '../../../client/pythonEnvironments/base/info';
8+
import * as envInfo from '../../../client/pythonEnvironments/base/info/env';
89
import * as externalDependencies from '../../../client/pythonEnvironments/common/externalDependencies';
910

1011
suite('Environment Info cache', () => {
1112
let getGlobalPersistentStoreStub: sinon.SinonStub;
1213
let areSameEnvironmentStub: sinon.SinonStub;
13-
let updatedValues: envInfo.PythonEnvInfo[] | undefined;
14+
let updatedValues: PythonEnvInfo[] | undefined;
1415

1516
const allEnvsComplete = () => true;
1617
const envInfoArray = [
1718
{
18-
kind: envInfo.PythonEnvKind.Conda, name: 'my-conda-env', defaultDisplayName: 'env-one',
19+
kind: PythonEnvKind.Conda, name: 'my-conda-env', defaultDisplayName: 'env-one',
1920
},
2021
{
21-
kind: envInfo.PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
22+
kind: PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
2223
},
2324
{
24-
kind: envInfo.PythonEnvKind.Pyenv, name: 'my-pyenv-env', defaultDisplayName: 'env-three',
25+
kind: PythonEnvKind.Pyenv, name: 'my-pyenv-env', defaultDisplayName: 'env-three',
2526
},
26-
] as envInfo.PythonEnvInfo[];
27+
] as PythonEnvInfo[];
2728

2829
setup(() => {
2930
areSameEnvironmentStub = sinon.stub(envInfo, 'areSameEnvironment');
3031
areSameEnvironmentStub.callsFake(
31-
(env1: envInfo.PythonEnvInfo, env2:envInfo.PythonEnvInfo) => env1.name === env2.name,
32+
(env1: PythonEnvInfo, env2:PythonEnvInfo) => env1.name === env2.name,
3233
);
3334

3435
getGlobalPersistentStoreStub = sinon.stub(externalDependencies, 'getGlobalPersistentStore');
3536
getGlobalPersistentStoreStub.returns({
3637
get() { return envInfoArray; },
37-
set(envs: envInfo.PythonEnvInfo[]) {
38+
set(envs: PythonEnvInfo[]) {
3839
updatedValues = envs;
3940
return Promise.resolve();
4041
},
@@ -94,54 +95,63 @@ suite('Environment Info cache', () => {
9495
assert.strictEqual(envs === envInfoArray, false);
9596
});
9697

97-
test('`getEnv` should return an environment that matches all non-undefined properties of its argument', () => {
98-
const env:envInfo.PythonEnvInfo = { name: 'my-venv-env' } as unknown as envInfo.PythonEnvInfo;
98+
test('`filterEnvs` should return environments that match its argument using areSameEnvironmnet', () => {
99+
const env:PythonEnvInfo = { name: 'my-venv-env' } as unknown as PythonEnvInfo;
99100
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
100101

101102
envsCache.initialize();
102103

103-
const result = envsCache.getEnv(env);
104+
const result = envsCache.filterEnvs(env);
104105

105-
assert.deepStrictEqual(result, {
106-
kind: envInfo.PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
107-
});
106+
assert.deepStrictEqual(result, [{
107+
kind: PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
108+
}]);
108109
});
109110

110-
test('`getEnv` should return a deep copy of an environment', () => {
111+
test('`filterEnvs` should return a deep copy of the matched environments', () => {
111112
const envToFind = {
112-
kind: envInfo.PythonEnvKind.System, name: 'my-system-env', defaultDisplayName: 'env-system',
113-
} as unknown as envInfo.PythonEnvInfo;
114-
const env:envInfo.PythonEnvInfo = { name: 'my-system-env' } as unknown as envInfo.PythonEnvInfo;
113+
kind: PythonEnvKind.System, name: 'my-system-env', defaultDisplayName: 'env-system',
114+
} as unknown as PythonEnvInfo;
115+
const env:PythonEnvInfo = { name: 'my-system-env' } as unknown as PythonEnvInfo;
115116
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
116117

117118
envsCache.setAllEnvs([...envInfoArray, envToFind]);
118119

119-
const result = envsCache.getEnv(env)!;
120-
result.name = 'some-other-name';
120+
const result = envsCache.filterEnvs(env)!;
121+
result[0].name = 'some-other-name';
121122

122-
assert.ok(result !== envToFind);
123+
assert.notDeepStrictEqual(result[0], envToFind);
123124
});
124125

125-
test('`getEnv` should return undefined if no environment matches the properties of its argument', () => {
126-
const env:envInfo.PythonEnvInfo = { name: 'my-nonexistent-env' } as unknown as envInfo.PythonEnvInfo;
126+
test('`filterEnvs` should return an empty array if no environment matches the properties of its argument', () => {
127+
const env:PythonEnvInfo = { name: 'my-nonexistent-env' } as unknown as PythonEnvInfo;
127128
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
128129

129130
envsCache.initialize();
130131

131-
const result = envsCache.getEnv(env);
132+
const result = envsCache.filterEnvs(env);
133+
134+
assert.deepStrictEqual(result, []);
135+
});
136+
137+
test('`filterEnvs` should return undefined if the cache hasn\'t been initialized', () => {
138+
const env:PythonEnvInfo = { name: 'my-nonexistent-env' } as unknown as PythonEnvInfo;
139+
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
140+
141+
const result = envsCache.filterEnvs(env);
132142

133143
assert.strictEqual(result, undefined);
134144
});
135145

136146
test('`flush` should write complete environment info objects to persistent storage', async () => {
137147
const otherEnv = {
138-
kind: envInfo.PythonEnvKind.OtherGlobal,
148+
kind: PythonEnvKind.OtherGlobal,
139149
name: 'my-other-env',
140150
defaultDisplayName: 'env-five',
141151
};
142152
const updatedEnvInfoArray = [
143-
otherEnv, { kind: envInfo.PythonEnvKind.System, name: 'my-system-env' },
144-
] as envInfo.PythonEnvInfo[];
153+
otherEnv, { kind: PythonEnvKind.System, name: 'my-system-env' },
154+
] as PythonEnvInfo[];
145155
const expected = [
146156
otherEnv,
147157
];
@@ -155,15 +165,15 @@ suite('Environment Info cache', () => {
155165
});
156166

157167
test('`flush` should not write to persistent storage if there are no environment info objects in-memory', async () => {
158-
const envsCache = new PythonEnvInfoCache((env) => env.kind === envInfo.PythonEnvKind.MacDefault);
168+
const envsCache = new PythonEnvInfoCache((env) => env.kind === PythonEnvKind.MacDefault);
159169

160170
await envsCache.flush();
161171

162172
assert.strictEqual(updatedValues, undefined);
163173
});
164174

165175
test('`flush` should not write to persistent storage if there are no complete environment info objects', async () => {
166-
const envsCache = new PythonEnvInfoCache((env) => env.kind === envInfo.PythonEnvKind.MacDefault);
176+
const envsCache = new PythonEnvInfoCache((env) => env.kind === PythonEnvKind.MacDefault);
167177

168178
envsCache.initialize();
169179
await envsCache.flush();

0 commit comments

Comments
 (0)