@@ -200,9 +200,7 @@ describe("databases", () => {
200200
201201 it ( "should remove a database item" , async ( ) => {
202202 const mockDbItem = createMockDB ( ) ;
203- const removeMock = jest
204- . spyOn ( fs , "remove" )
205- . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
203+ await fs . ensureDir ( mockDbItem . databaseUri . fsPath ) ;
206204
207205 // pretend that this item is the first workspace folder in the list
208206 jest
@@ -229,13 +227,14 @@ describe("databases", () => {
229227 expect ( workspace . updateWorkspaceFolders ) . toBeCalledWith ( 0 , 1 ) ;
230228
231229 // should also delete the db contents
232- expect ( removeMock ) . toBeCalledWith ( mockDbItem . databaseUri . fsPath ) ;
230+ await expect ( fs . pathExists ( mockDbItem . databaseUri . fsPath ) ) . resolves . toBe (
231+ false ,
232+ ) ;
233233 } ) ;
234234
235235 it ( "should remove a database item outside of the extension controlled area" , async ( ) => {
236236 const mockDbItem = createMockDB ( ) ;
237- const removeMock = jest . spyOn ( fs , "remove" ) ;
238- removeMock . mockReset ( ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
237+ await fs . ensureDir ( mockDbItem . databaseUri . fsPath ) ;
239238
240239 // pretend that this item is the first workspace folder in the list
241240 jest
@@ -263,16 +262,16 @@ describe("databases", () => {
263262 expect ( workspace . updateWorkspaceFolders ) . toBeCalledWith ( 0 , 1 ) ;
264263
265264 // should NOT delete the db contents
266- expect ( removeMock ) . not . toBeCalled ( ) ;
265+ await expect ( fs . pathExists ( mockDbItem . databaseUri . fsPath ) ) . resolves . toBe (
266+ true ,
267+ ) ;
267268 } ) ;
268269
269270 it ( "should register and deregister a database when adding and removing it" , async ( ) => {
270271 // similar test as above, but also check the call to sendRequestSpy to make sure they send the
271272 // registration messages.
272273 const mockDbItem = createMockDB ( ) ;
273274
274- jest . spyOn ( fs , "remove" ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
275-
276275 await ( databaseManager as any ) . addDatabaseItem (
277276 { } as ProgressCallback ,
278277 { } as CancellationToken ,
@@ -411,98 +410,93 @@ describe("databases", () => {
411410 } ) ;
412411
413412 describe ( "isAffectedByTest" , ( ) => {
414- const directoryStats = new fs . Stats ( ) ;
415- const fileStats = new fs . Stats ( ) ;
416- beforeEach ( ( ) => {
417- jest . spyOn ( directoryStats , "isDirectory" ) . mockReturnValue ( true ) ;
418- jest . spyOn ( fileStats , "isDirectory" ) . mockReturnValue ( false ) ;
413+ let directoryPath : string ;
414+ let projectPath : string ;
415+ let qlFilePath : string ;
416+
417+ beforeEach ( async ( ) => {
418+ directoryPath = join ( dir . name , "dir" ) ;
419+ await fs . ensureDir ( directoryPath ) ;
420+ projectPath = join ( directoryPath , "dir.testproj" ) ;
421+ await fs . writeFile ( projectPath , "" ) ;
422+ qlFilePath = join ( directoryPath , "test.ql" ) ;
423+ await fs . writeFile ( qlFilePath , "" ) ;
419424 } ) ;
420425
421426 it ( "should return true for testproj database in test directory" , async ( ) => {
422- jest . spyOn ( fs , "stat" ) . mockResolvedValue ( directoryStats ) ;
423- const db = createMockDB (
424- sourceLocationUri ( ) ,
425- Uri . file ( "/path/to/dir/dir.testproj" ) ,
426- ) ;
427- expect ( await db . isAffectedByTest ( "/path/to/dir" ) ) . toBe ( true ) ;
427+ const db = createMockDB ( sourceLocationUri ( ) , Uri . file ( projectPath ) ) ;
428+ expect ( await db . isAffectedByTest ( directoryPath ) ) . toBe ( true ) ;
428429 } ) ;
429430
430431 it ( "should return false for non-existent test directory" , async ( ) => {
431- jest . spyOn ( fs , "stat" ) . mockImplementation ( ( ) => {
432- throw new Error ( "Simulated Error: ENOENT" ) ;
433- } ) ;
434432 const db = createMockDB (
435433 sourceLocationUri ( ) ,
436- Uri . file ( "/path/to/dir/dir.testproj" ) ,
434+ Uri . file ( join ( dir . name , "non-existent/non-existent.testproj" ) ) ,
435+ ) ;
436+ expect ( await db . isAffectedByTest ( join ( dir . name , "non-existent" ) ) ) . toBe (
437+ false ,
437438 ) ;
438- expect ( await db . isAffectedByTest ( "/path/to/dir" ) ) . toBe ( false ) ;
439439 } ) ;
440440
441441 it ( "should return false for non-testproj database in test directory" , async ( ) => {
442- jest . spyOn ( fs , "stat" ) . mockResolvedValue ( directoryStats ) ;
442+ const anotherProjectPath = join ( directoryPath , "dir.proj" ) ;
443+ await fs . writeFile ( anotherProjectPath , "" ) ;
444+
443445 const db = createMockDB (
444446 sourceLocationUri ( ) ,
445- Uri . file ( "/path/to/dir/dir.proj" ) ,
447+ Uri . file ( anotherProjectPath ) ,
446448 ) ;
447- expect ( await db . isAffectedByTest ( "/path/to/dir" ) ) . toBe ( false ) ;
449+ expect ( await db . isAffectedByTest ( directoryPath ) ) . toBe ( false ) ;
448450 } ) ;
449451
450452 it ( "should return false for testproj database outside test directory" , async ( ) => {
451- jest . spyOn ( fs , "stat" ) . mockResolvedValue ( directoryStats ) ;
453+ const anotherProjectDir = join ( dir . name , "other" ) ;
454+ await fs . ensureDir ( anotherProjectDir ) ;
455+ const anotherProjectPath = join ( anotherProjectDir , "other.testproj" ) ;
456+ await fs . writeFile ( anotherProjectPath , "" ) ;
457+
452458 const db = createMockDB (
453459 sourceLocationUri ( ) ,
454- Uri . file ( "/path/to/other/dir.testproj" ) ,
460+ Uri . file ( anotherProjectPath ) ,
455461 ) ;
456- expect ( await db . isAffectedByTest ( "/path/to/dir" ) ) . toBe ( false ) ;
462+ expect ( await db . isAffectedByTest ( directoryPath ) ) . toBe ( false ) ;
457463 } ) ;
458464
459465 it ( "should return false for testproj database for prefix directory" , async ( ) => {
460- jest . spyOn ( fs , "stat" ) . mockResolvedValue ( directoryStats ) ;
461- const db = createMockDB (
462- sourceLocationUri ( ) ,
463- Uri . file ( "/path/to/dir/dir.testproj" ) ,
464- ) ;
465- // /path/to/d is a prefix of /path/to/dir/dir.testproj, but
466- // /path/to/dir/dir.testproj is not under /path/to/d
467- expect ( await db . isAffectedByTest ( "/path/to/d" ) ) . toBe ( false ) ;
466+ const db = createMockDB ( sourceLocationUri ( ) , Uri . file ( projectPath ) ) ;
467+ // /d is a prefix of /dir/dir.testproj, but
468+ // /dir/dir.testproj is not under /d
469+ expect ( await db . isAffectedByTest ( join ( directoryPath , "d" ) ) ) . toBe ( false ) ;
468470 } ) ;
469471
470472 it ( "should return true for testproj database for test file" , async ( ) => {
471- jest . spyOn ( fs , "stat" ) . mockResolvedValue ( fileStats ) ;
472- const db = createMockDB (
473- sourceLocationUri ( ) ,
474- Uri . file ( "/path/to/dir/dir.testproj" ) ,
475- ) ;
476- expect ( await db . isAffectedByTest ( "/path/to/dir/test.ql" ) ) . toBe ( true ) ;
473+ const db = createMockDB ( sourceLocationUri ( ) , Uri . file ( projectPath ) ) ;
474+ expect ( await db . isAffectedByTest ( qlFilePath ) ) . toBe ( true ) ;
477475 } ) ;
478476
479477 it ( "should return false for non-existent test file" , async ( ) => {
480- jest . spyOn ( fs , "stat" ) . mockImplementation ( ( ) => {
481- throw new Error ( "Simulated Error: ENOENT" ) ;
482- } ) ;
483- const db = createMockDB (
484- sourceLocationUri ( ) ,
485- Uri . file ( "/path/to/dir/dir.testproj" ) ,
486- ) ;
487- expect ( await db . isAffectedByTest ( "/path/to/dir/test.ql" ) ) . toBe ( false ) ;
478+ const otherTestFile = join ( directoryPath , "other-test.ql" ) ;
479+ const db = createMockDB ( sourceLocationUri ( ) , Uri . file ( projectPath ) ) ;
480+ expect ( await db . isAffectedByTest ( otherTestFile ) ) . toBe ( false ) ;
488481 } ) ;
489482
490483 it ( "should return false for non-testproj database for test file" , async ( ) => {
491- jest . spyOn ( fs , "stat" ) . mockResolvedValue ( fileStats ) ;
484+ const anotherProjectPath = join ( directoryPath , "dir.proj" ) ;
485+ await fs . writeFile ( anotherProjectPath , "" ) ;
486+
492487 const db = createMockDB (
493488 sourceLocationUri ( ) ,
494- Uri . file ( "/path/to/dir/dir.proj" ) ,
489+ Uri . file ( anotherProjectPath ) ,
495490 ) ;
496- expect ( await db . isAffectedByTest ( "/path/to/dir/test.ql" ) ) . toBe ( false ) ;
491+ expect ( await db . isAffectedByTest ( qlFilePath ) ) . toBe ( false ) ;
497492 } ) ;
498493
499494 it ( "should return false for testproj database not matching test file" , async ( ) => {
500- jest . spyOn ( fs , "stat" ) . mockResolvedValue ( fileStats ) ;
501- const db = createMockDB (
502- sourceLocationUri ( ) ,
503- Uri . file ( "/path/to/dir/dir.testproj" ) ,
504- ) ;
505- expect ( await db . isAffectedByTest ( "/path/to/test.ql" ) ) . toBe ( false ) ;
495+ const otherTestFile = join ( dir . name , "test.ql" ) ;
496+ await fs . writeFile ( otherTestFile , "" ) ;
497+
498+ const db = createMockDB ( sourceLocationUri ( ) , Uri . file ( projectPath ) ) ;
499+ expect ( await db . isAffectedByTest ( otherTestFile ) ) . toBe ( false ) ;
506500 } ) ;
507501 } ) ;
508502
0 commit comments