| 1 |
# Pre-Commit Test Integration |
| 2 |
|
| 3 |
## Overview |
| 4 |
|
| 5 |
The test suite is now automatically executed as part of the pre-commit hook, ensuring all commits maintain code quality and passing tests. |
| 6 |
|
| 7 |
## What Happens on Commit |
| 8 |
|
| 9 |
When you run `git commit`, the pre-commit hook now performs **5 validation steps**: |
| 10 |
|
| 11 |
``` |
| 12 |
1. Python syntax validation (all files) |
| 13 |
2. Common issue detection (modified files) |
| 14 |
3. Core module import testing |
| 15 |
4. Test suite execution (37 tests) ← NEW! |
| 16 |
5. Secret/credential detection |
| 17 |
``` |
| 18 |
|
| 19 |
## Test Execution |
| 20 |
|
| 21 |
```bash |
| 22 |
4. Running test suite... |
| 23 |
============================= test session starts ============================== |
| 24 |
platform linux -- Python 3.13.7, pytest-8.4.2, pluggy-1.6.0 |
| 25 |
rootdir: /home/storm/git/fenrir |
| 26 |
configfile: pytest.ini |
| 27 |
collected 37 items |
| 28 |
|
| 29 |
tests/integration/test_remote_control.py .................... [ 54%] |
| 30 |
tests/unit/test_settings_validation.py ................. [100%] |
| 31 |
|
| 32 |
============================== 37 passed in 0.44s ============================== |
| 33 |
✓ All tests passed |
| 34 |
``` |
| 35 |
|
| 36 |
**Performance:** Tests complete in **< 1 second**, adding minimal overhead to the commit process. |
| 37 |
|
| 38 |
## Behavior |
| 39 |
|
| 40 |
### ✅ Tests Pass - Commit Allowed |
| 41 |
```bash |
| 42 |
$ git commit -m "Add new feature" |
| 43 |
|
| 44 |
Fenrir Pre-commit Validation |
| 45 |
================================== |
| 46 |
1. Validating Python syntax... |
| 47 |
✓ Syntax validation passed |
| 48 |
|
| 49 |
2. Checking modified files... |
| 50 |
✓ No common issues found |
| 51 |
|
| 52 |
3. Testing core module imports... |
| 53 |
✓ Core module imports successful |
| 54 |
|
| 55 |
4. Running test suite... |
| 56 |
✓ All tests passed (37 passed in 0.44s) |
| 57 |
|
| 58 |
5. Checking for potential secrets... |
| 59 |
✓ No potential secrets found |
| 60 |
|
| 61 |
================================================== |
| 62 |
✓ All pre-commit validations passed |
| 63 |
Commit allowed to proceed |
| 64 |
``` |
| 65 |
|
| 66 |
### ❌ Tests Fail - Commit Blocked |
| 67 |
```bash |
| 68 |
$ git commit -m "Broken feature" |
| 69 |
|
| 70 |
Fenrir Pre-commit Validation |
| 71 |
================================== |
| 72 |
[... earlier checks pass ...] |
| 73 |
|
| 74 |
4. Running test suite... |
| 75 |
✗ Test suite failed |
| 76 |
Run: pytest tests/ -v (to see details) |
| 77 |
|
| 78 |
================================================== |
| 79 |
✗ Pre-commit validation failed |
| 80 |
Commit blocked - please fix issues above |
| 81 |
|
| 82 |
Quick fixes: |
| 83 |
• Python syntax: python3 tools/validate_syntax.py --fix |
| 84 |
• Run tests: pytest tests/ -v |
| 85 |
• Review flagged files manually |
| 86 |
• Re-run commit after fixes |
| 87 |
``` |
| 88 |
|
| 89 |
## Installation |
| 90 |
|
| 91 |
The pre-commit hook is installed via: |
| 92 |
|
| 93 |
```bash |
| 94 |
# One-time setup |
| 95 |
./tools/install_validation_hook.sh |
| 96 |
|
| 97 |
# Or manually |
| 98 |
ln -sf ../../tools/pre-commit-hook .git/hooks/pre-commit |
| 99 |
``` |
| 100 |
|
| 101 |
## Requirements |
| 102 |
|
| 103 |
### Required |
| 104 |
- Python 3.7+ |
| 105 |
- Git repository |
| 106 |
|
| 107 |
### Optional but Recommended |
| 108 |
- `pytest` - For running tests (gracefully skipped if not installed) |
| 109 |
|
| 110 |
If pytest is not installed, you'll see: |
| 111 |
``` |
| 112 |
4. Running test suite... |
| 113 |
⚠ pytest not installed - skipping tests |
| 114 |
Install with: pip install pytest |
| 115 |
Or full test suite: pip install -r tests/requirements.txt |
| 116 |
``` |
| 117 |
|
| 118 |
The commit will still proceed, but tests won't run. |
| 119 |
|
| 120 |
## Benefits |
| 121 |
|
| 122 |
1. **Catch bugs early** - Tests run before code reaches the repository |
| 123 |
2. **Maintain quality** - Broken code can't be committed |
| 124 |
3. **Fast feedback** - Tests complete in < 1 second |
| 125 |
4. **Zero overhead** - Gracefully degrades if pytest isn't installed |
| 126 |
5. **Confidence** - Know that all commits pass tests |
| 127 |
|
| 128 |
## Bypassing the Hook |
| 129 |
|
| 130 |
**Not recommended**, but if you need to commit without running tests: |
| 131 |
|
| 132 |
```bash |
| 133 |
# Skip all pre-commit checks (use with caution!) |
| 134 |
git commit --no-verify -m "Emergency hotfix" |
| 135 |
``` |
| 136 |
|
| 137 |
**Warning:** Only use `--no-verify` for legitimate emergencies. Bypassing tests defeats their purpose. |
| 138 |
|
| 139 |
## Troubleshooting |
| 140 |
|
| 141 |
### Tests fail on commit but pass manually |
| 142 |
|
| 143 |
```bash |
| 144 |
# Check environment matches |
| 145 |
cd /home/storm/git/fenrir |
| 146 |
pytest tests/ -v |
| 147 |
|
| 148 |
# Verify PYTHONPATH |
| 149 |
echo $PYTHONPATH |
| 150 |
``` |
| 151 |
|
| 152 |
### Hook doesn't run tests |
| 153 |
|
| 154 |
```bash |
| 155 |
# Check pytest is installed |
| 156 |
pytest --version |
| 157 |
|
| 158 |
# Install if missing |
| 159 |
pip install pytest |
| 160 |
|
| 161 |
# Or full test dependencies |
| 162 |
pip install -r tests/requirements.txt |
| 163 |
``` |
| 164 |
|
| 165 |
### Hook takes too long |
| 166 |
|
| 167 |
The test suite is designed to run in < 1 second. If it's slower: |
| 168 |
|
| 169 |
```bash |
| 170 |
# Check test timing |
| 171 |
pytest tests/ --durations=10 |
| 172 |
|
| 173 |
# Look for slow tests (should all be < 100ms) |
| 174 |
``` |
| 175 |
|
| 176 |
## Statistics |
| 177 |
|
| 178 |
- **Tests Executed:** 37 |
| 179 |
- **Execution Time:** 0.44 seconds |
| 180 |
- **Pass Rate:** 100% |
| 181 |
- **Coverage:** Unit tests (17) + Integration tests (20) |
| 182 |
|
| 183 |
## Future Enhancements |
| 184 |
|
| 185 |
Possible additions to pre-commit validation: |
| 186 |
|
| 187 |
- **Coverage threshold** - Require minimum test coverage percentage |
| 188 |
- **Performance regression** - Warn if tests get slower |
| 189 |
- **Incremental testing** - Only run tests for modified code |
| 190 |
- **Parallel execution** - Use `pytest -n auto` (requires pytest-xdist) |
| 191 |
|
| 192 |
## Related Documentation |
| 193 |
|
| 194 |
- `tests/README.md` - Test strategy overview |
| 195 |
- `tests/TESTING_GUIDE.md` - Comprehensive testing guide |
| 196 |
- `TESTING_SUMMARY.md` - Test implementation summary |
| 197 |
- `tools/pre-commit-hook` - Pre-commit hook source code |
| 198 |
|
| 199 |
## Summary |
| 200 |
|
| 201 |
Adding tests to the pre-commit hook ensures: |
| 202 |
- ✅ All commits have passing tests |
| 203 |
- ✅ Regressions are caught immediately |
| 204 |
- ✅ Code quality is maintained |
| 205 |
- ✅ Minimal performance impact (< 1 second) |
| 206 |
- ✅ Graceful degradation without pytest |
| 207 |
|
| 208 |
**Result:** Higher code quality with virtually zero developer friction. |