Skip to content

Commit 011906c

Browse files
committed
Add tests and fix formatting issues
1 parent fad2c7f commit 011906c

8 files changed

+198
-32
lines changed

src/extension.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import parse from 'json-to-ast';
3-
import { pluginSnippets } from './constants';
4-
import { getASTNode, getRangeFromASTNode } from './helpers';
3+
import {pluginSnippets} from './constants';
4+
import {getASTNode, getRangeFromASTNode} from './helpers';
55

66
export const activate = (context: vscode.ExtensionContext) => {
77
const collection = vscode.languages.createDiagnosticCollection('Dev Proxy');
@@ -14,10 +14,10 @@ export const activate = (context: vscode.ExtensionContext) => {
1414

1515
context.subscriptions.push(
1616
vscode.workspace.onDidChangeTextDocument(event => {
17-
if (event.document.getText() === "") {
17+
if (event.document.getText() === '') {
1818
collection.delete(event.document.uri);
1919
return;
20-
};
20+
}
2121
updateDiagnostics(event.document, collection);
2222
})
2323
);
@@ -115,9 +115,12 @@ const updateDiagnostics = (
115115
'Identifier',
116116
'enabled'
117117
);
118-
const isEnabled = (enabledNode?.value as parse.LiteralNode).value as boolean;
118+
const isEnabled = (enabledNode?.value as parse.LiteralNode)
119+
.value as boolean;
119120
const pluginSnippet = pluginSnippets[pluginName];
120-
const requiresConfig = pluginSnippet.config ? pluginSnippet.config.required : false;
121+
const requiresConfig = pluginSnippet.config
122+
? pluginSnippet.config.required
123+
: false;
121124

122125
if (requiresConfig) {
123126
// check to see if the plugin has a config section
@@ -132,7 +135,9 @@ const updateDiagnostics = (
132135
new vscode.Diagnostic(
133136
getRangeFromASTNode(pluginNode),
134137
`${pluginName} requires a config section.`,
135-
vscode.DiagnosticSeverity.Error
138+
isEnabled
139+
? vscode.DiagnosticSeverity.Error
140+
: vscode.DiagnosticSeverity.Warning
136141
)
137142
);
138143
} else {
@@ -151,7 +156,9 @@ const updateDiagnostics = (
151156
new vscode.Diagnostic(
152157
getRangeFromASTNode(configSectionNode.value),
153158
`${configSectionName} config section is missing. Use '${pluginSnippet.config?.name}' snippet to create one.`,
154-
isEnabled ? vscode.DiagnosticSeverity.Error : vscode.DiagnosticSeverity.Warning
159+
isEnabled
160+
? vscode.DiagnosticSeverity.Error
161+
: vscode.DiagnosticSeverity.Warning
155162
)
156163
);
157164
}
@@ -163,4 +170,4 @@ const updateDiagnostics = (
163170
collection.set(document.uri, diagnostics);
164171
};
165172

166-
export const deactivate = () => { };
173+
export const deactivate = () => {};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/rc.schema.json",
3+
"plugins": [
4+
{
5+
"name": "GenericRandomErrorPlugin",
6+
"enabled": false,
7+
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
8+
"configSection": "genericRandomErrorPlugin"
9+
}
10+
],
11+
"urlsToWatch": ["https://jsonplaceholder.typicode.com/posts"]
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/rc.schema.json",
3+
"plugins": [
4+
{
5+
"name": "GenericRandomErrorPlugin",
6+
"enabled": true,
7+
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
8+
"configSection": "genericRandomErrorPlugin"
9+
}
10+
],
11+
"urlsToWatch": ["https://jsonplaceholder.typicode.com/posts"]
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/rc.schema.json",
3+
"plugins": [
4+
{
5+
"name": "GenericRandomErrorPlugin",
6+
"enabled": false,
7+
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
8+
}
9+
],
10+
"urlsToWatch": ["https://jsonplaceholder.typicode.com/posts"]
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/rc.schema.json",
3+
"plugins": [
4+
{
5+
"name": "GenericRandomErrorPlugin",
6+
"enabled": true,
7+
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
8+
}
9+
],
10+
"urlsToWatch": ["https://jsonplaceholder.typicode.com/posts"]
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/rc.schema.json",
3+
"plugins": [],
4+
"urlsToWatch": []
5+
}

src/test/extension.test.ts

Lines changed: 117 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,123 @@
11
import * as assert from 'assert';
2-
3-
// You can import and use all API from the 'vscode' module
4-
// as well as import your extension to test it
52
import * as vscode from 'vscode';
6-
// import * as myExtension from '../../extension';
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';
78

89
suite('Extension Test Suite', () => {
9-
vscode.window.showInformationMessage('Start all tests.');
10+
test('should activate when JSON file is opened', async () => {
11+
assert.strictEqual(
12+
vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')
13+
?.isActive,
14+
false
15+
);
16+
await vscode.workspace.openTextDocument({
17+
language: 'json',
18+
content: '',
19+
});
20+
await sleep(1000);
21+
assert.strictEqual(
22+
vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')
23+
?.isActive,
24+
true
25+
);
26+
});
27+
28+
test('should show error when no urlsToWatch found', async () => {
29+
const document = await vscode.workspace.openTextDocument({
30+
language: 'json',
31+
content: JSON.stringify(configUrlsToWatchRequired, null, 2),
32+
});
33+
await sleep(1000);
34+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
35+
assert.strictEqual(diagnostics?.length, 1);
36+
assert.strictEqual(
37+
diagnostics?.[0].message,
38+
'Add at least one url to watch.'
39+
);
40+
assert.strictEqual(
41+
diagnostics?.[0].severity,
42+
vscode.DiagnosticSeverity.Error
43+
);
44+
});
45+
46+
test('should show error when plugin requires config section', async () => {
47+
const document = await vscode.workspace.openTextDocument({
48+
language: 'json',
49+
content: JSON.stringify(configPluginConfigRequired, null, 2),
50+
});
51+
await sleep(1000);
52+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
53+
assert.strictEqual(diagnostics?.length, 1);
54+
assert.strictEqual(
55+
diagnostics?.[0].message,
56+
'GenericRandomErrorPlugin requires a config section.'
57+
);
58+
assert.strictEqual(
59+
diagnostics?.[0].severity,
60+
vscode.DiagnosticSeverity.Error
61+
);
62+
});
1063

11-
test('Sample test', () => {
12-
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
13-
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
14-
});
64+
test('should show warning when disabled plugin requires config section', async () => {
65+
const document = await vscode.workspace.openTextDocument({
66+
language: 'json',
67+
content: JSON.stringify(configPluginConfigRequiredDisabled, null, 2),
68+
});
69+
await sleep(1000);
70+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
71+
assert.strictEqual(diagnostics?.length, 1);
72+
assert.strictEqual(
73+
diagnostics?.[0].message,
74+
'GenericRandomErrorPlugin requires a config section.'
75+
);
76+
assert.strictEqual(
77+
diagnostics?.[0].severity,
78+
vscode.DiagnosticSeverity.Warning
79+
);
80+
});
81+
82+
test('should show error when plugin config section is not defined', async () => {
83+
const document = await vscode.workspace.openTextDocument({
84+
language: 'json',
85+
content: JSON.stringify(configPluginConfigMissing, null, 2),
86+
});
87+
await sleep(1000);
88+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
89+
assert.strictEqual(diagnostics?.length, 1);
90+
assert.strictEqual(
91+
diagnostics?.[0].message,
92+
"genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one."
93+
);
94+
assert.strictEqual(
95+
diagnostics?.[0].severity,
96+
vscode.DiagnosticSeverity.Error
97+
);
98+
});
99+
100+
test('should show warning when disabled plugin config section is not defined', async () => {
101+
const document = await vscode.workspace.openTextDocument({
102+
language: 'json',
103+
content: JSON.stringify(configPluginConfigMissingDisabled, null, 2),
104+
});
105+
await sleep(1000);
106+
const diagnostics = vscode.languages.getDiagnostics(document.uri);
107+
assert.strictEqual(diagnostics?.length, 1);
108+
assert.strictEqual(
109+
diagnostics?.[0].message,
110+
"genericRandomErrorPlugin config section is missing. Use 'devproxy-plugin-generic-random-error-config' snippet to create one."
111+
);
112+
assert.strictEqual(
113+
diagnostics?.[0].severity,
114+
vscode.DiagnosticSeverity.Error
115+
);
116+
});
15117
});
118+
119+
function sleep(ms: number): Promise<void> {
120+
return new Promise(resolve => {
121+
setTimeout(resolve, ms);
122+
});
123+
}

tsconfig.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"compilerOptions": {
3-
"module": "Node16",
4-
"target": "ES2022",
5-
"lib": [
6-
"ES2022"
7-
],
8-
"sourceMap": true,
9-
"rootDir": "src",
10-
"strict": true /* enable all strict type-checking options */
11-
/* Additional Checks */
12-
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
13-
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
14-
// "noUnusedParameters": true, /* Report errors on unused parameters. */
15-
}
2+
"compilerOptions": {
3+
"module": "Node16",
4+
"target": "ES2022",
5+
"lib": ["ES2022"],
6+
"sourceMap": true,
7+
"rootDir": "src",
8+
"strict": true /* enable all strict type-checking options */,
9+
"resolveJsonModule": true,
10+
"esModuleInterop": true
11+
/* Additional Checks */
12+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
13+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
14+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
15+
}
1616
}

0 commit comments

Comments
 (0)