| 29 | |
| 30 | |
| 31 | class TestReporter(unittest.TestCase): |
| 32 | |
| 33 | test_issue = _make_issue() |
| 34 | |
| 35 | # ------------------------------------------------------------------ # |
| 36 | # JSON # |
| 37 | # ------------------------------------------------------------------ # |
| 38 | |
| 39 | def test_to_json(self): |
| 40 | reporter = Reporter([self.test_issue], "json") |
| 41 | output = reporter.to_json() |
| 42 | |
| 43 | output_json = json.loads(output) |
| 44 | |
| 45 | # Check issues summary |
| 46 | self.assertEqual(output_json["summary"]["issue_count"], 1) |
| 47 | |
| 48 | # Check issue fields |
| 49 | issue_json = output_json["issues"][0] |
| 50 | self.assertEqual(issue_json["rule_id"], self.test_issue.rule_id) |
| 51 | self.assertEqual(issue_json["description"], self.test_issue.description) |
| 52 | self.assertEqual(issue_json["file_path"], self.test_issue.file_path) |
| 53 | self.assertEqual(issue_json["line_number"], self.test_issue.line_number) |
| 54 | self.assertEqual(issue_json["code"], self.test_issue.code) |
| 55 | self.assertEqual(issue_json["severity"], self.test_issue.severity) |
| 56 | self.assertEqual(issue_json["remediation"], self.test_issue.remediation) |
| 57 | |
| 58 | def test_to_json_issue_count_matches_actual_issues(self): |
| 59 | issues = [_make_issue(rule_id=f"PY{i:03d}") for i in range(5)] |
| 60 | reporter = Reporter(issues, "json") |
| 61 | output_json = json.loads(reporter.to_json()) |
| 62 | self.assertEqual(output_json["summary"]["issue_count"], 5) |
| 63 | self.assertEqual(len(output_json["issues"]), 5) |
| 64 | |
| 65 | def test_to_json_includes_cwe_field(self): |
| 66 | issue = _make_issue(cwe="CWE-78") |
| 67 | reporter = Reporter([issue], "json") |
| 68 | output_json = json.loads(reporter.to_json()) |
| 69 | self.assertEqual(output_json["issues"][0]["cwe"], "CWE-78") |
| 70 | |
| 71 | # ------------------------------------------------------------------ # |
| 72 | # SARIF # |
| 73 | # ------------------------------------------------------------------ # |
| 74 | |
| 75 | def test_to_sarif(self): |
| 76 | reporter = Reporter([self.test_issue], "sarif") |
| 77 | output = reporter.to_sarif() |
| 78 | |
| 79 | output_json = json.loads(output) |
| 80 | |
| 81 | # Check top level SARIF fields |
| 82 | self.assertEqual(output_json.get("version"), "2.1.0") |
| 83 | self.assertEqual( |
| 84 | output_json.get("schema_uri"), |
| 85 | "https://raw.githubusercontent.com/oasis-tcs/" |
| 86 | "sarif-spec/master/Schemata/sarif-schema-2.1.0.json", |
| 87 | ) |
| 88 |
nothing calls this directly
no test coverage detected