diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a98138c5..efd987c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -89,6 +89,8 @@ jobs: - name: swift test run: swift test + env: + STACKNUDGE_TESTS_NO_KEYCHAIN: "1" # notify.sh is the hook side, which swift test can't reach. Runs here # rather than in the shell job because the panel post it asserts on is diff --git a/Makefile b/Makefile index 66937698..3de3d95d 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,7 @@ test: echo " sudo xcode-select -s /Applications/Xcode.app/Contents/Developer"; \ exit 1; \ fi - @swift test + @STACKNUDGE_TESTS_NO_KEYCHAIN=1 swift test # Drive notify.sh against a fake panel socket under a throwaway $HOME, so the # hook side gets covered too. macOS only; a no-op elsewhere. Needs no Xcode. @@ -77,7 +77,7 @@ typecheck-tests: # filters to matching Class.method. .PHONY: test-without-xcode test-without-xcode: - @./scripts/run-tests-without-xcode.sh $(NAME) + @STACKNUDGE_TESTS_NO_KEYCHAIN=1 ./scripts/run-tests-without-xcode.sh $(NAME) # One-shot dev cycle: rebuild, reinstall the app, refresh notify.sh in # ~/.stack-nudge so hook-side changes propagate, kickstart the daemon. diff --git a/Tests/StackNudgePanelCoreTests/IntegrationPageTests.swift b/Tests/StackNudgePanelCoreTests/IntegrationPageTests.swift index 57066d9e..a9aa7631 100644 --- a/Tests/StackNudgePanelCoreTests/IntegrationPageTests.swift +++ b/Tests/StackNudgePanelCoreTests/IntegrationPageTests.swift @@ -142,6 +142,7 @@ final class IntegrationPageTests: XCTestCase { private func navCheckingGh(_ answer: Bool, asked: @escaping () -> Void) -> PanelNav { let nav = PanelNav() nav.checkGitHubCLILogin = { asked(); return answer } + nav.githubToken = { nil } nav.runOffMain = { $0() } nav.runOnMain = { $0() } return nav diff --git a/panel/GitHubAuth.swift b/panel/GitHubAuth.swift index ad0dbf5e..755f97fe 100644 --- a/panel/GitHubAuth.swift +++ b/panel/GitHubAuth.swift @@ -52,6 +52,9 @@ enum GitHubAuth { // MARK: - Keychain static func token() -> String? { + // A test binary reading the real item makes macOS ask for the user's password; tests stub it. + precondition(ProcessInfo.processInfo.environment["STACKNUDGE_TESTS_NO_KEYCHAIN"] == nil, + "GitHubAuth.token reached the Keychain from a test") var item: CFTypeRef? let status = SecItemCopyMatching(baseQuery(returningData: true) as CFDictionary, &item) guard status == errSecSuccess, let data = item as? Data, diff --git a/panel/PanelNav.swift b/panel/PanelNav.swift index e3b63c5e..018ee4ae 100644 --- a/panel/PanelNav.swift +++ b/panel/PanelNav.swift @@ -1979,6 +1979,11 @@ final class PanelNav: ObservableObject { } } + // The Keychain readers, swapped out by tests: a fresh test binary reading + // the real items makes macOS ask the user for their password. + var githubToken: () -> String? = GitHubAuth.token + var slackBotToken: () -> String? = SlackCredentials.botToken + func loadFromConfig() { let config = ConfigFile.read() hotkeyDisplay = config["STACKNUDGE_PANEL_HOTKEY"] ?? "cmd+opt+n" @@ -2008,7 +2013,7 @@ final class PanelNav: ObservableObject { githubActivityEnabled = ConfigFile.bool(config, "STACKNUDGE_GITHUB_ACTIVITY", default: false) hideShippedTickets = ConfigFile.bool(config, "STACKNUDGE_HIDE_SHIPPED", default: false) githubUseGH = ConfigFile.bool(config, "STACKNUDGE_GITHUB_USE_GH", default: true) - githubSignedIn = githubUsesCLILogin || GitHubAuth.token() != nil + githubSignedIn = githubUsesCLILogin || githubToken() != nil refreshGitHubCLILogin() quotaTrackingEnabled = ConfigFile.bool(config, "STACKNUDGE_QUOTA_TRACKING", default: true) quotaAlertsEnabled = ConfigFile.bool(config, "STACKNUDGE_QUOTA_ALERTS", default: true) @@ -2036,7 +2041,7 @@ final class PanelNav: ObservableObject { let rawSlackIdle = Int(config["STACKNUDGE_SLACK_IDLE_MIN"] ?? "") ?? 5 slackIdleMinutes = SlackDelivery.idleMinuteOptions .min(by: { abs($0 - rawSlackIdle) < abs($1 - rawSlackIdle) }) ?? 5 - slackTokenPresent = SlackCredentials.botToken() != nil + slackTokenPresent = slackBotToken() != nil let memberID = config["STACKNUDGE_SLACK_MEMBER_ID"]? .trimmingCharacters(in: .whitespaces) ?? "" slackMemberID = memberID.isEmpty ? nil : memberID @@ -2497,7 +2502,7 @@ final class PanelNav: ObservableObject { private func applyGitHubCLILogin(_ loggedIn: Bool) { if githubUsesCLILogin != loggedIn { githubUsesCLILogin = loggedIn } - let signedIn = loggedIn || GitHubAuth.token() != nil + let signedIn = loggedIn || githubToken() != nil if githubSignedIn != signedIn { githubSignedIn = signedIn } } diff --git a/panel/SlackCredentials.swift b/panel/SlackCredentials.swift index f14d2068..e4b16a3e 100644 --- a/panel/SlackCredentials.swift +++ b/panel/SlackCredentials.swift @@ -28,6 +28,9 @@ enum SlackCredentials { // MARK: - Keychain static func botToken() -> String? { + // A test binary reading the real item makes macOS ask for the user's password; tests stub it. + precondition(ProcessInfo.processInfo.environment["STACKNUDGE_TESTS_NO_KEYCHAIN"] == nil, + "SlackCredentials.botToken reached the Keychain from a test") var item: CFTypeRef? let status = SecItemCopyMatching(baseQuery(returningData: true) as CFDictionary, &item) guard status == errSecSuccess, let data = item as? Data,