Skip to content

Commit 97d5251

Browse files
committed
Use correct areSameEnvironment + fix stub
1 parent d7e9f7d commit 97d5251

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

src/client/pythonEnvironments/base/envsCache.ts

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

44
import { cloneDeep } from 'lodash';
5-
import { IFileSystem } from '../../common/platform/types';
65
import { getGlobalPersistentStore, IPersistentStore } from '../common/externalDependencies';
7-
import { areSameEnvironment, PartialPythonEnvironment } from '../info';
8-
import { PythonEnvInfo } from './info';
6+
import { areSameEnvironment, PythonEnvInfo } from './info';
97

108
/**
119
* Represents the environment info cache to be used by the cache locator.
@@ -82,11 +80,7 @@ export class PythonEnvInfoCache implements IEnvsCache {
8280
public getEnv(env: PythonEnvInfo | string): PythonEnvInfo | undefined {
8381
// This will have to be updated when areSameEnvironment's signature changes.
8482
// See https://github.com/microsoft/vscode-python/pull/14026/files#r493720817.
85-
const result = this.envsList?.find((info) => areSameEnvironment(
86-
info as unknown as PartialPythonEnvironment,
87-
env as unknown as PartialPythonEnvironment,
88-
{} as unknown as IFileSystem,
89-
));
83+
const result = this.envsList?.find((info) => areSameEnvironment(info, env));
9084

9185
if (result) {
9286
return cloneDeep(result);

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

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

1110
suite('Environment Info cache', () => {
1211
let getGlobalPersistentStoreStub: sinon.SinonStub;
1312
let areSameEnvironmentStub: sinon.SinonStub;
14-
let updatedValues: PythonEnvInfo[] | undefined;
13+
let updatedValues: envInfo.PythonEnvInfo[] | undefined;
1514

1615
const allEnvsComplete = () => true;
1716
const envInfoArray = [
1817
{
19-
kind: PythonEnvKind.Conda, name: 'my-conda-env', defaultDisplayName: 'env-one',
18+
kind: envInfo.PythonEnvKind.Conda, name: 'my-conda-env', defaultDisplayName: 'env-one',
2019
},
2120
{
22-
kind: PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
21+
kind: envInfo.PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
2322
},
2423
{
25-
kind: PythonEnvKind.Pyenv, name: 'my-pyenv-env', defaultDisplayName: 'env-three',
24+
kind: envInfo.PythonEnvKind.Pyenv, name: 'my-pyenv-env', defaultDisplayName: 'env-three',
2625
},
27-
] as PythonEnvInfo[];
26+
] as envInfo.PythonEnvInfo[];
2827

2928
setup(() => {
3029
areSameEnvironmentStub = sinon.stub(envInfo, 'areSameEnvironment');
3130
areSameEnvironmentStub.callsFake(
32-
(env1: PythonEnvInfo, env2:PythonEnvInfo) => env1.name === env2.name,
31+
(env1: envInfo.PythonEnvInfo, env2:envInfo.PythonEnvInfo) => env1.name === env2.name,
3332
);
3433

3534
getGlobalPersistentStoreStub = sinon.stub(externalDependencies, 'getGlobalPersistentStore');
3635
getGlobalPersistentStoreStub.returns({
37-
value: envInfoArray,
38-
updateValue: async (envs: PythonEnvInfo[]) => {
36+
get() { return envInfoArray; },
37+
set(envs: envInfo.PythonEnvInfo[]) {
3938
updatedValues = envs;
4039
return Promise.resolve();
4140
},
@@ -59,7 +58,7 @@ suite('Environment Info cache', () => {
5958
test('The in-memory env info array is undefined if there is no value in persistent storage when initializing the cache', () => {
6059
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
6160

62-
getGlobalPersistentStoreStub.returns({ value: undefined });
61+
getGlobalPersistentStoreStub.returns({ get() { return undefined; } });
6362
envsCache.initialize();
6463
const result = envsCache.getAllEnvs();
6564

@@ -96,23 +95,23 @@ suite('Environment Info cache', () => {
9695
});
9796

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

102101
envsCache.initialize();
103102

104103
const result = envsCache.getEnv(env);
105104

106105
assert.deepStrictEqual(result, {
107-
kind: PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
106+
kind: envInfo.PythonEnvKind.Venv, name: 'my-venv-env', defaultDisplayName: 'env-two',
108107
});
109108
});
110109

111110
test('`getEnv` should return a deep copy of an environment', () => {
112111
const envToFind = {
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;
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;
116115
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
117116

118117
envsCache.setAllEnvs([...envInfoArray, envToFind]);
@@ -124,7 +123,7 @@ suite('Environment Info cache', () => {
124123
});
125124

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

130129
envsCache.initialize();
@@ -136,13 +135,13 @@ suite('Environment Info cache', () => {
136135

137136
test('`flush` should write complete environment info objects to persistent storage', async () => {
138137
const otherEnv = {
139-
kind: PythonEnvKind.OtherGlobal,
138+
kind: envInfo.PythonEnvKind.OtherGlobal,
140139
name: 'my-other-env',
141140
defaultDisplayName: 'env-five',
142141
};
143142
const updatedEnvInfoArray = [
144-
otherEnv, { kind: PythonEnvKind.System, name: 'my-system-env' },
145-
] as PythonEnvInfo[];
143+
otherEnv, { kind: envInfo.PythonEnvKind.System, name: 'my-system-env' },
144+
] as envInfo.PythonEnvInfo[];
146145
const expected = [
147146
otherEnv,
148147
];
@@ -156,15 +155,15 @@ suite('Environment Info cache', () => {
156155
});
157156

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

161160
await envsCache.flush();
162161

163162
assert.strictEqual(updatedValues, undefined);
164163
});
165164

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

169168
envsCache.initialize();
170169
await envsCache.flush();

0 commit comments

Comments
 (0)