@@ -18,12 +18,14 @@ import { IWorkspaceService } from '../../client/common/application/types';
18
18
import {
19
19
defaultInterpreterPathSetting ,
20
20
InterpreterPathService ,
21
+ isGlobalSettingCopiedKey ,
22
+ workspaceFolderKeysForWhichTheCopyIsDone_Key ,
21
23
workspaceKeysForWhichTheCopyIsDone_Key
22
24
} from '../../client/common/interpreterPathService' ;
23
25
import { InterpreterConfigurationScope , IPersistentState , IPersistentStateFactory } from '../../client/common/types' ;
24
26
import { createDeferred , sleep } from '../../client/common/utils/async' ;
25
27
26
- suite ( 'xInterpreter Path Service' , async ( ) => {
28
+ suite ( 'Interpreter Path Service' , async ( ) => {
27
29
let interpreterPathService : InterpreterPathService ;
28
30
let persistentStateFactory : TypeMoq . IMock < IPersistentStateFactory > ;
29
31
let workspaceService : TypeMoq . IMock < IWorkspaceService > ;
@@ -50,8 +52,19 @@ suite('xInterpreter Path Service', async () => {
50
52
sinon . restore ( ) ;
51
53
} ) ;
52
54
53
- test ( 'If the one-off transfer to new storage has not happened yet for the workspace, do it and record the transfer' , async ( ) => {
54
- const update = sinon . stub ( InterpreterPathService . prototype , 'update' ) ;
55
+ test ( 'Ensure execution of method copyOldInterpreterStorageValuesToNew() goes as expected' , async ( ) => {
56
+ const _copyWorkspaceFolderValueToNewStorage = sinon . stub (
57
+ InterpreterPathService . prototype ,
58
+ '_copyWorkspaceFolderValueToNewStorage'
59
+ ) ;
60
+ const _copyWorkspaceValueToNewStorage = sinon . stub (
61
+ InterpreterPathService . prototype ,
62
+ '_copyWorkspaceValueToNewStorage'
63
+ ) ;
64
+ const _moveGlobalSettingValueToNewStorage = sinon . stub (
65
+ InterpreterPathService . prototype ,
66
+ '_moveGlobalSettingValueToNewStorage'
67
+ ) ;
55
68
const workspaceConfig = TypeMoq . Mock . ofType < WorkspaceConfiguration > ( ) ;
56
69
workspaceService . setup ( ( w ) => w . getConfiguration ( 'python' , resource ) ) . returns ( ( ) => workspaceConfig . object ) ;
57
70
workspaceConfig
@@ -65,54 +78,144 @@ suite('xInterpreter Path Service', async () => {
65
78
// tslint:disable-next-line: no-any
66
79
} as any )
67
80
) ;
81
+
82
+ interpreterPathService = new InterpreterPathService ( persistentStateFactory . object , workspaceService . object , [ ] ) ;
83
+ await interpreterPathService . copyOldInterpreterStorageValuesToNew ( resource ) ;
84
+
85
+ assert ( _copyWorkspaceFolderValueToNewStorage . calledWith ( resource , 'workspaceFolderPythonPath' ) ) ;
86
+ assert ( _copyWorkspaceValueToNewStorage . calledWith ( resource , 'workspacePythonPath' ) ) ;
87
+ assert ( _moveGlobalSettingValueToNewStorage . calledWith ( 'globalPythonPath' ) ) ;
88
+ } ) ;
89
+
90
+ test ( 'If the one-off transfer to new storage has not happened yet for the workspace folder, do it and record the transfer' , async ( ) => {
91
+ const update = sinon . stub ( InterpreterPathService . prototype , 'update' ) ;
92
+ const persistentState = TypeMoq . Mock . ofType < IPersistentState < string [ ] > > ( ) ;
93
+ workspaceService . setup ( ( w ) => w . getWorkspaceFolderIdentifier ( resource ) ) . returns ( ( ) => resource . fsPath ) ;
94
+ persistentStateFactory
95
+ . setup ( ( p ) => p . createGlobalPersistentState < string [ ] > ( workspaceFolderKeysForWhichTheCopyIsDone_Key , [ ] ) )
96
+ . returns ( ( ) => persistentState . object ) ;
97
+ persistentState . setup ( ( p ) => p . value ) . returns ( ( ) => [ '...storedWorkspaceFolderKeys' ] ) ;
98
+ persistentState
99
+ . setup ( ( p ) => p . updateValue ( [ resource . fsPath , '...storedWorkspaceFolderKeys' ] ) )
100
+ . returns ( ( ) => Promise . resolve ( ) )
101
+ . verifiable ( TypeMoq . Times . once ( ) ) ;
102
+
103
+ interpreterPathService = new InterpreterPathService ( persistentStateFactory . object , workspaceService . object , [ ] ) ;
104
+ await interpreterPathService . _copyWorkspaceFolderValueToNewStorage ( resource , 'workspaceFolderPythonPath' ) ;
105
+
106
+ assert ( update . calledWith ( resource , ConfigurationTarget . WorkspaceFolder , 'workspaceFolderPythonPath' ) ) ;
107
+ persistentState . verifyAll ( ) ;
108
+ } ) ;
109
+
110
+ test ( 'If the one-off transfer to new storage has already happened for the workspace folder, do not update and simply return' , async ( ) => {
111
+ const update = sinon . stub ( InterpreterPathService . prototype , 'update' ) ;
68
112
const persistentState = TypeMoq . Mock . ofType < IPersistentState < string [ ] > > ( ) ;
69
113
workspaceService . setup ( ( w ) => w . getWorkspaceFolderIdentifier ( resource ) ) . returns ( ( ) => resource . fsPath ) ;
114
+ persistentStateFactory
115
+ . setup ( ( p ) => p . createGlobalPersistentState < string [ ] > ( workspaceFolderKeysForWhichTheCopyIsDone_Key , [ ] ) )
116
+ . returns ( ( ) => persistentState . object ) ;
117
+ persistentState . setup ( ( p ) => p . value ) . returns ( ( ) => [ resource . fsPath , '...storedWorkspaceKeys' ] ) ;
118
+ persistentState . setup ( ( p ) => p . updateValue ( TypeMoq . It . isAny ( ) ) ) . verifiable ( TypeMoq . Times . never ( ) ) ;
119
+
120
+ interpreterPathService = new InterpreterPathService ( persistentStateFactory . object , workspaceService . object , [ ] ) ;
121
+ await interpreterPathService . _copyWorkspaceFolderValueToNewStorage ( resource , 'workspaceFolderPythonPath' ) ;
122
+
123
+ assert ( update . notCalled ) ;
124
+ persistentState . verifyAll ( ) ;
125
+ } ) ;
126
+
127
+ test ( 'If the one-off transfer to new storage has not happened yet for the workspace, do it and record the transfer' , async ( ) => {
128
+ const workspaceFileUri = Uri . parse ( 'path/to/workspaceFile' ) ;
129
+ const expectedWorkspaceKey = 'PATH\\TO\\WORKSPACEFILE' ;
130
+ const update = sinon . stub ( InterpreterPathService . prototype , 'update' ) ;
131
+ const persistentState = TypeMoq . Mock . ofType < IPersistentState < string [ ] > > ( ) ;
132
+ workspaceService . setup ( ( w ) => w . workspaceFile ) . returns ( ( ) => workspaceFileUri ) ;
70
133
persistentStateFactory
71
134
. setup ( ( p ) => p . createGlobalPersistentState < string [ ] > ( workspaceKeysForWhichTheCopyIsDone_Key , [ ] ) )
72
135
. returns ( ( ) => persistentState . object ) ;
73
136
persistentState . setup ( ( p ) => p . value ) . returns ( ( ) => [ '...storedWorkspaceKeys' ] ) ;
74
137
persistentState
75
- . setup ( ( p ) => p . updateValue ( [ resource . fsPath , '...storedWorkspaceKeys' ] ) )
138
+ . setup ( ( p ) => p . updateValue ( [ expectedWorkspaceKey , '...storedWorkspaceKeys' ] ) )
76
139
. returns ( ( ) => Promise . resolve ( ) )
77
140
. verifiable ( TypeMoq . Times . once ( ) ) ;
78
141
79
142
interpreterPathService = new InterpreterPathService ( persistentStateFactory . object , workspaceService . object , [ ] ) ;
80
- await interpreterPathService . copyOldInterpreterStorageValuesToNew ( resource ) ;
143
+ await interpreterPathService . _copyWorkspaceValueToNewStorage ( resource , 'workspacePythonPath' ) ;
81
144
82
- update . calledWith ( resource , ConfigurationTarget . WorkspaceFolder , 'workspaceFolderPythonPath' ) ;
83
- update . calledWith ( resource , ConfigurationTarget . Workspace , 'workspacePythonPath' ) ;
84
- update . calledWith ( undefined , ConfigurationTarget . Global , 'globalPythonPath' ) ;
145
+ assert ( update . calledWith ( resource , ConfigurationTarget . Workspace , 'workspacePythonPath' ) ) ;
85
146
persistentState . verifyAll ( ) ;
86
147
} ) ;
87
148
88
149
test ( 'If the one-off transfer to new storage has already happened for the workspace, do not update and simply return' , async ( ) => {
150
+ const workspaceFileUri = Uri . parse ( 'path/to/workspaceFile' ) ;
151
+ const expectedWorkspaceKey = 'PATH\\TO\\WORKSPACEFILE' ;
89
152
const update = sinon . stub ( InterpreterPathService . prototype , 'update' ) ;
90
- const workspaceConfig = TypeMoq . Mock . ofType < WorkspaceConfiguration > ( ) ;
91
- workspaceService . setup ( ( w ) => w . getConfiguration ( 'python' , resource ) ) . returns ( ( ) => workspaceConfig . object ) ;
92
- workspaceConfig
93
- . setup ( ( w ) => w . inspect < string > ( 'pythonPath' ) )
94
- . returns (
95
- ( ) =>
96
- ( {
97
- globalValue : 'globalPythonPath' ,
98
- workspaceFolderValue : 'workspaceFolderPythonPath' ,
99
- workspaceValue : 'workspacePythonPath'
100
- // tslint:disable-next-line: no-any
101
- } as any )
102
- ) ;
103
153
const persistentState = TypeMoq . Mock . ofType < IPersistentState < string [ ] > > ( ) ;
104
- workspaceService . setup ( ( w ) => w . getWorkspaceFolderIdentifier ( resource ) ) . returns ( ( ) => resource . fsPath ) ;
154
+ workspaceService . setup ( ( w ) => w . workspaceFile ) . returns ( ( ) => workspaceFileUri ) ;
105
155
persistentStateFactory
106
156
. setup ( ( p ) => p . createGlobalPersistentState < string [ ] > ( workspaceKeysForWhichTheCopyIsDone_Key , [ ] ) )
107
157
. returns ( ( ) => persistentState . object ) ;
108
- persistentState . setup ( ( p ) => p . value ) . returns ( ( ) => [ resource . fsPath , '...storedWorkspaceKeys' ] ) ;
109
- persistentState
110
- . setup ( ( p ) => p . updateValue ( TypeMoq . It . isAny ( ) ) )
158
+ persistentState . setup ( ( p ) => p . value ) . returns ( ( ) => [ expectedWorkspaceKey , '...storedWorkspaceKeys' ] ) ;
159
+ persistentState . setup ( ( p ) => p . updateValue ( TypeMoq . It . isAny ( ) ) ) . verifiable ( TypeMoq . Times . never ( ) ) ;
160
+
161
+ interpreterPathService = new InterpreterPathService ( persistentStateFactory . object , workspaceService . object , [ ] ) ;
162
+ await interpreterPathService . _copyWorkspaceValueToNewStorage ( resource , 'workspacePythonPath' ) ;
163
+
164
+ assert ( update . notCalled ) ;
165
+ persistentState . verifyAll ( ) ;
166
+ } ) ;
167
+
168
+ test ( 'Do not update workspace settings and if a folder is directly opened' , async ( ) => {
169
+ const update = sinon . stub ( InterpreterPathService . prototype , 'update' ) ;
170
+ const persistentState = TypeMoq . Mock . ofType < IPersistentState < string [ ] > > ( ) ;
171
+ workspaceService . setup ( ( w ) => w . workspaceFile ) . returns ( ( ) => undefined ) ;
172
+ persistentStateFactory
173
+ . setup ( ( p ) => p . createGlobalPersistentState < string [ ] > ( workspaceKeysForWhichTheCopyIsDone_Key , [ ] ) )
174
+ . returns ( ( ) => persistentState . object ) ;
175
+ persistentState . setup ( ( p ) => p . value ) . verifiable ( TypeMoq . Times . never ( ) ) ;
176
+ persistentState . setup ( ( p ) => p . updateValue ( TypeMoq . It . isAny ( ) ) ) . verifiable ( TypeMoq . Times . never ( ) ) ;
177
+
178
+ interpreterPathService = new InterpreterPathService ( persistentStateFactory . object , workspaceService . object , [ ] ) ;
179
+ await interpreterPathService . _copyWorkspaceValueToNewStorage ( resource , 'workspacePythonPath' ) ;
180
+
181
+ assert ( update . notCalled ) ;
182
+ persistentState . verifyAll ( ) ;
183
+ } ) ;
184
+
185
+ test ( 'If the one-off transfer to new storage has not happened yet for the user setting, do it, record the transfer and remove the original user setting' , async ( ) => {
186
+ const update = sinon . stub ( InterpreterPathService . prototype , 'update' ) ;
187
+ const persistentState = TypeMoq . Mock . ofType < IPersistentState < boolean > > ( ) ;
188
+ persistentStateFactory
189
+ . setup ( ( p ) => p . createGlobalPersistentState < boolean > ( isGlobalSettingCopiedKey , false ) )
190
+ . returns ( ( ) => persistentState . object ) ;
191
+ persistentState . setup ( ( p ) => p . value ) . returns ( ( ) => false ) ;
192
+ persistentState . setup ( ( p ) => p . updateValue ( true ) ) . verifiable ( TypeMoq . Times . once ( ) ) ;
193
+ const workspaceConfig = TypeMoq . Mock . ofType < WorkspaceConfiguration > ( ) ;
194
+ workspaceService . setup ( ( w ) => w . getConfiguration ( 'python' ) ) . returns ( ( ) => workspaceConfig . object ) ;
195
+ workspaceConfig
196
+ . setup ( ( w ) => w . update ( 'pythonPath' , undefined , ConfigurationTarget . Global ) )
111
197
. returns ( ( ) => Promise . resolve ( ) )
112
- . verifiable ( TypeMoq . Times . never ( ) ) ;
198
+ . verifiable ( TypeMoq . Times . once ( ) ) ;
113
199
114
200
interpreterPathService = new InterpreterPathService ( persistentStateFactory . object , workspaceService . object , [ ] ) ;
115
- await interpreterPathService . copyOldInterpreterStorageValuesToNew ( resource ) ;
201
+ await interpreterPathService . _moveGlobalSettingValueToNewStorage ( 'globalPythonPath' ) ;
202
+
203
+ assert ( update . calledWith ( undefined , ConfigurationTarget . Global , 'globalPythonPath' ) ) ;
204
+ persistentState . verifyAll ( ) ;
205
+ workspaceConfig . verifyAll ( ) ;
206
+ } ) ;
207
+
208
+ test ( 'If the one-off transfer to new storage has already happened for the user setting, do not update and simply return' , async ( ) => {
209
+ const update = sinon . stub ( InterpreterPathService . prototype , 'update' ) ;
210
+ const persistentState = TypeMoq . Mock . ofType < IPersistentState < boolean > > ( ) ;
211
+ persistentStateFactory
212
+ . setup ( ( p ) => p . createGlobalPersistentState < boolean > ( isGlobalSettingCopiedKey , false ) )
213
+ . returns ( ( ) => persistentState . object ) ;
214
+ persistentState . setup ( ( p ) => p . value ) . returns ( ( ) => true ) ;
215
+ persistentState . setup ( ( p ) => p . updateValue ( TypeMoq . It . isAny ( ) ) ) . verifiable ( TypeMoq . Times . never ( ) ) ;
216
+
217
+ interpreterPathService = new InterpreterPathService ( persistentStateFactory . object , workspaceService . object , [ ] ) ;
218
+ await interpreterPathService . _moveGlobalSettingValueToNewStorage ( 'globalPythonPath' ) ;
116
219
117
220
assert ( update . notCalled ) ;
118
221
persistentState . verifyAll ( ) ;
0 commit comments