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
166 lines (132 loc) · 7 KB
/
venvUtils.getVenvForWorkspace.unit.test.ts
File metadata and controls
166 lines (132 loc) · 7 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
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';
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');
});
});