Buckets:

download
raw
12.3 kB
#!/usr/bin/env python3
"""
Coverage Strategy Analyzer
Analyzes coverage gaps and suggests a systematic approach
to reach 80% coverage without excessive effort.
"""
from dataclasses import dataclass
from pathlib import Path
from typing import List
@dataclass
class ModuleAnalysis:
"""Analysis of a module's coverage situation."""
name: str
current_coverage: float
lines_of_code: int
testable_lines: int # Lines that can be tested without external deps
external_deps: List[str]
priority: str # high, medium, low
effort_hours: int
def analyze_modules() -> List[ModuleAnalysis]:
"""Analyze modules for coverage improvement potential."""
modules = [
ModuleAnalysis(
name="agents/agent_base.py",
current_coverage=75.19,
lines_of_code=400,
testable_lines=350,
external_deps=[],
priority="high",
effort_hours=4,
),
ModuleAnalysis(
name="autonomous/exploit_validator.py",
current_coverage=29.95,
lines_of_code=300,
testable_lines=150,
external_deps=["sqlmap", "nuclei"],
priority="high",
effort_hours=8,
),
ModuleAnalysis(
name="database/models.py",
current_coverage=67.67,
lines_of_code=500,
testable_lines=450,
external_deps=["postgresql"],
priority="medium",
effort_hours=6,
),
ModuleAnalysis(
name="tools/nuclei_integration.py",
current_coverage=64.00,
lines_of_code=400,
testable_lines=200,
external_deps=["nuclei_binary"],
priority="medium",
effort_hours=6,
),
ModuleAnalysis(
name="tools/ffuf_integration.py",
current_coverage=71.43,
lines_of_code=350,
testable_lines=280,
external_deps=["ffuf_binary"],
priority="medium",
effort_hours=4,
),
ModuleAnalysis(
name="tools/tool_registry.py",
current_coverage=61.26,
lines_of_code=250,
testable_lines=220,
external_deps=[],
priority="high",
effort_hours=3,
),
]
return modules
def generate_strategy() -> str:
"""Generate coverage improvement strategy."""
modules = analyze_modules()
report = """
╔══════════════════════════════════════════════════════════════════════════╗
║ COVERAGE IMPROVEMENT STRATEGY ║
║ Target: 80% Coverage ║
╚══════════════════════════════════════════════════════════════════════════╝
📊 CURRENT SITUATION
═══════════════════════════════════════════════════════════════════════════
"""
total_effort = 0
high_priority = []
for m in modules:
gap = 80 - m.current_coverage
tests_needed = int(m.testable_lines * (gap / 100) / 10) # ~10 lines per test
report += f"""
📁 {m.name}
Current: {m.current_coverage:.1f}% | Target: 80% | Gap: {gap:.1f}%
Lines: {m.lines_of_code} | Testable: {m.testable_lines}
Priority: {m.priority.upper()} | Effort: ~{m.effort_hours}h
Tests needed: ~{tests_needed}
"""
total_effort += m.effort_hours
if m.priority == "high":
high_priority.append((m, tests_needed))
report += """
🎯 RECOMMENDED APPROACH: "Low Hanging Fruit First"
═══════════════════════════════════════════════════════════════════════════
Instead of 800+ tests across all modules, focus on HIGH-IMPACT modules:
PHASE 1: Quick Wins (2-3 days, +15% coverage)
───────────────────────────────────────────────────────────────────────────
"""
# High priority modules first
for m, tests in high_priority:
gap = 80 - m.current_coverage
report += f"""
✅ {m.name}
• Add {tests} focused unit tests
• Mock external: {", ".join(m.external_deps) if m.external_deps else "None needed"}
• Expected gain: +{gap:.1f}% coverage
• Time: {m.effort_hours}h
"""
report += f"""
PHASE 2: Strategic Refactoring (1 week)
═══════════════════════════════════════════════════════════════════════════
Instead of testing untestable code, refactor it:
1. EXTRACT EXTERNAL DEPENDENCIES
Before: After:
┌─────────────────────┐ ┌─────────────────────┐
│ def run_nuclei(): │ │ class NucleiRunner: │
│ subprocess.run() │ → │ def execute(): │
│ parse output │ │ subprocess │
│ return results │ │ def parse(): │
└─────────────────────┘ │ parse logic │
└─────────────────────┘
→ Test parse() without running nuclei
→ Mock execute() for integration tests
2. USE DEPENDENCY INJECTION
class ExploitValidator:
def __init__(self, sqlmap_client=None):
self.sqlmap = sqlmap_client or RealSqlmapClient()
→ Pass mock in tests, real client in production
3. CREATE TEST INTERFACES
@dataclass
class ScanResult:
target: str
findings: List[Finding]
→ Pure data classes are easy to test
→ Business logic separate from I/O
PHASE 3: Smart Test Patterns (3-4 days)
═══════════════════════════════════════════════════════════════════════════
Pattern 1: Test Happy Path + Edge Cases Only
───────────────────────────────────────────────────────────────────────────
Don't test: All error handling branches (expensive)
Do test: Critical business logic paths
Example:
```python
def test_scan_creation_happy_path(): # ✅ Test this
scan = create_scan("example.com")
assert scan.status == "pending"
def test_scan_invalid_target(): # ✅ Test this
with pytest.raises(ValidationError):
create_scan("") # Empty target
def test_scan_all_error_codes(): # ❌ Skip this (80 tests!)
for code in range(400, 600):
...
```
Pattern 2: Property-Based Testing for Validators
───────────────────────────────────────────────────────────────────────────
```python
from hypothesis import given, strategies as st
@given(target=st.sampled_from(["example.com", "test.org", "localhost"]))
def test_target_validation(target):
result = validate_target(target)
assert result.is_valid == is_valid_target(target)
```
→ One test covers hundreds of cases
Pattern 3: Snapshot Testing for Output
───────────────────────────────────────────────────────────────────────────
```python
def test_report_generation(snapshot):
report = generate_report(scan_results)
snapshot.assert_match(report)
```
→ Catch unexpected changes without manual assertions
📈 EXPECTED RESULTS
═══════════════════════════════════════════════════════════════════════════
With this strategy:
┌─────────────────────────────────────────────────────────────────────────┐
│ │
│ Total Effort: ~{total_effort} hours (1-2 weeks) │
│ New Tests: ~150-200 (not 800+) │
│ Coverage Gain: +15-20% → 80%+ overall │
│ Maintainability: Significantly improved │
│ │
└─────────────────────────────────────────────────────────────────────────┘
🛠️ TOOLS TO USE
═══════════════════════════════════════════════════════════════════════════
1. pytest-mock → Powerful mocking
2. pytest-snapshot → Snapshot testing
3. hypothesis → Property-based testing
4. pytest-cov → Coverage tracking
5. mutmut → Mutation testing (quality check)
🚀 IMMEDIATE NEXT STEPS
═══════════════════════════════════════════════════════════════════════════
1. TODAY (30 min):
pip install pytest-mock hypothesis pytest-snapshot
2. THIS WEEK:
• Pick ONE high-priority module
• Refactor to extract 1 external dependency
• Add 20-30 focused tests
• Verify coverage improvement
3. NEXT WEEK:
• Repeat for 2nd module
• Review coverage report
• Adjust strategy if needed
📋 SPECIFIC MODULE RECOMMENDATIONS
═══════════════════════════════════════════════════════════════════════════
🥇 START HERE: tools/tool_registry.py (61% → 80%)
• No external dependencies
• Pure Python logic
• 3-4 hours effort
• +20% coverage gain
🥈 NEXT: agents/agent_base.py (75% → 80%)
• Already good coverage
• Small gap to close
• 4 hours effort
• +5% coverage gain
🥉 THEN: autonomous/exploit_validator.py (30% → 50%)
• Refactor SQLMap integration
• Extract testable logic
• 8 hours effort
• +20% coverage gain
═══════════════════════════════════════════════════════════════════════════
💡 REMEMBER: 80% coverage of GOOD code > 100% coverage of BAD code
Focus on testability first, coverage second!
"""
return report
def main():
"""Main entry point."""
strategy = generate_strategy()
print(strategy)
# Save to file
output = Path("COVERAGE_STRATEGY.md")
output.write_text(strategy)
print(f"\n📄 Strategy saved to: {output}")
if __name__ == "__main__":
main()

Xet Storage Details

Size:
12.3 kB
·
Xet hash:
ccb2405a730c70cbb3efe17e9c15fcb4bab639ebfb4bc0a739f79a953c9e4ca2

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.