|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import assert from 'node:assert'; |
| 5 | +import { Uri } from 'vscode'; |
| 6 | +import { PythonEnvironment } from '../../api'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Test the logic used in environment pickers to include interpreter paths in descriptions |
| 10 | + */ |
| 11 | +suite('Environment Picker Description Logic', () => { |
| 12 | + const createMockEnvironment = ( |
| 13 | + displayPath: string, |
| 14 | + description?: string, |
| 15 | + name: string = 'Python 3.9.0', |
| 16 | + ): PythonEnvironment => ({ |
| 17 | + envId: { id: 'test', managerId: 'test-manager' }, |
| 18 | + name, |
| 19 | + displayName: name, |
| 20 | + displayPath, |
| 21 | + version: '3.9.0', |
| 22 | + environmentPath: Uri.file(displayPath), |
| 23 | + description, |
| 24 | + sysPrefix: '/path/to/prefix', |
| 25 | + execInfo: { run: { executable: displayPath } }, |
| 26 | + }); |
| 27 | + |
| 28 | + suite('Description formatting with interpreter path', () => { |
| 29 | + test('should use displayPath as description when no original description exists', () => { |
| 30 | + const env = createMockEnvironment('/usr/local/bin/python'); |
| 31 | + |
| 32 | + // This is the logic from our updated picker |
| 33 | + const pathDescription = env.displayPath; |
| 34 | + const description = |
| 35 | + env.description && env.description.trim() ? `${env.description} (${pathDescription})` : pathDescription; |
| 36 | + |
| 37 | + assert.strictEqual(description, '/usr/local/bin/python'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should append displayPath to existing description in parentheses', () => { |
| 41 | + const env = createMockEnvironment('/home/user/.venv/bin/python', 'Virtual Environment'); |
| 42 | + |
| 43 | + // This is the logic from our updated picker |
| 44 | + const pathDescription = env.displayPath; |
| 45 | + const description = |
| 46 | + env.description && env.description.trim() ? `${env.description} (${pathDescription})` : pathDescription; |
| 47 | + |
| 48 | + assert.strictEqual(description, 'Virtual Environment (/home/user/.venv/bin/python)'); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should handle complex paths correctly', () => { |
| 52 | + const complexPath = '/usr/local/anaconda3/envs/my-project-env/bin/python'; |
| 53 | + const env = createMockEnvironment(complexPath, 'Conda Environment'); |
| 54 | + |
| 55 | + // This is the logic from our updated picker |
| 56 | + const pathDescription = env.displayPath; |
| 57 | + const description = |
| 58 | + env.description && env.description.trim() ? `${env.description} (${pathDescription})` : pathDescription; |
| 59 | + |
| 60 | + assert.strictEqual(description, `Conda Environment (${complexPath})`); |
| 61 | + }); |
| 62 | + |
| 63 | + test('should handle empty description correctly', () => { |
| 64 | + const env = createMockEnvironment('/opt/python/bin/python', ''); |
| 65 | + |
| 66 | + // This is the logic from our updated picker |
| 67 | + const pathDescription = env.displayPath; |
| 68 | + const description = |
| 69 | + env.description && env.description.trim() ? `${env.description} (${pathDescription})` : pathDescription; |
| 70 | + |
| 71 | + // Empty string should be treated like no description, so just use path |
| 72 | + assert.strictEqual(description, '/opt/python/bin/python'); |
| 73 | + }); |
| 74 | + |
| 75 | + test('should handle Windows paths correctly', () => { |
| 76 | + const windowsPath = 'C:\\Python39\\python.exe'; |
| 77 | + const env = createMockEnvironment(windowsPath, 'System Python'); |
| 78 | + |
| 79 | + // This is the logic from our updated picker |
| 80 | + const pathDescription = env.displayPath; |
| 81 | + const description = |
| 82 | + env.description && env.description.trim() ? `${env.description} (${pathDescription})` : pathDescription; |
| 83 | + |
| 84 | + assert.strictEqual(description, 'System Python (C:\\Python39\\python.exe)'); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments