Files
Marius CiepluchandClaude Fable 5.1 0d7d3f0dc9 chore: format the whole tree and lint the scratch measurement scripts; CI has been red since 25ae4b4
CI runs ruff format --check and ruff check over the whole repository; 29 files had drifted from the formatter and
four measurement scripts carried lint findings, so every push since 25ae4b4 failed before pytest ran.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-10-14 02:49:44 +02:00

75 lines
2.5 KiB
Python

"""Checks for fix-site and ordinary labels."""
import pytest
from openultrasast.hunt.labels import label_rows
from openultrasast.hunt.table import FeatureTable
def positive():
return {
"kind": "vulnerability",
"path": "app.py",
"function": "handle",
"function_lines": [10, 20],
"family": "injection",
"case_id": "verified-case",
}
def test_labels_match_name_or_span_on_same_path():
rows = [
{"path": "app.py", "function": "handle", "line": 1},
{"path": "app.py", "function": "nested", "line": 12},
{"path": "other.py", "function": "handle", "line": 12},
{"path": "app.py", "function": "other", "line": 30},
]
labelled = label_rows(rows, positive())
assert [r["label"] for r in labelled] == [1, 1, None, None]
for data in labelled[:2]:
assert data["family"] == "injection"
assert data["fix_span"] == [10, 20]
assert data["label_ref"] == "verified-case"
assert data["verification"] is None
assert "label" not in rows[0]
def test_feature_function_and_ordinary_rows():
rows = [{"path": "app.py", "line": 1, "feature.source.function": "handle"}]
assert label_rows(rows, positive())[0]["label"] == 1
ordinary = label_rows([{"path": "app.py", "line": 1}], {"kind": "ordinary", "case_id": "draw"})
assert ordinary[0]["label"] == 0
assert len(FeatureTable(ordinary).labelled().rows) == 1
@pytest.mark.parametrize("kind", [None, "fix", "unknown"])
def test_unknown_change_kind_rejected(kind):
with pytest.raises(ValueError):
label_rows([], {"kind": kind})
def test_positive_requires_traceable_reference():
change = positive()
del change["case_id"]
with pytest.raises(ValueError, match="case_id"):
label_rows([], change)
def test_every_verified_target_of_a_change_is_a_positive():
change = {
**positive(),
"case_id": "c1",
"targets": [
{"case_id": "c1", "path": "app.py", "function": "handle", "function_lines": [10, 20]},
{"case_id": "c2", "path": "lib.py", "function": "check", "function_lines": [3, 8]},
],
}
rows = [
{"path": "app.py", "line": 12, "function": "handle"},
{"path": "lib.py", "line": 5, "function": "check"},
{"path": "lib.py", "line": 40, "function": "other"},
]
out = label_rows(rows, change)
assert [r["label"] for r in out] == [1, 1, None]
assert out[1]["label_ref"] == "c2" and out[1]["fix_span"] == [3, 8] and out[2]["label_ref"] is None