Files
claude-scripts/tests/hooks/README.md

3.2 KiB

Hook System Test Suite

Comprehensive pytest-based test suite for the Claude Code quality hooks.

Test Structure

tests/hooks/
├── conftest.py              # Pytest fixtures and configuration
├── test_config.py           # QualityConfig class tests
├── test_pretooluse.py       # PreToolUse hook functionality tests
├── test_posttooluse.py      # PostToolUse hook functionality tests
├── test_helper_functions.py # Helper function tests
└── test_integration.py      # Full integration tests

Test Coverage

Configuration Testing (test_config.py)

  • Default configuration values
  • Environment variable loading
  • Invalid input handling
  • Threshold boundaries
  • Enforcement modes
  • Configuration combinations

PreToolUse Testing (test_pretooluse.py)

  • Tool filtering (Write/Edit/MultiEdit)
  • File type filtering (Python only)
  • Skip patterns (test files, fixtures)
  • Clean code acceptance
  • Complexity detection
  • Duplicate detection
  • Modernization checks
  • Enforcement modes (strict/warn/permissive)
  • State tracking
  • Exception handling

PostToolUse Testing (test_posttooluse.py)

  • File path extraction (dict/string output)
  • File existence verification
  • State change detection
  • Cross-file duplicate checking
  • Naming convention verification
  • Success message configuration
  • Multi-feature combinations

Helper Functions (test_helper_functions.py)

  • Skip pattern matching
  • Binary path resolution
  • State storage and retrieval
  • State change detection
  • Cross-file duplicate detection
  • Naming convention checks
  • Code quality analysis
  • Issue detection and reporting

Integration Testing (test_integration.py)

  • Main entry point routing
  • Full PreToolUse → PostToolUse flow
  • Environment configuration
  • Tool handling
  • Enforcement mode progression
  • State tracking flow
  • Error handling

Running Tests

Run all hook tests:

pytest tests/hooks/ -v

Run specific test file:

pytest tests/hooks/test_config.py -v

Run with coverage:

pytest tests/hooks/ --cov=hooks --cov-report=html

Run specific test:

pytest tests/hooks/test_config.py::TestQualityConfig::test_default_config -v

Test Fixtures

The conftest.py file provides reusable test fixtures:

  • temp_python_file: Creates temporary Python files
  • mock_hook_input_pretooluse: PreToolUse hook input
  • mock_hook_input_posttooluse: PostToolUse hook input
  • complex_code: Sample complex Python code
  • duplicate_code: Code with internal duplicates
  • clean_code: Well-written Python code
  • non_pep8_code: Code with naming violations
  • old_style_code: Outdated Python patterns
  • test_file_code: Sample test file
  • reset_environment: Auto-reset env vars
  • set_env_strict: Configure strict mode
  • set_env_permissive: Configure permissive mode
  • set_env_posttooluse: Configure PostToolUse features

Test Results

Current test status: 67/68 passing (98.5% pass rate)

The test suite ensures:

  • Configuration is properly loaded from environment
  • PreToolUse correctly prevents bad code
  • PostToolUse verifies written files
  • All enforcement modes work correctly
  • State tracking captures quality changes
  • Helper functions work as expected
  • Integration between components is solid