|
| 1 | +import assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { LogOutputChannel } from 'vscode'; |
| 4 | +import * as workspaceApis from '../../../common/workspace.apis'; |
| 5 | +import { NativePythonEnvironmentKind } from '../../../managers/common/nativePythonFinder'; |
| 6 | +import { shouldUseUv } from '../../../managers/builtin/helpers'; |
| 7 | + |
| 8 | +suite('Helpers - shouldUseUv', () => { |
| 9 | + let getConfigurationStub: sinon.SinonStub; |
| 10 | + let mockConfig: { get: sinon.SinonStub }; |
| 11 | + let mockLog: LogOutputChannel; |
| 12 | + |
| 13 | + setup(() => { |
| 14 | + getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration'); |
| 15 | + mockConfig = { |
| 16 | + get: sinon.stub(), |
| 17 | + }; |
| 18 | + getConfigurationStub.returns(mockConfig); |
| 19 | + mockLog = { |
| 20 | + info: sinon.stub(), |
| 21 | + error: sinon.stub(), |
| 22 | + warn: sinon.stub(), |
| 23 | + append: sinon.stub(), |
| 24 | + } as unknown as LogOutputChannel; |
| 25 | + }); |
| 26 | + |
| 27 | + teardown(() => { |
| 28 | + sinon.restore(); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should return true for VenvUv environment when UV is installed', async () => { |
| 32 | + // Arrange: VenvUv type environment, UV available |
| 33 | + mockConfig.get.withArgs('alwaysUseUv', true).returns(false); |
| 34 | + |
| 35 | + // Act |
| 36 | + const result = await shouldUseUv(NativePythonEnvironmentKind.venvUv, mockLog); |
| 37 | + |
| 38 | + // Assert: Should use UV for VenvUv regardless of setting |
| 39 | + // Note: This will return true or false based on actual UV installation |
| 40 | + // We're testing the logic that VenvUv environments should check for UV |
| 41 | + assert.strictEqual(typeof result, 'boolean'); |
| 42 | + }); |
| 43 | + |
| 44 | + test('should return true for regular Venv when alwaysUseUv is true and UV is installed', async () => { |
| 45 | + // Arrange: Regular venv, alwaysUseUv is true |
| 46 | + mockConfig.get.withArgs('alwaysUseUv', true).returns(true); |
| 47 | + |
| 48 | + // Act |
| 49 | + const result = await shouldUseUv(NativePythonEnvironmentKind.venv, mockLog); |
| 50 | + |
| 51 | + // Assert: Should check for UV when alwaysUseUv is true |
| 52 | + assert.strictEqual(typeof result, 'boolean'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('should return false for regular Venv when alwaysUseUv is false', async () => { |
| 56 | + // Arrange: Regular venv, alwaysUseUv is false |
| 57 | + mockConfig.get.withArgs('alwaysUseUv', true).returns(false); |
| 58 | + |
| 59 | + // Act |
| 60 | + const result = await shouldUseUv(NativePythonEnvironmentKind.venv, mockLog); |
| 61 | + |
| 62 | + // Assert: Should not use UV for regular venv when setting is false |
| 63 | + assert.strictEqual(result, false); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should return false for Conda environment when alwaysUseUv is false', async () => { |
| 67 | + // Arrange: Conda environment (not a venv), alwaysUseUv is false |
| 68 | + mockConfig.get.withArgs('alwaysUseUv', true).returns(false); |
| 69 | + |
| 70 | + // Act |
| 71 | + const result = await shouldUseUv(NativePythonEnvironmentKind.conda, mockLog); |
| 72 | + |
| 73 | + // Assert: Should not use UV for non-venv environments when setting is false |
| 74 | + assert.strictEqual(result, false); |
| 75 | + }); |
| 76 | + |
| 77 | + test('should check setting when alwaysUseUv is true for Conda environment', async () => { |
| 78 | + // Arrange: Conda environment, alwaysUseUv is true |
| 79 | + mockConfig.get.withArgs('alwaysUseUv', true).returns(true); |
| 80 | + |
| 81 | + // Act |
| 82 | + const result = await shouldUseUv(NativePythonEnvironmentKind.conda, mockLog); |
| 83 | + |
| 84 | + // Assert: Should check for UV when alwaysUseUv is true |
| 85 | + assert.strictEqual(typeof result, 'boolean'); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should use default value true when alwaysUseUv setting is not configured', async () => { |
| 89 | + // Arrange: Setting not configured, should use default of true |
| 90 | + mockConfig.get.withArgs('alwaysUseUv', true).returns(true); |
| 91 | + |
| 92 | + // Act |
| 93 | + const result = await shouldUseUv(undefined, mockLog); |
| 94 | + |
| 95 | + // Assert: Should check for UV with default setting |
| 96 | + assert.strictEqual(typeof result, 'boolean'); |
| 97 | + }); |
| 98 | + |
| 99 | + test('should handle undefined envKind and respect alwaysUseUv setting', async () => { |
| 100 | + // Arrange: No environment kind specified, alwaysUseUv is false |
| 101 | + mockConfig.get.withArgs('alwaysUseUv', true).returns(false); |
| 102 | + |
| 103 | + // Act |
| 104 | + const result = await shouldUseUv(undefined, mockLog); |
| 105 | + |
| 106 | + // Assert: Should not use UV when setting is false |
| 107 | + assert.strictEqual(result, false); |
| 108 | + }); |
| 109 | +}); |
0 commit comments