fix(test): stop tests from registering xctest as a real login item - #134
Merged
Conversation
Incident: testBootstrapWithStoredTrueAndServiceNotFoundRegistersInstead called AppSettings.bootstrap() on the real SMAppService.mainApp, which for the .notFound status a bare `swift test` process reports calls setLaunchAtStartup(true) -> SMAppService.mainApp.register() for real. That registered the xctest test-runner binary itself as a macOS login item (confirmed via `sfltool dumpbtm`: entry #74, Bundle Identifier com.apple.dt.xctest.tool, Disposition enabled). The test's own comment already flagged this as hitting the real API; it just didn't treat that as a defect. Unregistered via the same code path (a one-off test calling SMAppService.mainApp.unregister()), confirmed removed by re-reading dumpbtm (status flipped to disabled) and by the live API (SMAppService.mainApp.status.rawValue == 0, .notRegistered). Fix: introduce a LoginItemService protocol seam over SMAppService.mainApp (toggleLaunchAtStartup.swift) and inject it through AppSettings instead of reaching for the singleton directly. Production code is unaffected — the default parameter value is still SMAppService.mainApp. Tests now use FakeLoginItemService, an in-memory spy, so no test can reach the real OS-level registry. This also made the .requiresApproval branch of bootstrap() reachable and tested for the first time (it was previously unreachable from a real swift test process and had zero coverage — added testBootstrapWithStoredTrueAndRequiresApprovalFollowsOSStateInstead). Added SMAppServiceTestIsolationTests as a regression guard: it scans every test file's source for the literal string "SMAppService.mainApp" and fails if found (excluding the fake and itself), so this class of bug can't be silently reintroduced.
kazukinakai
added a commit
that referenced
this pull request
Aug 6, 2026
Ported from fix/smappservice-test-isolation (PR #134): this branch's AppSettingsTests.swift still had the pre-fix version, which calls the real SMAppService.mainApp.register() and re-registered the xctest test-runner as a login item after this branch's own test runs. See PR #134 for the full incident writeup.
kazukinakai
added a commit
that referenced
this pull request
Aug 6, 2026
Ported from fix/smappservice-test-isolation (PR #134): this branch's AppSettingsTests.swift still had the pre-fix version, which calls the real SMAppService.mainApp.register() and re-registered the xctest test-runner as a login item after this branch's own test runs. See PR #134 for the full incident writeup.
This was referenced Aug 6, 2026
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.
Incident
While running
swift testrepeatedly across worktrees today,testBootstrapWithStoredTrueAndServiceNotFoundRegistersInstead(pre-existing, unrelated to today's feature work) calledAppSettings.bootstrap()against the realSMAppService.mainApp. For the.notFoundstatus a bareswift testprocess reliably reports,bootstrap()callssetLaunchAtStartup(true)→SMAppService.mainApp.register()— for real. That registered thexctesttest-runner binary itself as a macOS login item. The test's own existing comment already said as much ("this only exercises whatever status the real API reports....notFound(confirmed empirically)") — it just didn't treat the side effect as a defect.Confirmed via
sfltool dumpbtm: entry#74,Bundle Identifier: com.apple.dt.xctest.tool,Disposition: [enabled, allowed, not notified].Cleanup already done (outside this diff)
Unregistered through the same code path — a one-off test called
SMAppService.mainApp.unregister()directly — and confirmed removed:sfltool dumpbtmre-read: Disposition flipped to[disabled, allowed, not notified]SMAppService.mainApp.status.rawValue == 0(.notRegistered)(A second, unrelated
CmdIME.appentry also appears indumpbtm— that's the user's real installed app's legitimate "Launch at Login" registration; not touched.)Fix
toggleLaunchAtStartup.swift: newLoginItemServiceprotocol seam overSMAppService.mainApp(status/register()/unregister()).SMAppServiceconforms for free via an extension.setLaunchAtStartup(_:service:)now takes an injectable service, defaulting to the realSMAppService.mainApp— production behavior unchanged.AppSettings: gains an injectableloginItemService(same default), used everywhere it previously reached forSMAppService.mainAppdirectly (init,bootstrap(), the$launchAtStartupsink).FakeLoginItemService(test-only): in-memory spy, so tests never touch the real registry.AppSettingsTests: the offending test now injects a fake with.notFoundstatus and assertsregisterCallCount == 1instead of relying on (and mutating) real system state. Also addedtestBootstrapWithStoredTrueAndRequiresApprovalFollowsOSStateInstead— the.requiresApprovalbranch was previously unreachable from a real test process and had zero coverage; the fake makes it directly testable.SMAppServiceTestIsolationTests(new): regression guard that scans every test file's source for the literal stringSMAppService.mainAppand fails if found (excluding the fake and itself), so this class of bug can't be silently reintroduced.Acceptance
Re-checked
sfltool dumpbtmafter this full test run: thexctestentry's Disposition/Generation are unchanged from the cleaned-up state — confirms no new real registration occurs.