Skip to content

Commit 75e242d

Browse files
authored
fix(tests): closeAllEditors() sometimes fails #3676
Problem: - closeAllEditors() sometimes fails (example below). - Its failure message has a race with the waitUntil() callback. 2 failing 1) FileViewerManager "after each" hook for "opens a new editor if no document exists": Error: The following editors were still open after closeAllEditors(): extension-output-amazonwebservices.aws-toolkit-vscode-#17-test channel /us-west-2/bucket-name/test1.txt at /Users/runner/work/aws-toolkit-vscode/aws-toolkit-vscode/src/test/testUtil.ts:276:15 at Generator.next (<anonymous>) at fulfilled (dist/src/test/testUtil.js:9:58) 2) "before each" hook: beforeEach for "Test AslVisualizationManager managedVisualizations set does not add duplicate renders when multiple Vis active": Error: The following editors were still open after closeAllEditors(): extension-output-amazonwebservices.aws-toolkit-vscode-#1-AWS Toolkit Logs at Object.<anonymous> (src/test/testUtil.ts:276:15) at Generator.next (<anonymous>) at fulfilled (dist/src/test/testUtil.js:9:58) Solution: - Increase the waitUntil() timeout. - Store the editor list from waitUntil() and use that in the failure message. - Delete redundant, unused closeAllEditors() from src/shared/utilities/vsCodeUtils.ts.
1 parent ca3d6d4 commit 75e242d

File tree

2 files changed

+12
-40
lines changed

2 files changed

+12
-40
lines changed

src/shared/utilities/vsCodeUtils.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,14 @@ import * as nls from 'vscode-nls'
88
import { getIdeProperties } from '../extensionUtilities'
99
import * as pathutils from './pathUtils'
1010
import { getLogger } from '../logger/logger'
11-
import { CancellationError, Timeout, waitTimeout, waitUntil } from './timeoutUtils'
11+
import { CancellationError, Timeout, waitTimeout } from './timeoutUtils'
1212
import { telemetry } from '../telemetry/telemetry'
1313
import * as semver from 'semver'
1414
import { isNonNullable } from './tsUtils'
1515

1616
// TODO: Consider NLS initialization/configuration here & have packages to import localize from here
1717
export const localize = nls.loadMessageBundle()
1818

19-
/**
20-
* Executes the close all editors command and waits for all visible editors to disappear
21-
*/
22-
export async function closeAllEditors() {
23-
await vscode.commands.executeCommand('workbench.action.closeAllEditors')
24-
25-
// The output channel counts as an editor, but you can't really close that...
26-
const noVisibleEditor: boolean | undefined = await waitUntil(
27-
async () => {
28-
const visibleEditors = vscode.window.visibleTextEditors.filter(
29-
editor => !editor.document.fileName.includes('extension-output') // Output channels are named with the prefix 'extension-output'
30-
)
31-
32-
return visibleEditors.length === 0
33-
},
34-
{
35-
timeout: 2500, // Arbitrary values. Should succeed except when VS Code is lagging heavily.
36-
interval: 250,
37-
truthy: true,
38-
}
39-
)
40-
41-
if (!noVisibleEditor) {
42-
throw new Error(
43-
`Editor "${
44-
vscode.window.activeTextEditor!.document.fileName
45-
}" was still open after executing "closeAllEditors"`
46-
)
47-
}
48-
}
49-
5019
/**
5120
* Checks if an extension is installed and active.
5221
*/

src/test/testUtil.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,31 +294,34 @@ export async function closeAllEditors(): Promise<void> {
294294
// Note: `workbench.action.closeAllEditors` is unreliable.
295295
const closeAllCmd = 'openEditors.closeAll'
296296

297-
// Output channels are named with the prefix 'extension-output'
297+
// Output channels are named with prefix "extension-output". https://github.com/microsoft/vscode/issues/148993#issuecomment-1167654358
298298
// Maybe we can close these with a command?
299299
const ignorePatterns = [/extension-output/, /tasks/]
300+
const editors: vscode.TextEditor[] = []
300301

301302
const noVisibleEditor: boolean | undefined = await waitUntil(
302303
async () => {
303304
// Race: documents could appear after the call to closeAllEditors(), so retry.
304305
await vscode.commands.executeCommand(closeAllCmd)
305-
const visibleEditors = vscode.window.visibleTextEditors.filter(
306-
editor => !ignorePatterns.find(p => p.test(editor.document.fileName))
306+
editors.length = 0
307+
editors.push(
308+
...vscode.window.visibleTextEditors.filter(
309+
editor => !ignorePatterns.find(p => p.test(editor.document.fileName))
310+
)
307311
)
308312

309-
return visibleEditors.length === 0
313+
return editors.length === 0
310314
},
311315
{
312-
timeout: 2500, // Arbitrary values. Should succeed except when VS Code is lagging heavily.
316+
timeout: 5000, // Arbitrary values. Should succeed except when VS Code is lagging heavily.
313317
interval: 250,
314318
truthy: true,
315319
}
316320
)
317321

318322
if (!noVisibleEditor) {
319-
const editors = vscode.window.visibleTextEditors.map(editor => `\t${editor.document.fileName}`)
320-
321-
throw new Error(`The following editors were still open after closeAllEditors():\n${editors.join('\n')}`)
323+
const editorNames = editors.map(editor => `\t${editor.document.fileName}`)
324+
throw new Error(`Editors were still open after closeAllEditors():\n${editorNames.join('\n')}`)
322325
}
323326
}
324327

0 commit comments

Comments
 (0)