fix(test): stub macOS Keychain in TestReadClaudeCodeCredentials#16498
fix(test): stub macOS Keychain in TestReadClaudeCodeCredentials#16498briandevans wants to merge 2 commits into
Conversation
read_claude_code_credentials() checks the macOS Keychain before falling back to the JSON file. On machines with Claude Code installed the Keychain lookup returns a real token, causing all 5 tests to fail: 3 that expect None get a live credential, and the one that checks the file path gets the Keychain token instead of the fixture value. Add an autouse fixture that stubs _read_claude_code_credentials_from_keychain to return None, keeping tests isolated from the host environment. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR improves test isolation for read_claude_code_credentials() by preventing TestReadClaudeCodeCredentials from reading live macOS Keychain credentials on developer machines, ensuring the tests reliably exercise the JSON credentials-file code path.
Changes:
- Add an
autousepytest fixture to stub_read_claude_code_credentials_from_keychain()toNonefor all tests inTestReadClaudeCodeCredentials.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…RunOauthSetupToken The same macOS Keychain bypass that broke TestReadClaudeCodeCredentials also affects resolve_anthropic_token() and run_oauth_setup_token() which both call read_claude_code_credentials() internally. Add the same autouse _no_keychain fixture to both classes so all 10 affected tests are isolated from the host environment. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Extended the fix in commit |
|
Closing — superseded by our own #15958, which is a strictly more comprehensive fix:
#15958 covers all three keychain stubs plus the botocore fixture. No reason to keep both open — closing this one in favour of the more complete PR. |
Summary
read_claude_code_credentials()checks the macOS Keychain before falling back to the JSON credentials filesk-ant-oat01-*tokenTestReadClaudeCodeCredentialstests fail on such machines: the 4 tests expectingNoneget the real credential, and the positive test reads the Keychain token instead of its fixture valueautousefixture to the class that stubs_read_claude_code_credentials_from_keychainto returnNone, keeping all tests isolated from the host environmentRoot cause
The tests only patched
agent.anthropic_adapter.Path.home; they did not account for the Keychain path that short-circuits first.Test plan
pytest tests/agent/test_anthropic_adapter.py::TestReadClaudeCodeCredentials— all 5 pass (was 5 failures on macOS with Claude Code installed)mainthen green after fix🤖 Generated with Claude Code