|
1 | 1 | import assert from 'node:assert'; |
| 2 | +import path from 'node:path'; |
2 | 3 | import * as sinon from 'sinon'; |
3 | | -import { isPoetryVirtualenvsInProject, nativeToPythonEnv } from '../../../managers/poetry/poetryUtils'; |
4 | | -import * as utils from '../../../managers/common/utils'; |
5 | 4 | import { EnvironmentManager, PythonEnvironment, PythonEnvironmentApi, PythonEnvironmentInfo } from '../../../api'; |
| 5 | +import * as pathUtils from '../../../common/utils/pathUtils'; |
| 6 | +import * as platformUtils from '../../../common/utils/platformUtils'; |
6 | 7 | import { NativeEnvInfo } from '../../../managers/common/nativePythonFinder'; |
| 8 | +import * as utils from '../../../managers/common/utils'; |
| 9 | +import { |
| 10 | + getDefaultPoetryCacheDir, |
| 11 | + getDefaultPoetryVirtualenvsPath, |
| 12 | + isPoetryVirtualenvsInProject, |
| 13 | + nativeToPythonEnv, |
| 14 | +} from '../../../managers/poetry/poetryUtils'; |
7 | 15 |
|
8 | 16 | suite('isPoetryVirtualenvsInProject', () => { |
9 | 17 | test('should return false when env var is not set', () => { |
@@ -157,3 +165,155 @@ suite('nativeToPythonEnv - POETRY_VIRTUALENVS_IN_PROJECT integration', () => { |
157 | 165 | assert.strictEqual(capturedInfo!.group, undefined, 'Non-global path should not be global'); |
158 | 166 | }); |
159 | 167 | }); |
| 168 | + |
| 169 | +suite('getDefaultPoetryCacheDir', () => { |
| 170 | + let isWindowsStub: sinon.SinonStub; |
| 171 | + let isMacStub: sinon.SinonStub; |
| 172 | + let getUserHomeDirStub: sinon.SinonStub; |
| 173 | + let originalLocalAppData: string | undefined; |
| 174 | + let originalAppData: string | undefined; |
| 175 | + |
| 176 | + setup(() => { |
| 177 | + isWindowsStub = sinon.stub(platformUtils, 'isWindows'); |
| 178 | + isMacStub = sinon.stub(platformUtils, 'isMac'); |
| 179 | + getUserHomeDirStub = sinon.stub(pathUtils, 'getUserHomeDir'); |
| 180 | + |
| 181 | + // Save original env vars |
| 182 | + originalLocalAppData = process.env.LOCALAPPDATA; |
| 183 | + originalAppData = process.env.APPDATA; |
| 184 | + }); |
| 185 | + |
| 186 | + teardown(() => { |
| 187 | + sinon.restore(); |
| 188 | + // Restore original env vars |
| 189 | + if (originalLocalAppData === undefined) { |
| 190 | + delete process.env.LOCALAPPDATA; |
| 191 | + } else { |
| 192 | + process.env.LOCALAPPDATA = originalLocalAppData; |
| 193 | + } |
| 194 | + if (originalAppData === undefined) { |
| 195 | + delete process.env.APPDATA; |
| 196 | + } else { |
| 197 | + process.env.APPDATA = originalAppData; |
| 198 | + } |
| 199 | + }); |
| 200 | + |
| 201 | + test('Windows: uses LOCALAPPDATA when available', () => { |
| 202 | + isWindowsStub.returns(true); |
| 203 | + process.env.LOCALAPPDATA = 'C:\\Users\\test\\AppData\\Local'; |
| 204 | + |
| 205 | + const result = getDefaultPoetryCacheDir(); |
| 206 | + |
| 207 | + assert.strictEqual(result, path.join('C:\\Users\\test\\AppData\\Local', 'pypoetry', 'Cache')); |
| 208 | + }); |
| 209 | + |
| 210 | + test('Windows: falls back to APPDATA when LOCALAPPDATA is not set', () => { |
| 211 | + isWindowsStub.returns(true); |
| 212 | + delete process.env.LOCALAPPDATA; |
| 213 | + process.env.APPDATA = 'C:\\Users\\test\\AppData\\Roaming'; |
| 214 | + |
| 215 | + const result = getDefaultPoetryCacheDir(); |
| 216 | + |
| 217 | + assert.strictEqual(result, path.join('C:\\Users\\test\\AppData\\Roaming', 'pypoetry', 'Cache')); |
| 218 | + }); |
| 219 | + |
| 220 | + test('Windows: returns undefined when neither LOCALAPPDATA nor APPDATA is set', () => { |
| 221 | + isWindowsStub.returns(true); |
| 222 | + delete process.env.LOCALAPPDATA; |
| 223 | + delete process.env.APPDATA; |
| 224 | + |
| 225 | + const result = getDefaultPoetryCacheDir(); |
| 226 | + |
| 227 | + assert.strictEqual(result, undefined); |
| 228 | + }); |
| 229 | + |
| 230 | + test('macOS: uses ~/Library/Caches/pypoetry', () => { |
| 231 | + isWindowsStub.returns(false); |
| 232 | + isMacStub.returns(true); |
| 233 | + getUserHomeDirStub.returns('/Users/test'); |
| 234 | + |
| 235 | + const result = getDefaultPoetryCacheDir(); |
| 236 | + |
| 237 | + assert.strictEqual(result, path.join('/Users/test', 'Library', 'Caches', 'pypoetry')); |
| 238 | + }); |
| 239 | + |
| 240 | + test('Linux: uses ~/.cache/pypoetry', () => { |
| 241 | + isWindowsStub.returns(false); |
| 242 | + isMacStub.returns(false); |
| 243 | + getUserHomeDirStub.returns('/home/test'); |
| 244 | + |
| 245 | + const result = getDefaultPoetryCacheDir(); |
| 246 | + |
| 247 | + assert.strictEqual(result, path.join('/home/test', '.cache', 'pypoetry')); |
| 248 | + }); |
| 249 | + |
| 250 | + test('returns undefined when home directory is not available (non-Windows)', () => { |
| 251 | + isWindowsStub.returns(false); |
| 252 | + getUserHomeDirStub.returns(undefined); |
| 253 | + |
| 254 | + const result = getDefaultPoetryCacheDir(); |
| 255 | + |
| 256 | + assert.strictEqual(result, undefined); |
| 257 | + }); |
| 258 | +}); |
| 259 | + |
| 260 | +suite('getDefaultPoetryVirtualenvsPath', () => { |
| 261 | + let isWindowsStub: sinon.SinonStub; |
| 262 | + let isMacStub: sinon.SinonStub; |
| 263 | + let getUserHomeDirStub: sinon.SinonStub; |
| 264 | + let originalLocalAppData: string | undefined; |
| 265 | + |
| 266 | + setup(() => { |
| 267 | + isWindowsStub = sinon.stub(platformUtils, 'isWindows'); |
| 268 | + isMacStub = sinon.stub(platformUtils, 'isMac'); |
| 269 | + getUserHomeDirStub = sinon.stub(pathUtils, 'getUserHomeDir'); |
| 270 | + originalLocalAppData = process.env.LOCALAPPDATA; |
| 271 | + }); |
| 272 | + |
| 273 | + teardown(() => { |
| 274 | + sinon.restore(); |
| 275 | + if (originalLocalAppData === undefined) { |
| 276 | + delete process.env.LOCALAPPDATA; |
| 277 | + } else { |
| 278 | + process.env.LOCALAPPDATA = originalLocalAppData; |
| 279 | + } |
| 280 | + }); |
| 281 | + |
| 282 | + test('appends virtualenvs to cache directory', () => { |
| 283 | + isWindowsStub.returns(false); |
| 284 | + isMacStub.returns(false); |
| 285 | + getUserHomeDirStub.returns('/home/test'); |
| 286 | + |
| 287 | + const result = getDefaultPoetryVirtualenvsPath(); |
| 288 | + |
| 289 | + assert.strictEqual(result, path.join('/home/test', '.cache', 'pypoetry', 'virtualenvs')); |
| 290 | + }); |
| 291 | + |
| 292 | + test('Windows: returns correct virtualenvs path', () => { |
| 293 | + isWindowsStub.returns(true); |
| 294 | + process.env.LOCALAPPDATA = 'C:\\Users\\test\\AppData\\Local'; |
| 295 | + |
| 296 | + const result = getDefaultPoetryVirtualenvsPath(); |
| 297 | + |
| 298 | + assert.strictEqual(result, path.join('C:\\Users\\test\\AppData\\Local', 'pypoetry', 'Cache', 'virtualenvs')); |
| 299 | + }); |
| 300 | + |
| 301 | + test('macOS: returns correct virtualenvs path', () => { |
| 302 | + isWindowsStub.returns(false); |
| 303 | + isMacStub.returns(true); |
| 304 | + getUserHomeDirStub.returns('/Users/test'); |
| 305 | + |
| 306 | + const result = getDefaultPoetryVirtualenvsPath(); |
| 307 | + |
| 308 | + assert.strictEqual(result, path.join('/Users/test', 'Library', 'Caches', 'pypoetry', 'virtualenvs')); |
| 309 | + }); |
| 310 | + |
| 311 | + test('returns undefined when cache dir is not available', () => { |
| 312 | + isWindowsStub.returns(false); |
| 313 | + getUserHomeDirStub.returns(undefined); |
| 314 | + |
| 315 | + const result = getDefaultPoetryVirtualenvsPath(); |
| 316 | + |
| 317 | + assert.strictEqual(result, undefined); |
| 318 | + }); |
| 319 | +}); |
0 commit comments