Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Action Testing #828

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules/
npm-debug.log
lsp/
testFixture/.terraform.lock.hcl
testFixture/.vscode
.terraform/
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/index",
"--extensionTestsPath=${workspaceFolder}/out/test/integration/index",
"${workspaceFolder}/testFixture"
],
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
Expand Down
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
"yauzl": "^2.10.0"
},
"devDependencies": {
"@types/chai": "^4.2.22",
"@types/glob": "^7.1.3",
"@types/mocha": "^8.2.2",
"@types/node": "^12.12.54",
Expand All @@ -305,6 +306,7 @@
"@types/yauzl": "^2.9.1",
"@typescript-eslint/eslint-plugin": "^3.9.0",
"@typescript-eslint/parser": "^3.9.0",
"chai": "^4.3.4",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
Expand Down
24 changes: 0 additions & 24 deletions src/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export let platformEol: string;

export async function open(docUri: vscode.Uri): Promise<void> {
try {
await activated();
doc = await vscode.workspace.openTextDocument(docUri);
editor = await vscode.window.showTextDocument(doc);
} catch (e) {
Expand All @@ -23,29 +22,6 @@ export function getExtensionId(): string {
return `${pjson.publisher}.${pjson.name}`;
}

let _activatedPromise: Promise<void>;
async function activated() {
if (!_activatedPromise) {
try {
// The extensionId is `publisher.name` from package.json
const extId = getExtensionId()
const ext = vscode.extensions.getExtension(extId);
if (!ext.isActive) {
console.log('Activating hashicorp.terraform extension');
await ext.activate();
} else {
console.log('hashicorp.terraform is already active');
}
// TODO: implement proper synchronization/status check in LS
// give server(s) some time to startup
_activatedPromise = sleep(8000);
} catch (err) {
_activatedPromise = Promise.reject(err);
}
}
return _activatedPromise;
}

export const testFolderPath = path.resolve(__dirname, '..', '..', 'testFixture');

export const getDocPath = (p: string): string => {
Expand Down
47 changes: 47 additions & 0 deletions src/test/integration/codeAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// import * as vscode from 'vscode';
// import * as assert from 'assert';
// import { expect } from 'chai';
// import { getDocUri, open } from '../helper';
// import { config } from '../../vscodeUtils';

// Enable when https://github.com/hashicorp/terraform-ls/pull/680 is merged
// suite('code actions', () => {
// teardown(async () => {
// await vscode.commands.executeCommand('workbench.action.closeAllEditors');
// });

// test('supported actions', async () => {
// await vscode.workspace
// .getConfiguration('terraform')
// .update('languageServer', { external: true }, vscode.ConfigurationTarget.Workspace);
// await vscode.workspace
// .getConfiguration('editor')
// .update('codeActionsOnSave', { "source.formatAll.terraform": true }, vscode.ConfigurationTarget.Workspace);

// const docUri = getDocUri('actions.tf');
// await open(docUri);

// const supported = [
// new vscode.CodeAction('Format Document', vscode.CodeActionKind.Source.append('formatAll').append('terraform')),
// ];

// for (let index = 0; index < supported.length; index++) {
// const wanted = supported[index];
// const requested = wanted.kind.value.toString();

// const actions: vscode.CodeAction[] = await vscode.commands.executeCommand<vscode.CodeAction[]>(
// 'vscode.executeCodeActionProvider',
// docUri,
// new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)),
// requested,
// );

// assert.ok(actions);
// expect(actions).not.to.be.undefined;

// assert.strictEqual(actions.length, 1);
// assert.strictEqual(actions[0].title, wanted.title);
// assert.strictEqual(actions[0].kind.value, wanted.kind.value);
// }
// });
// });
59 changes: 59 additions & 0 deletions src/test/integration/completion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as vscode from 'vscode';
import * as assert from 'assert';
import { expect } from 'chai';
import { getDocUri, open } from '../helper';

suite('completion', () => {
teardown(async () => {
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});

test('simple completion', async () => {
const wanted = new vscode.CompletionList([
new vscode.CompletionItem('data', vscode.CompletionItemKind.Class),
new vscode.CompletionItem('locals', vscode.CompletionItemKind.Class),
new vscode.CompletionItem('module', vscode.CompletionItemKind.Class),
new vscode.CompletionItem('output', vscode.CompletionItemKind.Class),
new vscode.CompletionItem('provider', vscode.CompletionItemKind.Class),
new vscode.CompletionItem('resource', vscode.CompletionItemKind.Class),
new vscode.CompletionItem('terraform', vscode.CompletionItemKind.Class),
new vscode.CompletionItem('variable', vscode.CompletionItemKind.Class),
new vscode.CompletionItem({ label: 'fore', description: 'For Each' }, vscode.CompletionItemKind.Snippet),
new vscode.CompletionItem({ label: 'module', description: 'Module' }, vscode.CompletionItemKind.Snippet),
new vscode.CompletionItem({ label: 'output', description: 'Output' }, vscode.CompletionItemKind.Snippet),
new vscode.CompletionItem(
{ label: 'provisioner', description: 'Provisioner' },
vscode.CompletionItemKind.Snippet,
),
new vscode.CompletionItem({ label: 'vare', description: 'Empty variable' }, vscode.CompletionItemKind.Snippet),
new vscode.CompletionItem({ label: 'varm', description: 'Map variable' }, vscode.CompletionItemKind.Snippet),
]);

const docUri = getDocUri('actions.tf');
await open(docUri);

const list: vscode.CompletionList = await vscode.commands.executeCommand<vscode.CompletionList>(
'vscode.executeCompletionItemProvider',
docUri,
new vscode.Position(0, 0),
);

assert.ok(list);
expect(list).not.to.be.undefined;
expect(list.items).not.to.be.undefined;
expect(list.items.length).to.be.greaterThanOrEqual(1);

for (let index = 0; index < list.items.length; index++) {
const element = list.items[index];
assert.ok(element);
expect(element).not.to.be.undefined;

const w = wanted.items[index];
assert.ok(w);
expect(w).not.to.be.undefined;
assert.strictEqual(element.kind, w.kind);
// this can either be a string or a vscode.CompletionItemLabel, so use deep
assert.deepStrictEqual(element.label, w.label);
}
});
});
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import * as vscode from 'vscode';
import * as assert from 'assert';
import { getDocUri, open } from './helper';
import { expect } from 'chai';
import { getDocUri, open } from '../helper';

suite('document symbols', () => {
teardown(async () => {
return await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});

const docUri = getDocUri('sample.tf');

test('returns symbols', async () => {
Expand All @@ -19,8 +24,11 @@ async function testSymbols(docUri: vscode.Uri, symbolNames: string[]) {
// Executing the command `vscode.executeDocumentSymbolProvider` to simulate requesting doc symbols
const symbols = (await vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', docUri)) as vscode.SymbolInformation[];

assert.equal(symbols.length, symbolNames.length);
assert.ok(symbols);
expect(symbols).not.to.be.undefined;

assert.strictEqual(symbols.length, symbolNames.length);
symbols.forEach((symbol, i) => {
assert.equal(symbol.name, symbolNames[i]);
assert.strictEqual(symbol.name, symbolNames[i]);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import * as assert from 'assert';
import * as vscode from 'vscode';
import { Utils } from 'vscode-uri';
import { getDocUri, getExtensionId, open, testFolderPath } from './helper';
import { getDocUri, getExtensionId, open, testFolderPath } from '../helper';


const extId = getExtensionId()
const ext = vscode.extensions.getExtension(extId);

suite('moduleCallers', () => {
teardown(async () => {
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});

test('should execute language server command', async () => {
const extId = getExtensionId()
const ext = vscode.extensions.getExtension(extId);

const documentUri = getDocUri('modules/sample.tf');
await open(documentUri);

assert.ok(ext.isActive);

const client = ext.exports.clientHandler.getClient(documentUri);

const moduleUri = Utils.dirname(documentUri).toString();
const response = await ext.exports.moduleCallers(client, moduleUri);
assert.strictEqual(response.moduleCallers.length, 1);
Expand Down
Loading