Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney committed Aug 29, 2019
2 parents f1c9e1f + 9510531 commit 649a331
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
out/
.vscode-test/
test/testFixture/.vscode
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ os:
- osx
- linux
before_install:
- if [ $TRAVIS_OS_NAME == "linux" ]; then export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
sh -e /etc/init.d/xvfb start; sleep 3; fi
- npm install -g [email protected]
install:
- |
if [ $TRAVIS_OS_NAME == "linux" ]; then
export DISPLAY=':99.0';
/usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
fi
- npm install
- npm run vscode:prepublish
- npm install -g vsce
- vsce package
script:
- npm test --silent
- npm test
deploy:
provider : npm
email : [email protected]
Expand Down
15 changes: 14 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
"${workspaceRoot}/test/*.test.ts"
],
"preLaunchTask": "compile typescript"
},
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test",
"${workspaceRoot}/test/testFixture"
],
"outFiles": ["${workspaceFolder}/out/test/**/*.js"]
}
]
}
}
20 changes: 10 additions & 10 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"compile": "tsc -watch -p ./",
"update-vscode": "node ./node_modules/vscode/bin/install",
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "mocha --ui tdd out/test/extension.test.js"
"test": "sh scripts/e2e.sh"
},
"devDependencies": {
"@types/mocha": "^2.2.33",
Expand All @@ -173,6 +173,6 @@
"vscode-languageclient": "5.2.1",
"vscode-nls": "^3.2.1",
"vscode-uri": "^2.0.3",
"yaml-language-server": "0.5.6"
"yaml-language-server": "0.5.7"
}
}
7 changes: 7 additions & 0 deletions scripts/e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

export CODE_TESTS_PATH="$(pwd)/out/test"
export CODE_TESTS_WORKSPACE="$(pwd)/test/testFixture"
export CODE_DISABLE_EXTENSIONS=1

node "$(pwd)/node_modules/vscode/bin/test"
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as path from 'path';
import { workspace, ExtensionContext, extensions } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, NotificationType } from 'vscode-languageclient';
import { URI } from 'vscode-uri';
import { schemaContributor, CUSTOM_SCHEMA_REQUEST, CUSTOM_CONTENT_REQUEST } from './schema-contributor'
import { schemaContributor, CUSTOM_SCHEMA_REQUEST, CUSTOM_CONTENT_REQUEST } from './schema-contributor';

export interface ISchemaAssociations {
[pattern: string]: string[];
Expand Down
62 changes: 62 additions & 0 deletions test/completion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from 'vscode';
import { getDocUri, activate, testCompletion, updateSettings, testCompletionNotEmpty, resetSettings } from './helper';


describe('Completion should work in multiple different scenarios', () => {
const docUri = getDocUri('completion/completion.yaml');
const travisUri = getDocUri('completion/.travis.yml');

afterEach(async () => {
await resetSettings("schemas", {});
await resetSettings("schemaStore.enable", true);
});

it('completion works with local schema', async () => {
await activate(docUri);
await updateSettings("schemas", {
"./schemas/basic_completion_schema.json": "completion.yaml"
});
await testCompletion(docUri, new vscode.Position(0, 0), {
items: [
{
label: "my_key",
kind: 9
}
]
});
});

it('completion works with external schema', async () => {
await activate(docUri);
await updateSettings("schemas", {
"https://gist.githubusercontent.com/JPinkney/4c4a43977932402c2a09a677f29287c3/raw/4d4f638b37ddeda84fb27e6b2cf14d3dc0793029/a.yaml": "completion.yaml"
});
await testCompletion(docUri, new vscode.Position(0, 0), {
items: [
{
label: "version",
kind: 9
}
]
});
});

it('completion works with schema store schema', async () => {
await activate(travisUri);
await updateSettings("schemaStore.enable", true);
await testCompletionNotEmpty(travisUri, new vscode.Position(0, 0));
});

it('completion does not work with schema store disabled and no schemas set', async () => {
await activate(travisUri);
await updateSettings("schemaStore.enable", false);
await testCompletion(travisUri, new vscode.Position(0, 0), {
items: []
});
});
});
23 changes: 0 additions & 23 deletions test/extension.test.ts

This file was deleted.

129 changes: 129 additions & 0 deletions test/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* 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';
import assert = require('assert');

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

/**
* Activates the redhat.vscode-yaml extension
*/
export async function activate(docUri: vscode.Uri) {
const ext = vscode.extensions.getExtension('redhat.vscode-yaml')!;
const activation = await ext.activate();
try {
doc = await vscode.workspace.openTextDocument(docUri);
editor = await vscode.window.showTextDocument(doc);
await sleep(2000); // Wait for server activation
return activation;
} catch (e) {
console.error(e);
}
}

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

export const getDocPath = (p: string) => {
return path.resolve(__dirname, '../../test/testFixture', p);
};

export const getDocUri = (p: string) => {
return vscode.Uri.file(getDocPath(p));
};

export const updateSettings = (setting: any, value: any) => {
const yamlConfiguration = vscode.workspace.getConfiguration("yaml");
return yamlConfiguration.update(setting, value, false);
}

export const resetSettings = (setting: any, value: any) => {
const yamlConfiguration = vscode.workspace.getConfiguration("yaml");
return yamlConfiguration.update(setting, value, false);
}

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));
}

