File tree Expand file tree Collapse file tree
Sources/ObservableDefaults/NSUbiquitousKeyValueStore
Tests/ObservableDefaultsTests/Utils Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -25,6 +25,10 @@ import Foundation
2525 userDefaults = UserDefaults ( suiteName: Self . suiteName) !
2626 }
2727
28+ init ( suiteName: String ) {
29+ userDefaults = UserDefaults ( suiteName: suiteName) !
30+ }
31+
2832 // MARK: - Object methods
2933
3034 func object( forKey aKey: String ) -> Any ? {
Original file line number Diff line number Diff line change @@ -27,13 +27,22 @@ public struct NSUbiquitousKeyValueStoreWrapper: Sendable {
2727 /// A TaskLocal variable to determine if the current environment is a test environment.
2828 @TaskLocal
2929 static var isTestEnvironment : Bool = false
30+
31+ /// A TaskLocal suite override used to isolate mock cloud storage per test.
32+ @TaskLocal
33+ static var testSuiteName : String ?
3034 #endif
3135
3236 /// A private property to store the cloud store.
3337 private var store : ObservableDefaultsCloudStoreProtocol {
3438 #if DEBUG
35- return Self . isTestEnvironment ? MockUbiquitousKeyValueStore
36- . default : NSUbiquitousKeyValueStore . default
39+ if Self . isTestEnvironment {
40+ if let testSuiteName = Self . testSuiteName {
41+ return MockUbiquitousKeyValueStore ( suiteName: testSuiteName)
42+ }
43+ return MockUbiquitousKeyValueStore . default
44+ }
45+ return NSUbiquitousKeyValueStore . default
3746 #else
3847 return NSUbiquitousKeyValueStore . default
3948 #endif
Original file line number Diff line number Diff line change @@ -18,9 +18,13 @@ import Testing
1818 testCase: Test . Case ? ,
1919 performing function: @Sendable ( ) async throws -> Void ) async throws
2020 {
21+ let suiteName = " ObservableDefaults.TestMode. \( UUID ( ) . uuidString) "
2122 try await NSUbiquitousKeyValueStoreWrapper . $isTestEnvironment
2223 . withValue ( value) {
23- try await function ( )
24+ try await NSUbiquitousKeyValueStoreWrapper . $testSuiteName
25+ . withValue ( suiteName) {
26+ try await function ( )
27+ }
2428 }
2529 }
2630 }
Original file line number Diff line number Diff line change @@ -24,13 +24,16 @@ extension UserDefaults {
2424}
2525
2626extension UserDefaults {
27+ private static var currentMockSuiteName : String {
28+ NSUbiquitousKeyValueStoreWrapper . testSuiteName ?? MockUbiquitousKeyValueStore . suiteName
29+ }
30+
2731 static var mock : UserDefaults {
28- UserDefaults ( suiteName: MockUbiquitousKeyValueStore . suiteName ) !
32+ UserDefaults ( suiteName: currentMockSuiteName ) !
2933 }
3034
3135 static func clearMock( ) {
32- mock. removePersistentDomain ( forName: MockUbiquitousKeyValueStore . suiteName )
36+ mock. removePersistentDomain ( forName: currentMockSuiteName )
3337 mock. synchronize ( )
3438 }
3539}
36-
You can’t perform that action at this time.
0 commit comments