Skip to content

Commit 4a08300

Browse files
authored
Add inMemory value to defaultFileStorage (#126)
* Fix test dependency values * Add `inMemory` for defaultAppStorage, matching defaultFileStorage
1 parent 2c840cf commit 4a08300

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

Sources/Sharing/Documentation.docc/Articles/Testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ struct EntryPoint: App {
126126
init() {
127127
if ProcessInfo.processInfo.environment["UI_TESTING"] != nil {
128128
prepareDependencies {
129-
$0.defaultAppStorage = .testValue
130-
$0.defaultFileStorage = .testValue
129+
$0.defaultAppStorage = .inMemory
130+
$0.defaultFileStorage = .inMemory
131131
}
132132
}
133133
}

Sources/Sharing/SharedKeys/AppStorageKey.swift

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,33 @@
509509
}
510510

511511
extension DependencyValues {
512+
/// Default file storage used by ``SharedReaderKey/appStorage(_:)``.
513+
///
514+
/// Use this dependency to override the manner in which
515+
/// ``SharedReaderKey/appStorage(_:)`` interacts with UserDefaults. For
516+
/// example, while your app is running for UI tests you probably do not want your features writing
517+
/// changes to your actual UserDefaults suite, which would cause that data to bleed over from test to test.
518+
///
519+
/// So, for that situation you can use the ``inMemory`` value so that each
520+
/// run of the app starts with a fresh suite that will never interfere with other tests:
521+
///
522+
/// ```swift
523+
/// import Dependencies
524+
/// import Sharing
525+
/// import SwiftUI
526+
///
527+
/// @main
528+
/// struct MyApp: App {
529+
/// init() {
530+
/// if ProcessInfo.processInfo.environment["UITesting"] == "true"
531+
/// prepareDependencies {
532+
/// $0.defaultAppStorage = .inMemory
533+
/// }
534+
/// }
535+
/// }
536+
/// // ...
537+
/// }
538+
/// ```
512539
public var defaultAppStorage: UserDefaults {
513540
get { self[DefaultAppStorageKey.self].value }
514541
set { self[DefaultAppStorageKey.self].value = newValue }
@@ -521,28 +548,32 @@
521548
}
522549

523550
private enum DefaultAppStorageKey: DependencyKey {
524-
static var testValue: UncheckedSendable<UserDefaults> {
525-
let suiteName: String
526-
// NB: Due to a bug in iOS 16 and lower, UserDefaults does not observe changes when using
527-
// file-based suites. Go back to using temporary directory always when we drop iOS 16
528-
// support.
529-
if #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10, visionOS 1) {
530-
suiteName = "co.pointfree.Sharing.\(UUID().uuidString)"
531-
} else {
532-
suiteName = "\(NSTemporaryDirectory())co.pointfree.Sharing.\(UUID().uuidString)"
533-
}
534-
return UncheckedSendable(
535-
UserDefaults(suiteName: suiteName)!
536-
)
551+
static var liveValue: UncheckedSendable<UserDefaults> {
552+
UncheckedSendable(.standard)
537553
}
538554
static var previewValue: UncheckedSendable<UserDefaults> {
539-
testValue
555+
UncheckedSendable(.inMemory)
540556
}
541-
static var liveValue: UncheckedSendable<UserDefaults> {
542-
UncheckedSendable(UserDefaults.standard)
557+
static var testValue: UncheckedSendable<UserDefaults> {
558+
UncheckedSendable(.inMemory)
543559
}
544560
}
545561

562+
extension UserDefaults {
563+
public static var inMemory: UserDefaults {
564+
let suiteName: String
565+
// NB: Due to a bug in iOS 16 and lower, UserDefaults does not observe changes when using
566+
// file-based suites. Go back to using temporary directory always when we drop iOS 16
567+
// support.
568+
if #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10, visionOS 1) {
569+
suiteName = "co.pointfree.Sharing.\(UUID().uuidString)"
570+
} else {
571+
suiteName = "\(NSTemporaryDirectory())co.pointfree.Sharing.\(UUID().uuidString)"
572+
}
573+
return UserDefaults(suiteName: suiteName)!
574+
}
575+
}
576+
546577
private enum AppStorageKeyFormatWarningEnabledKey: DependencyKey {
547578
static let liveValue = true
548579
static let testValue = true

0 commit comments

Comments
 (0)