Format and print all violations. Returns exit code (0 = success, 1 = failures). Args: results: Checker results to format. max_violations_per_checker: Maximum violations to print per checker before truncating output. Set to 0 for unlimited.
(
results: dict[str, CheckerResults], max_violations_per_checker: int = 10
)
| 471 | |
| 472 | |
| 473 | def format_and_print_results( |
| 474 | results: dict[str, CheckerResults], max_violations_per_checker: int = 10 |
| 475 | ) -> int: |
| 476 | """Format and print all violations. Returns exit code (0 = success, 1 = failures). |
| 477 | |
| 478 | Args: |
| 479 | results: Checker results to format. |
| 480 | max_violations_per_checker: Maximum violations to print per checker before |
| 481 | truncating output. Set to 0 for unlimited. |
| 482 | """ |
| 483 | total_violations = 0 |
| 484 | |
| 485 | for checker_name, checker_results in sorted(results.items()): |
| 486 | if not checker_results.has_violations(): |
| 487 | continue |
| 488 | |
| 489 | file_count = checker_results.file_count() |
| 490 | violation_count = checker_results.total_violations() |
| 491 | total_violations += violation_count |
| 492 | |
| 493 | print(f"\n{'=' * 80}") |
| 494 | print( |
| 495 | f"[{checker_name}] Found {violation_count} violation(s) in {file_count} file(s):" |
| 496 | ) |
| 497 | print("=" * 80) |
| 498 | |
| 499 | printed_violations = 0 |
| 500 | truncated = False |
| 501 | for file_path in sorted(checker_results.violations.keys()): |
| 502 | if ( |
| 503 | max_violations_per_checker > 0 |
| 504 | and printed_violations >= max_violations_per_checker |
| 505 | ): |
| 506 | truncated = True |
| 507 | break |
| 508 | rel_path = os.path.relpath(file_path, PROJECT_ROOT) |
| 509 | # Normalize to forward slashes for consistent cross-platform output |
| 510 | rel_path = rel_path.replace("\\", "/") |
| 511 | file_violations = checker_results.violations[file_path] |
| 512 | |
| 513 | print(f"\n{rel_path}:") |
| 514 | for violation in file_violations.violations: |
| 515 | if ( |
| 516 | max_violations_per_checker > 0 |
| 517 | and printed_violations >= max_violations_per_checker |
| 518 | ): |
| 519 | truncated = True |
| 520 | break |
| 521 | print(f" Line {violation.line_number}: {violation.content}") |
| 522 | printed_violations += 1 |
| 523 | |
| 524 | if truncated: |
| 525 | remaining = violation_count - printed_violations |
| 526 | print( |
| 527 | f"\n ... stopped after {max_violations_per_checker} violations" |
| 528 | f" ({remaining} more not shown)" |
| 529 | ) |
| 530 |
no test coverage detected