This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
DevKeychain is a Swift package providing a modern, type-safe interface to Apple's keychain services. It supports version 26 of Apple's OSes and requires a Swift 6.2+ toolchain.
- Build:
swift build - Test all:
swift test - Test specific:
swift test --filter <TestName> - Test with coverage:
swift test --enable-code-coverage
- Lint:
Scripts/lint(usesswift format lint --recursive --strict) - Format:
Scripts/format(usesswift format --in-place --recursive)
- Install:
Scripts/install-git-hooks(installs pre-push hook that runs lint)
The codebase follows a protocol-based architecture with dependency injection:
KeychainServicesprotocol abstracts Security framework operationsStandardKeychainServicesprovides real implementation- Mock implementations enable comprehensive testing
- All keychain item types implement
KeychainItemAdditionAttributesandKeychainItemQueryprotocols
Core Layer (Sources/DevKeychain/Core/):
Keychain.swift: Main keychain interfaceKeychainServices.swift: Protocol abstraction over Security frameworkKeychainItemAdditionAttributes.swift&KeychainItemQuery.swift: Generic protocols for type-safe operations
Keychain Item Types (Sources/DevKeychain/Keychain Items/):
GenericPassword.swift: Service/account-based passwordsInternetPassword.swift: Server/account-based credentials- Each type has nested
AdditionAttributesandQuerytypes
Error Handling (Sources/DevKeychain/Errors/):
KeychainServicesError.swift: Wraps OSStatus errors from Security frameworkKeychainItemMappingError.swift: Data conversion and mapping errors
Uses Swift Testing framework with comprehensive mocking:
- All tests inject mock
KeychainServicesimplementations DevTestingdependency provides Stub system for mocking- Tests are organized by component in
Tests/DevKeychainTests/ - Integration tests in
App/Tests/use real keychain operations
- Nested Types: Related functionality grouped within parent types
- Generic Protocols: Type-safe operations without runtime checks
- Extension-based Helpers:
Dictionary+KeychainItemMapping.swiftfor attribute conversion - Dependency Injection: Constructor injection for
KeychainServices
- Create new file in
Sources/DevKeychain/Keychain Items/ - Implement
KeychainItemAdditionAttributesprotocol for adding items - Implement
KeychainItemQueryprotocol for querying items - Add comprehensive tests with both success and failure scenarios
- Follow the pattern established by
GenericPasswordandInternetPassword
- Always use mock
KeychainServicesin unit tests - Use
DevTesting.Stubfor consistent mocking behavior - Test both success and error scenarios
- Use randomized test data via
RandomValueGenerating+DevKeychain.swift - Follow naming convention:
<ComponentName>Tests.swift
- Swift API Design Guidelines strictly enforced
- 120 character line limit (configured in
.swift-format) - All public APIs must be documented
- Use descriptive variable names and clear function signatures
The project uses GitHub Actions for continuous integration:
- Linting: Automatically checks code formatting on all pull requests using
swift format - Testing: Builds and tests on iOS, macOS, tvOS, and watchOS
- Coverage: Generates code coverage reports using xccovPretty
- Uses Xcode 26.3 and macOS 26 runners
- Swift 6.2+ toolchain required
- Xcode 26.3 for CI/CD
- Apple platforms only (iOS/macOS/tvOS/visionOS/watchOS version 26)
- Uses modern Swift concurrency features
The codebase uses a consistent stub-based mocking pattern built on the DevTesting framework:
- Stub-based mocks: All mocks use
Stub<Input, Output>orThrowingStub<Input, Output, Error> - Force-unwrapped stubs: Stub properties are declared with
!— tests must configure them - Swift 6 concurrency: All stub properties marked
nonisolated(unsafe) - Argument structures: Complex parameters use dedicated structures (e.g.,
LogErrorArguments)
- File naming:
Mock[ProtocolName].swift - Type naming:
Mock[ProtocolName] - Stub properties:
[functionName]Stub - Location:
Tests/DevKeychainTests/Testing Helpers/
- Use
@Testwith Swift Testing framework - Use
#expect()and#require()for assertions - Always configure stubs before use to avoid crashes
- Leverage DevTesting's call tracking for verification
Follow the project's Markdown Style Guide:
- Line length: 100 characters max
- Code blocks: Use 4-space indentation, not fenced blocks
- Lists: Use
-for bullets, align continuation lines with text - Spacing: 2 blank lines between major sections, 1 after headers
- Terminology: Use "function" over "method", "type" over "class"
When writing Markdown documentation, reference @Documentation/MarkdownStyleGuide.md to
ensure consistent formatting, structure, and style across all project documentation.
The project follows strict spacing conventions for readability and consistency:
- 2 blank lines between major sections including:
- Between the last property declaration and first function declaration
- Between all function/computed property implementations at the same scope level
- Between top-level type declarations (class, struct, enum, protocol, extension)
- Before MARK comments that separate major sections
- 1 blank line for minor separations:
- Between property declarations and nested type definitions
- Between all function definitions in protocols
- After headers in documentation
- After MARK comments that separate major sections
- File endings: All Swift files must end with exactly one blank line
The Scripts/ directory contains utility scripts for development workflow automation:
Scripts/install-git-hooks:
- Installs a pre-push git hook that runs
Scripts/lintfor formatting validation - Works correctly with git worktrees
- Run once per repository to set up automated code quality workflow
Scripts/lint:
- Runs
swift format lintvalidation with strict mode enabled - Checks
App/,Sources/, andTests/directories recursively - Returns non-zero exit code if formatting issues are found
- Used by pre-push hook and can be run manually for code quality verification
Scripts/format:
- Runs
swift format --in-placeto automatically fix formatting issues - Formats
App/,Sources/, andTests/directories recursively
Sources/DevKeychain/Core/Keychain.swift: Main API entry pointSources/DevKeychain/Core/KeychainServices.swift: Core protocol abstractionSources/DevKeychain/Keychain Items/GenericPassword.swift: Example keychain item implementationTests/DevKeychainTests/Testing Helpers/: Mock implementations and test utilitiesPackage.swift: Dependencies and platform requirements.swift-format: Code formatting configurationScripts/: Development workflow automation scripts