@@ -307,31 +307,48 @@ export const SessionConnectionMock = jest.fn<
307
307
* A mock contents manager.
308
308
*/
309
309
export const ContentsManagerMock = jest . fn < Contents . IManager , [ ] > ( ( ) => {
310
- const files = new Map < string , Contents . IModel > ( ) ;
310
+ const files = new Map < string , Map < string , Contents . IModel > > ( ) ;
311
311
const dummy = new ContentsManager ( ) ;
312
312
const checkpoints = new Map < string , Contents . ICheckpointModel > ( ) ;
313
313
const checkPointContent = new Map < string , string > ( ) ;
314
314
315
315
const baseModel = Private . createFile ( { type : 'directory' } ) ;
316
- files . set ( '' , { ...baseModel , path : '' , name : '' } ) ;
316
+ // create the default drive
317
+ files . set (
318
+ '' ,
319
+ new Map < string , Contents . IModel > ( [
320
+ [ '' , { ...baseModel , path : '' , name : '' } ]
321
+ ] )
322
+ ) ;
317
323
318
324
const thisObject : Contents . IManager = {
319
325
...jest . requireActual ( '@jupyterlab/services' ) ,
320
326
newUntitled : jest . fn ( options => {
321
- const model = Private . createFile ( options || { } ) ;
322
- files . set ( model . path , model ) ;
327
+ const driveName = dummy . driveName ( options ?. path || '' ) ;
328
+ const localPath = dummy . localPath ( options ?. path || '' ) ;
329
+ // create the test file without the drive name
330
+ const createOptions = { ...options , path : localPath } ;
331
+ const model = Private . createFile ( createOptions || { } ) ;
332
+ // re-add the drive name to the model
333
+ const drivePath = driveName ? `${ driveName } :${ model . path } ` : model . path ;
334
+ const driveModel = {
335
+ ...model ,
336
+ path : drivePath
337
+ } ;
338
+ files . get ( driveName ) ! . set ( model . path , driveModel ) ;
323
339
fileChangedSignal . emit ( {
324
340
type : 'new' ,
325
341
oldValue : null ,
326
- newValue : model
342
+ newValue : driveModel
327
343
} ) ;
328
- return Promise . resolve ( model ) ;
344
+ return Promise . resolve ( driveModel ) ;
329
345
} ) ,
330
346
createCheckpoint : jest . fn ( path => {
331
347
const lastModified = new Date ( ) . toISOString ( ) ;
332
348
const data = { id : UUID . uuid4 ( ) , last_modified : lastModified } ;
333
349
checkpoints . set ( path , data ) ;
334
- checkPointContent . set ( path , files . get ( path ) ?. content ) ;
350
+ // TODO: handle drives
351
+ checkPointContent . set ( path , files . get ( '' ) ! . get ( path ) ?. content ) ;
335
352
return Promise . resolve ( data ) ;
336
353
} ) ,
337
354
listCheckpoints : jest . fn ( path => {
@@ -352,7 +369,8 @@ export const ContentsManagerMock = jest.fn<Contents.IManager, []>(() => {
352
369
if ( ! checkpoints . has ( path ) ) {
353
370
return Private . makeResponseError ( 404 ) ;
354
371
}
355
- ( files . get ( path ) as any ) . content = checkPointContent . get ( path ) ;
372
+ // TODO: handle drives
373
+ ( files . get ( '' ) ! . get ( path ) as any ) . content = checkPointContent . get ( path ) ;
356
374
return Promise . resolve ( ) ;
357
375
} ) ,
358
376
getSharedModelFactory : jest . fn ( ( ) => {
@@ -368,11 +386,14 @@ export const ContentsManagerMock = jest.fn<Contents.IManager, []>(() => {
368
386
return dummy . resolvePath ( root , path ) ;
369
387
} ) ,
370
388
get : jest . fn ( ( path , options ) => {
371
- path = Private . fixSlash ( path ) ;
372
- if ( ! files . has ( path ) ) {
389
+ const driveName = dummy . driveName ( path ) ;
390
+ const localPath = dummy . localPath ( path ) ;
391
+ const drive = files . get ( driveName ) ! ;
392
+ path = Private . fixSlash ( localPath ) ;
393
+ if ( ! drive . has ( path ) ) {
373
394
return Private . makeResponseError ( 404 ) ;
374
395
}
375
- const model = files . get ( path ) ! ;
396
+ const model = drive . get ( path ) ! ;
376
397
const overrides : { hash ?: string ; last_modified ?: string } = { } ;
377
398
if ( path == 'random-hash.txt' ) {
378
399
overrides . hash = Math . random ( ) . toString ( ) ;
@@ -385,10 +406,11 @@ export const ContentsManagerMock = jest.fn<Contents.IManager, []>(() => {
385
406
if ( model . type === 'directory' ) {
386
407
if ( options ?. content !== false ) {
387
408
const content : Contents . IModel [ ] = [ ] ;
388
- files . forEach ( fileModel => {
409
+ drive . forEach ( fileModel => {
410
+ const localPath = dummy . localPath ( fileModel . path ) ;
389
411
if (
390
412
// If file path is under this directory, add it to contents array.
391
- PathExt . dirname ( fileModel . path ) == model . path &&
413
+ PathExt . dirname ( localPath ) == model . path &&
392
414
// But the directory should exclude itself from the contents array.
393
415
fileModel !== model
394
416
) {
@@ -408,16 +430,20 @@ export const ContentsManagerMock = jest.fn<Contents.IManager, []>(() => {
408
430
return dummy . driveName ( path ) ;
409
431
} ) ,
410
432
rename : jest . fn ( ( oldPath , newPath ) => {
411
- oldPath = Private . fixSlash ( oldPath ) ;
412
- newPath = Private . fixSlash ( newPath ) ;
413
- if ( ! files . has ( oldPath ) ) {
433
+ const driveName = dummy . driveName ( oldPath ) ;
434
+ const drive = files . get ( driveName ) ! ;
435
+ let oldLocalPath = dummy . localPath ( oldPath ) ;
436
+ let newLocalPath = dummy . localPath ( newPath ) ;
437
+ oldLocalPath = Private . fixSlash ( oldLocalPath ) ;
438
+ newLocalPath = Private . fixSlash ( newLocalPath ) ;
439
+ if ( ! drive . has ( oldLocalPath ) ) {
414
440
return Private . makeResponseError ( 404 ) ;
415
441
}
416
- const oldValue = files . get ( oldPath ) ! ;
417
- files . delete ( oldPath ) ;
418
- const name = PathExt . basename ( newPath ) ;
419
- const newValue = { ...oldValue , name, path : newPath } ;
420
- files . set ( newPath , newValue ) ;
442
+ const oldValue = drive . get ( oldPath ) ! ;
443
+ drive . delete ( oldPath ) ;
444
+ const name = PathExt . basename ( newLocalPath ) ;
445
+ const newValue = { ...oldValue , name, path : newLocalPath } ;
446
+ drive . set ( newPath , newValue ) ;
421
447
fileChangedSignal . emit ( {
422
448
type : 'rename' ,
423
449
oldValue,
@@ -426,12 +452,15 @@ export const ContentsManagerMock = jest.fn<Contents.IManager, []>(() => {
426
452
return Promise . resolve ( newValue ) ;
427
453
} ) ,
428
454
delete : jest . fn ( path => {
429
- path = Private . fixSlash ( path ) ;
430
- if ( ! files . has ( path ) ) {
455
+ const driveName = dummy . driveName ( path ) ;
456
+ const localPath = dummy . localPath ( path ) ;
457
+ const drive = files . get ( driveName ) ! ;
458
+ path = Private . fixSlash ( localPath ) ;
459
+ if ( ! drive . has ( path ) ) {
431
460
return Private . makeResponseError ( 404 ) ;
432
461
}
433
- const oldValue = files . get ( path ) ! ;
434
- files . delete ( path ) ;
462
+ const oldValue = drive . get ( path ) ! ;
463
+ drive . delete ( path ) ;
435
464
fileChangedSignal . emit ( {
436
465
type : 'delete' ,
437
466
oldValue,
@@ -445,21 +474,22 @@ export const ContentsManagerMock = jest.fn<Contents.IManager, []>(() => {
445
474
}
446
475
path = Private . fixSlash ( path ) ;
447
476
const timeStamp = new Date ( ) . toISOString ( ) ;
448
- if ( files . has ( path ) ) {
477
+ const drive = files . get ( dummy . driveName ( path ) ) ! ;
478
+ if ( drive . has ( path ) ) {
449
479
const updates =
450
480
path == 'frozen-time-and-hash.txt'
451
481
? { }
452
482
: {
453
483
last_modified : timeStamp ,
454
484
hash : timeStamp
455
485
} ;
456
- files . set ( path , {
457
- ...files . get ( path ) ! ,
486
+ drive . set ( path , {
487
+ ...drive . get ( path ) ! ,
458
488
...options ,
459
489
...updates
460
490
} ) ;
461
491
} else {
462
- files . set ( path , {
492
+ drive . set ( path , {
463
493
path,
464
494
name : PathExt . basename ( path ) ,
465
495
content : '' ,
@@ -477,15 +507,21 @@ export const ContentsManagerMock = jest.fn<Contents.IManager, []>(() => {
477
507
fileChangedSignal . emit ( {
478
508
type : 'save' ,
479
509
oldValue : null ,
480
- newValue : files . get ( path ) !
510
+ newValue : drive . get ( path ) !
481
511
} ) ;
482
- return Promise . resolve ( files . get ( path ) ! ) ;
512
+ return Promise . resolve ( drive . get ( path ) ! ) ;
483
513
} ) ,
484
514
getDownloadUrl : jest . fn ( path => {
485
515
return dummy . getDownloadUrl ( path ) ;
486
516
} ) ,
487
517
addDrive : jest . fn ( drive => {
488
518
dummy . addDrive ( drive ) ;
519
+ files . set (
520
+ drive . name ,
521
+ new Map < string , Contents . IModel > ( [
522
+ [ '' , { ...baseModel , path : '' , name : '' } ]
523
+ ] )
524
+ ) ;
489
525
} ) ,
490
526
dispose : jest . fn ( )
491
527
} ;
0 commit comments