Skip to content

Use .inMemory UserDefaults in SidebarPersistenceMigratorTests#314

Merged
sbertix merged 1 commit into
mainfrom
sbertix/cleanup-tests-user-defaults
May 12, 2026
Merged

Use .inMemory UserDefaults in SidebarPersistenceMigratorTests#314
sbertix merged 1 commit into
mainfrom
sbertix/cleanup-tests-user-defaults

Conversation

@sbertix
Copy link
Copy Markdown
Collaborator

@sbertix sbertix commented May 12, 2026

Summary

  • SidebarPersistenceMigratorTests was overriding $0.defaultAppStorage = UserDefaults(suiteName: "\(#function).\(UUID().uuidString)")!, which cfprefsd persisted to ~/Library/Preferences/ — leaking ~16 plists per test run (thousands after many local/CI runs).
  • swift-sharing ships UserDefaults.inMemory for exactly this case: on macOS 14+/iOS 17+ it prefixes the suite name with NSTemporaryDirectory(), so cfprefsd writes the plist to /var/folders/.../T/co.pointfree.Sharing.<UUID>.plist (auto-reaped by the OS) instead of the user-visible ~/Library/Preferences/.
  • Net -15 lines: no helper functions, no per-test defer, no UUID-suffix gymnastics. Every leaking call site is now $0.defaultAppStorage = .inMemory.

Verification

  • make test passes (1097 tests, 78 suites).
  • Before: each make test run added 16 fresh plists to ~/Library/Preferences/ (with method-name + UUID filenames). After: 0 new files there; 16 fresh files in NSTemporaryDirectory() instead, where the OS handles cleanup.

Cleaning up the historical leaks

This PR stops the bleeding but doesn't touch the plists already sitting in your ~/Library/Preferences/ from prior runs of this (and earlier, since-deleted) test classes. Run locally to clean them up:

# Dry run — count what would be deleted (test-pattern plists only, never bundle-ID plists)
find ~/Library/Preferences -maxdepth 1 -type f \
  \( -name "*Tests.*.plist" -o -name "*\(\).plist" -o -name "*\(\).*.plist" \) | wc -l

# Delete them
find ~/Library/Preferences -maxdepth 1 -type f \
  \( -name "*Tests.*.plist" -o -name "*\(\).plist" -o -name "*\(\).*.plist" \) -delete

# Flush cfprefsd so it doesn't re-write any stragglers from memory
killall cfprefsd

The patterns target only Swift-test-shaped filenames (<ClassName>Tests.<...>.plist, <method>().plist, <method>().<UUID>.plist) — they will never match real app/system preference files, which use reverse-DNS bundle IDs (com.apple.*, com.microsoft.*, etc.).

Test plan

  • make test green locally
  • make check (swift-format + swiftlint) green
  • Two consecutive test runs verified: 0 new files in ~/Library/Preferences/

Each test created `UserDefaults(suiteName: "<func>.<UUID>")` which
cfprefsd persisted to ~/Library/Preferences, leaking ~16 plists per
run. swift-sharing's `UserDefaults.inMemory` routes the suite to
`NSTemporaryDirectory()` (OS-reaped) instead, fixing the leak without
hand-rolled cleanup.
@sbertix sbertix enabled auto-merge (squash) May 12, 2026 22:15
@sbertix sbertix merged commit 7700b84 into main May 12, 2026
2 checks passed
@sbertix sbertix deleted the sbertix/cleanup-tests-user-defaults branch May 12, 2026 22:22
@tuist
Copy link
Copy Markdown

tuist Bot commented May 12, 2026

🛠️ Tuist Run Report 🛠️

Builds 🔨

Scheme Status Duration Commit
supacode 1m 56s fd87e02d0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant