Skip to content

Commit bacc31b

Browse files
committed
Adding tests
1 parent 18c058c commit bacc31b

File tree

1 file changed

+335
-0
lines changed

1 file changed

+335
-0
lines changed
Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import { assert } from 'chai';
5+
import * as sinon from 'sinon';
6+
import * as TypeMoq from 'typemoq';
7+
import { WorkspaceConfiguration } from 'vscode';
8+
import { IPersistentState } from '../../../client/common/types';
9+
import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis';
10+
import * as windowApis from '../../../client/common/vscodeApis/windowApis';
11+
import * as extensionsApi from '../../../client/common/vscodeApis/extensionsApi';
12+
import { IServiceContainer } from '../../../client/ioc/types';
13+
import { InstallFormatterPrompt } from '../../../client/providers/prompts/installFormatterPrompt';
14+
import * as promptUtils from '../../../client/providers/prompts/promptUtils';
15+
import { AUTOPEP8_EXTENSION, BLACK_EXTENSION, IInstallFormatterPrompt } from '../../../client/providers/prompts/types';
16+
import { Common, ToolsExtensions } from '../../../client/common/utils/localize';
17+
18+
suite('Formatter Extension prompt tests', () => {
19+
let inFormatterExtensionExperimentStub: sinon.SinonStub;
20+
let doNotShowPromptStateStub: sinon.SinonStub;
21+
let prompt: IInstallFormatterPrompt;
22+
let serviceContainer: TypeMoq.IMock<IServiceContainer>;
23+
let persistState: TypeMoq.IMock<IPersistentState<boolean>>;
24+
let getConfigurationStub: sinon.SinonStub;
25+
let isExtensionEnabledStub: sinon.SinonStub;
26+
let pythonConfig: TypeMoq.IMock<WorkspaceConfiguration>;
27+
let editorConfig: TypeMoq.IMock<WorkspaceConfiguration>;
28+
let showInformationMessageStub: sinon.SinonStub;
29+
let installFormatterExtensionStub: sinon.SinonStub;
30+
let updateDefaultFormatterStub: sinon.SinonStub;
31+
32+
setup(() => {
33+
inFormatterExtensionExperimentStub = sinon.stub(promptUtils, 'inFormatterExtensionExperiment');
34+
inFormatterExtensionExperimentStub.resolves(true);
35+
36+
doNotShowPromptStateStub = sinon.stub(promptUtils, 'doNotShowPromptState');
37+
persistState = TypeMoq.Mock.ofType<IPersistentState<boolean>>();
38+
doNotShowPromptStateStub.returns(persistState.object);
39+
40+
getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration');
41+
pythonConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
42+
editorConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
43+
getConfigurationStub.callsFake((section: string) => {
44+
if (section === 'python') {
45+
return pythonConfig.object;
46+
}
47+
return editorConfig.object;
48+
});
49+
isExtensionEnabledStub = sinon.stub(extensionsApi, 'isExtensionEnabled');
50+
showInformationMessageStub = sinon.stub(windowApis, 'showInformationMessage');
51+
installFormatterExtensionStub = sinon.stub(promptUtils, 'installFormatterExtension');
52+
updateDefaultFormatterStub = sinon.stub(promptUtils, 'updateDefaultFormatter');
53+
54+
serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
55+
56+
prompt = new InstallFormatterPrompt(serviceContainer.object);
57+
});
58+
59+
teardown(() => {
60+
sinon.restore();
61+
});
62+
63+
test('Not in experiment', async () => {
64+
inFormatterExtensionExperimentStub.resolves(false);
65+
66+
await prompt.showInstallFormatterPrompt();
67+
assert.isTrue(doNotShowPromptStateStub.notCalled);
68+
});
69+
70+
test('Do not show was set', async () => {
71+
persistState.setup((p) => p.value).returns(() => true);
72+
73+
await prompt.showInstallFormatterPrompt();
74+
assert.isTrue(getConfigurationStub.notCalled);
75+
});
76+
77+
test('Formatting provider is set to none', async () => {
78+
persistState.setup((p) => p.value).returns(() => false);
79+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'none');
80+
81+
await prompt.showInstallFormatterPrompt();
82+
assert.isTrue(isExtensionEnabledStub.notCalled);
83+
});
84+
85+
test('Formatting provider is set to yapf', async () => {
86+
persistState.setup((p) => p.value).returns(() => false);
87+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'yapf');
88+
89+
await prompt.showInstallFormatterPrompt();
90+
assert.isTrue(isExtensionEnabledStub.notCalled);
91+
});
92+
93+
test('Formatting provider is set to autopep8, and autopep8 extension is set as default formatter', async () => {
94+
persistState.setup((p) => p.value).returns(() => false);
95+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'autopep8');
96+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => AUTOPEP8_EXTENSION);
97+
98+
await prompt.showInstallFormatterPrompt();
99+
assert.isTrue(isExtensionEnabledStub.notCalled);
100+
});
101+
102+
test('Formatting provider is set to black, and black extension is set as default formatter', async () => {
103+
persistState.setup((p) => p.value).returns(() => false);
104+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'black');
105+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => BLACK_EXTENSION);
106+
107+
await prompt.showInstallFormatterPrompt();
108+
assert.isTrue(isExtensionEnabledStub.notCalled);
109+
});
110+
111+
test('Prompt: user selects do not show', async () => {
112+
persistState.setup((p) => p.value).returns(() => false);
113+
persistState
114+
.setup((p) => p.updateValue(true))
115+
.returns(() => Promise.resolve())
116+
.verifiable(TypeMoq.Times.atLeastOnce());
117+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'autopep8');
118+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
119+
isExtensionEnabledStub.returns(undefined);
120+
121+
showInformationMessageStub.resolves(Common.doNotShowAgain);
122+
123+
await prompt.showInstallFormatterPrompt();
124+
assert.isTrue(
125+
showInformationMessageStub.calledWith(
126+
ToolsExtensions.installAutopep8FormatterPrompt,
127+
'Black',
128+
'Autopep8',
129+
Common.doNotShowAgain,
130+
),
131+
'showInformationMessage should be called',
132+
);
133+
persistState.verifyAll();
134+
});
135+
136+
test('Prompt (autopep8): user selects Autopep8', async () => {
137+
persistState.setup((p) => p.value).returns(() => false);
138+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'autopep8');
139+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
140+
isExtensionEnabledStub.returns(undefined);
141+
142+
showInformationMessageStub.resolves('Autopep8');
143+
144+
await prompt.showInstallFormatterPrompt();
145+
assert.isTrue(
146+
showInformationMessageStub.calledWith(
147+
ToolsExtensions.installAutopep8FormatterPrompt,
148+
'Black',
149+
'Autopep8',
150+
Common.doNotShowAgain,
151+
),
152+
'showInformationMessage should be called',
153+
);
154+
assert.isTrue(
155+
installFormatterExtensionStub.calledWith(AUTOPEP8_EXTENSION, undefined),
156+
'installFormatterExtension should be called',
157+
);
158+
});
159+
160+
test('Prompt (autopep8): user selects Black', async () => {
161+
persistState.setup((p) => p.value).returns(() => false);
162+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'autopep8');
163+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
164+
isExtensionEnabledStub.returns(undefined);
165+
166+
showInformationMessageStub.resolves('Black');
167+
168+
await prompt.showInstallFormatterPrompt();
169+
assert.isTrue(
170+
showInformationMessageStub.calledWith(
171+
ToolsExtensions.installAutopep8FormatterPrompt,
172+
'Black',
173+
'Autopep8',
174+
Common.doNotShowAgain,
175+
),
176+
'showInformationMessage should be called',
177+
);
178+
assert.isTrue(
179+
installFormatterExtensionStub.calledWith(BLACK_EXTENSION, undefined),
180+
'installFormatterExtension should be called',
181+
);
182+
});
183+
184+
test('Prompt (black): user selects Autopep8', async () => {
185+
persistState.setup((p) => p.value).returns(() => false);
186+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'black');
187+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
188+
isExtensionEnabledStub.returns(undefined);
189+
190+
showInformationMessageStub.resolves('Autopep8');
191+
192+
await prompt.showInstallFormatterPrompt();
193+
assert.isTrue(
194+
showInformationMessageStub.calledWith(
195+
ToolsExtensions.installBlackFormatterPrompt,
196+
'Black',
197+
'Autopep8',
198+
Common.doNotShowAgain,
199+
),
200+
'showInformationMessage should be called',
201+
);
202+
assert.isTrue(
203+
installFormatterExtensionStub.calledWith(AUTOPEP8_EXTENSION, undefined),
204+
'installFormatterExtension should be called',
205+
);
206+
});
207+
208+
test('Prompt (black): user selects Black', async () => {
209+
persistState.setup((p) => p.value).returns(() => false);
210+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'black');
211+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
212+
isExtensionEnabledStub.returns(undefined);
213+
214+
showInformationMessageStub.resolves('Black');
215+
216+
await prompt.showInstallFormatterPrompt();
217+
assert.isTrue(
218+
showInformationMessageStub.calledWith(
219+
ToolsExtensions.installBlackFormatterPrompt,
220+
'Black',
221+
'Autopep8',
222+
Common.doNotShowAgain,
223+
),
224+
'showInformationMessage should be called',
225+
);
226+
assert.isTrue(
227+
installFormatterExtensionStub.calledWith(BLACK_EXTENSION, undefined),
228+
'installFormatterExtension should be called',
229+
);
230+
});
231+
232+
test('Prompt: Black and Autopep8 installed user selects Black as default', async () => {
233+
persistState.setup((p) => p.value).returns(() => false);
234+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'black');
235+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
236+
isExtensionEnabledStub.returns({});
237+
238+
showInformationMessageStub.resolves('Black');
239+
240+
await prompt.showInstallFormatterPrompt();
241+
assert.isTrue(
242+
showInformationMessageStub.calledWith(
243+
ToolsExtensions.selectMultipleFormattersPrompt,
244+
'Black',
245+
'Autopep8',
246+
Common.doNotShowAgain,
247+
),
248+
'showInformationMessage should be called',
249+
);
250+
assert.isTrue(
251+
updateDefaultFormatterStub.calledWith(BLACK_EXTENSION, undefined),
252+
'updateDefaultFormatter should be called',
253+
);
254+
});
255+
256+
test('Prompt: Black and Autopep8 installed user selects Autopep8 as default', async () => {
257+
persistState.setup((p) => p.value).returns(() => false);
258+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'autopep8');
259+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
260+
isExtensionEnabledStub.returns({});
261+
262+
showInformationMessageStub.resolves('Autopep8');
263+
264+
await prompt.showInstallFormatterPrompt();
265+
assert.isTrue(
266+
showInformationMessageStub.calledWith(
267+
ToolsExtensions.selectMultipleFormattersPrompt,
268+
'Black',
269+
'Autopep8',
270+
Common.doNotShowAgain,
271+
),
272+
'showInformationMessage should be called',
273+
);
274+
assert.isTrue(
275+
updateDefaultFormatterStub.calledWith(AUTOPEP8_EXTENSION, undefined),
276+
'updateDefaultFormatter should be called',
277+
);
278+
});
279+
280+
test('Prompt: Black installed user selects Black as default', async () => {
281+
persistState.setup((p) => p.value).returns(() => false);
282+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'black');
283+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
284+
isExtensionEnabledStub.callsFake((extensionId) => {
285+
if (extensionId === BLACK_EXTENSION) {
286+
return {};
287+
}
288+
return undefined;
289+
});
290+
291+
showInformationMessageStub.resolves('Black');
292+
293+
await prompt.showInstallFormatterPrompt();
294+
assert.isTrue(
295+
showInformationMessageStub.calledWith(
296+
ToolsExtensions.selectBlackFormatterPrompt,
297+
Common.bannerLabelYes,
298+
Common.doNotShowAgain,
299+
),
300+
'showInformationMessage should be called',
301+
);
302+
assert.isTrue(
303+
updateDefaultFormatterStub.calledWith(BLACK_EXTENSION, undefined),
304+
'updateDefaultFormatter should be called',
305+
);
306+
});
307+
308+
test('Prompt: Autopep8 installed user selects Autopep8 as default', async () => {
309+
persistState.setup((p) => p.value).returns(() => false);
310+
pythonConfig.setup((p) => p.get('formatting.provider', TypeMoq.It.isAny())).returns(() => 'autopep8');
311+
editorConfig.setup((p) => p.get('defaultFormatter', TypeMoq.It.isAny())).returns(() => '');
312+
isExtensionEnabledStub.callsFake((extensionId) => {
313+
if (extensionId === AUTOPEP8_EXTENSION) {
314+
return {};
315+
}
316+
return undefined;
317+
});
318+
319+
showInformationMessageStub.resolves('Autopep8');
320+
321+
await prompt.showInstallFormatterPrompt();
322+
assert.isTrue(
323+
showInformationMessageStub.calledWith(
324+
ToolsExtensions.selectAutopep8FormatterPrompt,
325+
Common.bannerLabelYes,
326+
Common.doNotShowAgain,
327+
),
328+
'showInformationMessage should be called',
329+
);
330+
assert.isTrue(
331+
updateDefaultFormatterStub.calledWith(AUTOPEP8_EXTENSION, undefined),
332+
'updateDefaultFormatter should be called',
333+
);
334+
});
335+
});

0 commit comments

Comments
 (0)