@@ -93,8 +93,8 @@ describe("unittests:: tsserver:: events:: watchEvents", () => {
93
93
function updateFileOnHost ( session : TestSession , file : string , log : string , content ?: string ) {
94
94
// Change b.ts
95
95
session . logger . log ( `${ log } : ${ file } ` ) ;
96
- if ( content ) session . host . appendFile ( file , content ) ;
97
- else session . host . writeFile ( file , session . host . readFile ( "/user/username/projects/myproject/a.ts" ) ! ) ;
96
+ if ( content && session . host . fileExists ( file ) ) session . host . appendFile ( file , content ) ;
97
+ else session . host . writeFile ( file , content ?? session . host . readFile ( "/user/username/projects/myproject/a.ts" ) ! ) ;
98
98
session . host . runQueuedTimeoutCallbacks ( ) ;
99
99
}
100
100
@@ -150,12 +150,13 @@ describe("unittests:: tsserver:: events:: watchEvents", () => {
150
150
session : TestSession ,
151
151
file : string ,
152
152
eventType : "created" | "deleted" | "updated" ,
153
+ eventPath ?: string ,
153
154
) {
154
155
return collectWatchChanges (
155
156
session ,
156
157
( session . logger . host as TestServerHostWithCustomWatch ) . factoryData . watchUtils . pollingWatches ,
157
158
file ,
158
- file ,
159
+ eventPath ?? file ,
159
160
eventType ,
160
161
) ;
161
162
}
@@ -185,21 +186,21 @@ describe("unittests:: tsserver:: events:: watchEvents", () => {
185
186
}
186
187
}
187
188
188
- function addFile ( session : TestSession , path : string ) {
189
- updateFileOnHost ( session , path , "Add file" ) ;
189
+ function addFile ( session : TestSession , path : string , content ?: string , eventPath ?: string ) {
190
+ updateFileOnHost ( session , path , "Add file" , content ) ;
190
191
invokeWatchChange (
191
192
session ,
192
- collectDirectoryWatcherChanges ( session , "/user/username/projects/myproject" , path , "created" ) ,
193
+ collectDirectoryWatcherChanges ( session , ts . getDirectoryPath ( path ) , eventPath ?? path , "created" ) ,
193
194
) ;
194
195
session . host . runQueuedTimeoutCallbacks ( ) ;
195
196
}
196
197
197
- function changeFile ( session : TestSession , path : string , content ?: string ) {
198
+ function changeFile ( session : TestSession , path : string , content ?: string , eventPath ?: string ) {
198
199
updateFileOnHost ( session , path , "Change File" , content ) ;
199
200
invokeWatchChange (
200
201
session ,
201
- collectFileWatcherChanges ( session , path , "updated" ) ,
202
- collectDirectoryWatcherChanges ( session , ts . getDirectoryPath ( path ) , path , "updated" ) ,
202
+ collectFileWatcherChanges ( session , path , "updated" , eventPath ) ,
203
+ collectDirectoryWatcherChanges ( session , ts . getDirectoryPath ( path ) , eventPath ?? path , "updated" ) ,
203
204
) ;
204
205
session . host . runQueuedTimeoutCallbacks ( ) ;
205
206
}
@@ -321,4 +322,81 @@ describe("unittests:: tsserver:: events:: watchEvents", () => {
321
322
322
323
baselineTsserverLogs ( "events/watchEvents" , `canUseWatchEvents without canUseEvents` , session ) ;
323
324
} ) ;
325
+
326
+ it ( "canUseWatchEvents on windows" , ( ) => {
327
+ const inputHost = createServerHost ( {
328
+ "c:\\projects\\myproject\\tsconfig.json" : "{}" ,
329
+ "c:\\projects\\myproject\\a.ts" : `export class a { prop = "hello"; foo() { return this.prop; } }` ,
330
+ "c:\\projects\\myproject\\b.ts" : `export class b { prop = "hello"; foo() { return this.prop; } }` ,
331
+ "c:\\projects\\myproject\\m.ts" : `import { x } from "something"` ,
332
+ "c:\\projects\\myproject\\node_modules\\something\\index.d.ts" : `export const x = 10;` ,
333
+ [ libFile . path ] : libFile . content ,
334
+ } , { windowsStyleRoot : "c:\\" } ) ;
335
+ const logger = createLoggerWithInMemoryLogs ( inputHost ) ;
336
+ const host = createTestServerHostWithCustomWatch ( logger ) ;
337
+
338
+ const session = createSessionWithCustomEventHandler ( { host, canUseWatchEvents : true , logger } , handleWatchEvents ) ;
339
+ openFilesForSession ( [ "c:\\projects\\myproject\\a.ts" ] , session ) ;
340
+
341
+ // Directory watcher
342
+ addFile ( session , "c:/projects/myproject/c.ts" , `export xyx = 10;` , "c:\\projects\\myproject\\c.ts" ) ;
343
+
344
+ // File Watcher
345
+ changeFile ( session , "c:/projects/myproject/b.ts" , "export const ss = 20;" , "c:\\projects\\myproject\\b.ts" ) ;
346
+
347
+ // Close watcher
348
+ openFilesForSession ( [ "c:\\projects\\myproject\\b.ts" ] , session ) ;
349
+
350
+ // Re watch
351
+ closeFilesForSession ( [ "c:\\projects\\myproject\\b.ts" ] , session ) ;
352
+
353
+ // Update c.ts
354
+ changeFile ( session , "c:/projects/myproject/c.ts" , "export const ss = 20;" , "c:\\projects\\myproject\\b.ts" ) ;
355
+
356
+ // Update with npm install
357
+ session . logger . log ( "update with npm install" ) ;
358
+ session . host . appendFile ( "c:\\projects\\myproject\\node_modules\\something\\index.d.ts" , `export const y = 20;` ) ;
359
+ session . host . runQueuedTimeoutCallbacks ( ) ;
360
+ invokeWatchChange (
361
+ session ,
362
+ collectDirectoryWatcherChanges (
363
+ session ,
364
+ "c:/projects/myproject/node_modules" ,
365
+ "c:\\projects\\myproject\\node_modules\\something\\index.d.ts" ,
366
+ "updated" ,
367
+ ) ,
368
+ ) ;
369
+ session . host . runQueuedTimeoutCallbacks ( ) ;
370
+ host . runQueuedTimeoutCallbacks ( ) ;
371
+
372
+ // Add and change multiple files - combine and send multiple requests together
373
+ updateFileOnHost ( session , "c:/projects/myproject/d.ts" , "Add file" , "export const yy = 10;" ) ;
374
+ updateFileOnHost ( session , "c:/projects/myproject/c.ts" , "Change File" , `export const z = 30;` ) ;
375
+ updateFileOnHost ( session , "c:/projects/myproject/e.ts" , "Add File" , "export const zz = 40;" ) ;
376
+ invokeWatchChange (
377
+ session ,
378
+ collectDirectoryWatcherChanges ( session , "c:/projects/myproject" , "c:\\projects\\myproject\\d.ts" , "created" ) ,
379
+ collectFileWatcherChanges ( session , "c:/projects/myproject/c.ts" , "updated" , "c:\\projects\\myproject\\c.ts" ) ,
380
+ collectDirectoryWatcherChanges ( session , "c:/projects/myproject" , "c:\\projects\\myproject\\c.ts" , "updated" ) ,
381
+ collectDirectoryWatcherChanges ( session , "c:/projects/myproject" , "c:\\projects\\myproject\\e.ts" , "created" ) ,
382
+ ) ;
383
+ session . host . runQueuedTimeoutCallbacks ( ) ;
384
+
385
+ baselineTsserverLogs ( "events/watchEvents" , `canUseWatchEvents on windows` , session ) ;
386
+ function handleWatchEvents ( event : ts . server . ProjectServiceEvent ) {
387
+ switch ( event . eventName ) {
388
+ case ts . server . CreateFileWatcherEvent :
389
+ host . factoryData . watchFile ( event . data ) ;
390
+ break ;
391
+ case ts . server . CreateDirectoryWatcherEvent :
392
+ host . factoryData . watchDirectory ( event . data ) ;
393
+ break ;
394
+ case ts . server . CloseFileWatcherEvent :
395
+ host . factoryData . closeWatcher ( event . data ) ;
396
+ break ;
397
+ default :
398
+ break ;
399
+ }
400
+ }
401
+ } ) ;
324
402
} ) ;
0 commit comments