Skip to content

Commit 649a331

Browse files
committed
2 parents f1c9e1f + 9510531 commit 649a331

17 files changed

+345
-44
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules/
22
out/
3+
.vscode-test/
4+
test/testFixture/.vscode

.travis.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ os:
66
- osx
77
- linux
88
before_install:
9-
- if [ $TRAVIS_OS_NAME == "linux" ]; then export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
10-
sh -e /etc/init.d/xvfb start; sleep 3; fi
119
- npm install -g [email protected]
1210
install:
11+
- |
12+
if [ $TRAVIS_OS_NAME == "linux" ]; then
13+
export DISPLAY=':99.0';
14+
/usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
15+
fi
1316
- npm install
1417
- npm run vscode:prepublish
1518
- npm install -g vsce
1619
- vsce package
1720
script:
18-
- npm test --silent
21+
- npm test
1922
deploy:
2023
provider : npm
2124

.vscode/launch.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
"${workspaceRoot}/test/*.test.ts"
3131
],
3232
"preLaunchTask": "compile typescript"
33+
},
34+
{
35+
"name": "Extension Tests",
36+
"type": "extensionHost",
37+
"request": "launch",
38+
"runtimeExecutable": "${execPath}",
39+
"args": [
40+
"--disable-extensions",
41+
"--extensionDevelopmentPath=${workspaceFolder}",
42+
"--extensionTestsPath=${workspaceFolder}/out/test",
43+
"${workspaceRoot}/test/testFixture"
44+
],
45+
"outFiles": ["${workspaceFolder}/out/test/**/*.js"]
3346
}
3447
]
35-
}
48+
}

package-lock.json

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
"compile": "tsc -watch -p ./",
161161
"update-vscode": "node ./node_modules/vscode/bin/install",
162162
"postinstall": "node ./node_modules/vscode/bin/install",
163-
"test": "mocha --ui tdd out/test/extension.test.js"
163+
"test": "sh scripts/e2e.sh"
164164
},
165165
"devDependencies": {
166166
"@types/mocha": "^2.2.33",
@@ -173,6 +173,6 @@
173173
"vscode-languageclient": "5.2.1",
174174
"vscode-nls": "^3.2.1",
175175
"vscode-uri": "^2.0.3",
176-
"yaml-language-server": "0.5.6"
176+
"yaml-language-server": "0.5.7"
177177
}
178178
}

scripts/e2e.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
export CODE_TESTS_PATH="$(pwd)/out/test"
4+
export CODE_TESTS_WORKSPACE="$(pwd)/test/testFixture"
5+
export CODE_DISABLE_EXTENSIONS=1
6+
7+
node "$(pwd)/node_modules/vscode/bin/test"

src/extension.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as path from 'path';
1111
import { workspace, ExtensionContext, extensions } from 'vscode';
1212
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, NotificationType } from 'vscode-languageclient';
1313
import { URI } from 'vscode-uri';
14-
import { schemaContributor, CUSTOM_SCHEMA_REQUEST, CUSTOM_CONTENT_REQUEST } from './schema-contributor'
14+
import { schemaContributor, CUSTOM_SCHEMA_REQUEST, CUSTOM_CONTENT_REQUEST } from './schema-contributor';
1515

