Skip to content

Commit 0cc1ea6

Browse files
committed
tests: add unit tests for uvPythonInstaller
1 parent caa182e commit 0cc1ea6

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import assert from 'assert';
2+
import * as sinon from 'sinon';
3+
import { LogOutputChannel } from 'vscode';
4+
import { UvInstallStrings } from '../../../common/localize';
5+
import * as persistentState from '../../../common/persistentState';
6+
import { EventNames } from '../../../common/telemetry/constants';
7+
import * as telemetrySender from '../../../common/telemetry/sender';
8+
import * as windowApis from '../../../common/window.apis';
9+
import * as helpers from '../../../managers/builtin/helpers';
10+
import {
11+
clearDontAskAgain,
12+
isDontAskAgainSet,
13+
promptInstallPythonViaUv,
14+
} from '../../../managers/builtin/uvPythonInstaller';
15+
import { createMockLogOutputChannel } from '../../mocks/helper';
16+
17+
suite('uvPythonInstaller - promptInstallPythonViaUv', () => {
18+
let mockLog: LogOutputChannel;
19+
let mockApi: { refreshEnvironments: sinon.SinonStub };
20+
let isUvInstalledStub: sinon.SinonStub;
21+
let showInformationMessageStub: sinon.SinonStub;
22+
let sendTelemetryEventStub: sinon.SinonStub;
23+
let mockState: { get: sinon.SinonStub; set: sinon.SinonStub; clear: sinon.SinonStub };
24+
25+
setup(() => {
26+
mockLog = createMockLogOutputChannel();
27+
mockApi = { refreshEnvironments: sinon.stub().resolves() };
28+
29+
mockState = {
30+
get: sinon.stub(),
31+
set: sinon.stub().resolves(),
32+
clear: sinon.stub().resolves(),
33+
};
34+
sinon.stub(persistentState, 'getGlobalPersistentState').resolves(mockState);
35+
isUvInstalledStub = sinon.stub(helpers, 'isUvInstalled');
36+
showInformationMessageStub = sinon.stub(windowApis, 'showInformationMessage');
37+
sendTelemetryEventStub = sinon.stub(telemetrySender, 'sendTelemetryEvent');
38+
});
39+
40+
teardown(() => {
41+
sinon.restore();
42+
});
43+
44+
test('should return false when "Don\'t ask again" is set', async () => {
45+
mockState.get.resolves(true);
46+
47+
const result = await promptInstallPythonViaUv('activation', mockApi as any, mockLog);
48+
49+
assert.strictEqual(result, false);
50+
assert(showInformationMessageStub.notCalled, 'Should not show message when dont ask again is set');
51+
assert(sendTelemetryEventStub.notCalled, 'Should not send telemetry when skipping prompt');
52+
});
53+
54+
test('should show correct prompt when uv is installed', async () => {
55+
mockState.get.resolves(false);
56+
isUvInstalledStub.resolves(true);
57+
showInformationMessageStub.resolves(undefined); // User dismissed
58+
59+
await promptInstallPythonViaUv('activation', mockApi as any, mockLog);
60+
61+
assert(
62+
showInformationMessageStub.calledWith(
63+
UvInstallStrings.installPythonPrompt,
64+
UvInstallStrings.installPython,
65+
UvInstallStrings.dontAskAgain,
66+
),
67+
'Should show install Python prompt when uv is installed',
68+
);
69+
});
70+
71+
test('should show correct prompt when uv is NOT installed', async () => {
72+
mockState.get.resolves(false);
73+
isUvInstalledStub.resolves(false);
74+
showInformationMessageStub.resolves(undefined); // User dismissed
75+
76+
await promptInstallPythonViaUv('activation', mockApi as any, mockLog);
77+
78+
assert(
79+
showInformationMessageStub.calledWith(
80+
UvInstallStrings.installPythonAndUvPrompt,
81+
UvInstallStrings.installPython,
82+
UvInstallStrings.dontAskAgain,
83+
),
84+
'Should show install Python AND uv prompt when uv is not installed',
85+
);
86+
});
87+
88+
test('should set persistent state when user clicks "Don\'t ask again"', async () => {
89+
mockState.get.resolves(false);
90+
isUvInstalledStub.resolves(true);
91+
showInformationMessageStub.resolves(UvInstallStrings.dontAskAgain);
92+
93+
const result = await promptInstallPythonViaUv('activation', mockApi as any, mockLog);
94+
95+
assert.strictEqual(result, false);
96+
assert(mockState.set.calledWith('python-envs:uv:UV_INSTALL_PYTHON_DONT_ASK', true), 'Should set dont ask flag');
97+
});
98+
99+
test('should return false when user dismisses the dialog', async () => {
100+
mockState.get.resolves(false);
101+
isUvInstalledStub.resolves(true);
102+
showInformationMessageStub.resolves(undefined); // User dismissed
103+
104+
const result = await promptInstallPythonViaUv('activation', mockApi as any, mockLog);
105+
106+
assert.strictEqual(result, false);
107+
});
108+
109+
test('should send telemetry with correct trigger', async () => {
110+
mockState.get.resolves(false);
111+
isUvInstalledStub.resolves(true);
112+
showInformationMessageStub.resolves(undefined);
113+
114+
await promptInstallPythonViaUv('createEnvironment', mockApi as any, mockLog);
115+
116+
assert(
117+
sendTelemetryEventStub.calledWith(EventNames.UV_PYTHON_INSTALL_PROMPTED, undefined, {
118+
trigger: 'createEnvironment',
119+
}),
120+
'Should send telemetry with createEnvironment trigger',
121+
);
122+
});
123+
});
124+
125+
suite('uvPythonInstaller - isDontAskAgainSet and clearDontAskAgain', () => {
126+
let mockState: { get: sinon.SinonStub; set: sinon.SinonStub; clear: sinon.SinonStub };
127+
128+
setup(() => {
129+
mockState = {
130+
get: sinon.stub(),
131+
set: sinon.stub().resolves(),
132+
clear: sinon.stub().resolves(),
133+
};
134+
sinon.stub(persistentState, 'getGlobalPersistentState').resolves(mockState);
135+
});
136+
137+
teardown(() => {
138+
sinon.restore();
139+
});
140+
141+
test('isDontAskAgainSet should return true when flag is set', async () => {
142+
mockState.get.resolves(true);
143+
144+
const result = await isDontAskAgainSet();
145+
146+
assert.strictEqual(result, true);
147+
});
148+
149+
test('isDontAskAgainSet should return false when flag is not set', async () => {
150+
mockState.get.resolves(false);
151+
152+
const result = await isDontAskAgainSet();
153+
154+
assert.strictEqual(result, false);
155+
});
156+
157+
test('isDontAskAgainSet should return false when flag is undefined', async () => {
158+
mockState.get.resolves(undefined);
159+
160+
const result = await isDontAskAgainSet();
161+
162+
assert.strictEqual(result, false);
163+
});
164+
165+
test('clearDontAskAgain should set flag to false', async () => {
166+
await clearDontAskAgain();
167+
168+
assert(mockState.set.calledWith('python-envs:uv:UV_INSTALL_PYTHON_DONT_ASK', false), 'Should clear the flag');
169+
});
170+
});

0 commit comments

Comments
 (0)