From 2f48c43cb98126b98a93bb39c33616bc37820c1c Mon Sep 17 00:00:00 2001 From: Rizwan Date: Tue, 3 Jun 2025 23:05:46 +0200 Subject: [PATCH 1/3] Add test for saving after document clear --- Tests/StorageTests.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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") From 5fa03c652ed5307037503ed16687497c2fac7b99 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Tue, 3 Jun 2025 23:16:26 +0200 Subject: [PATCH 2/3] Fix storage folder creation and cross-platform tests --- Storage/Classes/Storage.swift | 19 +++++++++++++++++-- Tests/StorageTypeTests.swift | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Storage/Classes/Storage.swift b/Storage/Classes/Storage.swift index 43d0a59..33eb206 100644 --- a/Storage/Classes/Storage.swift +++ b/Storage/Classes/Storage.swift @@ -8,7 +8,9 @@ public protocol KeyValueStorable: AnyObject { func synchronize() -> Bool } +#if canImport(Darwin) extension NSUbiquitousKeyValueStore: KeyValueStorable {} +#endif /// A class that provides a simple way to store and retrieve Codable objects. /// The `Storage` class supports different storage types such as cache, document, and user defaults. @@ -24,13 +26,24 @@ 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 NSUbiquitousKeyValueStore.default) + /// - 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 self.filename = filename createFolderIfNotExists() } +#else + /// Defaults to `nil` on non-Darwin platforms. + public init(storageType: StorageType, filename: String, ubiquitousStore: KeyValueStorable? = nil) { + self.ubiquitousStore = ubiquitousStore + self.type = storageType + self.filename = filename + createFolderIfNotExists() + } +#endif /// Saves the given object to the specified storage type. /// @@ -40,6 +53,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)") @@ -116,7 +131,7 @@ public final class Storage where T: Codable { try? FileManager.default.removeItem(at: folder) } - try? fileManager.createDirectory(at: folder, withIntermediateDirectories: false, attributes: nil) + try? fileManager.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil) } /// Clears the stored data from the specified storage type. diff --git a/Tests/StorageTypeTests.swift b/Tests/StorageTypeTests.swift index a23da26..9db2d40 100644 --- a/Tests/StorageTypeTests.swift +++ b/Tests/StorageTypeTests.swift @@ -5,7 +5,8 @@ class StorageTypeTests: XCTestCase { func testCacheDirectory() { let storageType = StorageType.cache let folder = storageType.folder - XCTAssertTrue(folder.path.contains("Caches"), "Cache directory path should contain 'Caches'") + 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() { From 452eec1a50d8fb7da268cceacc6e7ed7f2ed5b17 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Tue, 3 Jun 2025 23:21:14 +0200 Subject: [PATCH 3/3] docs: note linux compatibility --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6c47908..ac29a05 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ If you are using `Coadble` 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