forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathlinterCommands.unit.test.ts
More file actions
177 lines (150 loc) · 8.48 KB
/
linterCommands.unit.test.ts
File metadata and controls
177 lines (150 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import { anything, capture, deepEqual, instance, mock, verify, when } from 'ts-mockito';
import { ApplicationShell } from '../../client/common/application/applicationShell';
import { CommandManager } from '../../client/common/application/commandManager';
import { DocumentManager } from '../../client/common/application/documentManager';
import { IApplicationShell, ICommandManager, IDocumentManager } from '../../client/common/application/types';
import { Commands } from '../../client/common/constants';
import { Product } from '../../client/common/types';
import { ServiceContainer } from '../../client/ioc/container';
import { LinterCommands } from '../../client/linters/linterCommands';
import { LinterManager } from '../../client/linters/linterManager';
import { LintingEngine } from '../../client/linters/lintingEngine';
import { ILinterInfo, ILinterManager, ILintingEngine } from '../../client/linters/types';
suite('Linting - Linter Commands', () => {
let linterCommands: LinterCommands;
let manager: ILinterManager;
let shell: IApplicationShell;
let docManager: IDocumentManager;
let cmdManager: ICommandManager;
let lintingEngine: ILintingEngine;
setup(() => {
const svcContainer = mock(ServiceContainer);
manager = mock(LinterManager);
shell = mock(ApplicationShell);
docManager = mock(DocumentManager);
cmdManager = mock(CommandManager);
lintingEngine = mock(LintingEngine);
when(svcContainer.get<ILinterManager>(ILinterManager)).thenReturn(instance(manager));
when(svcContainer.get<IApplicationShell>(IApplicationShell)).thenReturn(instance(shell));
when(svcContainer.get<IDocumentManager>(IDocumentManager)).thenReturn(instance(docManager));
when(svcContainer.get<ICommandManager>(ICommandManager)).thenReturn(instance(cmdManager));
when(svcContainer.get<ILintingEngine>(ILintingEngine)).thenReturn(instance(lintingEngine));
linterCommands = new LinterCommands(instance(svcContainer));
});
test('Commands are registered', () => {
verify(cmdManager.registerCommand(Commands.Set_Linter, anything())).once();
verify(cmdManager.registerCommand(Commands.Enable_Linter, anything())).once();
verify(cmdManager.registerCommand(Commands.Run_Linter, anything())).once();
});
test('Run Linting method will lint all open files', async () => {
when(lintingEngine.lintOpenPythonFiles()).thenResolve('Hello' as any);
const result = await linterCommands.runLinting();
expect(result).to.be.equal('Hello');
});
async function testEnableLintingWithCurrentState(
currentState: boolean,
selectedState: 'Enable' | 'Disable' | undefined,
) {
when(manager.isLintingEnabled(true, anything())).thenResolve(currentState);
const expectedQuickPickOptions = {
matchOnDetail: true,
matchOnDescription: true,
placeHolder: `current: ${currentState ? 'Enable' : 'Disable'}`,
};
when(shell.showQuickPick(anything(), anything())).thenResolve(selectedState as any);
await linterCommands.enableLintingAsync();
verify(shell.showQuickPick(anything(), anything())).once();
const options = capture(shell.showQuickPick).last()[0];
const quickPickOptions = capture(shell.showQuickPick).last()[1];
expect(options).to.deep.equal(['Enable', 'Disable']);
expect(quickPickOptions).to.deep.equal(expectedQuickPickOptions);
if (selectedState) {
verify(manager.enableLintingAsync(selectedState === 'Enable', anything())).once();
} else {
verify(manager.enableLintingAsync(anything(), anything())).never();
}
}
test("Enable linting should check if linting is enabled, and display current state of 'Enable' and select nothing", async () => {
await testEnableLintingWithCurrentState(true, undefined);
});
test("Enable linting should check if linting is enabled, and display current state of 'Enable' and select 'Enable'", async () => {
await testEnableLintingWithCurrentState(true, 'Enable');
});
test("Enable linting should check if linting is enabled, and display current state of 'Enable' and select 'Disable'", async () => {
await testEnableLintingWithCurrentState(true, 'Disable');
});
test("Enable linting should check if linting is enabled, and display current state of 'Disable' and select 'Enable'", async () => {
await testEnableLintingWithCurrentState(true, 'Enable');
});
test("Enable linting should check if linting is enabled, and display current state of 'Disable' and select 'Disable'", async () => {
await testEnableLintingWithCurrentState(true, 'Disable');
});
test('Set Linter should display a quickpick', async () => {
when(manager.getAllLinterInfos()).thenReturn([]);
when(manager.getActiveLinters(true, anything())).thenResolve([]);
when(shell.showQuickPick(anything(), anything())).thenResolve();
const expectedQuickPickOptions = {
matchOnDetail: true,
matchOnDescription: true,
placeHolder: 'current: none',
};
await linterCommands.setLinterAsync();
verify(shell.showQuickPick(anything(), anything()));
const quickPickOptions = capture(shell.showQuickPick).last()[1];
expect(quickPickOptions).to.deep.equal(expectedQuickPickOptions);
});
test('Set Linter should display a quickpick and currently active linter when only one is enabled', async () => {
const linterId = 'Hello World';
const activeLinters: ILinterInfo[] = [{ id: linterId } as any];
when(manager.getAllLinterInfos()).thenReturn([]);
when(manager.getActiveLinters(true, anything())).thenResolve(activeLinters);
when(shell.showQuickPick(anything(), anything())).thenResolve();
const expectedQuickPickOptions = {
matchOnDetail: true,
matchOnDescription: true,
placeHolder: `current: ${linterId}`,
};
await linterCommands.setLinterAsync();
verify(shell.showQuickPick(anything(), anything())).once();
const quickPickOptions = capture(shell.showQuickPick).last()[1];
expect(quickPickOptions).to.deep.equal(expectedQuickPickOptions);
});
test('Set Linter should display a quickpick and with message about multiple linters being enabled', async () => {
const activeLinters: ILinterInfo[] = [{ id: 'linterId' } as any, { id: 'linterId2' } as any];
when(manager.getAllLinterInfos()).thenReturn([]);
when(manager.getActiveLinters(true, anything())).thenResolve(activeLinters);
when(shell.showQuickPick(anything(), anything())).thenResolve();
const expectedQuickPickOptions = {
matchOnDetail: true,
matchOnDescription: true,
placeHolder: 'current: multiple selected',
};
await linterCommands.setLinterAsync();
verify(shell.showQuickPick(anything(), anything()));
const quickPickOptions = capture(shell.showQuickPick).last()[1];
expect(quickPickOptions).to.deep.equal(expectedQuickPickOptions);
});
test('Selecting a linter should display warning message about multiple linters', async () => {
const linters: ILinterInfo[] = [{ id: '1' }, { id: '2' }, { id: '3', product: 'Three' }] as any;
const activeLinters: ILinterInfo[] = [{ id: '1' }, { id: '3' }] as any;
when(manager.getAllLinterInfos()).thenReturn(linters);
when(manager.getActiveLinters(true, anything())).thenResolve(activeLinters);
when(shell.showQuickPick(anything(), anything())).thenResolve('3' as any);
when(shell.showWarningMessage(anything(), 'Yes', 'No')).thenResolve('Yes' as any);
const expectedQuickPickOptions = {
matchOnDetail: true,
matchOnDescription: true,
placeHolder: 'current: multiple selected',
};
await linterCommands.setLinterAsync();
verify(shell.showQuickPick(anything(), anything())).once();
verify(shell.showWarningMessage(anything(), 'Yes', 'No')).once();
const quickPickOptions = capture(shell.showQuickPick).last()[1];
expect(quickPickOptions).to.deep.equal(expectedQuickPickOptions);
verify(manager.setActiveLintersAsync(deepEqual([('Three' as any) as Product]), anything())).once();
});
});