Skip to content

Commit 44770ff

Browse files
Merge pull request #4 from kosli-dev/tests
Add unit tests for the code-review script
2 parents e633643 + e951d5e commit 44770ff

File tree

7 files changed

+1539
-5
lines changed

7 files changed

+1539
-5
lines changed

.github/workflows/test.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Run Tests
2+
3+
on:
4+
push:
5+
paths:
6+
- 'code-review/**'
7+
pull_request:
8+
paths:
9+
- 'code-review/**'
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: '3.11'
23+
24+
- name: Install dependencies
25+
run: |
26+
cd code-review
27+
python -m pip install --upgrade pip
28+
pip install -r test_requirements.txt
29+
30+
- name: Run tests
31+
run: |
32+
cd code-review
33+
python run_tests.py

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
.idea/
22
.vscode/
3+
4+
# Python virtual environments
5+
venv/
6+
**/venv/
7+
8+
# Python cache directories
9+
__pycache__/
10+
**/__pycache__/
11+
12+
# Pytest cache
13+
.pytest_cache/
14+
**/.pytest_cache/

code-review/Makefile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.PHONY: test clean
2+
3+
# Default target
4+
test: setup-venv install-test-deps run-tests
5+
6+
# Check if we're in a virtual environment and create one if not
7+
setup-venv:
8+
@echo "Checking virtual environment..."
9+
@if [ -z "$$VIRTUAL_ENV" ]; then \
10+
echo "No virtual environment detected. Creating one..."; \
11+
python3 -m venv venv; \
12+
echo "Virtual environment created. Please activate it with: source venv/bin/activate"; \
13+
echo "Then run 'make test' again."; \
14+
exit 1; \
15+
else \
16+
echo "Virtual environment detected: $$VIRTUAL_ENV"; \
17+
fi
18+
19+
# Install test requirements
20+
install-test-deps:
21+
@echo "Installing test requirements..."
22+
@pip install -r test_requirements.txt
23+
24+
# Run the tests
25+
run-tests:
26+
@echo "Running tests..."
27+
@python run_tests.py
28+
29+
# Clean up generated files
30+
clean:
31+
@echo "Cleaning up..."
32+
@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
33+
@find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
34+
@find . -name "*.pyc" -delete 2>/dev/null || true
35+
@echo "Cleanup complete."
36+
37+
# Help target
38+
help:
39+
@echo "Available targets:"
40+
@echo " test - Setup virtual environment, install dependencies, and run tests"
41+
@echo " setup-venv - Check/create virtual environment"
42+
@echo " install-test-deps - Install test requirements"
43+
@echo " run-tests - Run the test suite"
44+
@echo " clean - Remove Python cache files"
45+
@echo " help - Show this help message"

code-review/main.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,21 @@ def evaluate_attestation(commit_hash, attestation):
106106
"pass": False,
107107
"reason": "",
108108
"attestation_url": attestation.get("html_url", ""),
109+
"review_type": "Pull request",
109110
}
110111
pr_url = pull_requests[0].get("url", "") if pull_requests else ""
111112
if pr_url:
112113
result["pr_url"] = pr_url
113-
result["pr_number"] = int(pr_url.split('/')[-1])
114+
result["pr_number"] = int(pr_url.split("/")[-1])
114115
result["review_status"] = pull_requests[0].get("state", "")
115-
result["pr_approvers"] = [approver["username"] for approver in pull_requests[0].get("approvers", [])]
116-
result["review_type"] = "Pull request"
116+
result["pr_approvers"] = sorted(
117+
list(
118+
{
119+
approver["username"].strip()
120+
for approver in pull_requests[0].get("approvers", [])
121+
}
122+
)
123+
)
117124

118125
git_commit_info = attestation.get("git_commit_info", "")
119126
if git_commit_info and git_commit_info["sha1"] == commit_hash:
@@ -128,11 +135,11 @@ def evaluate_attestation(commit_hash, attestation):
128135
result["commit_timestamp"] = commit.get("timestamp", "")
129136
break # stop after finding the match
130137

131-
132138
att_type = attestation.get("attestation_type")
133139
is_compliant = attestation.get("is_compliant", False)
134140

135141
if att_type == "override":
142+
result["review_type"] = "Override"
136143
if is_compliant:
137144
result["pass"] = True
138145
result["reason"] = "Overridden as compliant"
@@ -255,7 +262,9 @@ def report_code_review_attestation(
255262
return response.json()
256263
except requests.exceptions.RequestException as e:
257264
print(f"Error making API request: {e}", file=sys.stderr)
258-
print(f"Response: {response.text}", file=sys.stderr)
265+
# Only try to access response.text if response exists
266+
if "response" in locals():
267+
print(f"Response: {response.text}", file=sys.stderr)
259268
sys.exit(1)
260269
finally:
261270
# Ensure the file is closed

code-review/run_tests.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Script to run the unit tests with specific configuration options.
4+
"""
5+
6+
import subprocess
7+
import sys
8+
9+
10+
def run_tests():
11+
"""Run the unit tests with specific configuration."""
12+
13+
# Command line options for pytest
14+
pytest_options = [
15+
"python3",
16+
"-m",
17+
"pytest",
18+
"test_main.py", # Test file to run
19+
"-v", # verbose output
20+
"--tb=short", # short traceback format
21+
"--maxfail=0", # Run all tests (don't stop on first failure)
22+
"--disable-warnings", # Suppress warnings
23+
"--no-header", # Don't show pytest header
24+
"-s", # Don't capture output (allows print statements and full error display)
25+
]
26+
27+
print("Running unit tests ...")
28+
print("=" * 60)
29+
30+
try:
31+
result = subprocess.run(pytest_options, check=False)
32+
return result.returncode
33+
except FileNotFoundError:
34+
print("Error: pytest not found. Please install it with: pip install pytest")
35+
return 1
36+
37+
38+
def run_tests_with_custom_order():
39+
"""Run tests with explicit ordering using pytest-ordering plugin."""
40+
41+
pytest_options = [
42+
"python3",
43+
"-m",
44+
"pytest",
45+
"test_main.py",
46+
"-v",
47+
"--tb=short",
48+
"--maxfail=0",
49+
"--durations=10",
50+
]
51+
52+
try:
53+
result = subprocess.run(pytest_options, check=False)
54+
return result.returncode
55+
except FileNotFoundError:
56+
print("Error: pytest not found. Please install it with: pip install pytest")
57+
return 1
58+
59+
60+
if __name__ == "__main__":
61+
print("Unit Test Runner for evaluate_attestation function")
62+
print("=" * 60)
63+
64+
# Run tests with default configuration
65+
exit_code = run_tests()
66+
67+
if exit_code == 0:
68+
print("\n✅ All tests passed!")
69+
else:
70+
print(f"\n❌ Some tests failed (exit code: {exit_code})")
71+
72+
sys.exit(exit_code)

0 commit comments

Comments
 (0)