Skip to content

Commit

Permalink
Added simple end to end tests for CodeLens and Hover scaffolding usag…
Browse files Browse the repository at this point in the history
…es. (#7)
  • Loading branch information
mosckital committed Jun 20, 2020
1 parent 066dedf commit 8c48b75
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 2 deletions.
25 changes: 25 additions & 0 deletions client/src/test/codeLens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as vscode from 'vscode';
import * as assert from 'assert';
import { getDocUri, activate } from './helper';

suite('Should show CodeLens', () => {
const docUri = getDocUri('diamond.py');

test('Show CodeLenses in diamond.py', async () => {
await testCodeLens(docUri, 4);
});
});

async function testCodeLens(
docUri: vscode.Uri,
expectedCodeLensNumber: number
) {
await activate(docUri);
const actualCodeLenses = (await vscode.commands.executeCommand(
'vscode.executeCodeLensProvider',
docUri
)) as vscode.CodeLens[];
assert.ok(actualCodeLenses.length === expectedCodeLensNumber);
// TODO: to add the check for the contents once the functionality has been
// fully implemented
}
47 changes: 47 additions & 0 deletions client/src/test/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* --------------------------------------------------------------------------------------------
* 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';

export let doc: vscode.TextDocument;
export let editor: vscode.TextEditor;
export let documentEol: string;
export let platformEol: string;

/**
* Activates the vscode.lsp-sample extension
*/
export async function activate(docUri: vscode.Uri) {
// The extensionId is `publisher.name` from package.json
const ext = vscode.extensions.getExtension('kaiyan.python-mro')!;
await ext.activate();
try {
doc = await vscode.workspace.openTextDocument(docUri);
editor = await vscode.window.showTextDocument(doc);
await sleep(2000); // Wait for server activation
} catch (e) {
console.error(e);
}
}

async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

export const getDocPath = (p: string) => {
return path.resolve(__dirname, '../../../tests/examples', p);
};
export const getDocUri = (p: string) => {
return vscode.Uri.file(getDocPath(p));
};

export async function setTestContent(content: string): Promise<boolean> {
const all = new vscode.Range(
doc.positionAt(0),
doc.positionAt(doc.getText().length)
);
return editor.edit(eb => eb.replace(all, content));
}
42 changes: 42 additions & 0 deletions client/src/test/hover.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as vscode from 'vscode';
import * as assert from 'assert';
import { getDocUri, activate } from './helper';

suite('Should show Hover', () => {
const docUri = getDocUri('diamond.py');

test('Show Hover in diamond.py', async () => {
await testHover(docUri, new vscode.Position(9, 3));
});
});

async function testHover(
docUri: vscode.Uri,
hoverPosition: vscode.Position
) {
await activate(docUri);
const actualHoverResults = (await vscode.commands.executeCommand(
'vscode.executeHoverProvider',
docUri,
hoverPosition,
)) as vscode.Hover[];
assert.ok(actualHoverResults.length > 0);
let foundMRO = false;
actualHoverResults.forEach(hover => {
if (hover.contents.length > 0) {
let firstLine = hover.contents[0];
let firstLineContent: string;
if (typeof firstLine === 'string') {
firstLineContent = firstLine;
} else {
firstLineContent = firstLine.value;
}
if (firstLineContent === 'Target class name') {
foundMRO = true;
}
}
});
assert.ok(foundMRO);
// TODO: to add the check for the contents once the functionality has been
// fully implemented
}
43 changes: 43 additions & 0 deletions client/src/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as path from 'path';
import * as Mocha from 'mocha';
import * as glob from 'glob';

export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
});
mocha.useColors(true);
mocha.timeout(100000);

const testsRoot = __dirname;

return new Promise((resolve, reject) => {
glob('**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return reject(err);
}

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));

try {
// Run the mocha test
mocha.run(failures => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`));
} else {
resolve();
}
});
} catch (err) {
console.error(err);
reject(err);
}
});
});
}
27 changes: 27 additions & 0 deletions client/src/test/runTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';

import { runTests } from 'vscode-test';

async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../../');

// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './index');

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
}
}

main();
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": {
"name": "Kaiyan"
},
"publisher": "kaiyan",
"version": "0.0.1",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -44,7 +45,7 @@
"messages",
"verbose"
],
"default": "verbose",
"default": "off",
"description": "Traces the communication between VS Code and the language server."
}
}
Expand All @@ -61,7 +62,7 @@
"compile": "tsc -b",
"watch": "tsc -b -w",
"postinstall": "cd client && npm install && cd ../server && npm install && cd ..",
"test": "sh ./scripts/e2e.sh"
"test": "sh ./tests/e2e.sh"
},
"devDependencies": {
"@types/vscode": "^1.46.0",
Expand Down
6 changes: 6 additions & 0 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

export CODE_TESTS_PATH="$(pwd)/client/out/test"
export CODE_TESTS_WORKSPACE="$(pwd)/tests/examples"

node "$(pwd)/client/out/test/runTest"

0 comments on commit 8c48b75

Please sign in to comment.