Sfoglia il codice sorgente

Add test for disk storage path block

onevcat 7 anni fa
parent
commit
ae5ff28769

+ 2 - 4
Sources/Cache/ImageCache.swift

@@ -224,8 +224,6 @@ open class ImageCache {
             fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
         }
         
-        let cacheName = "com.onevcat.Kingfisher.ImageCache.\(name)"
-
         let totalMemory = ProcessInfo.processInfo.physicalMemory
         let costLimit = totalMemory / 4
         let memoryStorage = MemoryStorage.Backend<Image>(config:
@@ -237,10 +235,10 @@ open class ImageCache {
             directory: path.flatMap { URL(string: $0) }
         )
         if let closure = diskCachePathClosure {
-            diskConfig.cachePathBlock = diskCachePathClosure
-            defer { diskConfig.cachePathBlock = nil }
+            diskConfig.cachePathBlock = closure
         }
         let diskStorage = try DiskStorage.Backend<Data>(config: diskConfig)
+        diskConfig.cachePathBlock = nil
         
         self.init(memoryStorage: memoryStorage, diskStorage: diskStorage, name: name)
     }

+ 14 - 0
Tests/KingfisherTests/ImageCacheTests.swift

@@ -69,6 +69,20 @@ class ImageCacheTests: XCTestCase {
         XCTAssertEqual(
             cache.diskStorage.directoryURL.path,
             (customPath as NSString).appendingPathComponent("com.onevcat.Kingfisher.ImageCache.test"))
+        clearCaches([cache])
+    }
+    
+    func testCustomCachePathByBlock() {
+        let cache = try! ImageCache(name: "test", path: nil, diskCachePathClosure: { (url, path) -> URL in
+            let modifiedPath = path + "-modified"
+            return url.appendingPathComponent(modifiedPath, isDirectory: true)
+        })
+        let cacheURL = try! FileManager.default.url(
+            for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
+        XCTAssertEqual(
+            cache.diskStorage.directoryURL.path,
+            (cacheURL.path as NSString).appendingPathComponent("com.onevcat.Kingfisher.ImageCache.test-modified"))
+        clearCaches([cache])
     }
     
     func testMaxCachePeriodInSecond() {