Skip to content

Commit 96871a7

Browse files
committed
add integration testing
1 parent b45d431 commit 96871a7

File tree

4 files changed

+143
-3
lines changed

4 files changed

+143
-3
lines changed

.vscode-test.mjs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
import { defineConfig } from '@vscode/test-cli';
1+
import * as path from "path";
2+
import * as fs from "fs";
3+
import * as os from "os";
4+
import { defineConfig } from "@vscode/test-cli";
5+
6+
let cwd = fs.mkdtempSync(path.join(os.tmpdir(), "coded_project"));
7+
fs.writeFileSync(path.join(cwd, "dub.sdl"), 'name "codedproject"\n');
8+
fs.mkdirSync(path.join(cwd, "source"));
9+
fs.writeFileSync(
10+
path.join(cwd, "source", "app.d"),
11+
'import std.stdio;\n\nvoid main() {\n\twriteln("hello world");\n}\n'
12+
);
213

314
export default defineConfig({
4-
files: 'out/test/**/*.test.js',
15+
files: "out/test/**/*.test.js",
16+
workspaceFolder: cwd,
17+
env: {
18+
PROJECT_DIR: cwd,
19+
},
520
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"displayName": "DUB Editor",
44
"description": "GUI editor for opening dub.sdl and dub.json files",
55
"publisher": "webfreak",
6+
"version": "0.1.5",
67
"repository": {
78
"type": "git",
89
"url": "https://github.com/Pure-D/dub-editor.git"
@@ -81,7 +82,7 @@
8182
"pretest": "npm run compile-tests && npm run compile && npm run lint",
8283
"check-types": "tsc",
8384
"lint": "eslint src",
84-
"test": "vscode-test",
85+
"test": "vscode-test --timeout 120000",
8586
"grammar": "js-yaml syntaxes/sdl.yml > syntaxes/sdl.json"
8687
},
8788
"dependencies": {

src/test/it.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
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
5+
import * as vscode from "vscode";
6+
import { sleep, testCompletion } from "./utils";
7+
// import * as myExtension from '../../extension';
8+
9+
suite("Integration Tests", () => {
10+
vscode.window.showInformationMessage("Start all tests.");
11+
12+
// sanity test that we have the correct window open
13+
let workspaces = vscode.workspace.workspaceFolders;
14+
assert.strictEqual(workspaces?.length, 1);
15+
assert.strictEqual(
16+
workspaces[0].uri.fsPath.toLowerCase(),
17+
process.env["PROJECT_DIR"]!.toLowerCase()
18+
);
19+
let workspace = workspaces[0];
20+
21+
test("check dub-editor installed", async () => {
22+
let coded = vscode.extensions.getExtension("webfreak.dub-editor")!;
23+
assert.notStrictEqual(coded, undefined, "dub-editor not installed?!");
24+
});
25+
26+
function file(relative: string): vscode.Uri {
27+
return vscode.Uri.joinPath(workspace.uri, relative);
28+
}
29+
30+
test("Wait for dub-editor extension", async () => {
31+
let coded = vscode.extensions.getExtension("webfreak.dub-editor")!;
32+
await coded.activate();
33+
await sleep(5000); // give sufficient startup time
34+
});
35+
36+
test("Recipe file", async () => {
37+
let recipe = await vscode.window.showTextDocument(
38+
await vscode.workspace.openTextDocument(file("dub.sdl")),
39+
vscode.ViewColumn.One
40+
);
41+
42+
await recipe.edit((edit) => {
43+
edit.insert(new vscode.Position(2, 0), "dep");
44+
});
45+
46+
await testCompletion(
47+
recipe,
48+
new vscode.Position(2, 3),
49+
new vscode.CompletionList([
50+
new vscode.CompletionItem(
51+
"dependency",
52+
vscode.CompletionItemKind.Field
53+
),
54+
]),
55+
"contains"
56+
);
57+
});
58+
59+
// test('interactive', () => new Promise((resolve, reject) => {}));
60+
});

src/test/utils.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as assert from 'assert';
2+
import * as vscode from 'vscode';
3+
4+
export function sleep(ms: number): Promise<void> {
5+
return new Promise((resolve) => setTimeout(resolve, ms));
6+
}
7+
8+
export async function testCompletion(
9+
editor: vscode.TextEditor,
10+
position: vscode.Position,
11+
expectedCompletionList: vscode.CompletionList,
12+
type: "exact" | "contains",
13+
testKeys: (keyof vscode.CompletionItem)[] = ["label", "kind"]
14+
) {
15+
editor = await vscode.window.showTextDocument(editor.document, editor.viewColumn);
16+
await sleep(500);
17+
editor.selection = new vscode.Selection(position, position);
18+
await sleep(500);
19+
20+
// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
21+
const actualCompletionList = (await vscode.commands.executeCommand(
22+
'vscode.executeCompletionItemProvider',
23+
editor.document.uri,
24+
position
25+
)) as vscode.CompletionList;
26+
27+
if (type === "exact") {
28+
assert.strictEqual(actualCompletionList.items.length, expectedCompletionList.items.length);
29+
expectedCompletionList.items.forEach((expectedItem, i) => {
30+
const actualItem = actualCompletionList.items[i];
31+
testKeys.forEach(key => {
32+
assert.strictEqual(actualItem[key], expectedItem[key],
33+
"completion "
34+
+ JSON.stringify(expectedItem.label)
35+
+ " mismatch on key " + JSON.stringify(key) + ":\n"
36+
+ "expected = " + JSON.stringify(expectedItem[key]) + "\n"
37+
+ " actual = " + JSON.stringify(actualItem[key]));
38+
});
39+
});
40+
} else if (type === "contains") {
41+
assert.ok(actualCompletionList.items.length >= expectedCompletionList.items.length,
42+
"Expected at least " + expectedCompletionList.items.length
43+
+ " completions, but only got " + actualCompletionList.items.length);
44+
expectedCompletionList.items.forEach((expectedItem, i) => {
45+
const actualItem = actualCompletionList.items.find(i => i.label == expectedItem.label);
46+
if (!actualItem)
47+
assert.fail("can't find completion item "
48+
+ JSON.stringify(expectedItem.label)
49+
+ " in "
50+
+ JSON.stringify(actualCompletionList.items.map(c => c.label)));
51+
52+
testKeys.forEach(key => {
53+
assert.strictEqual(actualItem[key], expectedItem[key],
54+
"completion "
55+
+ JSON.stringify(expectedItem.label)
56+
+ " mismatch on key " + JSON.stringify(key) + ":\n"
57+
+ "expected = " + JSON.stringify(expectedItem[key]) + "\n"
58+
+ " actual = " + JSON.stringify(actualItem[key]));
59+
});
60+
});
61+
} else {
62+
throw new Error("invalid type");
63+
}
64+
}

0 commit comments

Comments
 (0)