From fb97ffc1609dca6186eb6532095b39bde9938658 Mon Sep 17 00:00:00 2001 From: Johannes Date: Fri, 1 Jul 2022 15:39:51 +0200 Subject: [PATCH] be tolerant to other events happing but still enforce that the expected events are there fixes https://github.com/microsoft/vscode/issues/153288 --- .../src/singlefolder-tests/workspace.test.ts | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts index a055cf45520..6e088ce9ee5 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts @@ -286,9 +286,6 @@ suite('vscode API - workspace', () => { sub.dispose(); }); - function assertEqualPath(a: string, b: string): void { - assert.ok(pathEquals(a, b), `${a} <-> ${b}`); - } test('events: onDidOpenTextDocument, onDidChangeTextDocument, onDidSaveTextDocument', async () => { const file = await createRandomFile(); @@ -296,23 +293,20 @@ suite('vscode API - workspace', () => { await revertAllDirty(); // needed for a clean state for `onDidSaveTextDocument` (#102365) - const pendingAsserts: Function[] = []; - let onDidOpenTextDocument = false; + const onDidOpenTextDocument = new Set(); + const onDidChangeTextDocument = new Set(); + const onDidSaveTextDocument = new Set(); + disposables.push(vscode.workspace.onDidOpenTextDocument(e => { - pendingAsserts.push(() => assertEqualPath(e.uri.fsPath, file.fsPath)); - onDidOpenTextDocument = true; + onDidOpenTextDocument.add(e); })); - let onDidChangeTextDocument = false; disposables.push(vscode.workspace.onDidChangeTextDocument(e => { - pendingAsserts.push(() => assertEqualPath(e.document.uri.fsPath, file.fsPath)); - onDidChangeTextDocument = true; + onDidChangeTextDocument.add(e.document); })); - let onDidSaveTextDocument = false; disposables.push(vscode.workspace.onDidSaveTextDocument(e => { - pendingAsserts.push(() => assertEqualPath(e.uri.fsPath, file.fsPath)); - onDidSaveTextDocument = true; + onDidSaveTextDocument.add(e); })); const doc = await vscode.workspace.openTextDocument(file); @@ -323,10 +317,10 @@ suite('vscode API - workspace', () => { }); await doc.save(); - assert.ok(onDidOpenTextDocument); - assert.ok(onDidChangeTextDocument); - assert.ok(onDidSaveTextDocument); - pendingAsserts.forEach(assert => assert()); + assert.ok(Array.from(onDidOpenTextDocument).find(e => e.uri.toString() === file.toString()), 'did Open: ' + file.toString()); + assert.ok(Array.from(onDidChangeTextDocument).find(e => e.uri.toString() === file.toString()), 'did Change: ' + file.toString()); + assert.ok(Array.from(onDidSaveTextDocument).find(e => e.uri.toString() === file.toString()), 'did Save: ' + file.toString()); + disposeAll(disposables); return deleteFile(file); }); @@ -334,14 +328,13 @@ suite('vscode API - workspace', () => { test('events: onDidSaveTextDocument fires even for non dirty file when saved', async () => { const file = await createRandomFile(); const disposables: vscode.Disposable[] = []; - const pendingAsserts: Function[] = []; await revertAllDirty(); // needed for a clean state for `onDidSaveTextDocument` (#102365) - let onDidSaveTextDocument = false; + const onDidSaveTextDocument = new Set(); + disposables.push(vscode.workspace.onDidSaveTextDocument(e => { - pendingAsserts.push(() => assertEqualPath(e.uri.fsPath, file.fsPath)); - onDidSaveTextDocument = true; + onDidSaveTextDocument.add(e); })); const doc = await vscode.workspace.openTextDocument(file); @@ -349,7 +342,7 @@ suite('vscode API - workspace', () => { await vscode.commands.executeCommand('workbench.action.files.save'); assert.ok(onDidSaveTextDocument); - pendingAsserts.forEach(fn => fn()); + assert.ok(Array.from(onDidSaveTextDocument).find(e => e.uri.toString() === file.toString()), 'did Save: ' + file.toString()); disposeAll(disposables); return deleteFile(file); });