|
| 1 | +# cpp-linter-hooks Development Instructions |
| 2 | + |
| 3 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 4 | + |
| 5 | +cpp-linter-hooks is a Python package that provides pre-commit hooks for automatically formatting and linting C/C++ code using `clang-format` and `clang-tidy`. The package installs specific versions of these tools and integrates them into pre-commit workflows. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Bootstrap and Install Dependencies |
| 10 | +- `python -m pip install --upgrade pip` |
| 11 | +- `pip install .[dev]` -- installs the package in development mode with all dev dependencies |
| 12 | +- **Time expectation**: 2-3 minutes for initial install |
| 13 | + |
| 14 | +### Build and Test Commands |
| 15 | +- `pytest -vv -k "not (test_get_version_from_dependency_success or test_get_version_from_dependency_missing_dependency or test_get_version_from_dependency_malformed_toml or test_resolve_install_tool_already_installed_correct_version or test_default_versions)"` -- runs working tests, excluding 5 known failing tests related to installation path detection |
| 16 | +- **NEVER CANCEL**: Test suite takes 6-8 minutes. Set timeout to 15+ minutes. |
| 17 | +- `coverage run --source=tests,cpp_linter_hooks -m pytest -vv -k "not (test_get_version_from_dependency_success or test_get_version_from_dependency_missing_dependency or test_get_version_from_dependency_malformed_toml or test_resolve_install_tool_already_installed_correct_version or test_default_versions)"` -- runs tests with coverage |
| 18 | +- **NEVER CANCEL**: Coverage test takes 8-10 minutes. Set timeout to 20+ minutes. |
| 19 | +- `coverage report` -- shows coverage after running coverage tests |
| 20 | +- `coverage xml` -- generates XML coverage report |
| 21 | + |
| 22 | +### Test the Hooks Directly |
| 23 | +- `python -m cpp_linter_hooks.clang_format --help` -- shows clang-format hook help |
| 24 | +- `python -m cpp_linter_hooks.clang_tidy --help` -- shows clang-tidy hook help |
| 25 | +- `python -m cpp_linter_hooks.clang_format --version=21 file.c` -- formats a C file using clang-format version 21 |
| 26 | +- `python -m cpp_linter_hooks.clang_tidy --version=21 --checks=readability-* file.c` -- lints a C file using clang-tidy |
| 27 | +- **Time expectation**: Each hook takes 15-20 seconds on first run (tool installation), then 1-2 seconds |
| 28 | + |
| 29 | +### Pre-commit Setup and Testing |
| 30 | +**WARNING**: Network timeouts may affect pre-commit setup in CI environments. For testing, use local system hooks: |
| 31 | + |
| 32 | +Create test scenario: |
| 33 | +```bash |
| 34 | +mkdir /tmp/test_precommit && cd /tmp/test_precommit |
| 35 | +git init && git config user.email "test@example.com" && git config user.name "Test User" |
| 36 | +echo '#include <stdio.h> |
| 37 | +int main(){printf("test");return 0;}' > test.c |
| 38 | +``` |
| 39 | + |
| 40 | +Create `.pre-commit-config.yaml`: |
| 41 | +```yaml |
| 42 | +repos: |
| 43 | + - repo: local |
| 44 | + hooks: |
| 45 | + - id: clang-format |
| 46 | + name: clang-format |
| 47 | + entry: clang-format-hook |
| 48 | + language: system |
| 49 | + types_or: [c++, c] |
| 50 | + args: [--version=21] |
| 51 | +``` |
| 52 | +
|
| 53 | +Run: `git add . && git commit -m "initial" && pre-commit run --all-files` |
| 54 | +- **Time expectation**: 15-20 seconds |
| 55 | + |
| 56 | +**NOTE**: The main repository testing script `sh testing/run.sh` may fail due to network timeouts when installing clang tools from PyPI. This is a known limitation in sandboxed environments. |
| 57 | + |
| 58 | +### Linting and Formatting |
| 59 | +**WARNING**: Standard pre-commit setup may fail due to network issues. The repository uses: |
| 60 | +- `pre-commit run --all-files` -- may timeout in CI environments |
| 61 | +- The project has a `.pre-commit-config.yaml` with ruff for Python formatting |
| 62 | +- **Time expectation**: When working, takes 2-3 minutes |
| 63 | + |
| 64 | +## Validation Scenarios |
| 65 | + |
| 66 | +### Always test these scenarios after making changes: |
| 67 | + |
| 68 | +1. **Basic Hook Functionality**: |
| 69 | + ```bash |
| 70 | + echo '#include <stdio.h> |
| 71 | + int main(){printf("test");return 0;}' > /tmp/test.c |
| 72 | + python -m cpp_linter_hooks.clang_format --version=21 /tmp/test.c |
| 73 | + cat /tmp/test.c # Verify formatting applied |
| 74 | + ``` |
| 75 | + |
| 76 | +2. **Clang-tidy Linting**: |
| 77 | + ```bash |
| 78 | + echo '[ |
| 79 | + { |
| 80 | + "directory": "/tmp", |
| 81 | + "command": "gcc -o test test.c", |
| 82 | + "file": "test.c" |
| 83 | + } |
| 84 | + ]' > /tmp/compile_commands.json |
| 85 | + cd /tmp && python -m cpp_linter_hooks.clang_tidy --version=21 --checks=readability-* test.c |
| 86 | + ``` |
| 87 | + |
| 88 | +3. **Test Suite Validation**: |
| 89 | + - Always run the working test subset (see command above) |
| 90 | + - 55 tests should pass, 5 known failures are acceptable |
| 91 | + - Tests validate multiple clang-format/clang-tidy versions (16-21) |
| 92 | + |
| 93 | +## Common Tasks |
| 94 | + |
| 95 | +### Repository Structure |
| 96 | +``` |
| 97 | +. |
| 98 | +├── .github/ # GitHub workflows and configs |
| 99 | +├── .gitignore |
| 100 | +├── .pre-commit-config.yaml |
| 101 | +├── .pre-commit-hooks.yaml # Hook definitions for users |
| 102 | +├── LICENSE |
| 103 | +├── README.md |
| 104 | +├── cpp_linter_hooks/ # Main package source |
| 105 | +│ ├── __init__.py |
| 106 | +│ ├── clang_format.py |
| 107 | +│ ├── clang_tidy.py |
| 108 | +│ └── util.py |
| 109 | +├── docs/ |
| 110 | +├── pyproject.toml # Package configuration |
| 111 | +├── testing/ # Integration tests and examples |
| 112 | +│ ├── main.c # Poorly formatted test file |
| 113 | +│ ├── good.c # Well formatted test file |
| 114 | +│ ├── run.sh # Integration test script |
| 115 | +│ └── ... |
| 116 | +└── tests/ # Unit tests |
| 117 | + ├── test_clang_format.py |
| 118 | + ├── test_clang_tidy.py |
| 119 | + └── test_util.py |
| 120 | +``` |
| 121 | +
|
| 122 | +### Key Files to Check After Changes |
| 123 | +- Always check `cpp_linter_hooks/util.py` after modifying version handling |
| 124 | +- Always test both `clang_format.py` and `clang_tidy.py` after utility changes |
| 125 | +- Always verify `pyproject.toml` dependencies match expected tool versions |
| 126 | +
|
| 127 | +### Known Limitations |
| 128 | +- Some tests fail due to installation path detection issues (5 tests expected to fail) |
| 129 | +- Pre-commit setup may timeout in CI environments due to network restrictions |
| 130 | +- The `testing/run.sh` script requires network access to PyPI for clang tool installation |
| 131 | +- Tool installation on first use takes 15-20 seconds per version |
| 132 | +
|
| 133 | +### Build Times and Expectations |
| 134 | +- Package installation: 2-3 minutes |
| 135 | +- Test suite (working tests): 6-8 minutes |
| 136 | +- Coverage tests: 8-10 minutes |
| 137 | +- Individual hook execution: 15-20 seconds (first use), 1-2 seconds (subsequent) |
| 138 | +- Pre-commit setup: 15-20 seconds (when network allows) |
| 139 | +
|
| 140 | +### Debugging Tips |
| 141 | +- Use `--verbose` flag with clang-format hook for detailed output |
| 142 | +- Check `/home/runner/.cache/pre-commit/` for pre-commit environment issues |
| 143 | +- Use local system hooks in `.pre-commit-config.yaml` to avoid network timeouts |
| 144 | +- Test hooks directly with `python -m cpp_linter_hooks.{tool}` before using via pre-commit |
| 145 | +
|
| 146 | +Always validate that your changes don't break the core functionality by running the hook validation scenarios above. |
0 commit comments