3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- import * as fs from 'fs-extra'
7
6
import assert from 'assert'
7
+ import * as fs from 'fs'
8
8
import * as path from 'path'
9
9
import * as filesystemUtilities from '../../../shared/filesystemUtilities'
10
10
import * as vscode from 'vscode'
11
11
import { WinstonToolkitLogger } from '../../../shared/logger/winstonToolkitLogger'
12
12
import { MockOutputChannel } from '../../mockOutputChannel'
13
- import { waitUntil } from '../../../shared/utilities/timeoutUtils'
13
+ import { sleep , waitUntil } from '../../../shared/utilities/timeoutUtils'
14
14
15
15
/**
16
16
* 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(
23
23
expected : string [ ] ,
24
24
unexpected : string [ ] = [ ]
25
25
) : 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 )
32
29
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
+ }
43
43
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
+ )
49
50
}
50
51
51
- setTimeout ( ( ) => reject ( new Error ( 'Timed out waiting for log message' ) ) , 10000 )
52
+ resolve ( )
52
53
} )
53
54
54
55
return logger . dispose ( ) . then ( ( ) => check )
0 commit comments