Skip to content

Commit

Permalink
Fix integration tests (#433)
Browse files Browse the repository at this point in the history
* WIP: fix tests

* More test fixes

* Add glob types

* equal -> strictEqual

* Remove "Extension Tests From Server"

This is already tested.

* Add integration tests to CI

* Remove test-compile

* Remove unnecessary npx

* Start xvfb

* Export display

* Set env var correctly

* Pass the workspace

* Pass in the src path

* Increase sleeps

* Base tests off of VS Code samples

* Fix path

* Install azure-account

* Pin azure-pipelines-language-server

* Format

* More format
  • Loading branch information
winstliu authored Nov 28, 2021
1 parent f6a0991 commit ef83544
Show file tree
Hide file tree
Showing 16 changed files with 3,583 additions and 3,089 deletions.
8 changes: 8 additions & 0 deletions .azure-pipelines/common-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ steps:
- script: npm run unittest
displayName: Run unit tests

- script: Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
displayName: Start xvfb

- script: npm run test
displayName: Run integration tests
env:
DISPLAY: ':99.0'

# Acquire the `vsce` tool and use it to package
- script: |
npm install -g vsce
Expand Down
18 changes: 3 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,14 @@
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceFolder}/src/test/workspace",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
"--extensionTestsPath=${workspaceFolder}/out/test/index"
],
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
]
},
{
"name": "Extension Tests From Server",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceFolder}/examples/",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/testfromserver"
],
"outFiles": [
"${workspaceFolder}/out/testfromserver/**/*.js"
]
"preLaunchTask": "npm: compile:test",
},
{
"name": "Attach to Server",
Expand Down
5,950 changes: 3,283 additions & 2,667 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,27 @@
"compile": "webpack --mode production --progress --color && node copyStaticFiles.js",
"compile:dev": "webpack --mode development --progress --color && node copyStaticFiles.js",
"watch": "node copyStaticFiles.js && webpack --mode development --progress --color --watch",
"test": "tsc -p ./ && node ./out/test/runTest.js",
"unittest": "tsc -p ./ && npx mocha -u tdd out/unittest/*.js"
"compile:test": "npm run compile:dev && tsc -p ./",
"test": "npm run compile:test && node ./out/test/runTest.js",
"unittest": "npm run compile:test && mocha -u tdd out/unittest/*.js"
},
"devDependencies": {
"@types/fs-extra": "4.0.5",
"@types/glob": "^7.2.0",
"@types/html-to-text": "^5.1.2",
"@types/mocha": "5.2.5",
"@types/mocha": "^9.0.0",
"@types/mustache": "0.8.32",
"@types/node": "^14.16.0",
"@types/vscode": "^1.45.1",
"@vscode/test-electron": "^1.6.2",
"ajv": "^6.9.1",
"assert": "1.4.1",
"glob": "^7.1.6",
"mocha": "5.2.0",
"mocha": "^9.1.1",
"ts-loader": "^8.0.14",
"ts-node": "7.0.1",
"tslint": "5.8.0",
"typescript": "^4.1.0",
"vscode-test": "^1.3.0",
"webpack": "^5.17.0",
"webpack-cli": "^4.4.0"
},
Expand All @@ -155,8 +157,8 @@
"@azure/arm-subscriptions": "^3.0.0",
"@azure/ms-rest-azure-env": "^2.0.0",
"@azure/ms-rest-nodeauth": "^3.0.6",
"azure-pipelines-language-server": "0.6.7",
"azure-devops-node-api": "^11.0.1",
"azure-pipelines-language-server": "0.6.7",
"html-to-text": "^5.1.1",
"mustache": "3.0.1",
"shelljs": "^0.3.0",
Expand Down
22 changes: 0 additions & 22 deletions src/configure/test/extension.test.ts

This file was deleted.

124 changes: 0 additions & 124 deletions src/dtdata.ts

This file was deleted.

45 changes: 45 additions & 0 deletions src/test/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// From https://github.com/microsoft/vscode-extension-samples/blob/main/lsp-sample/client/src/test/helper.ts

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('ms-azure-devops.azure-pipelines')!;
await ext.activate();
try {
doc = await vscode.workspace.openTextDocument(docUri);
editor = await vscode.window.showTextDocument(doc);
await sleep(5000); // 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, '../../src/test/workspace', 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));
}
39 changes: 39 additions & 0 deletions src/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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',
color: true,
});
mocha.timeout(100000);

const testsRoot = __dirname;

return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return e(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) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
console.error(err);
e(err);
}
});
});
}
58 changes: 41 additions & 17 deletions src/test/runTest.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
import * as cp from 'child_process';
import * as path from 'path';

import { runTests } from 'vscode-test';
import {
downloadAndUnzipVSCode,
resolveCliPathFromVSCodeExecutablePath,
runTests
} from '@vscode/test-electron';

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

// The path to the extension test script
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/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);
}
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');

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

// If the first argument is a path to a file/folder/workspace,
// the launched VS Code instance will open it.
// workspace isn't copied to out because it's all YAML files.
const launchArgs = [path.resolve(__dirname, '../../src/test/workspace')];

const vscodeExecutablePath = await downloadAndUnzipVSCode();
const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath);

cp.spawnSync(cliPath, ['--install-extension', 'ms-vscode.azure-account'], {
encoding: 'utf-8',
stdio: 'inherit'
});

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

main();
main();
Loading

0 comments on commit ef83544

Please sign in to comment.