import unittest from tests.fakes import BASE, pr_ref from werkrevisor.redact import Redactor from werkrevisor.report import ReviewResult, render_comment from werkrevisor.verdict import Finding, Reason, Verdict class ReasonOrderTest(unittest.TestCase): def test_reasons_are_listed_from_the_most_to_the_least_severe(self): reasons = [Reason("a", "first yellow", Verdict.YELLOW), Reason("b", "orange", Verdict.ORANGE), Reason("c", "second yellow", Verdict.YELLOW), Reason("d", "red", Verdict.RED)] body = render_comment(ReviewResult(verdict=Verdict.RED, reasons=reasons), pr_ref(), BASE, 3, Redactor()) listed = [line for line in body.splitlines() if line.startswith("- **")] self.assertEqual(listed, ["- **RED** red", "- **ORANGE** orange", "- **YELLOW** first yellow", "- **YELLOW** second yellow"]) def finding(header, severity="medium", known=True, line=1): return Finding("a.py", line, line, header, "Text.", severity, known) class FindingOrderTest(unittest.TestCase): def numbered(self, findings, max_findings=10): result = ReviewResult(verdict=Verdict.ORANGE, findings=findings) body = render_comment(result, pr_ref(), BASE, max_findings, Redactor()) return [line for line in body.splitlines() if line[:1].isdigit() and ". **" in line] def test_findings_are_listed_from_high_to_low_severity(self): findings = [finding("First Low", "low", line=1), finding("Possible Bug", "high", line=2), finding("Second Low", "low", line=3), finding("Missing Test", "medium", line=4)] self.assertEqual(self.numbered(findings), ["1. **[high] Possible Bug** at `a.py:2`", "2. **[medium] Missing Test** at `a.py:4`", "3. **[low] First Low** at `a.py:1`", "4. **[low] Second Low** at `a.py:3`"]) def test_the_cut_at_max_findings_keeps_the_severe_ones(self): findings = [finding("Low", "low"), finding("Bug", "high")] self.assertEqual(self.numbered(findings, max_findings=1), ["1. **[high] Bug** at `a.py:1`"]) def test_unknown_severity_is_shown_as_counted_as_medium(self): findings = [finding("[critical] Possible Bug", known=False, line=1), finding("Log Noise", "low", line=2)] self.assertEqual(self.numbered(findings), ["1. **[critical] Possible Bug** at `a.py:1`, severity unknown, counted as medium", "2. **[low] Log Noise** at `a.py:2`"])