Skip to content

Commit 3c2f4af

Browse files
committed
Smarter way of ensuring activation
1 parent a24fa12 commit 3c2f4af

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

client/src/test/diagnostics.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as vscode from 'vscode';
77
import * as assert from 'assert';
8-
import { getDocUri, activate } from './helper';
8+
import { getDocUri, activate, runOnActivate } from './helper';
99
import { toRange } from './util';
1010

1111
suite('Should get diagnostics', () => {
@@ -147,8 +147,11 @@ suite('Should get diagnostics', () => {
147147
async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: vscode.Diagnostic[]) {
148148
await activate(docUri);
149149

150-
const actualDiagnostics = vscode.languages.getDiagnostics(docUri);
151-
150+
// Use this method first to ensure the extension is activated.
151+
const actualDiagnostics = await runOnActivate(
152+
() => vscode.languages.getDiagnostics(docUri),
153+
(result) => result.length > 0
154+
);
152155
assert.equal(actualDiagnostics.length, expectedDiagnostics.length, "Count");
153156

154157
expectedDiagnostics.forEach((expectedDiagnostic, i) => {

client/src/test/foldingRanges.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as assert from 'assert';
3-
import { getDocUri, activate } from './helper';
3+
import { getDocUri, activate, runOnActivate } from './helper';
44

55
suite('Should get folding ranges', () => {
66
test('formatting.class.template', async () => {
@@ -24,10 +24,16 @@ suite('Should get folding ranges', () => {
2424

2525
async function testFoldingRanges(docUri: vscode.Uri, expectedFoldingRanges: vscode.FoldingRange[]) {
2626
await activate(docUri);
27-
const actualFoldingRanges = await vscode.commands.executeCommand<vscode.FoldingRange[]>(
27+
const action = () => vscode.commands.executeCommand<vscode.FoldingRange[]>(
2828
'vscode.executeFoldingRangeProvider',
2929
docUri
3030
)
31+
32+
// Use this method first to ensure the extension is activated.
33+
const actualFoldingRanges = await runOnActivate(
34+
action,
35+
(result) => Array.isArray(result) && result.length > 0
36+
);
3137

3238
assert.equal(actualFoldingRanges.length ?? 0, expectedFoldingRanges.length, "Count");
3339

client/src/test/formatting.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as assert from 'assert';
3-
import { getDocUri, activate } from './helper';
3+
import { getDocUri, activate, runOnActivate } from './helper';
44
import { toRange } from './util';
55

66
suite('Should get text edits', () => {
@@ -42,11 +42,18 @@ suite('Should get text edits', () => {
4242

4343
async function testTextEdits(docUri: vscode.Uri, expectedTextEdits: vscode.TextEdit[]) {
4444
await activate(docUri);
45-
const actualEdits = await vscode.commands.executeCommand<vscode.TextEdit[]>(
45+
await vscode.window.showTextDocument(docUri);
46+
const action = () => vscode.commands.executeCommand<vscode.TextEdit[]>(
4647
'vscode.executeFormatDocumentProvider',
4748
docUri,
4849
{ tabSize: 4, insertSpaces: true }
49-
)
50+
);
51+
52+
// Use this method first to ensure the extension is activated.
53+
const actualEdits = await runOnActivate(
54+
action,
55+
(result) => Array.isArray(result) && result.length > 0
56+
);
5057

5158
assert.equal(actualEdits.length ?? 0, expectedTextEdits.length, "Count");
5259

client/src/test/helper.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export let doc: vscode.TextDocument;
1010
export let editor: vscode.TextEditor;
1111
export let documentEol: string;
1212
export let platformEol: string;
13+
const TIMEOUTMS = 5000;
1314

1415
/**
1516
* Activates the vscode.lsp-sample extension
@@ -21,16 +22,31 @@ export async function activate(docUri: vscode.Uri) {
2122
try {
2223
doc = await vscode.workspace.openTextDocument(docUri);
2324
editor = await vscode.window.showTextDocument(doc);
24-
await sleep(500); // Wait for server activation
2525
} catch (e) {
2626
console.error(e);
2727
}
2828
}
2929

30+
export function getTimeout() {
31+
return Date.now() + TIMEOUTMS;
32+
}
33+
3034
async function sleep(ms: number) {
3135
return new Promise(resolve => setTimeout(resolve, ms));
3236
}
3337

38+
export async function runOnActivate<T>(action: () => T|Thenable<T>, test: (result: T) => boolean): Promise<T> {
39+
const timeout = getTimeout();
40+
while (Date.now() < timeout) {
41+
const result = await action();
42+
if (test(result)) {
43+
return result;
44+
}
45+
await sleep(100);
46+
}
47+
throw new Error(`Timed out after ${TIMEOUTMS}`);
48+
}
49+
3450
export const getDocPath = (p: string) => {
3551
return path.resolve(__dirname, '../../../../test/fixtures', p);
3652
};

0 commit comments

Comments
 (0)