Skip to content

Commit 9e15e42

Browse files
Added tests and implementation for creating a persistence store
1 parent aa0c4fd commit 9e15e42

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

Sources/CodableDatastore/Persistence/Disk Persistence/DiskPersistence.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,25 @@ extension DiskPersistence where AccessMode == ReadOnly {
110110
}
111111
}
112112

113+
extension DiskPersistence {
114+
var snapshotsURL: URL {
115+
storeURL.appendingPathComponent("Snapshots", isDirectory: true)
116+
}
117+
118+
var backupsURL: URL {
119+
storeURL.appendingPathComponent("Backups", isDirectory: true)
120+
}
121+
}
122+
113123
extension DiskPersistence where AccessMode == ReadWrite {
114124
/// Create the persistence store if necessary.
115125
///
116126
/// It is useful to call this if you wish for stub directories to be created immediately before a data store
117127
/// is actually written to the disk.
118128
public func createPersistenceIfNecessary() async throws {
119-
129+
try FileManager.default.createDirectory(at: storeURL, withIntermediateDirectories: true)
130+
try FileManager.default.createDirectory(at: snapshotsURL, withIntermediateDirectories: true)
131+
try FileManager.default.createDirectory(at: backupsURL, withIntermediateDirectories: true)
120132
}
121133
}
122134

Tests/CodableDatastoreTests/DiskPersistenceTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import XCTest
1010
@testable import CodableDatastore
1111

1212
final class DiskPersistenceTests: XCTestCase {
13+
var temporaryStoreURL: URL = FileManager.default.temporaryDirectory
14+
15+
override func setUp() async throws {
16+
temporaryStoreURL = FileManager.default.temporaryDirectory.appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString, isDirectory: true);
17+
}
18+
19+
override func tearDown() async throws {
20+
try? FileManager.default.removeItem(at: temporaryStoreURL)
21+
}
22+
1323
func testTypes() throws {
1424
#if !os(Linux)
1525
XCTAssertTrue(type(of: try DiskPersistence.defaultStore() as Any) is DiskPersistence<ReadWrite>.Type)
@@ -43,4 +53,36 @@ final class DiskPersistenceTests: XCTestCase {
4353
XCTAssertEqual(url2.absoluteString, defaultDirectory)
4454
}
4555
#endif
56+
57+
func testNoFileCreatedOnInit() async throws {
58+
_ = try DiskPersistence(readWriteURL: temporaryStoreURL)
59+
XCTAssertThrowsError(try temporaryStoreURL.checkResourceIsReachable())
60+
61+
_ = DiskPersistence(readOnlyURL: temporaryStoreURL)
62+
XCTAssertThrowsError(try temporaryStoreURL.checkResourceIsReachable())
63+
}
64+
65+
func testThrowsWithRemoteURLs() async throws {
66+
XCTAssertThrowsError(try DiskPersistence(readWriteURL: URL(string: "https://apple.com/")!))
67+
}
68+
69+
func testStoreCreatedWhenAsked() async throws {
70+
let persistence = try DiskPersistence(readWriteURL: temporaryStoreURL)
71+
try await persistence.createPersistenceIfNecessary()
72+
73+
XCTAssertTrue(try temporaryStoreURL.checkResourceIsReachable())
74+
XCTAssertTrue(try temporaryStoreURL.appendingPathComponent("Snapshots", isDirectory: true).checkResourceIsReachable())
75+
XCTAssertTrue(try temporaryStoreURL.appendingPathComponent("Backups", isDirectory: true).checkResourceIsReachable())
76+
}
77+
78+
func testStoreCreatesOnlyIfNecessary() async throws {
79+
let persistence = try DiskPersistence(readWriteURL: temporaryStoreURL)
80+
try await persistence.createPersistenceIfNecessary()
81+
// This second time should be a no-op and shouldn't throw
82+
try await persistence.createPersistenceIfNecessary()
83+
84+
XCTAssertTrue(try temporaryStoreURL.checkResourceIsReachable())
85+
XCTAssertTrue(try temporaryStoreURL.appendingPathComponent("Snapshots", isDirectory: true).checkResourceIsReachable())
86+
XCTAssertTrue(try temporaryStoreURL.appendingPathComponent("Backups", isDirectory: true).checkResourceIsReachable())
87+
}
4688
}

0 commit comments

Comments
 (0)