Skip to content

Commit 87efe9b

Browse files
committed
Use areSameEnvironment in getEnv
1 parent cd7a19c commit 87efe9b

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/client/pythonEnvironments/base/envsCache.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Licensed under the MIT License.
33

44
import { cloneDeep } from 'lodash';
5+
import { IFileSystem } from '../../common/platform/types';
56
import { IPersistentState } from '../../common/types';
67
import { createGlobalPersistentStore } from '../common/externalDependencies';
8+
import { areSameEnvironment, PartialPythonEnvironment } from '../info';
79
import { PythonEnvInfo } from './info';
810

911
/**
@@ -32,12 +34,13 @@ export interface IEnvsCache {
3234
/**
3335
* Return a specific environmnent info object.
3436
*
35-
* @param env The environment info data that will be used to look for an environment info object in the cache.
36-
* This object may contain incomplete environment info.
37+
* @param env The environment info data that will be used to look for
38+
* an environment info object in the cache, or a unique environment key.
39+
* If passing an environment info object, it may contain incomplete environment info.
3740
* @return The environment info object that matches all non-undefined keys from the `env` param,
3841
* `undefined` otherwise.
3942
*/
40-
getEnv(env: Partial<PythonEnvInfo>): PythonEnvInfo | undefined;
43+
getEnv(env: PythonEnvInfo | string): PythonEnvInfo | undefined;
4144

4245
/**
4346
* Writes the content of the in-memory cache to persistent storage.
@@ -67,18 +70,14 @@ export class PythonEnvInfoCache implements IEnvsCache {
6770
this.envsList = cloneDeep(envs);
6871
}
6972

70-
public getEnv(env: Partial<PythonEnvInfo>): PythonEnvInfo | undefined {
71-
// Retrieve all keys with non-undefined values.
72-
type EnvParamKeys = keyof typeof env;
73-
const keys = (Object.keys(env) as unknown as EnvParamKeys[]).filter((key) => env[key] !== undefined);
74-
75-
// Return the first object where the values match env's.
76-
return this.envsList?.find((info) => {
77-
// Check if there is any mismatch between the values of the in-memory info and env.
78-
const mismatch = keys.some((key) => info[key] !== env[key]);
79-
80-
return !mismatch;
81-
});
73+
public getEnv(env: PythonEnvInfo | string): PythonEnvInfo | undefined {
74+
// This will have to be updated when areSameEnvironment's signature changes.
75+
// See https://github.com/microsoft/vscode-python/pull/14026/files#r493720817.
76+
return this.envsList?.find((info) => areSameEnvironment(
77+
info as unknown as PartialPythonEnvironment,
78+
env as unknown as PartialPythonEnvironment,
79+
{} as unknown as IFileSystem,
80+
));
8281
}
8382

8483
public async flush(): Promise<void> {

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import * as assert from 'assert';
55
import * as sinon from 'sinon';
66
import { CompleteEnvInfoFunction, PythonEnvInfoCache } from '../../../client/pythonEnvironments/base/envsCache';
77
import { PythonEnvInfo, PythonEnvKind } from '../../../client/pythonEnvironments/base/info';
8-
import * as externalDeps from '../../../client/pythonEnvironments/common/externalDependencies';
8+
import * as externalDependencies from '../../../client/pythonEnvironments/common/externalDependencies';
9+
import * as envInfo from '../../../client/pythonEnvironments/info';
910

1011
suite('Environment Info cache', () => {
1112
let createGlobalPersistentStoreStub: sinon.SinonStub;
13+
let areSameEnvironmentStub: sinon.SinonStub;
1214
let updatedValues: PythonEnvInfo[] | undefined;
1315

1416
const allEnvsComplete: CompleteEnvInfoFunction = () => true;
@@ -25,7 +27,12 @@ suite('Environment Info cache', () => {
2527
] as PythonEnvInfo[];
2628

2729
setup(() => {
28-
createGlobalPersistentStoreStub = sinon.stub(externalDeps, 'createGlobalPersistentStore');
30+
areSameEnvironmentStub = sinon.stub(envInfo, 'areSameEnvironment');
31+
areSameEnvironmentStub.callsFake(
32+
(env1: PythonEnvInfo, env2:PythonEnvInfo) => env1.name === env2.name,
33+
);
34+
35+
createGlobalPersistentStoreStub = sinon.stub(externalDependencies, 'createGlobalPersistentStore');
2936
createGlobalPersistentStoreStub.returns({
3037
value: envInfoArray,
3138
updateValue: async (envs: PythonEnvInfo[]) => {
@@ -37,6 +44,7 @@ suite('Environment Info cache', () => {
3744

3845
teardown(() => {
3946
createGlobalPersistentStoreStub.restore();
47+
areSameEnvironmentStub.restore();
4048
updatedValues = undefined;
4149
});
4250

@@ -78,9 +86,11 @@ suite('Environment Info cache', () => {
7886

7987
test('`getEnv` should return an environment that matches all non-undefined properties of its argument', () => {
8088
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
89+
const env:PythonEnvInfo = { name: 'my-venv-env' } as unknown as PythonEnvInfo;
90+
8191
envsCache.initialize();
8292

83-
const result = envsCache.getEnv({ name: 'my-venv-env' });
93+
const result = envsCache.getEnv(env);
8494

8595
assert.deepStrictEqual(result, {
8696
id: 'someid2', kind: PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
@@ -89,9 +99,11 @@ suite('Environment Info cache', () => {
8999

90100
test('`getEnv` should return undefined if no environment matches the properties of its argument', () => {
91101
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
102+
const env:PythonEnvInfo = { name: 'my-nonexistent-env' } as unknown as PythonEnvInfo;
103+
92104
envsCache.initialize();
93105

94-
const result = envsCache.getEnv({ name: 'my-nonexistent-env' });
106+
const result = envsCache.getEnv(env);
95107

96108
assert.strictEqual(result, undefined);
97109
});

0 commit comments

Comments
 (0)