|
1 | 1 | import * as assert from 'assert';
|
2 | 2 | import * as vscode from 'vscode';
|
3 |
| -import configUrlsToWatchRequired from './examples/config-urls-to-watch-required.json'; |
4 |
| -import configPluginConfigRequired from './examples/config-plugin-config-required.json'; |
5 |
| -import configPluginConfigRequiredDisabled from './examples/config-plugin-config-required-disabled.json'; |
6 |
| -import configPluginConfigMissing from './examples/config-plugin-config-missing.json'; |
7 |
| -import configPluginConfigMissingDisabled from './examples/config-plugin-config-missing.json'; |
8 |
| -import {isConfigFile} from '../helpers'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import { |
| 5 | + getASTNode, |
| 6 | + getStartPositionFromASTNode, |
| 7 | + isConfigFile, |
| 8 | + sleep, |
| 9 | +} from '../helpers'; |
9 | 10 | import * as path from 'path';
|
| 11 | +import parse from 'json-to-ast'; |
10 | 12 | import {createCodeLensForPluginNodes} from '../codelens';
|
11 | 13 |
|
12 | 14 | suite('Extension Test Suite', () => {
|
13 |
| - test('should activate when JSON file is opened', async () => { |
14 |
| - assert.strictEqual( |
15 |
| - vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit') |
16 |
| - ?.isActive, |
17 |
| - false |
18 |
| - ); |
| 15 | + test('should activate when untitled JSON file is opened', async () => { |
| 16 | + const extensionId = 'garrytrinder.dev-proxy-toolkit'; |
19 | 17 | await vscode.workspace.openTextDocument({
|
20 | 18 | language: 'json',
|
21 | 19 | content: '',
|
22 | 20 | });
|
23 | 21 | await sleep(1000);
|
24 |
| - assert.strictEqual( |
25 |
| - vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit') |
26 |
| - ?.isActive, |
27 |
| - true |
28 |
| - ); |
29 |
| - }); |
30 | 22 |
|
31 |
| - test('should show error when no urlsToWatch found', async () => { |
32 |
| - const document = await vscode.workspace.openTextDocument({ |
33 |
| - language: 'json', |
34 |
| - content: JSON.stringify(configUrlsToWatchRequired, null, 2), |
35 |
| - }); |
36 |
| - await sleep(1000); |
37 |
| - const diagnostics = vscode.languages.getDiagnostics(document.uri); |
38 |
| - assert.strictEqual(diagnostics?.length, 1); |
39 |
| - assert.strictEqual( |
40 |
| - diagnostics?.[0].message, |
41 |
| - 'Add at least one url to watch.' |
42 |
| - ); |
43 |
| - assert.strictEqual( |
44 |
| - diagnostics?.[0].severity, |
45 |
| - vscode.DiagnosticSeverity.Error |
46 |
| - ); |
| 23 | + const expected = true; |
| 24 | + const actual = vscode.extensions.getExtension(extensionId)?.isActive; |
| 25 | + assert.strictEqual(actual, expected); |
47 | 26 | });
|
48 | 27 |
|
49 |
| - test('should show error when plugin requires config section', async () => { |
50 |
| - const document = await vscode.workspace.openTextDocument({ |
51 |
| - language: 'json', |
52 |
| - content: JSON.stringify(configPluginConfigRequired, null, 2), |
53 |
| - }); |
| 28 | + test('should activate when JSON file is opened from disk', async () => { |
| 29 | + const extensionId = 'garrytrinder.dev-proxy-toolkit'; |
| 30 | + const fileName = 'foo.json'; |
| 31 | + const filePath = path.resolve(__dirname, 'examples', fileName); |
| 32 | + await vscode.workspace.openTextDocument(filePath); |
54 | 33 | await sleep(1000);
|
55 |
| - const diagnostics = vscode.languages.getDiagnostics(document.uri); |
56 |
| - assert.strictEqual(diagnostics?.length, 1); |
57 |
| - assert.strictEqual( |
58 |
| - diagnostics?.[0].message, |
59 |
| - 'GenericRandomErrorPlugin requires a config section.' |
60 |
| - ); |
61 |
| - assert.strictEqual( |
62 |
| - diagnostics?.[0].severity, |
63 |
| - vscode.DiagnosticSeverity.Error |
64 |
| - ); |
| 34 | + |
| 35 | + const expected = true; |
| 36 | + const actual = vscode.extensions.getExtension(extensionId)?.isActive; |
| 37 | + assert.strictEqual(actual, expected); |
65 | 38 | });
|
| 39 | +}); |
66 | 40 |
|
67 |
| - test('should show warning when disabled plugin requires config section', async () => { |
68 |
| - const document = await vscode.workspace.openTextDocument({ |
69 |
| - language: 'json', |
70 |
| - content: JSON.stringify(configPluginConfigRequiredDisabled, null, 2), |
71 |
| - }); |
72 |
| - await sleep(1000); |
| 41 | +suite('urlsToWatch', () => { |
| 42 | + test('should show error when opening document with no urlsToWatch found', async () => { |
| 43 | + const fileName = 'config-urls-to-watch-required.json'; |
| 44 | + const filePath = path.resolve(__dirname, 'examples', fileName); |
| 45 | + const document = await vscode.workspace.openTextDocument(filePath); |
73 | 46 | const diagnostics = vscode.languages.getDiagnostics(document.uri);
|
74 |
| - assert.strictEqual(diagnostics?.length, 1); |
75 |
| - assert.strictEqual( |
76 |
| - diagnostics?.[0].message, |
77 |
| - 'GenericRandomErrorPlugin requires a config section.' |
78 |
| - ); |
79 |
| - assert.strictEqual( |
80 |
| - diagnostics?.[0].severity, |
81 |
| - vscode.DiagnosticSeverity.Warning |
82 |
| - ); |
| 47 | + |
| 48 | + const expected = { |
| 49 | + message: 'Add at least one url to watch.', |
| 50 | + severity: vscode.DiagnosticSeverity.Error, |
| 51 | + }; |
| 52 | + const actual = { |
| 53 | + message: diagnostics[0]?.message, |
| 54 | + severity: diagnostics[0]?.severity, |
| 55 | + }; |
| 56 | + assert.deepStrictEqual(actual, expected); |
83 | 57 | });
|
84 | 58 |
|
85 |
| - test('should show error when plugin config section is not defined', async () => { |
| 59 | + test('should show error when document changes and has no urlsToWatch found', async () => { |
| 60 | + const fileName = 'config-urls-to-watch-required.json'; |
| 61 | + const filePath = path.resolve(__dirname, 'examples', fileName); |
| 62 | + const fileContents = fs.readFileSync(filePath, 'utf8'); |
86 | 63 | const document = await vscode.workspace.openTextDocument({
|
87 | 64 | language: 'json',
|
88 |
| - content: JSON.stringify(configPluginConfigMissing, null, 2), |
| 65 | + content: fileContents, |
89 | 66 | });
|
90 | 67 | await sleep(1000);
|
91 | 68 | const diagnostics = vscode.languages.getDiagnostics(document.uri);
|
92 |
| - assert.strictEqual(diagnostics?.length, 1); |
93 |
| - assert.strictEqual( |
94 |
| - diagnostics?.[0].message, |
95 |
| - "genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one." |
96 |
| - ); |
97 |
| - assert.strictEqual( |
98 |
| - diagnostics?.[0].severity, |
99 |
| - vscode.DiagnosticSeverity.Error |
100 |
| - ); |
| 69 | + |
| 70 | + const expected = { |
| 71 | + message: 'Add at least one url to watch.', |
| 72 | + severity: vscode.DiagnosticSeverity.Error, |
| 73 | + }; |
| 74 | + const actual = { |
| 75 | + message: diagnostics[0]?.message, |
| 76 | + severity: diagnostics[0]?.severity, |
| 77 | + }; |
| 78 | + assert.deepStrictEqual(actual, expected); |
101 | 79 | });
|
102 | 80 |
|
103 |
| - test('should show warning when disabled plugin config section is not defined', async () => { |
104 |
| - const document = await vscode.workspace.openTextDocument({ |
105 |
| - language: 'json', |
106 |
| - content: JSON.stringify(configPluginConfigMissingDisabled, null, 2), |
107 |
| - }); |
| 81 | + test('should have no error after adding a urlToWatch', async () => { |
| 82 | + const fileName = 'config-urls-to-watch-required.json'; |
| 83 | + const filePath = path.resolve(__dirname, 'examples', fileName); |
| 84 | + const document = await vscode.workspace.openTextDocument(filePath); |
108 | 85 | await sleep(1000);
|
109 |
| - const diagnostics = vscode.languages.getDiagnostics(document.uri); |
110 |
| - assert.strictEqual(diagnostics?.length, 1); |
111 |
| - assert.strictEqual( |
112 |
| - diagnostics?.[0].message, |
113 |
| - "genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one." |
| 86 | + const documentNode = parse(document.getText()) as parse.ObjectNode; |
| 87 | + const urlsToWatchNode = getASTNode( |
| 88 | + documentNode.children, |
| 89 | + 'Identifier', |
| 90 | + 'urlsToWatch' |
114 | 91 | );
|
115 |
| - assert.strictEqual( |
116 |
| - diagnostics?.[0].severity, |
117 |
| - vscode.DiagnosticSeverity.Error |
| 92 | + const position = getStartPositionFromASTNode( |
| 93 | + urlsToWatchNode?.value as parse.ArrayNode |
118 | 94 | );
|
| 95 | + const edit = new vscode.WorkspaceEdit(); |
| 96 | + edit.insert(document.uri, position, '"https://example.com"'); |
| 97 | + await vscode.workspace.applyEdit(edit); |
| 98 | + const diagnostics = vscode.languages.getDiagnostics(document.uri); |
| 99 | + |
| 100 | + const expected = 0; |
| 101 | + const actual = diagnostics.length; |
| 102 | + assert.strictEqual(actual, expected); |
119 | 103 | });
|
120 | 104 | });
|
121 | 105 |
|
@@ -187,23 +171,90 @@ suite('isConfigFile', () => {
|
187 | 171 | });
|
188 | 172 | });
|
189 | 173 |
|
190 |
| -suite('Plugins Code Lens', () => { |
| 174 | +suite('plugins', () => { |
| 175 | + test('should show error when plugin requires config section', async () => { |
| 176 | + const fileName = 'config-plugin-config-required.json'; |
| 177 | + const filePath = path.resolve(__dirname, 'examples', fileName); |
| 178 | + const document = await vscode.workspace.openTextDocument(filePath); |
| 179 | + await sleep(1000); |
| 180 | + const diagnostics = vscode.languages.getDiagnostics(document.uri); |
| 181 | + |
| 182 | + const expected = { |
| 183 | + message: 'GenericRandomErrorPlugin requires a config section.', |
| 184 | + severity: vscode.DiagnosticSeverity.Error, |
| 185 | + }; |
| 186 | + const actual = { |
| 187 | + message: diagnostics[0]?.message, |
| 188 | + severity: diagnostics[0]?.severity, |
| 189 | + }; |
| 190 | + assert.deepStrictEqual(actual, expected); |
| 191 | + }); |
| 192 | + |
| 193 | + test('should show warning when disabled plugin requires config section', async () => { |
| 194 | + const fileName = 'config-plugin-config-required-disabled.json'; |
| 195 | + const filePath = path.resolve(__dirname, 'examples', fileName); |
| 196 | + const document = await vscode.workspace.openTextDocument(filePath); |
| 197 | + await sleep(1000); |
| 198 | + const diagnostics = vscode.languages.getDiagnostics(document.uri); |
| 199 | + |
| 200 | + const expected = { |
| 201 | + message: 'GenericRandomErrorPlugin requires a config section.', |
| 202 | + severity: vscode.DiagnosticSeverity.Warning, |
| 203 | + }; |
| 204 | + const actual = { |
| 205 | + message: diagnostics[0]?.message, |
| 206 | + severity: diagnostics[0]?.severity, |
| 207 | + }; |
| 208 | + assert.deepStrictEqual(actual, expected); |
| 209 | + }); |
| 210 | + |
| 211 | + test('should show error when plugin config section is not defined', async () => { |
| 212 | + const fileName = 'config-plugin-config-missing.json'; |
| 213 | + const filePath = path.resolve(__dirname, 'examples', fileName); |
| 214 | + const document = await vscode.workspace.openTextDocument(filePath); |
| 215 | + await sleep(1000); |
| 216 | + const diagnostics = vscode.languages.getDiagnostics(document.uri); |
| 217 | + |
| 218 | + const expected = { |
| 219 | + message: |
| 220 | + "genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one.", |
| 221 | + severity: vscode.DiagnosticSeverity.Error, |
| 222 | + }; |
| 223 | + const actual = { |
| 224 | + message: diagnostics[0]?.message, |
| 225 | + severity: diagnostics[0]?.severity, |
| 226 | + }; |
| 227 | + assert.deepStrictEqual(actual, expected); |
| 228 | + }); |
| 229 | + |
| 230 | + test('should show warning when disabled plugin config section is not defined', async () => { |
| 231 | + const fileName = 'config-plugin-config-missing-disabled.json'; |
| 232 | + const filePath = path.resolve(__dirname, 'examples', fileName); |
| 233 | + const document = await vscode.workspace.openTextDocument(filePath); |
| 234 | + await sleep(1000); |
| 235 | + const diagnostics = vscode.languages.getDiagnostics(document.uri); |
| 236 | + |
| 237 | + const expected = { |
| 238 | + message: |
| 239 | + "genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one.", |
| 240 | + severity: vscode.DiagnosticSeverity.Warning, |
| 241 | + }; |
| 242 | + const actual = { |
| 243 | + message: diagnostics[0]?.message, |
| 244 | + severity: diagnostics[0]?.severity, |
| 245 | + }; |
| 246 | + assert.deepStrictEqual(actual, expected); |
| 247 | + }); |
| 248 | + |
191 | 249 | test('should show code lens for each plugin', async () => {
|
192 | 250 | const fileName = 'config-plugins-codelens.json';
|
193 | 251 | const filePath = path.resolve(__dirname, 'examples', fileName);
|
194 | 252 | const document = await vscode.workspace.openTextDocument(filePath);
|
195 | 253 | await sleep(1000);
|
196 |
| - |
197 | 254 | const codeLens = createCodeLensForPluginNodes(document);
|
198 | 255 |
|
199 | 256 | const expected = 2;
|
200 | 257 | const actual = codeLens.length;
|
201 | 258 | assert.strictEqual(actual, expected);
|
202 | 259 | });
|
203 | 260 | });
|
204 |
| - |
205 |
| -function sleep(ms: number): Promise<void> { |
206 |
| - return new Promise(resolve => { |
207 |
| - setTimeout(resolve, ms); |
208 |
| - }); |
209 |
| -} |
|
0 commit comments