Skip to content

Commit d36d75d

Browse files
committed
Return deep copies
1 parent 93075c0 commit d36d75d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/client/pythonEnvironments/base/envsCache.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class PythonEnvInfoCache implements IEnvsCache {
7373
}
7474

7575
public getAllEnvs(): PythonEnvInfo[] | undefined {
76-
return this.envsList;
76+
return cloneDeep(this.envsList);
7777
}
7878

7979
public setAllEnvs(envs: PythonEnvInfo[]): void {
@@ -83,11 +83,17 @@ export class PythonEnvInfoCache implements IEnvsCache {
8383
public getEnv(env: PythonEnvInfo | string): PythonEnvInfo | undefined {
8484
// This will have to be updated when areSameEnvironment's signature changes.
8585
// See https://github.com/microsoft/vscode-python/pull/14026/files#r493720817.
86-
return this.envsList?.find((info) => areSameEnvironment(
86+
const result = this.envsList?.find((info) => areSameEnvironment(
8787
info as unknown as PartialPythonEnvironment,
8888
env as unknown as PartialPythonEnvironment,
8989
{} as unknown as IFileSystem,
9090
));
91+
92+
if (result) {
93+
return cloneDeep(result);
94+
}
95+
96+
return undefined;
9197
}
9298

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

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ suite('Environment Info cache', () => {
6666
assert.strictEqual(result, undefined);
6767
});
6868

69+
test('`getAllEnvs` should return a deep copy of the environments currently in memory', () => {
70+
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
71+
72+
envsCache.initialize();
73+
const envs = envsCache.getAllEnvs()!;
74+
75+
envs[0].name = 'some-other-name';
76+
77+
assert.ok(envs[0] !== envInfoArray[0]);
78+
});
79+
6980
test('`getAllEnvs` should return undefined if nothing has been set', () => {
7081
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
7182

@@ -85,8 +96,8 @@ suite('Environment Info cache', () => {
8596
});
8697

8798
test('`getEnv` should return an environment that matches all non-undefined properties of its argument', () => {
88-
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
8999
const env:PythonEnvInfo = { name: 'my-venv-env' } as unknown as PythonEnvInfo;
100+
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
90101

91102
envsCache.initialize();
92103

@@ -97,9 +108,24 @@ suite('Environment Info cache', () => {
97108
});
98109
});
99110

100-
test('`getEnv` should return undefined if no environment matches the properties of its argument', () => {
111+
test('`getEnv` should return a deep copy of an environment', () => {
112+
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;
101116
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
117+
118+
envsCache.setAllEnvs([...envInfoArray, envToFind]);
119+
120+
const result = envsCache.getEnv(env)!;
121+
result.name = 'some-other-name';
122+
123+
assert.ok(result !== envToFind);
124+
});
125+
126+
test('`getEnv` should return undefined if no environment matches the properties of its argument', () => {
102127
const env:PythonEnvInfo = { name: 'my-nonexistent-env' } as unknown as PythonEnvInfo;
128+
const envsCache = new PythonEnvInfoCache(allEnvsComplete);
103129

104130
envsCache.initialize();
105131

0 commit comments

Comments
 (0)