File tree Expand file tree Collapse file tree 4 files changed +41
-9
lines changed Expand file tree Collapse file tree 4 files changed +41
-9
lines changed Original file line number Diff line number Diff line change 55
66import * as vscode from 'vscode' ;
77import * as assert from 'assert' ;
8- import { getDocUri , activate } from './helper' ;
8+ import { getDocUri , activate , runOnActivate } from './helper' ;
99import { toRange } from './util' ;
1010
1111suite ( 'Should get diagnostics' , ( ) => {
@@ -147,8 +147,11 @@ suite('Should get diagnostics', () => {
147147async 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 ) => {
Original file line number Diff line number Diff line change 11import * as vscode from 'vscode' ;
22import * as assert from 'assert' ;
3- import { getDocUri , activate } from './helper' ;
3+ import { getDocUri , activate , runOnActivate } from './helper' ;
44
55suite ( 'Should get folding ranges' , ( ) => {
66 test ( 'formatting.class.template' , async ( ) => {
@@ -24,10 +24,16 @@ suite('Should get folding ranges', () => {
2424
2525async 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
Original file line number Diff line number Diff line change 11import * as vscode from 'vscode' ;
22import * as assert from 'assert' ;
3- import { getDocUri , activate } from './helper' ;
3+ import { getDocUri , activate , runOnActivate } from './helper' ;
44import { toRange } from './util' ;
55
66suite ( 'Should get text edits' , ( ) => {
@@ -42,11 +42,18 @@ suite('Should get text edits', () => {
4242
4343async 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
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ export let doc: vscode.TextDocument;
1010export let editor : vscode . TextEditor ;
1111export let documentEol : string ;
1212export 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+
3034async 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+
3450export const getDocPath = ( p : string ) => {
3551 return path . resolve ( __dirname , '../../../../test/fixtures' , p ) ;
3652} ;
You can’t perform that action at this time.
0 commit comments