|
| 1 | +import assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import * as logging from '../../../common/logging'; |
| 4 | +import * as persistentState from '../../../common/persistentState'; |
| 5 | +import * as workspaceApis from '../../../common/workspace.apis'; |
| 6 | +import { clearCondaCache, CONDA_PATH_KEY, getConda } from '../../../managers/conda/condaUtils'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Tests for getConda prioritization. |
| 10 | + * |
| 11 | + * The priority order should be: |
| 12 | + * 1. Settings (python.condaPath) - if set (non-empty) |
| 13 | + * 2. In-memory cache |
| 14 | + * 3. Persistent state |
| 15 | + * 4. PATH lookup (which) |
| 16 | + * 5. Known locations |
| 17 | + * 6. Native finder |
| 18 | + * |
| 19 | + * These tests verify the correct order by checking which functions are called and in what order. |
| 20 | + */ |
| 21 | +suite('Conda Utils - getConda prioritization', () => { |
| 22 | + let getConfigurationStub: sinon.SinonStub; |
| 23 | + let mockConfig: { get: sinon.SinonStub }; |
| 24 | + let mockState: { get: sinon.SinonStub; set: sinon.SinonStub }; |
| 25 | + let getWorkspacePersistentStateStub: sinon.SinonStub; |
| 26 | + |
| 27 | + setup(async () => { |
| 28 | + // Clear in-memory cache before each test |
| 29 | + await clearCondaCache(); |
| 30 | + |
| 31 | + mockConfig = { |
| 32 | + get: sinon.stub(), |
| 33 | + }; |
| 34 | + getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration'); |
| 35 | + getConfigurationStub.withArgs('python').returns(mockConfig); |
| 36 | + sinon.stub(logging, 'traceInfo'); |
| 37 | + |
| 38 | + mockState = { |
| 39 | + get: sinon.stub(), |
| 40 | + set: sinon.stub().resolves(), |
| 41 | + }; |
| 42 | + getWorkspacePersistentStateStub = sinon.stub(persistentState, 'getWorkspacePersistentState'); |
| 43 | + getWorkspacePersistentStateStub.resolves(mockState); |
| 44 | + }); |
| 45 | + |
| 46 | + teardown(() => { |
| 47 | + sinon.restore(); |
| 48 | + }); |
| 49 | + |
| 50 | + test('Priority 1: Settings path is used first when set', async () => { |
| 51 | + // Arrange: Settings returns a valid path |
| 52 | + const settingsPath = '/custom/path/to/conda'; |
| 53 | + mockConfig.get.withArgs('condaPath').returns(settingsPath); |
| 54 | + |
| 55 | + // Act |
| 56 | + const result = await getConda(); |
| 57 | + |
| 58 | + // Assert: Should use settings path immediately (no existence check for settings in getConda) |
| 59 | + assert.strictEqual(result, settingsPath); |
| 60 | + // Verify persistent state was NOT called (settings took priority) |
| 61 | + assert.ok(!mockState.get.called, 'Persistent state should not be checked when settings is set'); |
| 62 | + }); |
| 63 | + |
| 64 | + test('Settings check happens before any other source', async () => { |
| 65 | + // Arrange: Settings returns empty (no setting) |
| 66 | + mockConfig.get.withArgs('condaPath').returns(''); |
| 67 | + mockState.get.withArgs(CONDA_PATH_KEY).resolves(undefined); |
| 68 | + |
| 69 | + // Act |
| 70 | + try { |
| 71 | + await getConda(); |
| 72 | + } catch { |
| 73 | + // Expected to throw when nothing found |
| 74 | + } |
| 75 | + |
| 76 | + // Assert: Configuration was accessed first |
| 77 | + assert.ok(getConfigurationStub.calledWith('python'), 'Configuration should be checked'); |
| 78 | + assert.ok(mockConfig.get.calledWith('condaPath'), 'Settings should be checked'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('Persistent state is checked when settings is empty', async () => { |
| 82 | + // Arrange: No settings |
| 83 | + mockConfig.get.withArgs('condaPath').returns(''); |
| 84 | + |
| 85 | + // Persistent state returns undefined too |
| 86 | + mockState.get.withArgs(CONDA_PATH_KEY).resolves(undefined); |
| 87 | + |
| 88 | + // Act |
| 89 | + try { |
| 90 | + await getConda(); |
| 91 | + } catch { |
| 92 | + // Expected to throw when nothing found |
| 93 | + } |
| 94 | + |
| 95 | + // Assert: Both settings and persistent state were checked |
| 96 | + assert.ok(mockConfig.get.calledWith('condaPath'), 'Settings should be checked first'); |
| 97 | + assert.ok(mockState.get.calledWith(CONDA_PATH_KEY), 'Persistent state should be checked'); |
| 98 | + }); |
| 99 | + |
| 100 | + test('Settings path takes priority over cache', async () => { |
| 101 | + // Arrange: First set up so something would be cached |
| 102 | + // We can't easily test the cache without fs stubs, but we can verify |
| 103 | + // that settings is always checked first |
| 104 | + |
| 105 | + // Now set a settings path |
| 106 | + const settingsPath = '/custom/conda'; |
| 107 | + mockConfig.get.withArgs('condaPath').returns(settingsPath); |
| 108 | + |
| 109 | + // Act |
| 110 | + const result = await getConda(); |
| 111 | + |
| 112 | + // Assert: Should use settings |
| 113 | + assert.strictEqual(result, settingsPath); |
| 114 | + }); |
| 115 | + |
| 116 | + test('Settings with non-empty value is used regardless of validity', async () => { |
| 117 | + // This is key behavior: getConda() returns settings immediately without checking existence |
| 118 | + // The caller is responsible for validating the path if needed |
| 119 | + const settingsPath = '/nonexistent/conda'; |
| 120 | + mockConfig.get.withArgs('condaPath').returns(settingsPath); |
| 121 | + |
| 122 | + // Act |
| 123 | + const result = await getConda(); |
| 124 | + |
| 125 | + // Assert: Should return settings path directly |
| 126 | + assert.strictEqual(result, settingsPath); |
| 127 | + }); |
| 128 | + |
| 129 | + test('Code checks settings first in the function body', () => { |
| 130 | + // This is a structural test - verify the function checks settings at the top |
| 131 | + // by inspecting that getConfiguration is called synchronously |
| 132 | + // before any async operations |
| 133 | + |
| 134 | + // Arrange |
| 135 | + mockConfig.get.withArgs('condaPath').returns('/some/path'); |
| 136 | + |
| 137 | + // Start the call (don't await) |
| 138 | + const promise = getConda(); |
| 139 | + |
| 140 | + // Assert: Settings was checked synchronously (before promise resolves) |
| 141 | + assert.ok(getConfigurationStub.called, 'Configuration should be checked synchronously at function start'); |
| 142 | + |
| 143 | + // Clean up |
| 144 | + return promise; |
| 145 | + }); |
| 146 | +}); |
0 commit comments