forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvenvUtils.getVenvForWorkspace.unit.test.ts
More file actions
293 lines (232 loc) · 12.2 KB
/
venvUtils.getVenvForWorkspace.unit.test.ts
File metadata and controls
293 lines (232 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import * as assert from 'assert';
import * as fse from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import * as persistentState from '../../../common/persistentState';
import { getVenvForWorkspace, VENV_WORKSPACE_KEY } from '../../../managers/builtin/venvUtils';
/**
* Helper that replicates the VIRTUAL_ENV subpath check from getVenvForWorkspace
* using a specific path module, allowing cross-platform verification.
*/
function isVenvInsideWorkspace(
fsPath: string,
virtualEnv: string,
pathModule: typeof path.posix | typeof path.win32,
): boolean {
const rel = pathModule.relative(fsPath, virtualEnv);
return rel === '' || (!rel.startsWith('..') && !pathModule.isAbsolute(rel));
}
suite('VIRTUAL_ENV subpath check - cross-platform', () => {
suite('POSIX paths', () => {
const p = path.posix;
test('venv inside workspace should match', () => {
assert.strictEqual(isVenvInsideWorkspace('/proj/app', '/proj/app/.venv', p), true);
});
test('venv deeply nested inside workspace should match', () => {
assert.strictEqual(isVenvInsideWorkspace('/proj/app', '/proj/app/sub/dir/.venv', p), true);
});
test('venv equal to workspace should match', () => {
assert.strictEqual(isVenvInsideWorkspace('/proj/app', '/proj/app', p), true);
});
test('sibling with shared prefix should NOT match', () => {
assert.strictEqual(isVenvInsideWorkspace('/proj/app', '/proj/app2/.venv', p), false);
});
test('venv in completely different location should NOT match', () => {
assert.strictEqual(isVenvInsideWorkspace('/proj/app', '/other/place/.venv', p), false);
});
test('parent directory should NOT match', () => {
assert.strictEqual(isVenvInsideWorkspace('/proj/app', '/proj/.venv', p), false);
});
});
suite('Windows paths', () => {
const p = path.win32;
test('venv inside workspace should match', () => {
assert.strictEqual(isVenvInsideWorkspace('C:\\proj\\app', 'C:\\proj\\app\\.venv', p), true);
});
test('venv deeply nested inside workspace should match', () => {
assert.strictEqual(isVenvInsideWorkspace('C:\\proj\\app', 'C:\\proj\\app\\sub\\dir\\.venv', p), true);
});
test('venv equal to workspace should match', () => {
assert.strictEqual(isVenvInsideWorkspace('C:\\proj\\app', 'C:\\proj\\app', p), true);
});
test('sibling with shared prefix should NOT match', () => {
assert.strictEqual(isVenvInsideWorkspace('C:\\proj\\app', 'C:\\proj\\app2\\.venv', p), false);
});
test('venv on a different drive should NOT match', () => {
assert.strictEqual(isVenvInsideWorkspace('C:\\proj\\app', 'D:\\proj\\app\\.venv', p), false);
});
test('parent directory should NOT match', () => {
assert.strictEqual(isVenvInsideWorkspace('C:\\proj\\app', 'C:\\proj\\.venv', p), false);
});
test('case-insensitive drive letters should match', () => {
// path.win32.relative handles case-insensitive drive letters
assert.strictEqual(isVenvInsideWorkspace('c:\\proj\\app', 'C:\\proj\\app\\.venv', p), true);
});
test('UNC path inside workspace should match', () => {
assert.strictEqual(
isVenvInsideWorkspace('\\\\server\\share\\proj', '\\\\server\\share\\proj\\.venv', p),
true,
);
});
test('UNC path sibling should NOT match', () => {
assert.strictEqual(
isVenvInsideWorkspace('\\\\server\\share\\proj', '\\\\server\\share\\proj2\\.venv', p),
false,
);
});
});
});
suite('getVenvForWorkspace', () => {
let mockState: {
get: sinon.SinonStub;
set: sinon.SinonStub;
clear: sinon.SinonStub;
};
let getWorkspacePersistentStateStub: sinon.SinonStub;
let originalVirtualEnv: string | undefined;
let tmpDir: string;
setup(async () => {
originalVirtualEnv = process.env.VIRTUAL_ENV;
delete process.env.VIRTUAL_ENV;
tmpDir = path.join(os.tmpdir(), `venv-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
await fse.ensureDir(tmpDir);
mockState = {
get: sinon.stub(),
set: sinon.stub(),
clear: sinon.stub(),
};
getWorkspacePersistentStateStub = sinon.stub(persistentState, 'getWorkspacePersistentState');
getWorkspacePersistentStateStub.resolves(mockState);
});
teardown(async () => {
if (originalVirtualEnv !== undefined) {
process.env.VIRTUAL_ENV = originalVirtualEnv;
} else {
delete process.env.VIRTUAL_ENV;
}
sinon.restore();
await fse.remove(tmpDir);
});
test('should return persisted selection when available', async () => {
const workspacePath = path.join(tmpDir, 'projectA');
const venvPath = path.join(workspacePath, '.venv');
await fse.ensureDir(venvPath);
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves({ [workspacePath]: venvPath });
const result = await getVenvForWorkspace(workspacePath);
assert.strictEqual(result, venvPath, 'Should return persisted venv path');
});
test('should return persisted selection even when VIRTUAL_ENV is set', async () => {
const projectA = path.join(tmpDir, 'projectA');
const projectB = path.join(tmpDir, 'projectB');
const persistedVenv = path.join(projectA, '.venv');
const otherVenv = path.join(projectB, '.venv');
await fse.ensureDir(persistedVenv);
await fse.ensureDir(otherVenv);
process.env.VIRTUAL_ENV = otherVenv;
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves({ [projectA]: persistedVenv });
const result = await getVenvForWorkspace(projectA);
assert.strictEqual(result, persistedVenv, 'Persisted selection should take priority over VIRTUAL_ENV');
});
test('should fall back to VIRTUAL_ENV when no persisted selection and venv is inside workspace', async () => {
const workspacePath = path.join(tmpDir, 'projectA');
const virtualEnvPath = path.join(workspacePath, '.venv');
await fse.ensureDir(virtualEnvPath);
process.env.VIRTUAL_ENV = virtualEnvPath;
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves(undefined);
const result = await getVenvForWorkspace(workspacePath);
assert.strictEqual(result, virtualEnvPath, 'Should use VIRTUAL_ENV when it is inside the workspace');
});
test('should NOT use VIRTUAL_ENV when it points to a different project', async () => {
const projectA = path.join(tmpDir, 'projectA');
const projectB = path.join(tmpDir, 'projectB');
const otherVenv = path.join(projectB, '.venv');
await fse.ensureDir(projectA);
await fse.ensureDir(otherVenv);
process.env.VIRTUAL_ENV = otherVenv;
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves(undefined);
const result = await getVenvForWorkspace(projectA);
assert.strictEqual(result, undefined, 'Should NOT use VIRTUAL_ENV from a different project');
});
test('should clear stale persisted path when venv no longer exists', async () => {
const workspacePath = path.join(tmpDir, 'projectA');
const staleVenv = path.join(workspacePath, '.venv-old');
await fse.ensureDir(workspacePath);
// Note: staleVenv directory does NOT exist on disk
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves({ [workspacePath]: staleVenv });
const result = await getVenvForWorkspace(workspacePath);
assert.strictEqual(result, undefined, 'Should return undefined for stale path');
assert.ok(mockState.set.called, 'Should clear the stale entry from persistent state');
const setArgs = mockState.set.firstCall.args;
assert.strictEqual(setArgs[0], VENV_WORKSPACE_KEY, 'Should update the correct key');
assert.strictEqual(setArgs[1][workspacePath], undefined, 'Should have removed the stale workspace entry');
});
test('should return undefined when no persisted selection and no VIRTUAL_ENV', async () => {
const workspacePath = path.join(tmpDir, 'projectA');
await fse.ensureDir(workspacePath);
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves(undefined);
const result = await getVenvForWorkspace(workspacePath);
assert.strictEqual(result, undefined, 'Should return undefined when nothing is available');
});
test('should return undefined when persisted data has no entry for this workspace', async () => {
const projectA = path.join(tmpDir, 'projectA');
const projectB = path.join(tmpDir, 'projectB');
await fse.ensureDir(projectA);
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves({ [projectB]: '/some/path' });
const result = await getVenvForWorkspace(projectA);
assert.strictEqual(result, undefined, 'Should return undefined when no entry for this workspace');
});
test('should fall back to VIRTUAL_ENV when data access throws inside try block', async () => {
const workspacePath = path.join(tmpDir, 'projectA');
const virtualEnvPath = path.join(workspacePath, '.venv');
await fse.ensureDir(virtualEnvPath);
process.env.VIRTUAL_ENV = virtualEnvPath;
// Return data object with a getter that throws when accessing the workspace key
const badData: Record<string, string> = {};
Object.defineProperty(badData, workspacePath, {
get() {
throw new Error('corrupted data');
},
enumerable: true,
configurable: true,
});
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves(badData);
const result = await getVenvForWorkspace(workspacePath);
assert.strictEqual(result, virtualEnvPath, 'Should fall back to VIRTUAL_ENV when try block throws');
});
test('should not clear state when no envPath exists for the workspace key', async () => {
const workspacePath = path.join(tmpDir, 'projectA');
await fse.ensureDir(workspacePath);
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves({ other: '/some/path' });
await getVenvForWorkspace(workspacePath);
assert.ok(!mockState.set.called, 'Should not call set when there is no stale entry to clear');
});
test('should fall back to VIRTUAL_ENV when state.get rejects', async () => {
const workspacePath = path.join(tmpDir, 'projectA');
const virtualEnvPath = path.join(workspacePath, '.venv');
await fse.ensureDir(virtualEnvPath);
process.env.VIRTUAL_ENV = virtualEnvPath;
mockState.get.withArgs(VENV_WORKSPACE_KEY).rejects(new Error('storage corrupted'));
const result = await getVenvForWorkspace(workspacePath);
assert.strictEqual(result, virtualEnvPath, 'Should fall back to VIRTUAL_ENV when state.get rejects');
});
test('should fall back to VIRTUAL_ENV when getWorkspacePersistentState rejects', async () => {
const workspacePath = path.join(tmpDir, 'projectA');
const virtualEnvPath = path.join(workspacePath, '.venv');
await fse.ensureDir(virtualEnvPath);
process.env.VIRTUAL_ENV = virtualEnvPath;
getWorkspacePersistentStateStub.rejects(new Error('persistent state unavailable'));
const result = await getVenvForWorkspace(workspacePath);
assert.strictEqual(result, virtualEnvPath, 'Should fall back to VIRTUAL_ENV when persistent state fails');
});
test('should NOT use VIRTUAL_ENV for sibling path with shared prefix', async () => {
const projectA = path.join(tmpDir, 'app');
const siblingVenv = path.join(tmpDir, 'app2', '.venv');
await fse.ensureDir(projectA);
await fse.ensureDir(siblingVenv);
process.env.VIRTUAL_ENV = siblingVenv;
mockState.get.withArgs(VENV_WORKSPACE_KEY).resolves(undefined);
const result = await getVenvForWorkspace(projectA);
assert.strictEqual(result, undefined, 'Should NOT match sibling path with shared prefix');
});
});