diff --git a/Sources/Bodega/DiskStorage+Directories.swift b/Sources/Bodega/DiskStorage+Directories.swift new file mode 100644 index 0000000..fe1ead9 --- /dev/null +++ b/Sources/Bodega/DiskStorage+Directories.swift @@ -0,0 +1,35 @@ +import Foundation + +// An extension with common folders for storing data. +public extension DiskStorage { + + /// Returns a URL to a subfolder created in the documents directory based on the `pathComponent`. + /// - Parameter pathComponent: A path to append to the platform's documents directory. + static func documentsDirectory(appendingPath pathComponent: String) -> URL { + FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent) + } + + /// Returns a URL to a subfolder created in the caches directory based on the `pathComponent`. + /// - Parameter pathComponent: A path to append to the platform's caches directory. + static func cachesDirectory(appendingPath pathComponent: String) -> URL { + FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent) + } + + /// Returns a URL to a subfolder created in the temporary directory based on the `pathComponent`. + /// - Parameter pathComponent: A path to append to the platform's temporary directory. + static func temporaryDirectory(appendingPath pathComponent: String) -> URL { + FileManager.default.temporaryDirectory.appendingPathComponent(pathComponent) + } + + /// For apps that use the App Groups feature this function returns a URL that + /// appends a path to the app's group shared directory. + /// + /// - Parameters: + /// - identifier: The app's group identifier as declared in your app's App Groups Entitlement. + /// - pathComponent: A path to append to the app's group shared directory. + /// - Returns: A URL to a subfolder created in the app's group shared directory. + static func sharedContainerDirectory(forAppGroupIdentifier identifier: String, appendingPath pathComponent: String) -> URL { + FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: identifier)!.appendingPathComponent(pathComponent) + } + +} diff --git a/Tests/BodegaTests/DiskStorageTests.swift b/Tests/BodegaTests/DiskStorageTests.swift index b60ef50..6957599 100644 --- a/Tests/BodegaTests/DiskStorageTests.swift +++ b/Tests/BodegaTests/DiskStorageTests.swift @@ -6,7 +6,7 @@ final class DiskStorageTests: XCTestCase { private var storage: DiskStorage! override func setUp() async throws { - storage = DiskStorage(storagePath: Self.testStoragePath!) + storage = DiskStorage(storagePath: Self.testStoragePath) try await storage.removeAllData() } @@ -120,7 +120,7 @@ private extension DiskStorageTests { static let testData = Data("Test".utf8) static let testCacheKey = CacheKey(verbatim: "test-key") static let pathComponent = "Test" - static let testStoragePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent(DiskStorageTests.pathComponent) + static let testStoragePath = DiskStorage.temporaryDirectory(appendingPath: DiskStorageTests.pathComponent) func writeCacheKeys(count: Int) async throws { for i in 0..