|
1 | 1 | import assert from 'node:assert'; |
| 2 | +import path from 'node:path'; |
2 | 3 | import * as sinon from 'sinon'; |
3 | 4 | import { EnvironmentManager, PythonEnvironment, PythonEnvironmentApi, PythonEnvironmentInfo } from '../../../api'; |
4 | 5 | import * as childProcessApis from '../../../common/childProcess.apis'; |
| 6 | +import * as pathUtils from '../../../common/utils/pathUtils'; |
| 7 | +import * as platformUtils from '../../../common/utils/platformUtils'; |
5 | 8 | import { NativeEnvInfo } from '../../../managers/common/nativePythonFinder'; |
6 | 9 | import * as utils from '../../../managers/common/utils'; |
7 | 10 | import { |
| 11 | + getDefaultPoetryCacheDir, |
| 12 | + getDefaultPoetryVirtualenvsPath, |
8 | 13 | getPoetryVersion, |
9 | 14 | isPoetryVirtualenvsInProject, |
10 | 15 | nativeToPythonEnv, |
@@ -208,3 +213,155 @@ suite('getPoetryVersion - childProcess.apis mocking pattern', () => { |
208 | 213 | assert.strictEqual(version, undefined); |
209 | 214 | }); |
210 | 215 | }); |
| 216 | + |
| 217 | +suite('getDefaultPoetryCacheDir', () => { |
| 218 | + let isWindowsStub: sinon.SinonStub; |
| 219 | + let isMacStub: sinon.SinonStub; |
| 220 | + let getUserHomeDirStub: sinon.SinonStub; |
| 221 | + let originalLocalAppData: string | undefined; |
| 222 | + let originalAppData: string | undefined; |
| 223 | + |
| 224 | + setup(() => { |
| 225 | + isWindowsStub = sinon.stub(platformUtils, 'isWindows'); |
| 226 | + isMacStub = sinon.stub(platformUtils, 'isMac'); |
| 227 | + getUserHomeDirStub = sinon.stub(pathUtils, 'getUserHomeDir'); |
| 228 | + |
| 229 | + // Save original env vars |
| 230 | + originalLocalAppData = process.env.LOCALAPPDATA; |
| 231 | + originalAppData = process.env.APPDATA; |
| 232 | + }); |
| 233 | + |
| 234 | + teardown(() => { |
| 235 | + sinon.restore(); |
| 236 | + // Restore original env vars |
| 237 | + if (originalLocalAppData === undefined) { |
| 238 | + delete process.env.LOCALAPPDATA; |
| 239 | + } else { |
| 240 | + process.env.LOCALAPPDATA = originalLocalAppData; |
| 241 | + } |
| 242 | + if (originalAppData === undefined) { |
| 243 | + delete process.env.APPDATA; |
| 244 | + } else { |
| 245 | + process.env.APPDATA = originalAppData; |
| 246 | + } |
| 247 | + }); |
| 248 | + |
| 249 | + test('Windows: uses LOCALAPPDATA when available', () => { |
| 250 | + isWindowsStub.returns(true); |
| 251 | + process.env.LOCALAPPDATA = 'C:\\Users\\test\\AppData\\Local'; |
| 252 | + |
| 253 | + const result = getDefaultPoetryCacheDir(); |
| 254 | + |
| 255 | + assert.strictEqual(result, path.join('C:\\Users\\test\\AppData\\Local', 'pypoetry', 'Cache')); |
| 256 | + }); |
| 257 | + |
| 258 | + test('Windows: falls back to APPDATA when LOCALAPPDATA is not set', () => { |
| 259 | + isWindowsStub.returns(true); |
| 260 | + delete process.env.LOCALAPPDATA; |
| 261 | + process.env.APPDATA = 'C:\\Users\\test\\AppData\\Roaming'; |
| 262 | + |
| 263 | + const result = getDefaultPoetryCacheDir(); |
| 264 | + |
| 265 | + assert.strictEqual(result, path.join('C:\\Users\\test\\AppData\\Roaming', 'pypoetry', 'Cache')); |
| 266 | + }); |
| 267 | + |
| 268 | + test('Windows: returns undefined when neither LOCALAPPDATA nor APPDATA is set', () => { |
| 269 | + isWindowsStub.returns(true); |
| 270 | + delete process.env.LOCALAPPDATA; |
| 271 | + delete process.env.APPDATA; |
| 272 | + |
| 273 | + const result = getDefaultPoetryCacheDir(); |
| 274 | + |
| 275 | + assert.strictEqual(result, undefined); |
| 276 | + }); |
| 277 | + |
| 278 | + test('macOS: uses ~/Library/Caches/pypoetry', () => { |
| 279 | + isWindowsStub.returns(false); |
| 280 | + isMacStub.returns(true); |
| 281 | + getUserHomeDirStub.returns('/Users/test'); |
| 282 | + |
| 283 | + const result = getDefaultPoetryCacheDir(); |
| 284 | + |
| 285 | + assert.strictEqual(result, path.join('/Users/test', 'Library', 'Caches', 'pypoetry')); |
| 286 | + }); |
| 287 | + |
| 288 | + test('Linux: uses ~/.cache/pypoetry', () => { |
| 289 | + isWindowsStub.returns(false); |
| 290 | + isMacStub.returns(false); |
| 291 | + getUserHomeDirStub.returns('/home/test'); |
| 292 | + |
| 293 | + const result = getDefaultPoetryCacheDir(); |
| 294 | + |
| 295 | + assert.strictEqual(result, path.join('/home/test', '.cache', 'pypoetry')); |
| 296 | + }); |
| 297 | + |
| 298 | + test('returns undefined when home directory is not available (non-Windows)', () => { |
| 299 | + isWindowsStub.returns(false); |
| 300 | + getUserHomeDirStub.returns(undefined); |
| 301 | + |
| 302 | + const result = getDefaultPoetryCacheDir(); |
| 303 | + |
| 304 | + assert.strictEqual(result, undefined); |
| 305 | + }); |
| 306 | +}); |
| 307 | + |
| 308 | +suite('getDefaultPoetryVirtualenvsPath', () => { |
| 309 | + let isWindowsStub: sinon.SinonStub; |
| 310 | + let isMacStub: sinon.SinonStub; |
| 311 | + let getUserHomeDirStub: sinon.SinonStub; |
| 312 | + let originalLocalAppData: string | undefined; |
| 313 | + |
| 314 | + setup(() => { |
| 315 | + isWindowsStub = sinon.stub(platformUtils, 'isWindows'); |
| 316 | + isMacStub = sinon.stub(platformUtils, 'isMac'); |
| 317 | + getUserHomeDirStub = sinon.stub(pathUtils, 'getUserHomeDir'); |
| 318 | + originalLocalAppData = process.env.LOCALAPPDATA; |
| 319 | + }); |
| 320 | + |
| 321 | + teardown(() => { |
| 322 | + sinon.restore(); |
| 323 | + if (originalLocalAppData === undefined) { |
| 324 | + delete process.env.LOCALAPPDATA; |
| 325 | + } else { |
| 326 | + process.env.LOCALAPPDATA = originalLocalAppData; |
| 327 | + } |
| 328 | + }); |
| 329 | + |
| 330 | + test('appends virtualenvs to cache directory', () => { |
| 331 | + isWindowsStub.returns(false); |
| 332 | + isMacStub.returns(false); |
| 333 | + getUserHomeDirStub.returns('/home/test'); |
| 334 | + |
| 335 | + const result = getDefaultPoetryVirtualenvsPath(); |
| 336 | + |
| 337 | + assert.strictEqual(result, path.join('/home/test', '.cache', 'pypoetry', 'virtualenvs')); |
| 338 | + }); |
| 339 | + |
| 340 | + test('Windows: returns correct virtualenvs path', () => { |
| 341 | + isWindowsStub.returns(true); |
| 342 | + process.env.LOCALAPPDATA = 'C:\\Users\\test\\AppData\\Local'; |
| 343 | + |
| 344 | + const result = getDefaultPoetryVirtualenvsPath(); |
| 345 | + |
| 346 | + assert.strictEqual(result, path.join('C:\\Users\\test\\AppData\\Local', 'pypoetry', 'Cache', 'virtualenvs')); |
| 347 | + }); |
| 348 | + |
| 349 | + test('macOS: returns correct virtualenvs path', () => { |
| 350 | + isWindowsStub.returns(false); |
| 351 | + isMacStub.returns(true); |
| 352 | + getUserHomeDirStub.returns('/Users/test'); |
| 353 | + |
| 354 | + const result = getDefaultPoetryVirtualenvsPath(); |
| 355 | + |
| 356 | + assert.strictEqual(result, path.join('/Users/test', 'Library', 'Caches', 'pypoetry', 'virtualenvs')); |
| 357 | + }); |
| 358 | + |
| 359 | + test('returns undefined when cache dir is not available', () => { |
| 360 | + isWindowsStub.returns(false); |
| 361 | + getUserHomeDirStub.returns(undefined); |
| 362 | + |
| 363 | + const result = getDefaultPoetryVirtualenvsPath(); |
| 364 | + |
| 365 | + assert.strictEqual(result, undefined); |
| 366 | + }); |
| 367 | +}); |
0 commit comments