Stop tests leaking UserDefaults suite domains#878
Open
robinebers wants to merge 1 commit into
Open
Conversation
Owner
Author
|
I found one issue that should be fixed before merging. This covered every leaking test when the branch was created, but two layout test groups were added to Please update the branch from |
Every `make…Defaults` helper across the test suite created a fresh `UserDefaults(suiteName:)` with a UUID-embedded name, cleared it once at the top for a clean slate, and returned it — with nothing removing the suite afterward. Each run left a permanent domain behind, accumulating tens of thousands of `OpenUsageTests.*` domains in ~/Library/Preferences. Route every scratch suite through a single `XCTestCase.makeScratchDefaults` helper that keeps the clean-slate pre-clear and also registers an `addTeardownBlock` to remove the persistent domain once the test finishes. This mirrors the defer-based pattern the correct helpers already used, in one DRY place, and drops ~60 lines of duplicated boilerplate. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
826a686 to
c08d357
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
The test suite created unique
UserDefaults(suiteName:)domains but usually cleared them only before a test, never afterward, so repeatedswift testruns accumulated thousands of test preference plists in~/Library/Preferences. This routes every leaking scratch suite through one shared helper that removes its data in teardown. Test infrastructure only — no app behavior change.What was happening
removePersistentDomainonce at the top for a clean slate, and returned them.defer { removePersistentDomain }pattern; those remain unchanged.What this changes
XCTestCase.makeScratchDefaults(suiteName:)inTestSupport.swift. It pre-clears the suite and registers anaddTeardownBlockthat removes its persistent domain after the test.LayoutBootstrapTestsandLayoutPersistenceTestsadded after this branch was first created.UserDefaultsinstances to test bodies.Heads-up
macos-26CI runner),removePersistentDomainremoves the suite plist. On macOS 27 beta,cfprefsdrecreates an empty 42-byte dictionary for touched suites when the test process exits. The teardown still removes all test data, but a post-run filesystem cleanup is required to remove those empty shells.OpenUsageTestsprefixes used by Copilot, notification, and snapshot-cache tests:Tests
swift test --filter 'Layout(Bootstrap|Persistence)Tests'— 5 passed.swift test— 953 passed, 3 skipped, 0 failures.defaults domainswere zero afterward.