forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipenvUtils.getPipenvVirtualenvDirs.unit.test.ts
More file actions
175 lines (138 loc) · 6.36 KB
/
pipenvUtils.getPipenvVirtualenvDirs.unit.test.ts
File metadata and controls
175 lines (138 loc) · 6.36 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
import assert from 'assert';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { getPipenvVirtualenvDirs } from '../../../managers/pipenv/pipenvUtils';
/**
* Tests for getPipenvVirtualenvDirs.
*
* The function should return directories where pipenv virtualenvs are stored,
* checking these locations in priority order:
* 1. WORKON_HOME (if set and exists)
* 2. XDG_DATA_HOME/virtualenvs (if XDG_DATA_HOME is set and path exists)
* 3. ~/.local/share/virtualenvs (Linux/macOS default)
* 4. ~/.virtualenvs (Windows default)
*
* These tests use real temp directories for filesystem operations since
* native fs.existsSync cannot be stubbed (non-configurable property).
*/
suite('Pipenv Utils - getPipenvVirtualenvDirs', () => {
let originalEnv: NodeJS.ProcessEnv;
let tempDir: string;
setup(() => {
// Save original env
originalEnv = { ...process.env };
// Clear relevant env vars
delete process.env.WORKON_HOME;
delete process.env.XDG_DATA_HOME;
// Create a temp directory for tests
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pipenv-test-'));
});
teardown(() => {
// Restore original env
process.env = originalEnv;
// Clean up temp directory
if (tempDir && fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
test('Returns WORKON_HOME when set and exists', () => {
const workonPath = path.join(tempDir, 'workon_home');
fs.mkdirSync(workonPath);
process.env.WORKON_HOME = workonPath;
const dirs = getPipenvVirtualenvDirs();
assert.ok(dirs.includes(workonPath), 'WORKON_HOME should be included');
});
test('Ignores WORKON_HOME when set but does not exist', () => {
const workonPath = path.join(tempDir, 'nonexistent_workon');
// Don't create the directory
process.env.WORKON_HOME = workonPath;
const dirs = getPipenvVirtualenvDirs();
assert.ok(!dirs.includes(workonPath), 'Non-existent WORKON_HOME should not be included');
});
test('Returns XDG_DATA_HOME/virtualenvs when set and exists', () => {
const xdgBase = path.join(tempDir, 'xdg_data');
const xdgVenvs = path.join(xdgBase, 'virtualenvs');
fs.mkdirSync(xdgBase);
fs.mkdirSync(xdgVenvs);
process.env.XDG_DATA_HOME = xdgBase;
const dirs = getPipenvVirtualenvDirs();
assert.ok(dirs.includes(xdgVenvs), 'XDG_DATA_HOME/virtualenvs should be included');
});
test('Ignores XDG_DATA_HOME when virtualenvs subdir does not exist', () => {
const xdgBase = path.join(tempDir, 'xdg_data_novenvs');
fs.mkdirSync(xdgBase);
// Don't create virtualenvs subdir
process.env.XDG_DATA_HOME = xdgBase;
const dirs = getPipenvVirtualenvDirs();
const xdgVenvs = path.join(xdgBase, 'virtualenvs');
assert.ok(!dirs.includes(xdgVenvs), 'Non-existent XDG_DATA_HOME/virtualenvs should not be included');
});
test('WORKON_HOME takes precedence and appears first', () => {
const workonPath = path.join(tempDir, 'workon');
const xdgBase = path.join(tempDir, 'xdg');
const xdgVenvs = path.join(xdgBase, 'virtualenvs');
fs.mkdirSync(workonPath);
fs.mkdirSync(xdgBase);
fs.mkdirSync(xdgVenvs);
process.env.WORKON_HOME = workonPath;
process.env.XDG_DATA_HOME = xdgBase;
const dirs = getPipenvVirtualenvDirs();
assert.strictEqual(dirs[0], workonPath, 'WORKON_HOME should be first');
assert.ok(dirs.includes(xdgVenvs), 'XDG path should also be included');
});
test('Does not include duplicate paths', () => {
// This test only makes sense on non-Windows platforms where
// XDG_DATA_HOME/virtualenvs might match the default path
if (process.platform === 'win32') {
return;
}
// Create a unique path that will be used for both XDG and checked for duplicates
const venvBase = path.join(tempDir, 'unique_venvs');
const virtualenvsPath = path.join(venvBase, 'virtualenvs');
fs.mkdirSync(venvBase);
fs.mkdirSync(virtualenvsPath);
// Set XDG_DATA_HOME to the same base
process.env.XDG_DATA_HOME = venvBase;
const dirs = getPipenvVirtualenvDirs();
// Count occurrences of the path
const count = dirs.filter((d) => d === virtualenvsPath).length;
assert.strictEqual(count, 1, 'Path should not be duplicated');
});
test('Returns multiple directories when all exist', () => {
const workonPath = path.join(tempDir, 'workon_multi');
const xdgBase = path.join(tempDir, 'xdg_multi');
const xdgPath = path.join(xdgBase, 'virtualenvs');
fs.mkdirSync(workonPath);
fs.mkdirSync(xdgBase);
fs.mkdirSync(xdgPath);
process.env.WORKON_HOME = workonPath;
process.env.XDG_DATA_HOME = xdgBase;
const dirs = getPipenvVirtualenvDirs();
assert.ok(dirs.length >= 2, 'Should return at least two directories');
assert.strictEqual(dirs[0], workonPath, 'WORKON_HOME should be first');
assert.ok(dirs.includes(xdgPath), 'XDG path should be included');
});
test('Handles tilde expansion in WORKON_HOME', () => {
// Create the target directory in user's home
const customVenvsName = `.pipenv-test-tilde-${Date.now()}`;
const expandedPath = path.join(os.homedir(), customVenvsName);
let created = false;
try {
fs.mkdirSync(expandedPath);
created = true;
// Use path.sep for cross-platform compatibility
process.env.WORKON_HOME = `~${path.sep}${customVenvsName}`;
const dirs = getPipenvVirtualenvDirs();
// Normalize paths for comparison since untildify might produce different path formats
const normalizedDirs = dirs.map((d) => path.normalize(d));
const normalizedExpected = path.normalize(expandedPath);
assert.ok(normalizedDirs.includes(normalizedExpected), 'Tilde-expanded path should be included');
} finally {
// Clean up - only if directory was successfully created
if (created && fs.existsSync(expandedPath)) {
fs.rmSync(expandedPath, { recursive: true, force: true });
}
}
});
});