Skip to content

Commit adf3dfe

Browse files
fix: failing unit tests (#3787)
- These tests started failing when we upgrade our vscode min version to 1.68.0 - We got the error: "Error: ENOSPC: System limit for number of file watchers reached" - This commit stops using file watchers and uses a loop instead Signed-off-by: nkomonen <[email protected]>
1 parent 0f0fbc8 commit adf3dfe

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/test/shared/logger/winstonToolkitLogger.test.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import * as fs from 'fs-extra'
76
import assert from 'assert'
7+
import * as fs from 'fs'
88
import * as path from 'path'
99
import * as filesystemUtilities from '../../../shared/filesystemUtilities'
1010
import * as vscode from 'vscode'
1111
import { WinstonToolkitLogger } from '../../../shared/logger/winstonToolkitLogger'
1212
import { MockOutputChannel } from '../../mockOutputChannel'
13-
import { waitUntil } from '../../../shared/utilities/timeoutUtils'
13+
import { sleep, waitUntil } from '../../../shared/utilities/timeoutUtils'
1414

1515
/**
1616
* Disposes the logger then waits for the write streams to flush. The `expected` and `unexpected` arrays just look
@@ -23,32 +23,33 @@ async function checkFile(
2323
expected: string[],
2424
unexpected: string[] = []
2525
): Promise<void> {
26-
const check = new Promise<void>((resolve, reject) => {
27-
const watcher = fs.watch(path.dirname(logPath), {}, (_, name) => {
28-
if (name === path.basename(logPath)) {
29-
checkText()
30-
}
31-
})
26+
const check = new Promise<void>(async (resolve, reject) => {
27+
// Timeout if we wait too long for file to exist
28+
setTimeout(() => reject(new Error('Timed out waiting for log message')), 10_000)
3229

33-
function checkText() {
34-
const contents = fs.readFileSync(logPath)
35-
const errorMsg = unexpected
36-
.filter(t => contents.includes(t))
37-
.reduce((a, b) => a + `Unexpected message in log: ${b}\n`, '')
38-
39-
if (errorMsg) {
40-
watcher.close()
41-
return reject(new Error(errorMsg))
42-
}
30+
// Wait for file to exist
31+
while (!fs.existsSync(logPath)) {
32+
await sleep(200)
33+
}
34+
const contents = fs.readFileSync(logPath)
35+
36+
// Error if unexpected messages are in the log file
37+
const explicitUnexpectedMessages = unexpected
38+
.filter(t => contents.includes(t))
39+
.reduce((a, b) => a + `Unexpected message in log: ${b}\n`, '')
40+
if (explicitUnexpectedMessages) {
41+
return reject(new Error(explicitUnexpectedMessages))
42+
}
4343

44-
expected = expected.filter(t => !contents.includes(t))
45-
if (expected.length === 0) {
46-
watcher.close()
47-
resolve()
48-
}
44+
// Error if any of the expected messages are not in the log file
45+
const expectedMessagesNotInLogFile = expected.filter(t => !contents.includes(t))
46+
if (expectedMessagesNotInLogFile.length > 0) {
47+
reject(
48+
new Error(expectedMessagesNotInLogFile.reduce((a, b) => a + `Unexpected message in log: ${b}\n`, ''))
49+
)
4950
}
5051

51-
setTimeout(() => reject(new Error('Timed out waiting for log message')), 10000)
52+
resolve()
5253
})
5354

5455
return logger.dispose().then(() => check)

0 commit comments

Comments
 (0)