@@ -356,29 +356,46 @@ final class WebDAVTests: XCTestCase {
356356 func testCleanupFilesCacheRoot( ) {
357357 guard let ( account, password) = getAccount ( ) else { return XCTFail ( ) }
358358
359+ let listRealDirExpectation = XCTestExpectation ( description: " List files from real directory. " )
359360 let expectation = XCTestExpectation ( description: " List files from WebDAV " )
360361
361- let path = UUID ( ) . uuidString
362- let accountPath = AccountPath ( account: account, path: path)
362+ let realDir = createFolder ( account: account, password: password)
363+ let realDirAccountPath = AccountPath ( account: account, path: realDir)
364+
365+ let fakeDir = UUID ( ) . uuidString
366+ let fakeDirAccountPath = AccountPath ( account: account, path: fakeDir)
367+
368+ // Load real file into cache
369+ uploadData ( to: realDir, account: account, password: password)
370+ webDAV. listFiles ( atPath: realDir, account: account, password: password, caching: . doNotReturnCachedResult) { _, _ in
371+ listRealDirExpectation. fulfill ( )
372+ }
373+
374+ wait ( for: [ listRealDirExpectation] , timeout: 10000.0 )
375+
376+ XCTAssertNotNil ( webDAV. filesCache [ realDirAccountPath] )
363377
364378 // Place fake cache
365379
366- webDAV. filesCache [ accountPath ] = [ WebDAVFile ( path: path + " /fake.txt " , id: " 0 " , isDirectory: true , lastModified: Date ( ) , size: 0 , etag: " 0 " ) ]
367- XCTAssertNotNil ( webDAV. filesCache [ accountPath ] )
380+ webDAV. filesCache [ fakeDirAccountPath ] = [ WebDAVFile ( path: fakeDir + " /fake.txt " , id: " 0 " , isDirectory: true , lastModified: Date ( ) , size: 0 , etag: " 0 " ) ]
381+ XCTAssertNotNil ( webDAV. filesCache [ fakeDirAccountPath ] )
368382
369383 // List files
370384
371- webDAV. listFiles ( atPath: " / " , account: account, password: password, foldersFirst : false , caching: . ignoreCache ) { files, error in
385+ webDAV. listFiles ( atPath: " / " , account: account, password: password, caching: . doNotReturnCachedResult ) { files, error in
372386 XCTAssertNotNil ( files)
373387 XCTAssertNil ( error)
374388 expectation. fulfill ( )
375389 }
376390
377- wait ( for: [ expectation] , timeout: 10 .0)
391+ wait ( for: [ expectation] , timeout: 10000 .0)
378392
379393 // Check that the fake cached file was cleaned up
394+ XCTAssertNil ( webDAV. filesCache [ fakeDirAccountPath] )
395+ // Check that the real file still exists
396+ XCTAssertNotNil ( webDAV. filesCache [ realDirAccountPath] )
380397
381- XCTAssertNil ( webDAV . filesCache [ accountPath ] )
398+ deleteFile ( path : realDir , account : account , password : password )
382399 }
383400
384401 func testCleanupFilesCacheSubdirectory( ) {
@@ -397,7 +414,7 @@ final class WebDAVTests: XCTestCase {
397414
398415 // List files
399416
400- webDAV. listFiles ( atPath: folder, account: account, password: password, foldersFirst: false , caching: . ignoreCache ) { files, error in
417+ webDAV. listFiles ( atPath: folder, account: account, password: password, foldersFirst: false , caching: . doNotReturnCachedResult ) { files, error in
401418 XCTAssertNotNil ( files)
402419 XCTAssertNil ( error)
403420 expectation. fulfill ( )
@@ -413,23 +430,31 @@ final class WebDAVTests: XCTestCase {
413430
414431 //MARK: Disk Cache Cleanup
415432
416- func testCleanupDiskCacheFile ( ) {
433+ func testCleanupDiskCacheFileRoot ( ) {
417434 guard let ( account, password) = getAccount ( ) else { return XCTFail ( ) }
418435
419436 let expectation = XCTestExpectation ( description: " List files from WebDAV " )
420437
421438 // Add dummy file to disk cache
422439
423- let path = UUID ( ) . uuidString + " .txt "
440+ let dummyPath = UUID ( ) . uuidString + " .txt "
424441 let data = UUID ( ) . uuidString. data ( using: . utf8) !
425- let tempFileURL = webDAV. cachedDataURL ( forItemAtPath: path , account: account) !
426- XCTAssertNoThrow ( try webDAV. saveDataToDiskCache ( data, url: tempFileURL ) )
442+ let dummyFileURL = webDAV. cachedDataURL ( forItemAtPath: dummyPath , account: account) !
443+ XCTAssertNoThrow ( try webDAV. saveDataToDiskCache ( data, url: dummyFileURL ) )
427444
428- XCTAssert ( FileManager . default. fileExists ( atPath: tempFileURL. path) )
445+ XCTAssert ( FileManager . default. fileExists ( atPath: dummyFileURL. path) )
446+
447+ // Create real file
448+
449+ let realFile = uploadData ( account: account, password: password)
450+ let realFileURL = webDAV. cachedDataURL ( forItemAtPath: realFile. fileName, account: account) !
451+ downloadData ( path: realFile. fileName, account: account, password: password)
452+
453+ XCTAssert ( FileManager . default. fileExists ( atPath: realFileURL. path) )
429454
430455 // List files
431456
432- webDAV. listFiles ( atPath: " / " , account: account, password: password, foldersFirst: false , caching: . ignoreCache ) { files, error in
457+ webDAV. listFiles ( atPath: " / " , account: account, password: password, foldersFirst: false , caching: . doNotReturnCachedResult ) { files, error in
433458 XCTAssertNotNil ( files)
434459 XCTAssertNil ( error)
435460 expectation. fulfill ( )
@@ -438,8 +463,11 @@ final class WebDAVTests: XCTestCase {
438463 wait ( for: [ expectation] , timeout: 10.0 )
439464
440465 // Check that the fake cached file was cleaned up
466+ XCTAssertFalse ( FileManager . default. fileExists ( atPath: dummyFileURL. path) )
467+ // Check that the real file still exists
468+ XCTAssert ( FileManager . default. fileExists ( atPath: realFileURL. path) )
441469
442- XCTAssertFalse ( FileManager . default . fileExists ( atPath : tempFileURL . path ) )
470+ deleteFile ( path : realFile . fileName , account : account , password : password )
443471 }
444472
445473 func testCleanupDiskCacheFolder( ) {
@@ -450,13 +478,23 @@ final class WebDAVTests: XCTestCase {
450478 // Add dummy folder to disk cache
451479
452480 let path = UUID ( ) . uuidString
453- let tempFileURL = webDAV. cachedDataURL ( forItemAtPath: path, account: account) !
454- XCTAssertNoThrow ( try FileManager . default. createDirectory ( at: tempFileURL, withIntermediateDirectories: true ) )
455- XCTAssert ( FileManager . default. fileExists ( atPath: tempFileURL. path) )
481+ let dummyFileURL = webDAV. cachedDataURL ( forItemAtPath: path, account: account) !
482+ XCTAssertNoThrow ( try FileManager . default. createDirectory ( at: dummyFileURL, withIntermediateDirectories: true ) )
483+
484+ XCTAssert ( FileManager . default. fileExists ( atPath: dummyFileURL. path) )
485+
486+ // Create real folder
487+
488+ let folder = createFolder ( account: account, password: password)
489+ let realFile = uploadData ( to: folder, account: account, password: password)
490+ downloadData ( path: realFile. fileName, account: account, password: password)
491+ let realFileURL = webDAV. cachedDataURL ( forItemAtPath: realFile. fileName, account: account) !
492+
493+ XCTAssert ( FileManager . default. fileExists ( atPath: realFileURL. path) )
456494
457495 // List files
458496
459- webDAV. listFiles ( atPath: " / " , account: account, password: password, foldersFirst: false , caching: . ignoreCache ) { files, error in
497+ webDAV. listFiles ( atPath: " / " , account: account, password: password, foldersFirst: false , caching: . doNotReturnCachedResult ) { files, error in
460498 XCTAssertNotNil ( files)
461499 XCTAssertNil ( error)
462500 expectation. fulfill ( )
@@ -465,8 +503,11 @@ final class WebDAVTests: XCTestCase {
465503 wait ( for: [ expectation] , timeout: 10.0 )
466504
467505 // Check that the fake cached folder was cleaned up
506+ XCTAssertFalse ( FileManager . default. fileExists ( atPath: dummyFileURL. path) )
507+ // Check that the real file still exists
508+ XCTAssert ( FileManager . default. fileExists ( atPath: realFileURL. path) )
468509
469- XCTAssertFalse ( FileManager . default . fileExists ( atPath : tempFileURL . path ) )
510+ deleteFile ( path : folder , account : account , password : password )
470511 }
471512
472513 func testCleanupDiskCacheWithGoodFile( ) {
@@ -496,7 +537,7 @@ final class WebDAVTests: XCTestCase {
496537
497538 // List files
498539
499- webDAV. listFiles ( atPath: directory. path, account: account, password: password, foldersFirst : false , caching: . ignoreCache ) { files, error in
540+ webDAV. listFiles ( atPath: directory. path, account: account, password: password, caching: . doNotReturnCachedResult ) { files, error in
500541 XCTAssertNotNil ( files)
501542 XCTAssertNil ( error)
502543 expectation. fulfill ( )
@@ -511,6 +552,34 @@ final class WebDAVTests: XCTestCase {
511552 XCTAssertNoThrow ( try webDAV. deleteCachedData ( forItemAtPath: imagePath, account: account) )
512553 }
513554
555+ func testCleanupDiskCacheKeepingThumbnail( ) {
556+ guard let ( account, password) = getAccount ( ) else { return XCTFail ( ) }
557+ guard let imagePath = ProcessInfo . processInfo. environment [ " image_path " ] else {
558+ return XCTFail ( " You need to set the image_path in the environment. " )
559+ }
560+
561+ let expectation = XCTestExpectation ( description: " List files from WebDAV " )
562+
563+ // Download Thumbnail
564+ downloadThumbnail ( imagePath: imagePath, account: account, password: password)
565+ let cachedThumbnailURL = webDAV. cachedThumbnailURL ( forItemAtPath: imagePath, account: account, with: . default) !
566+ XCTAssert ( FileManager . default. fileExists ( atPath: cachedThumbnailURL. path) )
567+
568+ // List files
569+
570+ let imageURL = URL ( fileURLWithPath: imagePath, isDirectory: false )
571+ let directory = imageURL. deletingLastPathComponent ( )
572+
573+ webDAV. listFiles ( atPath: directory. path, account: account, password: password, caching: . doNotReturnCachedResult) { _, _ in
574+ expectation. fulfill ( )
575+ }
576+
577+ wait ( for: [ expectation] , timeout: 10.0 )
578+
579+ // Check that the cached thumbnail still exists
580+ XCTAssert ( FileManager . default. fileExists ( atPath: cachedThumbnailURL. path) )
581+ }
582+
514583 //MARK: Images
515584
516585 func testDownloadImage( ) {
@@ -719,11 +788,17 @@ final class WebDAVTests: XCTestCase {
719788 wait ( for: [ expectation] , timeout: 10.0 )
720789 }
721790
722- private func uploadData( account: SimpleAccount , password: String ) -> ( name: String , fileName: String , content: String ) {
791+ @discardableResult
792+ private func uploadData( to folder: String = " " , account: SimpleAccount , password: String ) -> ( name: String , fileName: String , content: String ) {
723793 let expectation = XCTestExpectation ( description: " Upload data " )
724794
725795 let name = UUID ( ) . uuidString
726- let fileName = name + " .txt "
796+ let fileName : String
797+ if folder. isEmpty {
798+ fileName = name + " .txt "
799+ } else {
800+ fileName = " \( folder) / \( name) .txt "
801+ }
727802 let content = UUID ( ) . uuidString
728803 let data = content. data ( using: . utf8) !
729804
@@ -763,6 +838,21 @@ final class WebDAVTests: XCTestCase {
763838 return folder
764839 }
765840
841+ private func downloadData( path file: String , account: SimpleAccount , password: String ) {
842+ let expectation = XCTestExpectation ( description: " Download file from WebDAV " )
843+
844+ try ? webDAV. deleteCachedData ( forItemAtPath: file, account: account)
845+
846+ webDAV. download ( fileAtPath: file, account: account, password: password) { image, error in
847+ XCTAssertNil ( error)
848+ XCTAssertNotNil ( image)
849+
850+ expectation. fulfill ( )
851+ }
852+
853+ wait ( for: [ expectation] , timeout: 10.0 )
854+ }
855+
766856 private func downloadImage( imagePath: String , account: SimpleAccount , password: String ) {
767857 let expectation = XCTestExpectation ( description: " Download image from WebDAV " )
768858
@@ -816,7 +906,7 @@ final class WebDAVTests: XCTestCase {
816906 ( " testCleanupFilesCacheRoot " , testCleanupFilesCacheRoot) ,
817907 ( " testCleanupFilesCacheSubdirectory " , testCleanupFilesCacheSubdirectory) ,
818908 // Disk Cache Cleanup
819- ( " testCleanupDiskCacheFile " , testCleanupDiskCacheFile ) ,
909+ ( " testCleanupDiskCacheFileRoot " , testCleanupDiskCacheFileRoot ) ,
820910 ( " testCleanupDiskCacheFolder " , testCleanupDiskCacheFolder) ,
821911 ( " testCleanupDiskCacheWithGoodFile " , testCleanupDiskCacheWithGoodFile) ,
822912 // Image Cache
0 commit comments