-
Notifications
You must be signed in to change notification settings - Fork 697
/
Copy pathcodeactions.integration.test.ts
348 lines (293 loc) · 12.1 KB
/
codeactions.integration.test.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as path from 'path';
import { describe, beforeAll, beforeEach, afterAll, test, expect, afterEach } from '@jest/globals';
import testAssetWorkspace from './testAssets/testAssetWorkspace';
import {
activateCSharpExtension,
closeAllEditorsAsync,
expectText,
openFileInWorkspaceAsync,
} from './integrationHelpers';
describe(`Code Actions Tests`, () => {
beforeAll(async () => {
await activateCSharpExtension();
});
beforeEach(async () => {
const fileName = path.join('src', 'app', 'CodeActions.cs');
await openFileInWorkspaceAsync(fileName);
});
afterAll(async () => {
await testAssetWorkspace.cleanupWorkspace();
});
afterEach(async () => {
await closeAllEditorsAsync();
});
test('Lightbulb displays actions', async () => {
const actions = await getCodeActions(new vscode.Range(0, 0, 0, 12));
expect(actions.length).toBeGreaterThanOrEqual(3);
// Verify we have unresolved code actions.
expect(actions[0].title).toBe('Remove unnecessary usings');
expect(actions[0].kind).toStrictEqual(vscode.CodeActionKind.QuickFix);
expect(actions[0].edit).toBeUndefined();
expect(actions[0].command).toBeUndefined();
expect(actions[1].title).toBe('Fix All: Remove unnecessary usings');
expect(actions[1].kind).toStrictEqual(vscode.CodeActionKind.QuickFix);
expect(actions[1].edit).toBeUndefined();
expect(actions[1].command).toBeDefined();
expect(actions[2].title).toBe('Suppress or configure issues');
expect(actions[2].kind).toStrictEqual(vscode.CodeActionKind.QuickFix);
expect(actions[2].edit).toBeUndefined();
expect(actions[2].command).toBeDefined();
});
test('Remove unnecessary usings applied', async () => {
const actions = await getCodeActions(new vscode.Range(0, 0, 0, 12), 10);
expect(actions[0].title).toBe('Remove unnecessary usings');
expect(actions[0].edit).toBeDefined();
await vscode.workspace.applyEdit(actions[0].edit!);
await expectText(vscode.window.activeTextEditor!.document, [
'namespace CodeActionsTests;',
'',
'class CodeActions',
'{',
' static void Do() { Method(); }',
' static void Method()',
' {',
' var x = 1;',
' Do();',
' }',
'}',
]);
});
test('Add accessibility modifiers applied', async () => {
const actions = await getCodeActions(new vscode.Range(6, 16, 6, 19), 10);
const action = actions.find((a) => a.title === 'Add accessibility modifiers');
expect(action).toBeDefined();
expect(action!.edit).toBeDefined();
await vscode.workspace.applyEdit(action!.edit!);
await expectText(vscode.window.activeTextEditor!.document, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'class CodeActions',
'{',
' private static void Do() { Method(); }',
' static void Method()',
' {',
' var x = 1;',
' Do();',
' }',
'}',
]);
});
test('Fix all in document', async () => {
const action = await getSpecificCodeAction(
new vscode.Range(6, 16, 6, 19),
'Fix All: Add accessibility modifiers'
);
expect(action.edit).toBeUndefined();
expect(action.command).toBeDefined();
await invokeQuickPickAction(action, /*quickPickIndex: Document*/ 0);
await expectText(vscode.window.activeTextEditor!.document, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'internal class CodeActions',
'{',
' private static void Do() { Method(); }',
' private static void Method()',
' {',
' var x = 1;',
' Do();',
' }',
'}',
]);
});
test('Fix all in project', async () => {
const action = await getSpecificCodeAction(
new vscode.Range(6, 16, 6, 19),
'Fix All: Add accessibility modifiers'
);
expect(action.edit).toBeUndefined();
expect(action.command).toBeDefined();
await invokeQuickPickAction(action, /*quickPickIndex: Project*/ 1);
await expectText(vscode.window.activeTextEditor!.document, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'internal class CodeActions',
'{',
' private static void Do() { Method(); }',
' private static void Method()',
' {',
' var x = 1;',
' Do();',
' }',
'}',
]);
const projectFile = vscode.workspace.textDocuments.find((d) => d.fileName.endsWith('CodeActionsInProject.cs'));
expect(projectFile).toBeDefined();
await expectText(projectFile!, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'internal class CodeActionsInProject',
'{',
'}',
]);
});
test('Fix all in solution', async () => {
const action = await getSpecificCodeAction(
new vscode.Range(6, 16, 6, 19),
'Fix All: Add accessibility modifiers'
);
expect(action.edit).toBeUndefined();
expect(action.command).toBeDefined();
await invokeQuickPickAction(action, /*quickPickIndex: Solution*/ 2);
await expectText(vscode.window.activeTextEditor!.document, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'internal class CodeActions',
'{',
' private static void Do() { Method(); }',
' private static void Method()',
' {',
' var x = 1;',
' Do();',
' }',
'}',
]);
const currentProjectFile = vscode.workspace.textDocuments.find((d) =>
d.fileName.endsWith('CodeActionsInProject.cs')
);
expect(currentProjectFile).toBeDefined();
await expectText(currentProjectFile!, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'internal class CodeActionsInProject',
'{',
'}',
]);
const otherProjectFile = vscode.workspace.textDocuments.find((d) =>
d.fileName.endsWith('CodeActionsInSolution.cs')
);
expect(otherProjectFile).toBeDefined();
await expectText(otherProjectFile!, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'internal class CodeActionsInSolution',
'{',
'}',
]);
});
test('Nested action', async () => {
const action = await getSpecificCodeAction(new vscode.Range(9, 12, 9, 12), 'Convert number');
expect(action.edit).toBeUndefined();
expect(action.command).toBeDefined();
await invokeQuickPickAction(action, /*quickPickIndex: Convert to binary*/ 0);
await expectText(vscode.window.activeTextEditor!.document, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'class CodeActions',
'{',
' static void Do() { Method(); }',
' static void Method()',
' {',
' var x = 0b1;',
' Do();',
' }',
'}',
]);
});
test('Suppress warning', async () => {
const action = await getSpecificCodeAction(new vscode.Range(9, 12, 9, 12), 'Suppress or configure issues');
expect(action.edit).toBeUndefined();
expect(action.command).toBeDefined();
await invokeQuickPickAction(action, /*quickPickIndex: Suppress CS0219 -> in Source*/ 0);
await expectText(vscode.window.activeTextEditor!.document, [
'using System;',
'',
'namespace CodeActionsTests;',
'',
'class CodeActions',
'{',
' static void Do() { Method(); }',
' static void Method()',
' {',
'#pragma warning disable CS0219 // Variable is assigned but its value is never used',
' var x = 1;',
'#pragma warning restore CS0219 // Variable is assigned but its value is never used',
' Do();',
' }',
'}',
]);
});
test('Configure code style option', async () => {
const action = await getSpecificCodeAction(new vscode.Range(6, 16, 6, 19), 'Suppress or configure issues');
expect(action.edit).toBeUndefined();
expect(action.command).toBeDefined();
await invokeQuickPickAction(action, /*quickPickIndex: Configure IDE0040 code style -> never*/ 0);
const editorConfigFile = vscode.workspace.textDocuments.find((d) => d.fileName.endsWith('.editorconfig'));
expect(editorConfigFile).toBeDefined();
expect(editorConfigFile!.getText()).toContain('dotnet_style_require_accessibility_modifiers = never');
});
test('Configure analyzer severity', async () => {
const action = await getSpecificCodeAction(new vscode.Range(6, 16, 6, 19), 'Suppress or configure issues');
expect(action.edit).toBeUndefined();
expect(action.command).toBeDefined();
await invokeQuickPickAction(action, /*quickPickIndex: Configure IDE0040 severity -> None*/ 4);
const editorConfigFile = vscode.workspace.textDocuments.find((d) => d.fileName.endsWith('.editorconfig'));
expect(editorConfigFile).toBeDefined();
expect(editorConfigFile!.getText()).toContain('dotnet_diagnostic.IDE0040.severity = none');
});
});
async function getCodeActions(
range: vscode.Range,
resolveCount: number | undefined = undefined
): Promise<vscode.CodeAction[]> {
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
vscode.window.activeTextEditor!.document.uri,
range,
/** kind **/ undefined,
resolveCount
);
return codeActions;
}
async function getSpecificCodeAction(range: vscode.Range, title: string): Promise<vscode.CodeAction> {
const codeActions = await getCodeActions(range, 100);
const action = codeActions.find((action) => action.title === title);
if (!action) {
throw new Error(`Code action '${title}' not found in ${codeActions.map((a) => a.title).join(', ')}`);
}
return action;
}
async function invokeQuickPickAction(codeAction: vscode.CodeAction, quickPickIndex: number): Promise<void> {
// Invoke, but do not await the command (the command blocks until a quick pick item is resolved)
const promise = vscode.commands.executeCommand(codeAction.command!.command, ...codeAction.command!.arguments!);
// workbench.action.quickOpenSelectNext selects the next quick pick item.
// It must be called at least once to select the first item.
for (let i = 0; i <= quickPickIndex; i++) {
// First call ensures the quick pick is populated with items and the first is selected.
await vscode.commands.executeCommand('workbench.action.quickOpenSelectNext');
}
// Accept the selected item
await vscode.commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem');
// Finally wait for the command to complete.
await promise;
}