export async function testCompletion(
docUri: vscode.Uri,
position: vscode.Position,
expectedCompletionList: vscode.CompletionList
) {

// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
const actualCompletionList = (await vscode.commands.executeCommand(
'vscode.executeCompletionItemProvider',
docUri,
position
)) as vscode.CompletionList;

assert.equal(actualCompletionList.items.length, expectedCompletionList.items.length);
expectedCompletionList.items.forEach((expectedItem, i) => {
const actualItem = actualCompletionList.items[i];
assert.equal(actualItem.label, expectedItem.label);
assert.equal(actualItem.kind, expectedItem.kind);
});
}

export async function testCompletionNotEmpty(
docUri: vscode.Uri,
position: vscode.Position
) {

// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
const actualCompletionList = (await vscode.commands.executeCommand(
'vscode.executeCompletionItemProvider',
docUri,
position
)) as vscode.CompletionList;

assert.notEqual(actualCompletionList.items.length, 0);
}

export async function testHover(
docUri: vscode.Uri,
position: vscode.Position,
expectedHover: vscode.Hover[]
) {

// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
const actualHoverResults = (await vscode.commands.executeCommand(
'vscode.executeHoverProvider',
docUri,
position
)) as vscode.Hover[];

assert.equal(actualHoverResults.length, expectedHover.length);
expectedHover.forEach((expectedItem, i) => {
const actualItem = actualHoverResults[i];
assert.equal((actualItem.contents[i] as vscode.MarkdownString).value, expectedItem.contents[i]);
});
}

export async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: vscode.Diagnostic[]) {
const actualDiagnostics = vscode.languages.getDiagnostics(docUri);

assert.equal(actualDiagnostics.length, expectedDiagnostics.length);

expectedDiagnostics.forEach((expectedDiagnostic, i) => {
const actualDiagnostic = actualDiagnostics[i]
assert.equal(actualDiagnostic.message, expectedDiagnostic.message)
assert.deepEqual(actualDiagnostic.range, expectedDiagnostic.range)
assert.equal(actualDiagnostic.severity, expectedDiagnostic.severity)
});
}
9 changes: 5 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
// to report the results back to the caller. When the tests are finished, return
// a possible error to the callback or null if none.

var testRunner = require('vscode/lib/testrunner');
import * as testRunner from 'vscode/lib/testrunner';

// You can directly control Mocha options by uncommenting the following lines
// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
testRunner.configure({
ui: 'tdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.)
useColors: true // colored output from test results
ui: 'bdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.)
useColors: true, // colored output from test results,
timeout: 100000
});

module.exports = testRunner;
module.exports = testRunner;
Loading

0 comments on commit 649a331

Please sign in to comment.