diff --git a/README.md b/README.md index 20ad879..8175a13 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ If you are using `Codable` protocol from Swift 4 and needs an easy way to store - [x] Documents - [x] User Default - [x] NSUbiquitousKeyValueStore +- [x] Linux compatibility - [ ] Name Spaced Stores - [x] Comprehensive Unit and Integration Test Coverage - [x] Complete Documentation diff --git a/Storage/Classes/Storage.swift b/Storage/Classes/Storage.swift index 7f022e4..9349bb1 100644 --- a/Storage/Classes/Storage.swift +++ b/Storage/Classes/Storage.swift @@ -26,8 +26,9 @@ public final class Storage where T: Codable { /// - Parameters: /// - storageType: The type of storage to use (cache, document, or user defaults). /// - filename: The name of the file to store the data. - /// - ubiquitousStore: Optional KeyValueStorable instance for .ubiquitousKeyValueStore type (defaults to platform specific store if available) + /// - ubiquitousStore: Optional KeyValueStorable instance for `.ubiquitousKeyValueStore` type. #if canImport(Darwin) + /// Defaults to `NSUbiquitousKeyValueStore.default` on Darwin platforms. public init(storageType: StorageType, filename: String, ubiquitousStore: KeyValueStorable? = NSUbiquitousKeyValueStore.default) { self.ubiquitousStore = ubiquitousStore self.type = storageType @@ -51,6 +52,8 @@ public final class Storage where T: Codable { let data = try JSONEncoder().encode(object) switch type { case .cache, .document: + // Ensure the directory exists in case it was removed by `clear()` + createFolderIfNotExists() try data.write(to: fileURL) case .userDefaults: UserDefaults.standard.set(data, forKey: type.userDefaultsKey + ".\(filename)") diff --git a/Tests/StorageTests.swift b/Tests/StorageTests.swift index 755ee40..b78829d 100644 --- a/Tests/StorageTests.swift +++ b/Tests/StorageTests.swift @@ -51,6 +51,29 @@ class StorageTests: XCTestCase { storage.clear() } + func testSaveAfterClearDocument() { + let filename = "clearThenSave.json" + let storage = Storage<[String]>(storageType: .document, filename: filename) + + // Save initial data + storage.save(["first"]) + XCTAssertEqual(storage.storedValue, ["first"], "Initial data should be saved") + + // Clear the storage + storage.clear() + + // Save new data after clearing + let newData = ["second", "third"] + storage.save(newData) + + // Verify the new data can be retrieved + let retrieved = storage.storedValue + XCTAssertEqual(retrieved, newData, "Data saved after clear should be retrievable") + + // Cleanup created storage + storage.clear() + } + func testSaveAndRetrieveUserDefaults() { let storage = Storage<[String]>(storageType: .userDefaults, filename: "testUserDefaults") diff --git a/Tests/StorageTypeTests.swift b/Tests/StorageTypeTests.swift index e98b1e6..be84d78 100644 --- a/Tests/StorageTypeTests.swift +++ b/Tests/StorageTypeTests.swift @@ -5,8 +5,8 @@ class StorageTypeTests: XCTestCase { func testCacheDirectory() { let storageType = StorageType.cache let folder = storageType.folder - let expected = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first! - XCTAssertTrue(folder.path.hasPrefix(expected), "Cache directory should be inside the system caches directory") + let path = folder.path + XCTAssertTrue(path.contains("Caches") || path.contains(".cache"), "Cache directory path should contain 'Caches' on Darwin or '.cache' on Linux") } func testDocumentDirectory() {