@@ -17,6 +17,28 @@ import {
17
17
18
18
// tslint:disable:max-func-body-length chai-vague-errors
19
19
20
+ function createMockStat ( ) : TypeMoq . IMock < FileStat > {
21
+ const stat = TypeMoq . Mock . ofType < FileStat > ( undefined , TypeMoq . MockBehavior . Strict ) ;
22
+ // This is necessary because passing "mock.object" to
23
+ // Promise.resolve() triggers the lookup.
24
+ //tslint:disable-next-line:no-any
25
+ stat . setup ( ( s : any ) => s . then )
26
+ . returns ( ( ) => undefined )
27
+ . verifiable ( TypeMoq . Times . atLeast ( 0 ) ) ;
28
+ return stat ;
29
+ }
30
+
31
+ function createMockLegacyStat ( ) : TypeMoq . IMock < fsextra . Stats > {
32
+ const stat = TypeMoq . Mock . ofType < fsextra . Stats > ( undefined , TypeMoq . MockBehavior . Strict ) ;
33
+ // This is necessary because passing "mock.object" to
34
+ // Promise.resolve() triggers the lookup.
35
+ //tslint:disable-next-line:no-any
36
+ stat . setup ( ( s : any ) => s . then )
37
+ . returns ( ( ) => undefined )
38
+ . verifiable ( TypeMoq . Times . atLeast ( 0 ) ) ;
39
+ return stat ;
40
+ }
41
+
20
42
//tslint:disable-next-line:no-any
21
43
type TempCallback = ( err : any , path : string , fd : number , cleanupCallback : ( ) => void ) => void ;
22
44
interface IRawFS {
@@ -251,48 +273,30 @@ suite('FileSystem paths', () => {
251
273
252
274
suite ( 'Raw FileSystem' , ( ) => {
253
275
let raw : TypeMoq . IMock < IRawFS > ;
254
- //let stat: TypeMoq.IMock<FileStat>;
255
276
let oldStat : TypeMoq . IMock < fsextra . Stats > ;
256
277
let filesystem : IRawFileSystem ;
257
278
setup ( ( ) => {
258
279
raw = TypeMoq . Mock . ofType < IRawFS > ( undefined , TypeMoq . MockBehavior . Strict ) ;
259
- //stat = TypeMoq.Mock.ofType<FileStat>(undefined, TypeMoq.MockBehavior.Strict);
260
- oldStat = TypeMoq . Mock . ofType < fsextra . Stats > ( undefined , TypeMoq . MockBehavior . Strict ) ;
280
+ oldStat = createMockLegacyStat ( ) ;
261
281
filesystem = new RawFileSystem (
262
- //raw.object,
263
282
raw . object ,
264
283
raw . object ,
265
284
raw . object ,
266
285
raw . object
267
286
) ;
268
-
269
- // This is necessary because passing "stat.object" to
270
- // Promise.resolve() triggers the lookup.
271
- //tslint:disable-next-line:no-any
272
- oldStat . setup ( ( s : any ) => s . then )
273
- . returns ( ( ) => undefined )
274
- . verifiable ( TypeMoq . Times . atLeast ( 0 ) ) ;
275
287
} ) ;
276
288
function verifyAll ( ) {
277
289
raw . verifyAll ( ) ;
278
- //stat.verifyAll();
279
290
oldStat . verifyAll ( ) ;
280
291
}
281
292
282
293
function setupOldStat ( stat : FileStat , old ?: TypeMoq . IMock < fsextra . Stats > | null ) {
283
294
if ( old === undefined ) {
284
295
old = oldStat ;
285
296
} else if ( old === null ) {
286
- old = TypeMoq . Mock . ofType < fsextra . Stats > ( undefined , TypeMoq . MockBehavior . Strict ) ;
297
+ old = createMockLegacyStat ( ) ;
287
298
}
288
299
289
- // This is necessary because passing "stat.object" to
290
- // Promise.resolve() triggers the lookup.
291
- //tslint:disable-next-line:no-any
292
- old ! . setup ( ( s : any ) => s . then )
293
- . returns ( ( ) => undefined )
294
- . verifiable ( TypeMoq . Times . atLeast ( 0 ) ) ;
295
-
296
300
if ( stat . type === FileType . File ) {
297
301
old ! . setup ( s => s . isFile ( ) )
298
302
. returns ( ( ) => true ) ;
@@ -419,22 +423,28 @@ suite('Raw FileSystem', () => {
419
423
suite ( 'rmtree' , ( ) => {
420
424
test ( 'wraps the low-level function' , async ( ) => {
421
425
const dirname = 'x/y/z/spam' ;
422
- raw . setup ( r => r . delete ( Uri . file ( dirname ) , { recursive : true , useTrash : false } ) )
426
+ const uri = Uri . file ( dirname ) ;
427
+ const stat = createMockStat ( ) ;
428
+ raw . setup ( r => r . stat ( uri ) )
429
+ . returns ( ( ) => Promise . resolve ( stat . object ) ) ;
430
+ raw . setup ( r => r . delete ( uri , { recursive : true , useTrash : false } ) )
423
431
. returns ( ( ) => Promise . resolve ( ) ) ;
424
432
425
433
await filesystem . rmtree ( dirname ) ;
426
434
427
435
verifyAll ( ) ;
436
+ stat . verifyAll ( ) ;
428
437
} ) ;
429
438
430
439
test ( 'fails if the directory does not exist' , async ( ) => {
431
440
const dirname = 'x/y/z/spam' ;
432
- raw . setup ( r => r . delete ( Uri . file ( dirname ) , { recursive : true , useTrash : false } ) )
441
+ raw . setup ( r => r . stat ( Uri . file ( dirname ) ) )
433
442
. throws ( new Error ( 'file not found' ) ) ;
434
443
435
444
const promise = filesystem . rmtree ( dirname ) ;
436
445
437
446
await expect ( promise ) . to . eventually . be . rejected ;
447
+ verifyAll ( ) ;
438
448
} ) ;
439
449
} ) ;
440
450
@@ -545,12 +555,32 @@ suite('Raw FileSystem', () => {
545
555
test ( 'read/write streams are used properly' , async ( ) => {
546
556
const src = 'x/y/z/spam.py' ;
547
557
const dest = 'x/y/z/spam.py.bak' ;
558
+ raw . setup ( r => r . dirname ( dest ) )
559
+ . returns ( ( ) => 'x/y/z' ) ;
560
+ const stat = createMockStat ( ) ;
561
+ raw . setup ( r => r . stat ( Uri . file ( 'x/y/z' ) ) )
562
+ . returns ( ( ) => Promise . resolve ( stat . object ) ) ;
548
563
raw . setup ( r => r . copy ( Uri . file ( src ) , Uri . file ( dest ) , { overwrite : true } ) )
549
564
. returns ( ( ) => Promise . resolve ( ) ) ;
550
565
551
566
await filesystem . copyFile ( src , dest ) ;
552
567
553
568
verifyAll ( ) ;
569
+ stat . verifyAll ( ) ;
570
+ } ) ;
571
+
572
+ test ( 'fails if the parent directory does not exist' , async ( ) => {
573
+ const src = '/tmp/spam.py' ;
574
+ const dest = '/tmp/__does_not_exist__/spam.py' ;
575
+ raw . setup ( r => r . dirname ( dest ) )
576
+ . returns ( ( ) => '/tmp/__does_not_exist__' ) ;
577
+ raw . setup ( r => r . stat ( Uri . file ( '/tmp/__does_not_exist__' ) ) )
578
+ . throws ( new Error ( 'file not found' ) ) ;
579
+
580
+ const promise = filesystem . copyFile ( src , dest ) ;
581
+
582
+ await expect ( promise ) . to . eventually . be . rejected ;
583
+ verifyAll ( ) ;
554
584
} ) ;
555
585
} ) ;
556
586
@@ -630,7 +660,7 @@ suite('FileSystem Utils', () => {
630
660
let deps : TypeMoq . IMock < IDeps > ;
631
661
let utils : IFileSystemUtils ;
632
662
setup ( ( ) => {
633
- stat = TypeMoq . Mock . ofType < FileStat > ( undefined , TypeMoq . MockBehavior . Strict ) ;
663
+ stat = createMockStat ( ) ;
634
664
filesystem = TypeMoq . Mock . ofType < IRawFileSystem > ( undefined , TypeMoq . MockBehavior . Strict ) ;
635
665
path = TypeMoq . Mock . ofType < IFileSystemPaths > ( undefined , TypeMoq . MockBehavior . Strict ) ;
636
666
tmp = TypeMoq . Mock . ofType < ITempFileSystem > ( undefined , TypeMoq . MockBehavior . Strict ) ;
@@ -642,13 +672,6 @@ suite('FileSystem Utils', () => {
642
672
( ( data : string ) => deps . object . getHashString ( data ) ) ,
643
673
( ( p : string ) => deps . object . glob ( p ) )
644
674
) ;
645
-
646
- // This is necessary because passing "stat.object" to
647
- // Promise.resolve() triggers the lookup.
648
- //tslint:disable-next-line:no-any
649
- stat . setup ( ( s : any ) => s . then )
650
- . returns ( ( ) => undefined )
651
- . verifiable ( TypeMoq . Times . atLeast ( 0 ) ) ;
652
675
} ) ;
653
676
function verifyAll ( ) {
654
677
filesystem . verifyAll ( ) ;
0 commit comments