4
4
import * as assert from 'assert' ;
5
5
import * as sinon from 'sinon' ;
6
6
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' ;
8
9
import * as externalDependencies from '../../../client/pythonEnvironments/common/externalDependencies' ;
9
10
10
11
suite ( 'Environment Info cache' , ( ) => {
11
12
let getGlobalPersistentStoreStub : sinon . SinonStub ;
12
13
let areSameEnvironmentStub : sinon . SinonStub ;
13
- let updatedValues : envInfo . PythonEnvInfo [ ] | undefined ;
14
+ let updatedValues : PythonEnvInfo [ ] | undefined ;
14
15
15
16
const allEnvsComplete = ( ) => true ;
16
17
const envInfoArray = [
17
18
{
18
- kind : envInfo . PythonEnvKind . Conda , name : 'my-conda-env' , defaultDisplayName : 'env-one' ,
19
+ kind : PythonEnvKind . Conda , name : 'my-conda-env' , defaultDisplayName : 'env-one' ,
19
20
} ,
20
21
{
21
- kind : envInfo . PythonEnvKind . Venv , name : 'my-venv-env' , defaultDisplayName : 'env-two' ,
22
+ kind : PythonEnvKind . Venv , name : 'my-venv-env' , defaultDisplayName : 'env-two' ,
22
23
} ,
23
24
{
24
- kind : envInfo . PythonEnvKind . Pyenv , name : 'my-pyenv-env' , defaultDisplayName : 'env-three' ,
25
+ kind : PythonEnvKind . Pyenv , name : 'my-pyenv-env' , defaultDisplayName : 'env-three' ,
25
26
} ,
26
- ] as envInfo . PythonEnvInfo [ ] ;
27
+ ] as PythonEnvInfo [ ] ;
27
28
28
29
setup ( ( ) => {
29
30
areSameEnvironmentStub = sinon . stub ( envInfo , 'areSameEnvironment' ) ;
30
31
areSameEnvironmentStub . callsFake (
31
- ( env1 : envInfo . PythonEnvInfo , env2 :envInfo . PythonEnvInfo ) => env1 . name === env2 . name ,
32
+ ( env1 : PythonEnvInfo , env2 :PythonEnvInfo ) => env1 . name === env2 . name ,
32
33
) ;
33
34
34
35
getGlobalPersistentStoreStub = sinon . stub ( externalDependencies , 'getGlobalPersistentStore' ) ;
35
36
getGlobalPersistentStoreStub . returns ( {
36
37
get ( ) { return envInfoArray ; } ,
37
- set ( envs : envInfo . PythonEnvInfo [ ] ) {
38
+ set ( envs : PythonEnvInfo [ ] ) {
38
39
updatedValues = envs ;
39
40
return Promise . resolve ( ) ;
40
41
} ,
@@ -94,54 +95,63 @@ suite('Environment Info cache', () => {
94
95
assert . strictEqual ( envs === envInfoArray , false ) ;
95
96
} ) ;
96
97
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 ;
99
100
const envsCache = new PythonEnvInfoCache ( allEnvsComplete ) ;
100
101
101
102
envsCache . initialize ( ) ;
102
103
103
- const result = envsCache . getEnv ( env ) ;
104
+ const result = envsCache . filterEnvs ( env ) ;
104
105
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
+ } ] ) ;
108
109
} ) ;
109
110
110
- test ( '`getEnv ` should return a deep copy of an environment ' , ( ) => {
111
+ test ( '`filterEnvs ` should return a deep copy of the matched environments ' , ( ) => {
111
112
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 ;
115
116
const envsCache = new PythonEnvInfoCache ( allEnvsComplete ) ;
116
117
117
118
envsCache . setAllEnvs ( [ ...envInfoArray , envToFind ] ) ;
118
119
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' ;
121
122
122
- assert . ok ( result !== envToFind ) ;
123
+ assert . notDeepStrictEqual ( result [ 0 ] , envToFind ) ;
123
124
} ) ;
124
125
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 ;
127
128
const envsCache = new PythonEnvInfoCache ( allEnvsComplete ) ;
128
129
129
130
envsCache . initialize ( ) ;
130
131
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 ) ;
132
142
133
143
assert . strictEqual ( result , undefined ) ;
134
144
} ) ;
135
145
136
146
test ( '`flush` should write complete environment info objects to persistent storage' , async ( ) => {
137
147
const otherEnv = {
138
- kind : envInfo . PythonEnvKind . OtherGlobal ,
148
+ kind : PythonEnvKind . OtherGlobal ,
139
149
name : 'my-other-env' ,
140
150
defaultDisplayName : 'env-five' ,
141
151
} ;
142
152
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 [ ] ;
145
155
const expected = [
146
156
otherEnv ,
147
157
] ;
@@ -155,15 +165,15 @@ suite('Environment Info cache', () => {
155
165
} ) ;
156
166
157
167
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 ) ;
159
169
160
170
await envsCache . flush ( ) ;
161
171
162
172
assert . strictEqual ( updatedValues , undefined ) ;
163
173
} ) ;
164
174
165
175
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 ) ;
167
177
168
178
envsCache . initialize ( ) ;
169
179
await envsCache . flush ( ) ;
0 commit comments