Skip to content
Merged
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
13 changes: 10 additions & 3 deletions Tests/ClearedTests/AppModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import Foundation

/// Settings backed by a throwaway defaults domain, so a test never reads or
/// writes the preferences of the person running it.
///
/// The suite name is an absolute path in the temporary directory, which
/// CFPreferences stores as a plain plist file there. A named suite would live in
/// ~/Library/Preferences, and `removePersistentDomain` empties it but leaves the
/// plist behind, so every test run used to add one file per test.
@MainActor
final class ScratchSettings {
let settings: Settings
private let domain = "ClearedTests.\(UUID().uuidString)"
private let path = FileManager.default.temporaryDirectory
.appending(path: "ClearedTests.\(UUID().uuidString)").path

init() {
settings = Settings(defaults: UserDefaults(suiteName: domain)!)
settings = Settings(defaults: UserDefaults(suiteName: path)!)
}

deinit {
UserDefaults.standard.removePersistentDomain(forName: domain)
// Best effort: anything left over sits in the temporary directory, which the system clears.
try? FileManager.default.removeItem(atPath: path + ".plist")
}
}

Expand Down
Loading