Download test_codeql_compare.py from perchscan/benchmark-results: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/datasets/perchscan/benchmark-results/resolve/refs%2Fpr%2F36/test_codeql_compare.py
- Command line
-
hf download hf://datasets/perchscan/benchmark-results@refs/pr/36/test_codeql_compare.py
-
curl -L -o test_codeql_compare.py https://huggingface.co/datasets/perchscan/benchmark-results/resolve/refs%2Fpr%2F36/test_codeql_compare.py
3.84 kB
| import json | |
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| from types import SimpleNamespace | |
| from codeql_compare import codeql_alert, method_span, metrics, run, selected_pairs | |
| class CodeQLComparisonTests(unittest.TestCase): | |
| def test_all_supported_selects_every_eligible_repository(self): | |
| revision = "a" * 40 | |
| eligible = {"pair_id": "eligible", "repository": "other/repo", "language": "python", | |
| "before": {"revision": revision}, "after": {"revision": revision}} | |
| unsupported = {**eligible, "pair_id": "unsupported", "language": "php"} | |
| missing_revision = {**eligible, "pair_id": "missing", | |
| "after": {"revision": None}} | |
| with tempfile.TemporaryDirectory() as directory: | |
| root = Path(directory) | |
| for domain in ("bug", "security"): | |
| path = root / domain / "benchmark.jsonl" | |
| path.parent.mkdir() | |
| path.write_text("".join(json.dumps(row) + "\n" for row in | |
| (eligible, unsupported, missing_revision))) | |
| cohort = root / "cohort.json" | |
| cohort.write_text(json.dumps({"bug": ["selected/repo"], | |
| "security": ["selected/repo"]})) | |
| args = SimpleNamespace(data=str(root), cohort=str(cohort), all_supported=True) | |
| self.assertEqual([row["pair_id"] for row in selected_pairs(args)["bug"]], | |
| ["eligible"]) | |
| def test_alert_requires_primary_location_inside_method_and_relevant_tag(self): | |
| side = { | |
| "path": "src/example.py", | |
| "state": {"method": {"source": "L0010| def check():\nL0011| return 1"}}, | |
| } | |
| sarif = { | |
| "runs": [{ | |
| "tool": {"driver": {"rules": [ | |
| {"id": "py/security", "properties": {"tags": ["security"]}}, | |
| {"id": "py/style", "properties": {"tags": ["maintainability"]}}, | |
| ]}}, | |
| "results": [ | |
| {"ruleId": "py/security", "locations": [{"physicalLocation": { | |
| "artifactLocation": {"uri": "src/example.py"}, | |
| "region": {"startLine": 11}}}]}, | |
| {"ruleId": "py/security", "locations": [{"physicalLocation": { | |
| "artifactLocation": {"uri": "src/example.py"}, | |
| "region": {"startLine": 30}}}]}, | |
| {"ruleId": "py/style", "locations": [{"physicalLocation": { | |
| "artifactLocation": {"uri": "src/example.py"}, | |
| "region": {"startLine": 10}}}]}, | |
| ], | |
| }], | |
| } | |
| with tempfile.TemporaryDirectory() as directory: | |
| file = Path(directory) / "result.sarif" | |
| file.write_text(json.dumps(sarif)) | |
| self.assertEqual(method_span(side), (10, 11)) | |
| self.assertEqual(codeql_alert(file, side, "security"), ["py/security"]) | |
| self.assertEqual(codeql_alert(file, side, "bug"), ["py/security"]) | |
| def test_counts_include_false_negatives_and_clean_methods(self): | |
| result = metrics(iter([(1, True), (1, False), (0, True), (0, False)])) | |
| self.assertEqual([result[key] for key in ("tp", "fp", "fn", "tn")], | |
| [1, 1, 1, 1]) | |
| self.assertEqual(result["f1"], 0.5) | |
| self.assertIsNone(metrics([(1, False), (0, False)])["precision"]) | |
| def test_scan_stage_has_bounded_runtime(self): | |
| with tempfile.TemporaryDirectory() as directory: | |
| code, _ = run([sys.executable, "-c", "import time; time.sleep(2)"], | |
| Path(directory) / "stage.log", timeout=0.1) | |
| self.assertEqual(code, 124) | |
| if __name__ == "__main__": | |
| unittest.main() | |