1616
export interface ISchemaAssociations {
1717
[pattern: string]: string[];

test/completion.test.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
* ------------------------------------------------------------------------------------------ */
5+
6+
import * as vscode from 'vscode';
7+
import { getDocUri, activate, testCompletion, updateSettings, testCompletionNotEmpty, resetSettings } from './helper';
8+
9+
10+
describe('Completion should work in multiple different scenarios', () => {
11+
const docUri = getDocUri('completion/completion.yaml');
12+
const travisUri = getDocUri('completion/.travis.yml');
13+
14+
afterEach(async () => {
15+
await resetSettings("schemas", {});
16+
await resetSettings("schemaStore.enable", true);
17+
});
18+
19+
it('completion works with local schema', async () => {
20+
await activate(docUri);
21+
await updateSettings("schemas", {
22+
"./schemas/basic_completion_schema.json": "completion.yaml"
23+
});
24+
await testCompletion(docUri, new vscode.Position(0, 0), {
25+
items: [
26+
{
27+
label: "my_key",
28+
kind: 9
29+
}
30+
]
31+
});
32+
});
33+
34+
it('completion works with external schema', async () => {
35+
await activate(docUri);
36+
await updateSettings("schemas", {
37+
"https://gist.githubusercontent.com/JPinkney/4c4a43977932402c2a09a677f29287c3/raw/4d4f638b37ddeda84fb27e6b2cf14d3dc0793029/a.yaml": "completion.yaml"
38+
});
39+
await testCompletion(docUri, new vscode.Position(0, 0), {
40+
items: [
41+
{
42+
label: "version",
43+
kind: 9
44+
}
45+
]
46+
});
47+
});
48+
49+
it('completion works with schema store schema', async () => {
50+
await activate(travisUri);
51+
await updateSettings("schemaStore.enable", true);
52+
await testCompletionNotEmpty(travisUri, new vscode.Position(0, 0));
53+
});
54+
55+
it('completion does not work with schema store disabled and no schemas set', async () => {
56+
await activate(travisUri);
57+
await updateSettings("schemaStore.enable", false);
58+
await testCompletion(travisUri, new vscode.Position(0, 0), {
59+
items: []
60+
});
61+
});
62+
});

test/extension.test.ts

-23
This file was deleted.

test/helper.ts

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
* ------------------------------------------------------------------------------------------ */
6+
7+
import * as vscode from 'vscode';
8+
import * as path from 'path';
9+
import assert = require('assert');
10+
11+
export let doc: vscode.TextDocument;
12+
export let editor: vscode.TextEditor;
13+
export let documentEol: string;
14+
export let platformEol: string;
15+
16+
/**
17+
* Activates the redhat.vscode-yaml extension
18+
*/
19+
export async function activate(docUri: vscode.Uri) {
20+
const ext = vscode.extensions.getExtension('redhat.vscode-yaml')!;
21+
const activation = await ext.activate();
22+
try {
23+
doc = await vscode.workspace.openTextDocument(docUri);
24+
editor = await vscode.window.showTextDocument(doc);
25+
await sleep(2000); // Wait for server activation
26+
return activation;
27+
} catch (e) {
28+
console.error(e);
29+
}
30+
}
31+
32+
export async function sleep(ms: number) {
33+
return new Promise(resolve => setTimeout(resolve, ms));
34+
}
35+
36+
export const getDocPath = (p: string) => {
37+
return path.resolve(__dirname, '../../test/testFixture', p);
38+
};
39+
40+
export const getDocUri = (p: string) => {
41+
return vscode.Uri.file(getDocPath(p));
42+
};
43+
44+
export const updateSettings = (setting: any, value: any) => {
45+
const yamlConfiguration = vscode.workspace.getConfiguration("yaml");
46+
return yamlConfiguration.update(setting, value, false);
47+
}
48+
49+
export const resetSettings = (setting: any, value: any) => {
50+
const yamlConfiguration = vscode.workspace.getConfiguration("yaml");
51+
return yamlConfiguration.update(setting, value, false);
52+
}
53+
54+
export async function setTestContent(content: string): Promise<boolean> {
55+
const all = new vscode.Range(
56+
doc.positionAt(0),
57+
doc.positionAt(doc.getText().length)
58+
);
59+
return editor.edit(eb => eb.replace(all, content));
60+
}
61+
62+
export async function testCompletion(
63+
docUri: vscode.Uri,
64+
position: vscode.Position,
65+
expectedCompletionList: vscode.CompletionList
66+
) {
67+
68+
// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
69+
const actualCompletionList = (await vscode.commands.executeCommand(
70+
'vscode.executeCompletionItemProvider',
71+
docUri,
72+
position
73+
)) as vscode.CompletionList;
74+
75+
assert.equal(actualCompletionList.items.length, expectedCompletionList.items.length);
76+
expectedCompletionList.items.forEach((expectedItem, i) => {
77+
const actualItem = actualCompletionList.items[i];
78+
assert.equal(actualItem.label, expectedItem.label);
79+
assert.equal(actualItem.kind, expectedItem.kind);
80+
});
81+
}
82+
83+
export async function testCompletionNotEmpty(
84+
docUri: vscode.Uri,
85+
position: vscode.Position
86+
) {
87+
88+
// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
89+
const actualCompletionList = (await vscode.commands.executeCommand(
90+
'vscode.executeCompletionItemProvider',
91+
docUri,
92+
position
93+
)) as vscode.CompletionList;
94+
95+
assert.notEqual(actualCompletionList.items.length, 0);
96+
}
97+
98+
export async function testHover(
99+
docUri: vscode.Uri,
100+
position: vscode.Position,
101+
expectedHover: vscode.Hover[]
102+
) {
103+
104+
// Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion
105+
const actualHoverResults = (await vscode.commands.executeCommand(
106+
'vscode.executeHoverProvider',
107+
docUri,
108+
position
109+
)) as vscode.Hover[];
110+
111+
assert.equal(actualHoverResults.length, expectedHover.length);
112+
expectedHover.forEach((expectedItem, i) => {
113+
const actualItem = actualHoverResults[i];
114+
assert.equal((actualItem.contents[i] as vscode.MarkdownString).value, expectedItem.contents[i]);
115+
});
116+
}
117+
118+
export async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: vscode.Diagnostic[]) {
119+
const actualDiagnostics = vscode.languages.getDiagnostics(docUri);
120+
121+
assert.equal(actualDiagnostics.length, expectedDiagnostics.length);
122+
123+
expectedDiagnostics.forEach((expectedDiagnostic, i) => {
124+
const actualDiagnostic = actualDiagnostics[i]
125+
assert.equal(actualDiagnostic.message, expectedDiagnostic.message)
126+
assert.deepEqual(actualDiagnostic.range, expectedDiagnostic.range)
127+
assert.equal(actualDiagnostic.severity, expectedDiagnostic.severity)
128+
});
129+
}

test/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
// to report the results back to the caller. When the tests are finished, return
1111
// a possible error to the callback or null if none.
1212

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

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

22-
module.exports = testRunner;
23+
module.exports = testRunner;

0 commit comments

Comments
 (0)