File size: 3,842 Bytes
49665ce
 
 
 
 
76359b1
49665ce
76359b1
49665ce
 
 
76359b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49665ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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()