Compare commits
1 Commits
step35/124
...
step35/132
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1470b44c3b |
@@ -1,169 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test Coverage Checker — 6.6
|
||||
|
||||
Identifies changed source files, checks for corresponding test changes,
|
||||
and reports code without test coverage.
|
||||
|
||||
Usage:
|
||||
python3 scripts/test_coverage_checker.py
|
||||
python3 scripts/test_coverage_checker.py --format json
|
||||
python3 scripts/test_coverage_checker.py --compare HEAD~1 # Compare against a specific ref
|
||||
|
||||
Acceptance:
|
||||
- Identifies changed source files (git diff --name-only HEAD)
|
||||
- Checks for corresponding test changes (matches source→test file mapping)
|
||||
- Reports: code without tests (lists coverage gaps)
|
||||
- Output: coverage gap (structured text/JSON)
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import List, Tuple, Optional
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def run_git_diff(ref: str = "HEAD") -> List[str]:
|
||||
"""Return list of changed file paths relative to given ref."""
|
||||
result = subprocess.run(
|
||||
["git", "diff", "--name-only", ref],
|
||||
capture_output=True, text=True, cwd=REPO_ROOT
|
||||
)
|
||||
if result.returncode != 0:
|
||||
print(f"ERROR: git diff failed: {result.stderr}")
|
||||
sys.exit(1)
|
||||
return [p for p in result.stdout.splitlines() if p.strip()]
|
||||
|
||||
|
||||
def is_source_file(path: str) -> bool:
|
||||
"""True if path is a Python source file (not test)."""
|
||||
return path.endswith(".py") and not path.startswith("tests/") and "/test" not in Path(path).name
|
||||
|
||||
|
||||
def is_test_file(path: str) -> bool:
|
||||
"""True if path is a test file."""
|
||||
if not path.endswith(".py"):
|
||||
return False
|
||||
name = Path(path).name
|
||||
# Test files: test_*.py or *_test.py or in tests/ directory
|
||||
return (name.startswith("test_") or name.endswith("_test.py") or path.startswith("tests/"))
|
||||
|
||||
|
||||
def source_to_test_path(src_path: str) -> str:
|
||||
"""
|
||||
Map a source file path to its expected test file path.
|
||||
Convention: scripts/<name>.py -> tests/test_<name>.py
|
||||
<module>.py -> tests/test_<module>.py
|
||||
"""
|
||||
name = Path(src_path).name
|
||||
stem = Path(name).stem # without .py
|
||||
# Common mapping: script name -> test_ prefix in tests/
|
||||
test_name = f"test_{stem}.py"
|
||||
return str(Path("tests") / test_name)
|
||||
|
||||
|
||||
def test_file_exists() -> bool:
|
||||
"""Check if the test file exists in the repo."""
|
||||
return (REPO_ROOT / test_rel).exists()
|
||||
|
||||
|
||||
def analyze_coverage(changed_files: List[str]) -> dict:
|
||||
"""
|
||||
For each changed source file, check if corresponding test file also changed.
|
||||
Returns structured coverage gap report.
|
||||
"""
|
||||
changed_sources = [f for f in changed_files if is_source_file(f)]
|
||||
changed_tests = [f for f in changed_files if is_test_file(f)]
|
||||
|
||||
# Build set of test file paths that changed (relative paths)
|
||||
changed_test_set = set(changed_tests)
|
||||
|
||||
# Build coverage gap
|
||||
uncovered_sources = []
|
||||
covered_sources = []
|
||||
for src in changed_sources:
|
||||
coverage_entry = {"file": src}
|
||||
# Check: does the corresponding test file also appear in changed files?
|
||||
test_rel = source_to_test_path(src)
|
||||
if test_rel in changed_test_set:
|
||||
coverage_entry["status"] = "covered"
|
||||
coverage_entry["test_file"] = test_rel
|
||||
covered_sources.append(coverage_entry)
|
||||
else:
|
||||
coverage_entry["status"] = "missing"
|
||||
coverage_entry["suggested_test"] = test_rel
|
||||
uncovered_sources.append(coverage_entry)
|
||||
|
||||
return {
|
||||
"repo": REPO_ROOT.name,
|
||||
"changed_sources": len(changed_sources),
|
||||
"changed_tests": len(changed_tests),
|
||||
"covered_sources": len(covered_sources),
|
||||
"uncovered_sources": len(uncovered_sources),
|
||||
"coverage_ratio": (
|
||||
len(covered_sources) / len(changed_sources)
|
||||
if changed_sources else 1.0
|
||||
),
|
||||
"covered": covered_sources,
|
||||
"uncovered": uncovered_sources,
|
||||
"all_changed": changed_files,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Test Coverage Checker")
|
||||
parser.add_argument("--format", choices=["text", "json"], default="text",
|
||||
help="Output format")
|
||||
parser.add_argument("--compare", default="HEAD",
|
||||
help="Git ref to compare against (default: HEAD)")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Step 1: Identify changed files
|
||||
print(f"Scanning changes vs {args.compare}...")
|
||||
changed_files = run_git_diff(args.compare)
|
||||
if not changed_files:
|
||||
print("No changed files detected.")
|
||||
sys.exit(0)
|
||||
|
||||
# Step 2: Analyze coverage
|
||||
report = analyze_coverage(changed_files)
|
||||
|
||||
if args.format == "json":
|
||||
print(json.dumps(report, indent=2))
|
||||
sys.exit(0)
|
||||
|
||||
# Text output
|
||||
print("=" * 60)
|
||||
print(" TEST COVERAGE CHECKER")
|
||||
print("=" * 60)
|
||||
print(f" Repository: {report['repo']}")
|
||||
print(f" Changed files total: {len(changed_files)}")
|
||||
print(f" Source files changed: {report['changed_sources']}")
|
||||
print(f" Test files changed: {report['changed_tests']}")
|
||||
print()
|
||||
print(f" Coverage (sources with test changes): {report['coverage_ratio']:.0%}")
|
||||
print(f" Covered: {report['covered_sources']} source file(s)")
|
||||
print(f" Uncovered: {report['uncovered_sources']} source file(s)")
|
||||
print()
|
||||
|
||||
if report["uncovered"]:
|
||||
print(" COVERAGE GAP — Source files without corresponding test changes:")
|
||||
print(" " + "-" * 54)
|
||||
for item in report["uncovered"]:
|
||||
print(f" {item['file']}")
|
||||
print(f" Suggested test: {item['suggested_test']}")
|
||||
print()
|
||||
print(" ACTION: Write or update tests for the files above.")
|
||||
sys.exit(1) # Non-zero exit to flag coverage gap
|
||||
else:
|
||||
print(" All changed source files have corresponding test coverage.")
|
||||
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
288
scripts/genome_diff.py
Executable file
288
scripts/genome_diff.py
Executable file
@@ -0,0 +1,288 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Codebase Genome Diff — Detect structural changes between two versions.
|
||||
|
||||
Compares two git refs (commits, branches, tags) and produces a human-readable
|
||||
report of structural changes:
|
||||
• Added/removed/renamed files
|
||||
• Changed functions/classes (signature modifications)
|
||||
• New dependencies (imports, requirements, etc.)
|
||||
|
||||
Usage:
|
||||
python3 scripts/genome_diff.py --ref1 <commit1> --ref2 <commit2>
|
||||
python3 scripts/genome_diff.py --ref1 main --ref2 feature-branch
|
||||
python3 scripts/genome_diff.py --ref1 v1.0 --ref2 v2.0 --output report.txt
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, SCRIPT_DIR)
|
||||
from diff_analyzer import DiffAnalyzer, ChangeCategory
|
||||
|
||||
|
||||
@dataclass
|
||||
class FunctionChange:
|
||||
file: str
|
||||
name: str
|
||||
kind: str # 'function' or 'class'
|
||||
change_type: str # 'added' or 'removed' (simplified)
|
||||
old_line: Optional[int] = None
|
||||
new_line: Optional[int] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class DependencyChange:
|
||||
file: str
|
||||
module: str
|
||||
change_type: str # 'added' or 'removed' or 'modified'
|
||||
line: int = 0
|
||||
|
||||
|
||||
@dataclass
|
||||
class GenomeDiffReport:
|
||||
ref1: str
|
||||
ref2: str
|
||||
file_changes: List[Dict[str, Any]] = field(default_factory=list)
|
||||
function_changes: List[FunctionChange] = field(default_factory=list)
|
||||
dependency_changes: List[DependencyChange] = field(default_factory=list)
|
||||
total_files_changed: int = 0
|
||||
total_functions_changed: int = 0
|
||||
total_dependencies_changed: int = 0
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"ref1": self.ref1,
|
||||
"ref2": self.ref2,
|
||||
"summary": {
|
||||
"files": self.total_files_changed,
|
||||
"functions": self.total_functions_changed,
|
||||
"dependencies": self.total_dependencies_changed,
|
||||
},
|
||||
"file_changes": self.file_changes,
|
||||
"function_changes": [fc.__dict__ for fc in self.function_changes],
|
||||
"dependency_changes": [dc.__dict__ for dc in self.dependency_changes],
|
||||
}
|
||||
|
||||
def human_report(self) -> str:
|
||||
lines = []
|
||||
lines.append(f"Codebase Genome Diff: {self.ref1} → {self.ref2}")
|
||||
lines.append("=" * 60)
|
||||
lines.append(f" Files changed: {self.total_files_changed}")
|
||||
lines.append(f" Functions changed: {self.total_functions_changed}")
|
||||
lines.append(f" Dependencies changed: {self.total_dependencies_changed}")
|
||||
lines.append("")
|
||||
|
||||
for fc in self.file_changes:
|
||||
kind = []
|
||||
if fc.get('is_new'):
|
||||
kind.append("NEW")
|
||||
if fc.get('is_deleted'):
|
||||
kind.append("DELETED")
|
||||
if fc.get('is_renamed'):
|
||||
kind.append("RENAMED")
|
||||
if fc.get('is_binary'):
|
||||
kind.append("BINARY")
|
||||
kind_str = f" [{', '.join(kind)}]" if kind else ""
|
||||
lines.append(f" {fc['path']}{kind_str} (+{fc['added_lines']}/-{fc['deleted_lines']})")
|
||||
lines.append("")
|
||||
|
||||
for fc in self.function_changes:
|
||||
op = {'added': '+', 'removed': '-', 'modified': '~'}.get(fc.change_type, '?')
|
||||
lines.append(f" [{op}] {fc.file}: {fc.kind} '{fc.name}'")
|
||||
lines.append("")
|
||||
|
||||
for dc in self.dependency_changes:
|
||||
op = '+' if dc.change_type == 'added' else '-'
|
||||
lines.append(f" [{op}] {dc.file}: {dc.module}")
|
||||
lines.append("")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def run_git_diff(ref1: str, ref2: str) -> str:
|
||||
result = subprocess.run(
|
||||
['git', 'diff', '--unified=0', f'{ref1}...{ref2}'],
|
||||
capture_output=True, text=True, cwd=SCRIPT_DIR
|
||||
)
|
||||
if result.returncode not in (0, 1):
|
||||
print(f"git diff failed: {result.stderr}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
return result.stdout
|
||||
|
||||
|
||||
def extract_function_changes(diff_text: str) -> List[FunctionChange]:
|
||||
changes: List[FunctionChange] = []
|
||||
pattern = re.compile(r'^([+\-])\s*(def|class)\s+(\w+)', re.MULTILINE)
|
||||
hunk_header_re = re.compile(r'^@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s+@@')
|
||||
current_old_line: Optional[int] = None
|
||||
current_new_line: Optional[int] = None
|
||||
|
||||
for line in diff_text.split('\n'):
|
||||
hdr = hunk_header_re.match(line)
|
||||
if hdr:
|
||||
current_old_line = int(hdr.group(1))
|
||||
current_new_line = int(hdr.group(3))
|
||||
continue
|
||||
m = pattern.match(line)
|
||||
if m:
|
||||
op = m.group(1)
|
||||
kind = m.group(2)
|
||||
name = m.group(3)
|
||||
change_type = "added" if op == '+' else "removed"
|
||||
line_num = current_new_line if change_type == "added" else current_old_line
|
||||
changes.append(FunctionChange(
|
||||
file="<unknown>",
|
||||
name=name,
|
||||
kind=kind,
|
||||
change_type=change_type,
|
||||
new_line=line_num if change_type == "added" else None,
|
||||
old_line=line_num if change_type == "removed" else None,
|
||||
))
|
||||
# Advance line counters heuristically
|
||||
if op == '-':
|
||||
if current_old_line is not None:
|
||||
current_old_line += 1
|
||||
elif op == '+':
|
||||
if current_new_line is not None:
|
||||
current_new_line += 1
|
||||
elif line.startswith(' '):
|
||||
if current_old_line is not None:
|
||||
current_old_line += 1
|
||||
if current_new_line is not None:
|
||||
current_new_line += 1
|
||||
# lines starting with other prefixes (like \\ No newline) ignored
|
||||
return changes
|
||||
|
||||
|
||||
def extract_dependency_changes(diff_text: str, analyzer: DiffAnalyzer) -> List[DependencyChange]:
|
||||
changes: List[DependencyChange] = []
|
||||
import_pattern = re.compile(
|
||||
r'^([+\-])\s*(?:import\s+([\w\.]+)|from\s+([\w\.]+)\s+import)',
|
||||
re.MULTILINE
|
||||
)
|
||||
file_diffs = analyzer._split_files(diff_text)
|
||||
for file_diff in file_diffs:
|
||||
file_match = re.search(r'^diff --git a/.*? b/(.*?)$', file_diff, re.MULTILINE)
|
||||
if not file_match:
|
||||
continue
|
||||
filepath = file_match.group(1)
|
||||
|
||||
# Scan each line for import changes
|
||||
for line in file_diff.split('\n'):
|
||||
m = import_pattern.match(line)
|
||||
if m:
|
||||
change_type = "added" if m.group(1) == '+' else "removed"
|
||||
module = m.group(2) or m.group(3)
|
||||
changes.append(DependencyChange(
|
||||
file=filepath,
|
||||
module=module,
|
||||
change_type=change_type,
|
||||
line=0
|
||||
))
|
||||
|
||||
# Detect if this file is a dependency manifest
|
||||
req_file_pattern = re.compile(
|
||||
r'^[\+\-].*?(requirements(.*?)\.txt|pyproject\.toml|setup\.py|Pipfile)'
|
||||
)
|
||||
if any(req_file_pattern.match(line) for line in file_diff.split('\n')):
|
||||
if not any(c.file == filepath and c.module == "<file>" for c in changes):
|
||||
changes.append(DependencyChange(
|
||||
file=filepath,
|
||||
module="<file>",
|
||||
change_type="modified",
|
||||
line=0
|
||||
))
|
||||
return changes
|
||||
|
||||
|
||||
def correlate_function_changes_with_files(diff_text: str, functions: List[FunctionChange]) -> List[FunctionChange]:
|
||||
result: List[FunctionChange] = []
|
||||
# Split diff into per-file sections
|
||||
file_sections: List[tuple[str, str]] = []
|
||||
current_file: Optional[str] = None
|
||||
current_lines: List[str] = []
|
||||
for line in diff_text.split('\n'):
|
||||
if line.startswith('diff --git'):
|
||||
if current_file is not None:
|
||||
file_sections.append((current_file, '\n'.join(current_lines)))
|
||||
m = re.match(r'^diff --git a/.*? b/(.*?)$', line)
|
||||
current_file = m.group(1) if m else "unknown"
|
||||
current_lines = [line]
|
||||
else:
|
||||
current_lines.append(line)
|
||||
if current_file is not None:
|
||||
file_sections.append((current_file, '\n'.join(current_lines)))
|
||||
|
||||
pattern = re.compile(r'^([+\-])\s*(def|class)\s+(\w+)', re.MULTILINE)
|
||||
for filepath, section in file_sections:
|
||||
for m in pattern.finditer(section):
|
||||
op = m.group(1)
|
||||
kind = m.group(2)
|
||||
name = m.group(3)
|
||||
change_type = "added" if op == '+' else "removed"
|
||||
result.append(FunctionChange(
|
||||
file=filepath,
|
||||
name=name,
|
||||
kind=kind,
|
||||
change_type=change_type
|
||||
))
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Codebase Genome Diff — structural changes between versions")
|
||||
parser.add_argument("--ref1", required=True, help="First git ref (commit, branch, tag)")
|
||||
parser.add_argument("--ref2", required=True, help="Second git ref")
|
||||
parser.add_argument("--output", help="Write report to file")
|
||||
parser.add_argument("--json", action="store_true", help="Output JSON instead of human report")
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
diff_text = run_git_diff(args.ref1, args.ref2)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if not diff_text.strip():
|
||||
print(f"No differences between {args.ref1} and {args.ref2}.")
|
||||
sys.exit(0)
|
||||
|
||||
analyzer = DiffAnalyzer()
|
||||
summary = analyzer.analyze(diff_text)
|
||||
|
||||
file_changes = [fc.to_dict() for fc in summary.files]
|
||||
func_changes = extract_function_changes(diff_text)
|
||||
func_changes = correlate_function_changes_with_files(diff_text, func_changes)
|
||||
dep_changes = extract_dependency_changes(diff_text, analyzer)
|
||||
|
||||
report = GenomeDiffReport(
|
||||
ref1=args.ref1,
|
||||
ref2=args.ref2,
|
||||
file_changes=file_changes,
|
||||
function_changes=func_changes,
|
||||
dependency_changes=dep_changes,
|
||||
total_files_changed=len(file_changes),
|
||||
total_functions_changed=len(func_changes),
|
||||
total_dependencies_changed=len(dep_changes),
|
||||
)
|
||||
|
||||
output = json.dumps(report.to_dict(), indent=2) if args.json else report.human_report()
|
||||
|
||||
if args.output:
|
||||
with open(args.output, 'w') as f:
|
||||
f.write(output + '\n')
|
||||
print(f"Report written to {args.output}")
|
||||
else:
|
||||
print(output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1,116 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Tests for coverage_checker — Issue #124 acceptance validation."""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "scripts"))
|
||||
|
||||
from coverage_checker import (
|
||||
is_source_file,
|
||||
is_test_file,
|
||||
source_to_test_path,
|
||||
analyze_coverage,
|
||||
)
|
||||
|
||||
|
||||
class TestSourceFileDetection:
|
||||
def test_script_in_scripts_dir(self):
|
||||
assert is_source_file("scripts/freshness.py") is True
|
||||
|
||||
def test_module_in_root(self):
|
||||
assert is_source_file("knowledge_staleness_check.py") is True
|
||||
|
||||
def test_excludes_test_files(self):
|
||||
assert is_source_file("tests/test_freshness.py") is False
|
||||
|
||||
def test_excludes_non_py(self):
|
||||
assert is_source_file("README.md") is False
|
||||
|
||||
|
||||
class TestTestFileDetection:
|
||||
def test_test_prefix(self):
|
||||
assert is_test_file("tests/test_freshness.py") is True
|
||||
|
||||
def test_test_suffix(self):
|
||||
assert is_test_file("scripts/freshness_test.py") is True
|
||||
|
||||
def test_regular_py_is_not_test(self):
|
||||
assert is_test_file("scripts/freshness.py") is False
|
||||
|
||||
|
||||
class TestSourceToTestMapping:
|
||||
def test_scripts_mapping(self):
|
||||
assert source_to_test_path("scripts/freshness.py") == "tests/test_freshness.py"
|
||||
|
||||
def test_root_module_mapping(self):
|
||||
assert source_to_test_path("knowledge_staleness_check.py") == "tests/test_knowledge_staleness_check.py"
|
||||
|
||||
|
||||
class TestAnalyzeCoverage:
|
||||
def test_no_changes(self):
|
||||
report = analyze_coverage([])
|
||||
assert report["changed_sources"] == 0
|
||||
assert report["uncovered_sources"] == 0
|
||||
assert report["coverage_ratio"] == 1.0
|
||||
|
||||
def test_all_covered(self):
|
||||
changed = [
|
||||
"scripts/freshness.py",
|
||||
"tests/test_freshness.py",
|
||||
"scripts/dedup.py",
|
||||
"tests/test_dedup.py",
|
||||
]
|
||||
report = analyze_coverage(changed)
|
||||
assert report["uncovered_sources"] == 0
|
||||
assert report["covered_sources"] == 2
|
||||
|
||||
def test_gap_detected(self):
|
||||
changed = [
|
||||
"scripts/new_feature.py",
|
||||
"README.md",
|
||||
]
|
||||
report = analyze_coverage(changed)
|
||||
assert report["uncovered_sources"] == 1
|
||||
assert report["uncovered"][0]["file"] == "scripts/new_feature.py"
|
||||
assert report["uncovered"][0]["suggested_test"] == "tests/test_new_feature.py"
|
||||
|
||||
def test_mixed_coverage(self):
|
||||
changed = [
|
||||
"scripts/covered.py",
|
||||
"tests/test_covered.py",
|
||||
"scripts/uncovered.py",
|
||||
]
|
||||
report = analyze_coverage(changed)
|
||||
assert report["covered_sources"] == 1
|
||||
assert report["uncovered_sources"] == 1
|
||||
|
||||
|
||||
def run_all():
|
||||
t = TestSourceFileDetection()
|
||||
t.test_script_in_scripts_dir()
|
||||
t.test_module_in_root()
|
||||
t.test_excludes_test_files()
|
||||
t.test_excludes_non_py()
|
||||
|
||||
t2 = TestTestFileDetection()
|
||||
t2.test_test_prefix()
|
||||
t2.test_test_suffix()
|
||||
t2.test_regular_py_is_not_test()
|
||||
|
||||
t3 = TestSourceToTestMapping()
|
||||
t3.test_scripts_mapping()
|
||||
t3.test_root_module_mapping()
|
||||
|
||||
t4 = TestAnalyzeCoverage()
|
||||
t4.test_no_changes()
|
||||
t4.test_all_covered()
|
||||
t4.test_gap_detected()
|
||||
t4.test_mixed_coverage()
|
||||
|
||||
print("All 11 tests passed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_all()
|
||||
Reference in New Issue
Block a user