Skip to content

Commit f32a8a8

Browse files
committed
Initialize testing infrastructure with conftest and pytest configuration, remove obsolete hello.py file, and add test files and fixtures for commit message generation.
1 parent 5243435 commit f32a8a8

8 files changed

Lines changed: 581 additions & 13 deletions

File tree

conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Root conftest file to set up the Python path for testing."""
2+
3+
import os
4+
import sys
5+
from pathlib import Path
6+
7+
# Add the src directory to the Python path
8+
root_dir = Path(__file__).parent.absolute()
9+
sys.path.insert(0, str(root_dir / "src"))

pytest.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[pytest]
2+
# Fail tests immediately if they try to get user input
3+
mock_use_standalone_module = true
4+
# Show more details about test failures
5+
addopts = -v
6+
# Timeout after 5 seconds (to prevent hanging on user input)
7+
timeout = 5

src/blueprint/hello.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Blueprint Tests
2+
3+
This directory contains tests for the Blueprint project.
4+
5+
## Structure
6+
7+
- `conftest.py`: Contains pytest fixtures and shared test configuration.
8+
- `unit/`: Contains unit tests for specific modules.
9+
10+
## Running Tests
11+
12+
Run all tests using pytest:
13+
14+
```bash
15+
# From project root
16+
pytest
17+
18+
# Run with verbose output
19+
pytest -v
20+
21+
# Run specific test file
22+
pytest tests/unit/test_commit_generator.py
23+
```
24+
25+
## Test Fixtures
26+
27+
The `conftest.py` file provides several fixtures that are useful for testing:
28+
29+
- `sample_git_diff`: A sample git diff for testing diff parsing functionality.
30+
- `sample_ai_response`: A sample AI service response for testing commit message generation.
31+
32+
## Writing Tests
33+
34+
When adding new tests:
35+
36+
1. Use the pytest fixture system where appropriate.
37+
2. Mock all external dependencies (subprocess, network calls, etc.).
38+
3. Follow the Arrange-Act-Assert (AAA) pattern in test methods.
39+
4. Add docstrings to test functions that explain what's being tested.
40+
41+
## Test Coverage
42+
43+
To generate a test coverage report:
44+
45+
```bash
46+
# From project root
47+
pytest --cov=blueprint tests/
48+
49+
# Generate HTML report
50+
pytest --cov=blueprint --cov-report=html tests/
51+
```
52+
53+
## Mocking Strategy
54+
55+
- Use `unittest.mock.patch` to mock dependencies like subprocess calls and AIService.
56+
- When testing functions that call git commands, mock subprocess functions.
57+
- For AI service interactions, mock the AIService class and its methods.

tests/conftest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Pytest configuration for Blueprint tests."""
2+
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def sample_git_diff():
8+
"""Return a sample git diff for testing."""
9+
return """diff --git a/src/blueprint/commit_generator.py b/src/blueprint/commit_generator.py
10+
index 123456..789abc 100644
11+
--- a/src/blueprint/commit_generator.py
12+
+++ b/src/blueprint/commit_generator.py
13+
@@ -10,7 +10,7 @@ def get_git_diff(max_chars: int = 5000) -> str:
14+
\"\"\"Get the git diff of staged changes, or unstaged if no staged changes.\"\"\"
15+
try:
16+
diff = subprocess.check_output(["git", "diff", "--cached"], text=True)
17+
- if not diff.strip():
18+
+ if not diff:
19+
diff = subprocess.check_output(["git", "diff"], text=True)
20+
return diff[:max_chars] # Limit to max_chars characters
21+
except subprocess.CalledProcessError:
22+
@@ -50,7 +50,7 @@ def parse_commit_messages(response: str) -> List[str]:
23+
messages = []
24+
for line in response.split("\\n"):
25+
if line.strip().startswith(("1.", "2.", "3.")):
26+
- messages.append(line.split(".", 1)[1])
27+
+ messages.append(line.split(".", 1)[1].strip())
28+
return messages"""
29+
30+
31+
@pytest.fixture
32+
def sample_ai_response():
33+
"""Return a sample AI response for testing."""
34+
return """Here are three commit message options for your changes:
35+
36+
1. Remove strip() from diff check and add strip() to parsed commit messages
37+
2. Fix string handling in git diff and commit message parsing
38+
3. Improve robustness of git diff checking and commit message parsing
39+
40+
These commit messages reflect the changes where you removed a strip() call in the git diff check and added one in the commit message parser."""

tests/run_tests.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
# Helper script to run tests with coverage
3+
4+
# Ensure we're in the project root directory
5+
cd "$(dirname "$0")/.."
6+
7+
# Colors for terminal output
8+
GREEN='\033[0;32m'
9+
YELLOW='\033[1;33m'
10+
RED='\033[0;31m'
11+
NC='\033[0m' # No Color
12+
13+
echo -e "${YELLOW}Running tests with coverage...${NC}"
14+
15+
# Check if pytest-cov and pytest-timeout are installed
16+
if ! python -c "import pytest_cov" 2>/dev/null; then
17+
echo "Installing pytest-cov..."
18+
uv pip install pytest-cov
19+
fi
20+
21+
if ! python -c "import pytest_timeout" 2>/dev/null; then
22+
echo "Installing pytest-timeout..."
23+
uv pip install pytest-timeout
24+
fi
25+
26+
# Create or update pytest.ini to ensure non-interactive tests
27+
cat > pytest.ini << EOF
28+
[pytest]
29+
# Fail tests immediately if they try to get user input
30+
mock_use_standalone_module = true
31+
# Show more details about test failures
32+
addopts = -v
33+
# Timeout after 5 seconds (to prevent hanging on user input)
34+
timeout = 5
35+
EOF
36+
37+
# Run tests with coverage and generate HTML report
38+
# Set PYTHONPATH to ensure blueprint module can be found
39+
PYTHONPATH=./src python -m pytest tests/ --cov=blueprint --cov-report=term --cov-report=html
40+
41+
# Check exit status
42+
if [ $? -eq 0 ]; then
43+
echo -e "${GREEN}Tests passed!${NC}"
44+
echo -e "HTML coverage report generated in htmlcov/"
45+
46+
# Open coverage report on macOS
47+
if [[ "$OSTYPE" == "darwin"* ]]; then
48+
open htmlcov/index.html
49+
fi
50+
else
51+
echo -e "${RED}Tests failed.${NC}"
52+
echo -e "${YELLOW}If tests are hanging or asking for input, check for non-mocked interactive components.${NC}"
53+
fi

0 commit comments

Comments
 (0)