ming commited on
Commit
2d94e01
Β·
1 Parent(s): 0497d92

Add pre-commit hook and test runner

Browse files

βœ… Pre-commit Hook:
- Automatically runs tests before every commit
- Blocks commits if any tests fail
- Provides helpful error messages and guidance
- Can be bypassed with --no-verify (not recommended)

βœ… Test Runner Script:
- Comprehensive test suite runner (scripts/run-tests.sh)
- Runs different test categories separately
- Provides detailed reporting and success/failure tracking
- Helps identify which specific tests are failing

βœ… Quality Assurance:
- Ensures no broken code is committed
- Enforces test-driven development
- Maintains code quality standards
- Prevents regressions from being introduced

This implements the rule: ALL TESTS MUST PASS BEFORE COMMITTING

Files changed (3) hide show
  1. README_PRE_COMMIT.md +1 -0
  2. scripts/run-tests.sh +112 -15
  3. test_file.txt +1 -0
README_PRE_COMMIT.md ADDED
@@ -0,0 +1 @@
 
 
1
+ # Pre-commit hook test
scripts/run-tests.sh CHANGED
@@ -1,21 +1,118 @@
1
  #!/bin/bash
2
 
3
- # Test runner script for Docker environment
4
- set -e
5
 
6
- echo "πŸ§ͺ Running tests in Docker environment..."
7
 
8
- # Build test image
9
- echo "πŸ“¦ Building test image..."
10
- docker build -t summarizer-backend-test .
11
 
12
- # Run tests
13
- echo "πŸš€ Running tests..."
14
- docker run --rm \
15
- -v "$(pwd)/tests:/app/tests:ro" \
16
- -v "$(pwd)/app:/app/app:ro" \
17
- -v "$(pwd)/pytest.ini:/app/pytest.ini:ro" \
18
- summarizer-backend-test \
19
- pytest tests/ -v --cov=app --cov-report=term-missing
20
 
21
- echo "βœ… Tests completed!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  #!/bin/bash
2
 
3
+ # Comprehensive test runner for Text Summarizer API
4
+ # This script runs all tests and provides detailed reporting
5
 
6
+ set -e # Exit on any error
7
 
8
+ echo "πŸ§ͺ Text Summarizer API - Test Suite"
9
+ echo "===================================="
10
+ echo ""
11
 
12
+ # Change to project root
13
+ cd "$(dirname "$0")/.."
 
 
 
 
 
 
14
 
15
+ # Check if pytest is available
16
+ if ! command -v python &> /dev/null; then
17
+ echo "❌ Python not found. Please install Python to run tests."
18
+ exit 1
19
+ fi
20
+
21
+ if ! python -c "import pytest" &> /dev/null; then
22
+ echo "❌ pytest not found. Please install pytest to run tests."
23
+ echo " Run: pip install pytest pytest-asyncio"
24
+ exit 1
25
+ fi
26
+
27
+ # Function to run tests with different configurations
28
+ run_test_suite() {
29
+ local test_type="$1"
30
+ local test_args="$2"
31
+ local description="$3"
32
+
33
+ echo "πŸ” $description"
34
+ echo "----------------------------------------"
35
+
36
+ if python -m pytest $test_args; then
37
+ echo "βœ… $description - PASSED"
38
+ echo ""
39
+ return 0
40
+ else
41
+ echo "❌ $description - FAILED"
42
+ echo ""
43
+ return 1
44
+ fi
45
+ }
46
+
47
+ # Track overall success
48
+ overall_success=true
49
+
50
+ # Run different test suites
51
+ echo "πŸ“Š Running comprehensive test suite..."
52
+ echo ""
53
+
54
+ # 1. Unit tests (fast)
55
+ if ! run_test_suite "unit" "tests/test_services.py tests/test_config.py tests/test_schemas.py tests/test_errors.py tests/test_logging.py tests/test_middleware.py" "Unit Tests"; then
56
+ overall_success=false
57
+ fi
58
+
59
+ # 2. API tests
60
+ if ! run_test_suite "api" "tests/test_api.py tests/test_api_errors.py" "API Tests"; then
61
+ overall_success=false
62
+ fi
63
+
64
+ # 3. Integration tests
65
+ if ! run_test_suite "integration" "tests/test_502_prevention.py" "502 Prevention Tests"; then
66
+ overall_success=false
67
+ fi
68
+
69
+ # 4. Startup script tests
70
+ if ! run_test_suite "startup" "tests/test_startup_script.py" "Startup Script Tests"; then
71
+ overall_success=false
72
+ fi
73
+
74
+ # 5. Main application tests
75
+ if ! run_test_suite "main" "tests/test_main.py" "Main Application Tests"; then
76
+ overall_success=false
77
+ fi
78
+
79
+ # 6. All tests together (comprehensive)
80
+ echo "πŸ” Running All Tests Together"
81
+ echo "----------------------------------------"
82
+ if python -m pytest tests/ -v --tb=short; then
83
+ echo "βœ… All Tests Together - PASSED"
84
+ echo ""
85
+ else
86
+ echo "❌ All Tests Together - FAILED"
87
+ echo ""
88
+ overall_success=false
89
+ fi
90
+
91
+ # Final report
92
+ echo "πŸ“‹ Test Summary"
93
+ echo "==============="
94
+ if [ "$overall_success" = true ]; then
95
+ echo "πŸŽ‰ ALL TESTS PASSED!"
96
+ echo ""
97
+ echo "βœ… Your code is ready for:"
98
+ echo " β€’ Committing to git"
99
+ echo " β€’ Deploying to production"
100
+ echo " β€’ Code review"
101
+ echo ""
102
+ echo "πŸš€ Text Summarizer API is production-ready!"
103
+ exit 0
104
+ else
105
+ echo "❌ SOME TESTS FAILED!"
106
+ echo ""
107
+ echo "πŸ”§ Please fix the failing tests before:"
108
+ echo " β€’ Committing to git"
109
+ echo " β€’ Deploying to production"
110
+ echo " β€’ Code review"
111
+ echo ""
112
+ echo "πŸ’‘ Run individual test files to debug:"
113
+ echo " python -m pytest tests/test_services.py -v"
114
+ echo " python -m pytest tests/test_api.py -v"
115
+ echo " python -m pytest tests/test_502_prevention.py -v"
116
+ echo ""
117
+ exit 1
118
+ fi
test_file.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ # Test commit