ming commited on
Commit
c35817c
Β·
1 Parent(s): 59650d1

Add comprehensive pre-commit hook documentation

Browse files

πŸ“š PRE_COMMIT_HOOK.md:
- Complete guide to the pre-commit hook system
- Explains how the 'ALL TESTS MUST PASS' rule works
- Provides usage examples and troubleshooting
- Documents best practices and benefits
- Includes emergency bypass procedures

βœ… Key Features Documented:
- Automatic test execution before commits
- Test blocking for failed tests
- Comprehensive test runner script
- Quality assurance benefits
- Team collaboration advantages

This ensures all developers understand and can effectively use the pre-commit hook system.

Files changed (1) hide show
  1. PRE_COMMIT_HOOK.md +145 -0
PRE_COMMIT_HOOK.md ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Pre-Commit Hook System
2
+
3
+ ## Overview
4
+
5
+ This project now enforces the rule: **ALL TESTS MUST PASS BEFORE COMMITTING**. This is implemented through a Git pre-commit hook that automatically runs tests before allowing any commit.
6
+
7
+ ## What It Does
8
+
9
+ ### βœ… Pre-Commit Hook (`.git/hooks/pre-commit`)
10
+ - **Automatically runs tests** before every `git commit`
11
+ - **Blocks commits** if any tests fail
12
+ - **Provides helpful error messages** and guidance
13
+ - **Can be bypassed** with `--no-verify` (not recommended)
14
+
15
+ ### βœ… Test Runner Script (`scripts/run-tests.sh`)
16
+ - **Comprehensive test suite runner** for manual testing
17
+ - **Runs different test categories** separately
18
+ - **Provides detailed reporting** and success/failure tracking
19
+ - **Helps identify** which specific tests are failing
20
+
21
+ ## How It Works
22
+
23
+ ### 1. Automatic Test Execution
24
+ Every time you run `git commit`, the pre-commit hook:
25
+ 1. Runs core tests (main, middleware, logging, errors)
26
+ 2. Checks if all tests pass
27
+ 3. Allows commit if tests pass
28
+ 4. Blocks commit if tests fail
29
+
30
+ ### 2. Test Categories
31
+ - **Core Tests**: Always run in pre-commit hook
32
+ - `test_main.py` - Main application tests
33
+ - `test_middleware.py` - Request middleware tests
34
+ - `test_logging.py` - Logging system tests
35
+ - `test_errors.py` - Error handling tests
36
+
37
+ - **Full Test Suite**: Available via `scripts/run-tests.sh`
38
+ - All test files including API, services, 502 prevention, etc.
39
+
40
+ ## Usage
41
+
42
+ ### Normal Development Workflow
43
+ ```bash
44
+ # Make your changes
45
+ git add .
46
+ git commit -m "Your commit message"
47
+ # Tests run automatically - commit only succeeds if tests pass
48
+ ```
49
+
50
+ ### Manual Test Running
51
+ ```bash
52
+ # Run comprehensive test suite
53
+ ./scripts/run-tests.sh
54
+
55
+ # Run specific test files
56
+ python -m pytest tests/test_main.py -v
57
+
58
+ # Run all tests
59
+ python -m pytest tests/ -v
60
+ ```
61
+
62
+ ### Emergency Bypass (NOT RECOMMENDED)
63
+ ```bash
64
+ # Only use in emergencies - bypasses all tests
65
+ git commit --no-verify -m "Emergency commit"
66
+ ```
67
+
68
+ ## Benefits
69
+
70
+ ### πŸ›‘οΈ Quality Assurance
71
+ - **Prevents broken code** from being committed
72
+ - **Enforces test-driven development**
73
+ - **Maintains code quality standards**
74
+ - **Prevents regressions** from being introduced
75
+
76
+ ### πŸš€ Development Efficiency
77
+ - **Catches issues early** before they reach the repository
78
+ - **Provides immediate feedback** on code changes
79
+ - **Reduces debugging time** in production
80
+ - **Maintains consistent code quality**
81
+
82
+ ### πŸ“Š Team Collaboration
83
+ - **Ensures all team members** follow the same standards
84
+ - **Prevents "it works on my machine"** issues
85
+ - **Maintains repository stability**
86
+ - **Facilitates code reviews**
87
+
88
+ ## Configuration
89
+
90
+ ### Pre-Commit Hook Location
91
+ - **File**: `.git/hooks/pre-commit`
92
+ - **Permissions**: Executable (`chmod +x`)
93
+ - **Scope**: Project-specific (not shared via git)
94
+
95
+ ### Test Runner Location
96
+ - **File**: `scripts/run-tests.sh`
97
+ - **Permissions**: Executable (`chmod +x`)
98
+ - **Scope**: Project-wide (shared via git)
99
+
100
+ ## Troubleshooting
101
+
102
+ ### Tests Fail in Pre-Commit Hook
103
+ 1. **Fix the failing tests** first
104
+ 2. **Run tests manually** to verify: `python -m pytest tests/ -v`
105
+ 3. **Try committing again**
106
+
107
+ ### Pre-Commit Hook Not Working
108
+ 1. **Check permissions**: `ls -la .git/hooks/pre-commit`
109
+ 2. **Make executable**: `chmod +x .git/hooks/pre-commit`
110
+ 3. **Verify content**: `cat .git/hooks/pre-commit`
111
+
112
+ ### Need to Disable Temporarily
113
+ ```bash
114
+ # Rename the hook to disable it
115
+ mv .git/hooks/pre-commit .git/hooks/pre-commit.disabled
116
+
117
+ # Rename back to enable it
118
+ mv .git/hooks/pre-commit.disabled .git/hooks/pre-commit
119
+ ```
120
+
121
+ ## Best Practices
122
+
123
+ ### βœ… Do
124
+ - **Write tests** for all new features
125
+ - **Fix failing tests** before committing
126
+ - **Run full test suite** before major changes
127
+ - **Use descriptive commit messages**
128
+
129
+ ### ❌ Don't
130
+ - **Bypass tests** with `--no-verify` unless absolutely necessary
131
+ - **Commit broken code** even temporarily
132
+ - **Ignore test failures** or warnings
133
+ - **Skip writing tests** for new functionality
134
+
135
+ ## Success Metrics
136
+
137
+ - βœ… **Zero broken commits** in repository history
138
+ - βœ… **All tests pass** before every commit
139
+ - βœ… **Consistent code quality** across all changes
140
+ - βœ… **Reduced production bugs** due to early detection
141
+ - βœ… **Faster development cycles** with immediate feedback
142
+
143
+ ---
144
+
145
+ **Remember**: The pre-commit hook is your friend! It helps maintain code quality and prevents issues from reaching production. Embrace it as part of your development workflow.