213 lines
6.2 KiB
Python
213 lines
6.2 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
Tests for update_checker.py — 5.3: Update Checker
|
||
|
|
|
||
|
|
Acceptance criteria verified:
|
||
|
|
✓ Compares installed vs latest
|
||
|
|
✓ Reports major/minor/patch updates
|
||
|
|
✓ Flags breaking changes (major)
|
||
|
|
✓ Output: update report
|
||
|
|
"""
|
||
|
|
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
from datetime import datetime
|
||
|
|
from pathlib import Path
|
||
|
|
from unittest.mock import patch, MagicMock
|
||
|
|
|
||
|
|
# Add scripts dir to path
|
||
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "scripts"))
|
||
|
|
|
||
|
|
import update_checker as uc
|
||
|
|
|
||
|
|
|
||
|
|
def test_parse_version():
|
||
|
|
assert uc.parse_version("1.2.3") == (1, 2, 3)
|
||
|
|
assert uc.parse_version("2.0.0") == (2, 0, 0)
|
||
|
|
assert uc.parse_version("0.9.0") == (0, 9, 0)
|
||
|
|
assert uc.parse_version("1.2") == (1, 2, 0)
|
||
|
|
assert uc.parse_version("1") == (1, 0, 0)
|
||
|
|
assert uc.parse_version("invalid") == (0, 0, 0)
|
||
|
|
print("PASS: parse_version")
|
||
|
|
|
||
|
|
|
||
|
|
def test_classify_update_patch():
|
||
|
|
result = uc.classify_update("1.2.3", "1.2.4")
|
||
|
|
assert result is not None
|
||
|
|
assert result['update_type'] == 'patch'
|
||
|
|
assert result['breaking_change'] is False
|
||
|
|
assert result['severity'] == 'low'
|
||
|
|
print("PASS: classify_update_patch")
|
||
|
|
|
||
|
|
|
||
|
|
def test_classify_update_minor():
|
||
|
|
result = uc.classify_update("1.2.3", "1.3.0")
|
||
|
|
assert result is not None
|
||
|
|
assert result['update_type'] == 'minor'
|
||
|
|
assert result['breaking_change'] is False
|
||
|
|
assert result['severity'] == 'medium'
|
||
|
|
print("PASS: classify_update_minor")
|
||
|
|
|
||
|
|
|
||
|
|
def test_classify_update_major():
|
||
|
|
result = uc.classify_update("1.2.3", "2.0.0")
|
||
|
|
assert result is not None
|
||
|
|
assert result['update_type'] == 'major'
|
||
|
|
assert result['breaking_change'] is True
|
||
|
|
assert result['severity'] == 'high'
|
||
|
|
print("PASS: classify_update_major")
|
||
|
|
|
||
|
|
|
||
|
|
def test_classify_update_no_change():
|
||
|
|
result = uc.classify_update("1.2.3", "1.2.3")
|
||
|
|
assert result is None
|
||
|
|
print("PASS: classify_update_no_change")
|
||
|
|
|
||
|
|
|
||
|
|
def test_classify_update_multiple_major():
|
||
|
|
result = uc.classify_update("1.0.0", "3.0.0")
|
||
|
|
assert result is not None
|
||
|
|
assert result['update_type'] == 'major'
|
||
|
|
assert result['breaking_change'] is True
|
||
|
|
print("PASS: classify_update_multiple_major")
|
||
|
|
|
||
|
|
|
||
|
|
def test_text_report_format():
|
||
|
|
updates = [{
|
||
|
|
'package': 'requests',
|
||
|
|
'installed': '2.28.0',
|
||
|
|
'latest': '2.31.0',
|
||
|
|
'update_type': 'minor',
|
||
|
|
'breaking_change': False,
|
||
|
|
'severity': 'medium',
|
||
|
|
}]
|
||
|
|
report = uc.generate_text_report(updates)
|
||
|
|
assert 'DEPENDENCY UPDATE REPORT' in report
|
||
|
|
assert 'requests' in report
|
||
|
|
assert '2.28.0' in report
|
||
|
|
assert '2.31.0' in report
|
||
|
|
assert 'MINOR' in report
|
||
|
|
assert 'MEDIUM' in report
|
||
|
|
print("PASS: text_report_format")
|
||
|
|
|
||
|
|
|
||
|
|
def test_text_report_shows_breaking():
|
||
|
|
updates = [{
|
||
|
|
'package': 'flask',
|
||
|
|
'installed': '2.0.0',
|
||
|
|
'latest': '3.0.0',
|
||
|
|
'update_type': 'major',
|
||
|
|
'breaking_change': True,
|
||
|
|
'severity': 'high',
|
||
|
|
}]
|
||
|
|
report = uc.generate_text_report(updates)
|
||
|
|
assert 'BREAKING CHANGE' in report.upper() or '⚠' in report
|
||
|
|
print("PASS: text_report_shows_breaking")
|
||
|
|
|
||
|
|
|
||
|
|
def test_json_report_structure():
|
||
|
|
updates = [
|
||
|
|
{
|
||
|
|
'package': 'pytest',
|
||
|
|
'installed': '8.0.0',
|
||
|
|
'latest': '8.2.0',
|
||
|
|
'update_type': 'minor',
|
||
|
|
'breaking_change': False,
|
||
|
|
'severity': 'medium',
|
||
|
|
},
|
||
|
|
{
|
||
|
|
'package': 'flask',
|
||
|
|
'installed': '2.0.0',
|
||
|
|
'latest': '3.0.0',
|
||
|
|
'update_type': 'major',
|
||
|
|
'breaking_change': True,
|
||
|
|
'severity': 'high',
|
||
|
|
}
|
||
|
|
]
|
||
|
|
report_json = uc.generate_json_report(updates)
|
||
|
|
data = json.loads(report_json)
|
||
|
|
assert 'generated_at' in data
|
||
|
|
assert data['total_updates'] == 2
|
||
|
|
assert 'summary' in data
|
||
|
|
assert data['summary']['major'] == 1
|
||
|
|
assert data['summary']['minor'] == 1
|
||
|
|
assert data['summary']['breaking'] == 1
|
||
|
|
print("PASS: json_report_structure")
|
||
|
|
|
||
|
|
|
||
|
|
def test_no_updates_report():
|
||
|
|
report = uc.generate_text_report([])
|
||
|
|
assert 'up to date' in report.lower() or 'all packages' in report.lower()
|
||
|
|
print("PASS: no_updates_report")
|
||
|
|
|
||
|
|
|
||
|
|
def test_end_to_end_integration():
|
||
|
|
"""End-to-end: check_updates with mocked data produces valid report."""
|
||
|
|
fake_installed = {
|
||
|
|
"test-pkg-old": "1.0.0",
|
||
|
|
"another-pkg": "2.5.3",
|
||
|
|
}
|
||
|
|
|
||
|
|
def fake_get_latest(pkg):
|
||
|
|
if pkg == "test-pkg-old":
|
||
|
|
return "1.2.4"
|
||
|
|
elif pkg == "another-pkg":
|
||
|
|
return "3.0.0"
|
||
|
|
return None
|
||
|
|
|
||
|
|
with patch('update_checker.get_installed_packages', return_value=fake_installed):
|
||
|
|
with patch('update_checker.get_latest_version', side_effect=fake_get_latest):
|
||
|
|
updates = uc.check_updates()
|
||
|
|
|
||
|
|
assert len(updates) == 2
|
||
|
|
|
||
|
|
test_pkg = next(u for u in updates if u['package'] == 'test-pkg-old')
|
||
|
|
assert test_pkg['update_type'] == 'minor'
|
||
|
|
assert test_pkg['breaking_change'] is False
|
||
|
|
|
||
|
|
another = next(u for u in updates if u['package'] == 'another-pkg')
|
||
|
|
assert another['update_type'] == 'major'
|
||
|
|
assert another['breaking_change'] is True
|
||
|
|
|
||
|
|
report = uc.generate_text_report(updates)
|
||
|
|
assert 'DEPENDENCY UPDATE REPORT' in report
|
||
|
|
assert 'MINOR' in report
|
||
|
|
assert 'BREAKING CHANGE' in report.upper()
|
||
|
|
|
||
|
|
print(f"PASS: end_to_end_integration ({len(updates)} updates)")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
passed = 0
|
||
|
|
failed = 0
|
||
|
|
tests = [
|
||
|
|
test_parse_version,
|
||
|
|
test_classify_update_patch,
|
||
|
|
test_classify_update_minor,
|
||
|
|
test_classify_update_major,
|
||
|
|
test_classify_update_no_change,
|
||
|
|
test_classify_update_multiple_major,
|
||
|
|
test_text_report_format,
|
||
|
|
test_text_report_shows_breaking,
|
||
|
|
test_json_report_structure,
|
||
|
|
test_no_updates_report,
|
||
|
|
test_end_to_end_integration,
|
||
|
|
]
|
||
|
|
for test_func in tests:
|
||
|
|
try:
|
||
|
|
test_func()
|
||
|
|
passed += 1
|
||
|
|
except AssertionError as e:
|
||
|
|
print(f"FAIL: {test_func.__name__} — {e}")
|
||
|
|
failed += 1
|
||
|
|
except Exception as e:
|
||
|
|
print(f"ERROR: {test_func.__name__} — {e}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|
||
|
|
failed += 1
|
||
|
|
print(f"\n{passed} passed, {failed} failed")
|
||
|
|
sys.exit(0 if failed == 0 else 1)
|