@@ -2,7 +2,9 @@ import * as assert from 'assert';
2
2
import * as path from 'path' ;
3
3
import * as vscode from 'vscode' ;
4
4
// 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' ;
6
8
7
9
const fixtureDir = path . resolve (
8
10
path . join ( __dirname , '..' , '..' , '..' , 'fixtures' ) ,
@@ -25,6 +27,10 @@ suite('Extension Tests', () => {
25
27
] ;
26
28
27
29
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
+ } ) ;
28
34
29
35
// This makes sure that we set the focus on the opened files (which is what
30
36
// actually triggers the extension for the project)
@@ -36,9 +42,9 @@ suite('Extension Tests', () => {
36
42
'workbench.action.acceptSelectedQuickOpenItem' ,
37
43
) ;
38
44
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
+
42
48
assert ( await currentTasksInclude ( [ expected [ 0 ] ] ) ) ;
43
49
44
50
// Now test for the second project
@@ -49,7 +55,8 @@ suite('Extension Tests', () => {
49
55
await vscode . commands . executeCommand (
50
56
'workbench.action.acceptSelectedQuickOpenItem' ,
51
57
) ;
52
- await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
58
+ // Wait until the second server is ready
59
+ await Promise . race ( [ whenWorkspacesActive [ 1 ] , timeoutReject ( 3000 ) ] ) ;
53
60
assert ( await currentTasksInclude ( expected ) ) ;
54
61
} ) . timeout ( 0 ) ;
55
62
} ) ;
@@ -77,3 +84,30 @@ async function currentTasksInclude(
77
84
) ,
78
85
) ;
79
86
}
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