@@ -25,21 +25,41 @@ import { Version } from "../../src/utilities/version";
2525import { folderContextPromise , globalWorkspaceContextPromise } from "./extension.test" ;
2626import { Workbench } from "../../src/utilities/commands" ;
2727
28- const waitForDiagnostics = ( uris : vscode . Uri [ ] , allowEmpty : boolean = true ) =>
29- new Promise < void > ( res =>
30- vscode . languages . onDidChangeDiagnostics ( e => {
31- const paths = e . uris . map ( u => u . fsPath ) ;
32- for ( const uri of uris ) {
33- if ( ! paths . includes ( uri . fsPath ) ) {
34- return ;
35- }
36- if ( ! allowEmpty && ! vscode . languages . getDiagnostics ( uri ) . length ) {
37- return ;
38- }
28+ // Wait for all the expected diagnostics to be recieved. This may happen over several
29+ // `onChangeDiagnostics` events.
30+ const waitForDiagnostics = ( expectedDiagnostics : { [ uri : string ] : vscode . Diagnostic [ ] } ) => {
31+ return new Promise < void > ( resolve => {
32+ // Keep a lookup of diagnostics we haven't encountered yet. When all array values in
33+ // this lookup are empty then we've seen all diagnostics and we can resolve successfully.
34+ const expected = { ...expectedDiagnostics } ;
35+ const disposable = vscode . languages . onDidChangeDiagnostics ( e => {
36+ const matchingPaths = Object . keys ( expectedDiagnostics ) . filter ( uri =>
37+ e . uris . some ( u => u . fsPath === uri )
38+ ) ;
39+ console . log ( ">>> Got diag for matching paths" , matchingPaths ) ;
40+ for ( const uri of matchingPaths ) {
41+ const actualDiagnostics = vscode . languages . getDiagnostics ( vscode . Uri . parse ( uri ) ) ;
42+ console . log ( ">>> " , uri , "Actual diagnostics" , actualDiagnostics ) ;
43+ console . log ( ">>> " , uri , "before" , expected [ uri ] . length ) ;
44+ expected [ uri ] = expected [ uri ] . filter ( expectedDiagnostic => {
45+ return ! actualDiagnostics . some ( actualDiagnostic =>
46+ isEqual ( actualDiagnostic , expectedDiagnostic )
47+ ) ;
48+ } ) ;
49+ console . log ( ">>> " , uri , "after" , expected [ uri ] . length ) ;
3950 }
40- res ( ) ;
41- } )
42- ) ;
51+
52+ const allDiagnosticsFulfilled = Object . values ( expected ) . every (
53+ diagnostics => diagnostics . length === 0
54+ ) ;
55+
56+ if ( allDiagnosticsFulfilled ) {
57+ disposable . dispose ( ) ;
58+ resolve ( ) ;
59+ }
60+ } ) ;
61+ } ) ;
62+ } ;
4363
4464const isEqual = ( d1 : vscode . Diagnostic , d2 : vscode . Diagnostic ) =>
4565 d1 . severity === d2 . severity &&
@@ -56,7 +76,7 @@ function assertHasDiagnostic(uri: vscode.Uri, expected: vscode.Diagnostic): vsco
5676 assert . notEqual (
5777 diagnostic ,
5878 undefined ,
59- `Could not find diagnostic matching:\n${ JSON . stringify ( expected ) } `
79+ `Could not find diagnostic matching:\n${ JSON . stringify ( expected ) } \nDiagnostics found:\n ${ JSON . stringify ( diagnostics ) } `
6080 ) ;
6181 return diagnostic ! ;
6282}
@@ -92,7 +112,7 @@ suite("DiagnosticsManager Test Suite", async function () {
92112 let cppHeaderUri : vscode . Uri ;
93113
94114 suiteSetup ( async function ( ) {
95- this . timeout ( 30000 ) ;
115+ this . timeout ( 60000 ) ;
96116
97117 workspaceContext = await globalWorkspaceContextPromise ;
98118 toolchain = workspaceContext . toolchain ;
@@ -112,7 +132,7 @@ suite("DiagnosticsManager Test Suite", async function () {
112132 } ) ;
113133
114134 suite ( "Parse diagnostics" , async ( ) => {
115- suite ( "Parse from task output" , async ( ) => {
135+ suite . only ( "Parse from task output" , async ( ) => {
116136 const expectedWarningDiagnostic = new vscode . Diagnostic (
117137 new vscode . Range ( new vscode . Position ( 1 , 8 ) , new vscode . Position ( 1 , 8 ) ) ,
118138 "Initialization of variable 'unused' was never used; consider replacing with assignment to '_' or removing it" ,
@@ -153,20 +173,21 @@ suite("DiagnosticsManager Test Suite", async function () {
153173 } ) ;
154174
155175 test ( "default diagnosticsStyle" , async ( ) => {
176+ console . log ( ">>> Updating diagnosticsStyle to default" ) ;
156177 await swiftConfig . update ( "diagnosticsStyle" , "default" ) ;
157- const task = createBuildAllTask ( folderContext ) ;
158- // Run actual task
159- const promise = waitForDiagnostics ( [ mainUri , funcUri ] ) ;
160- await executeTaskAndWaitForResult ( task ) ;
161- await promise ;
162- await waitForNoRunningTasks ( ) ;
163178
164- // Should have parsed correct severity
165- assertHasDiagnostic ( mainUri , expectedWarningDiagnostic ) ;
166- assertHasDiagnostic ( mainUri , expectedMainErrorDiagnostic ) ;
167- // Check parsed for other file
168- assertHasDiagnostic ( funcUri , expectedFuncErrorDiagnostic ) ;
169- } ) . timeout ( 2 * 60 * 1000 ) ; // Allow 2 minutes to build
179+ console . log ( ">>> Running actual task and waiting for diagnostics" ) ;
180+ await Promise . all ( [
181+ executeTaskAndWaitForResult ( createBuildAllTask ( folderContext ) ) ,
182+ waitForDiagnostics ( {
183+ [ mainUri . fsPath ] : [ expectedWarningDiagnostic , expectedMainErrorDiagnostic ] , // Should have parsed correct severity
184+ [ funcUri . fsPath ] : [ expectedFuncErrorDiagnostic ] , // Check parsed for other file
185+ } ) ,
186+ ] ) ;
187+
188+ console . log ( ">>> Waiting for no running tasks" ) ;
189+ await waitForNoRunningTasks ( ) ;
190+ } ) ;
170191
171192 test ( "swift diagnosticsStyle" , async function ( ) {
172193 // This is only supported in swift versions >=5.10.0
@@ -176,31 +197,29 @@ suite("DiagnosticsManager Test Suite", async function () {
176197 return ;
177198 }
178199 await swiftConfig . update ( "diagnosticsStyle" , "swift" ) ;
179- const task = createBuildAllTask ( folderContext ) ;
180- // Run actual task
181- const promise = waitForDiagnostics ( [ mainUri , funcUri ] ) ;
182- await executeTaskAndWaitForResult ( task ) ;
183- await promise ;
200+ await Promise . all ( [
201+ executeTaskAndWaitForResult ( createBuildAllTask ( folderContext ) ) ,
202+ waitForDiagnostics ( {
203+ [ mainUri . fsPath ] : [ expectedWarningDiagnostic , expectedMainErrorDiagnostic ] , // Should have parsed correct severity
204+ [ funcUri . fsPath ] : [ expectedFuncErrorDiagnostic ] , // Check parsed for other file
205+ } ) ,
206+ ] ) ;
184207 await waitForNoRunningTasks ( ) ;
185-
186- // Should have parsed severity
187- assertHasDiagnostic ( mainUri , expectedWarningDiagnostic ) ;
188- assertHasDiagnostic ( mainUri , expectedMainErrorDiagnostic ) ;
189- // Check parsed for other file
190- assertHasDiagnostic ( funcUri , expectedFuncErrorDiagnostic ) ;
191- } ) . timeout ( 2 * 60 * 1000 ) ; // Allow 2 minutes to build
208+ } ) ;
192209
193210 test ( "llvm diagnosticsStyle" , async ( ) => {
194211 await swiftConfig . update ( "diagnosticsStyle" , "llvm" ) ;
195- const task = createBuildAllTask ( folderContext ) ;
196- // Run actual task
197- const promise = waitForDiagnostics ( [ mainUri , funcUri ] ) ;
198- await executeTaskAndWaitForResult ( task ) ;
199- await promise ;
212+
213+ await Promise . all ( [
214+ executeTaskAndWaitForResult ( createBuildAllTask ( folderContext ) ) ,
215+ waitForDiagnostics ( {
216+ [ mainUri . fsPath ] : [ expectedWarningDiagnostic , expectedMainErrorDiagnostic ] , // Should have parsed correct severity
217+ [ funcUri . fsPath ] : [ expectedFuncErrorDiagnostic ] , // Check parsed for other file
218+ } ) ,
219+ ] ) ;
200220 await waitForNoRunningTasks ( ) ;
201221
202222 // Should have parsed severity
203- assertHasDiagnostic ( mainUri , expectedWarningDiagnostic ) ;
204223 const diagnostic = assertHasDiagnostic ( mainUri , expectedMainErrorDiagnostic ) ;
205224 // Should have parsed related note
206225 assert . equal ( diagnostic . relatedInformation ?. length , 1 ) ;
@@ -215,9 +234,7 @@ suite("DiagnosticsManager Test Suite", async function () {
215234 ) ,
216235 true
217236 ) ;
218- // Check parsed for other file
219- assertHasDiagnostic ( funcUri , expectedFuncErrorDiagnostic ) ;
220- } ) . timeout ( 2 * 60 * 1000 ) ; // Allow 2 minutes to build
237+ } ) ;
221238
222239 test ( "Parses C diagnostics" , async function ( ) {
223240 const swiftVersion = workspaceContext . toolchain . swiftVersion ;
@@ -228,12 +245,6 @@ suite("DiagnosticsManager Test Suite", async function () {
228245 }
229246
230247 await swiftConfig . update ( "diagnosticsStyle" , "llvm" ) ;
231- const task = createBuildAllTask ( cFolderContext ) ;
232- // Run actual task
233- const promise = waitForDiagnostics ( [ cUri ] ) ;
234- await executeTaskAndWaitForResult ( task ) ;
235- await promise ;
236- await waitForNoRunningTasks ( ) ;
237248
238249 // Should have parsed severity
239250 const expectedDiagnostic1 = new vscode . Diagnostic (
@@ -249,8 +260,13 @@ suite("DiagnosticsManager Test Suite", async function () {
249260 ) ;
250261 expectedDiagnostic2 . source = "swiftc" ;
251262
252- assertHasDiagnostic ( cUri , expectedDiagnostic1 ) ;
253- assertHasDiagnostic ( cUri , expectedDiagnostic2 ) ;
263+ await Promise . all ( [
264+ executeTaskAndWaitForResult ( createBuildAllTask ( cFolderContext ) ) ,
265+ waitForDiagnostics ( {
266+ [ cUri . fsPath ] : [ expectedDiagnostic1 , expectedDiagnostic2 ] ,
267+ } ) ,
268+ ] ) ;
269+ await waitForNoRunningTasks ( ) ;
254270 } ) ;
255271
256272 test ( "Parses C++ diagnostics" , async function ( ) {
@@ -262,12 +278,6 @@ suite("DiagnosticsManager Test Suite", async function () {
262278 }
263279
264280 await swiftConfig . update ( "diagnosticsStyle" , "llvm" ) ;
265- const task = createBuildAllTask ( cppFolderContext ) ;
266- // Run actual task
267- const promise = waitForDiagnostics ( [ cppUri ] ) ;
268- await executeTaskAndWaitForResult ( task ) ;
269- await promise ;
270- await waitForNoRunningTasks ( ) ;
271281
272282 // Should have parsed severity
273283 const expectedDiagnostic1 = new vscode . Diagnostic (
@@ -276,7 +286,6 @@ suite("DiagnosticsManager Test Suite", async function () {
276286 vscode . DiagnosticSeverity . Error
277287 ) ;
278288 expectedDiagnostic1 . source = "swiftc" ;
279- assertHasDiagnostic ( cppUri , expectedDiagnostic1 ) ;
280289
281290 // Should have parsed releated information
282291 const expectedDiagnostic2 = new vscode . Diagnostic (
@@ -285,6 +294,21 @@ suite("DiagnosticsManager Test Suite", async function () {
285294 vscode . DiagnosticSeverity . Error
286295 ) ;
287296 expectedDiagnostic2 . source = "swiftc" ;
297+
298+ await Promise . all ( [
299+ executeTaskAndWaitForResult ( createBuildAllTask ( cppFolderContext ) ) ,
300+ waitForDiagnostics ( {
301+ [ cppUri . fsPath ] : [ expectedDiagnostic1 , expectedDiagnostic2 ] ,
302+ } ) ,
303+ ] ) ;
304+ await waitForNoRunningTasks ( ) ;
305+ // const task = createBuildAllTask(cppFolderContext);
306+ // // Run actual task
307+ // const promise = Promise.resolve(); // waitForDiagnostics([cppUri]);
308+ // await executeTaskAndWaitForResult(task);
309+ // await promise;
310+ // await waitForNoRunningTasks();
311+
288312 const diagnostic = assertHasDiagnostic ( cppUri , expectedDiagnostic2 ) ;
289313 assert . equal (
290314 diagnostic . relatedInformation ! [ 0 ] . location . uri . fsPath ,
@@ -315,7 +339,7 @@ suite("DiagnosticsManager Test Suite", async function () {
315339 test ( "Parse partial line" , async ( ) => {
316340 const fixture = testSwiftTask ( "swift" , [ "build" ] , workspaceFolder , toolchain ) ;
317341 await vscode . tasks . executeTask ( fixture . task ) ;
318- const diagnosticsPromise = waitForDiagnostics ( [ mainUri ] ) ;
342+ const diagnosticsPromise = Promise . resolve ( ) ; // waitForDiagnostics([mainUri]);
319343 // Wait to spawn before writing
320344 fixture . process . write ( `${ mainUri . fsPath } :13:5: err` , "" ) ;
321345 fixture . process . write ( "or: Cannot find 'fo" , "" ) ;
@@ -331,7 +355,7 @@ suite("DiagnosticsManager Test Suite", async function () {
331355 test ( "Ignore duplicates" , async ( ) => {
332356 const fixture = testSwiftTask ( "swift" , [ "build" ] , workspaceFolder , toolchain ) ;
333357 await vscode . tasks . executeTask ( fixture . task ) ;
334- const diagnosticsPromise = waitForDiagnostics ( [ mainUri ] ) ;
358+ const diagnosticsPromise = Promise . resolve ( ) ; // waitForDiagnostics([mainUri]);
335359 // Wait to spawn before writing
336360 const output = `${ mainUri . fsPath } :13:5: error: Cannot find 'foo' in scope` ;
337361 fixture . process . write ( output ) ;
@@ -349,7 +373,7 @@ suite("DiagnosticsManager Test Suite", async function () {
349373 test ( "New set of swiftc diagnostics clear old list" , async ( ) => {
350374 let fixture = testSwiftTask ( "swift" , [ "build" ] , workspaceFolder , toolchain ) ;
351375 await vscode . tasks . executeTask ( fixture . task ) ;
352- let diagnosticsPromise = waitForDiagnostics ( [ mainUri ] ) ;
376+ let diagnosticsPromise = Promise . resolve ( ) ; // waitForDiagnostics([mainUri]);
353377 // Wait to spawn before writing
354378 fixture . process . write ( `${ mainUri . fsPath } :13:5: error: Cannot find 'foo' in scope` ) ;
355379 fixture . process . close ( 1 ) ;
@@ -363,7 +387,7 @@ suite("DiagnosticsManager Test Suite", async function () {
363387 // Run again but no diagnostics returned
364388 fixture = testSwiftTask ( "swift" , [ "build" ] , workspaceFolder , toolchain ) ;
365389 await vscode . tasks . executeTask ( fixture . task ) ;
366- diagnosticsPromise = waitForDiagnostics ( [ mainUri ] ) ;
390+ diagnosticsPromise = Promise . resolve ( ) ; // waitForDiagnostics([mainUri]);
367391 fixture . process . close ( 0 ) ;
368392 await waitForNoRunningTasks ( ) ;
369393 await diagnosticsPromise ;
@@ -920,7 +944,7 @@ suite("DiagnosticsManager Test Suite", async function () {
920944 await executeTaskAndWaitForResult ( task ) ;
921945
922946 // Open file
923- const promise = waitForDiagnostics ( [ mainUri ] , false ) ;
947+ const promise = Promise . resolve ( ) ; // waitForDiagnostics([mainUri], false);
924948 const document = await vscode . workspace . openTextDocument ( mainUri ) ;
925949 await vscode . languages . setTextDocumentLanguage ( document , "swift" ) ;
926950 await vscode . window . showTextDocument ( document ) ;
@@ -961,7 +985,7 @@ suite("DiagnosticsManager Test Suite", async function () {
961985 await executeTaskAndWaitForResult ( task ) ;
962986
963987 // Open file
964- const promise = waitForDiagnostics ( [ cUri ] , false ) ;
988+ const promise = Promise . resolve ( ) ; // waitForDiagnostics([cUri], false);
965989 const document = await vscode . workspace . openTextDocument ( cUri ) ;
966990 await vscode . languages . setTextDocumentLanguage ( document , "c" ) ;
967991 await vscode . window . showTextDocument ( document ) ;
0 commit comments