Tests/OrgAppTests/TestDefaults.swift

20 lines · 747 bytes

1 symbol in this file
 1import Foundation
 2
 3/// A defaults suite named `<prefix>-<UUID>`, kept in the temporary folder rather than
 4/// `~/Library/Preferences` and removed with its plist when released. cfprefsd may write a
 5/// plist again after it is removed, so the suite is a path outside the preferences folder.
 6final class TestDefaults: UserDefaults, @unchecked Sendable {
 7    let name: String
 8    private let path: String
 9
10    init(_ prefix: String) {
11        name = "\(prefix)-\(UUID().uuidString)"
12        path = FileManager.default.temporaryDirectory.appendingPathComponent(name).path
13        super.init(suiteName: path)!
14    }
15
16    deinit {
17        removePersistentDomain(forName: path)
18        try? FileManager.default.removeItem(atPath: path + ".plist")
19    }
20}