Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Storage/Classes/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public final class Storage<T> 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
Expand All @@ -51,6 +52,8 @@ public final class Storage<T> 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)")
Expand Down
23 changes: 23 additions & 0 deletions Tests/StorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions Tests/StorageTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down