@@ -2,7 +2,9 @@ import * as assert from 'assert';
22import * as path from 'path' ;
33import * as vscode from 'vscode' ;
44// tslint:disable-next-line: no-duplicate-imports
5- import { Uri } from 'vscode' ;
5+ import { Disposable , Uri } from 'vscode' ;
6+
7+ import * as extension from '../../src/extension' ;
68
79const fixtureDir = path . resolve (
810 path . join ( __dirname , '..' , '..' , '..' , 'fixtures' ) ,
@@ -25,6 +27,10 @@ suite('Extension Tests', () => {
2527 ] ;
2628
2729 await vscode . commands . executeCommand ( 'vscode.openFolder' , projectUri ) ;
30+ const whenWorkspacesActive = projects . map ( path => {
31+ const fsPath = Uri . file ( path ) . fsPath ;
32+ return whenWorkspaceActive ( fsPath ) ;
33+ } ) ;
2834
2935 // This makes sure that we set the focus on the opened files (which is what
3036 // actually triggers the extension for the project)
@@ -36,9 +42,9 @@ suite('Extension Tests', () => {
3642 'workbench.action.acceptSelectedQuickOpenItem' ,
3743 ) ;
3844 await vscode . commands . executeCommand ( 'workbench.action.keepEditor' ) ;
39- // Unfortunately, we need to wait a bit for the extension to kick in :(
40- // FIXME: See if we can directly import our extension and await its progress
41- await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
45+ // Wait until the first server is ready
46+ await Promise . race ( [ whenWorkspacesActive [ 0 ] , timeoutReject ( 3000 ) ] ) ;
47+
4248 assert ( await currentTasksInclude ( [ expected [ 0 ] ] ) ) ;
4349
4450 // Now test for the second project
@@ -49,7 +55,8 @@ suite('Extension Tests', () => {
4955 await vscode . commands . executeCommand (
5056 'workbench.action.acceptSelectedQuickOpenItem' ,
5157 ) ;
52- await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
58+ // Wait until the second server is ready
59+ await Promise . race ( [ whenWorkspacesActive [ 1 ] , timeoutReject ( 3000 ) ] ) ;
5360 assert ( await currentTasksInclude ( expected ) ) ;
5461 } ) . timeout ( 0 ) ;
5562} ) ;
@@ -77,3 +84,30 @@ async function currentTasksInclude(
7784 ) ,
7885 ) ;
7986}
87+
88+ /**
89+ * Returns a promise when a client workspace will become active with a given path.
90+ * @param fsPath normalized file system path of a URI
91+ */
92+ function whenWorkspaceActive (
93+ fsPath : string ,
94+ ) : Promise < extension . ClientWorkspace > {
95+ return new Promise ( resolve => {
96+ let disposable : Disposable | undefined ;
97+ disposable = extension . activeWorkspace . observe ( value => {
98+ if ( value && value . folder . uri . fsPath === fsPath ) {
99+ if ( disposable ) {
100+ disposable . dispose ( ) ;
101+ disposable = undefined ;
102+ }
103+
104+ resolve ( value ) ;
105+ }
106+ } ) ;
107+ } ) ;
108+ }
109+
110+ const timeoutReject = ( ms : number ) =>
111+ new Promise ( ( _ , reject ) =>
112+ setTimeout ( ( ) => reject ( new Error ( 'Timed out' ) ) , ms ) ,
113+ ) ;
0 commit comments