-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_codetf.py
More file actions
188 lines (143 loc) · 4.7 KB
/
test_codetf.py
File metadata and controls
188 lines (143 loc) · 4.7 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import json
from pathlib import Path
import jsonschema
import pytest
import requests
from pydantic import ValidationError
from codemodder.codetf import (
Change,
ChangeSet,
CodeTF,
DiffSide,
Finding,
Reference,
Result,
Rule,
)
from codemodder.codetf.v3.codetf import Finding as FindingV3
@pytest.fixture(autouse=True)
def disable_write_report():
"""
Override conftest to enable results to be written to disk for these tests.
"""
@pytest.fixture(autouse=True, scope="module")
def codetf_schema():
schema_path = "https://raw.githubusercontent.com/pixee/codemodder-specs/main/codetf.schema.json"
response = requests.get(schema_path)
yield json.loads(response.text)
def test_change():
diff = "--- a/test\n+++ b/test\n@@ -1,1 +1,1 @@\n-1\n+2\n"
changeset = ChangeSet(
path="test",
diff=diff,
changes=[
Change(
lineNumber=1,
description="Change 1 to 2",
),
],
)
result = changeset.model_dump()
assert result["path"] == "test"
assert result["diff"] == diff
assert result["changes"][0]["lineNumber"] == 1
assert result["changes"][0]["description"] == "Change 1 to 2"
assert result["changes"][0]["diffSide"] == DiffSide.RIGHT
assert result["changes"][0]["properties"] is None
assert result["changes"][0]["packageActions"] is None
@pytest.mark.parametrize("side", [DiffSide.LEFT, DiffSide.RIGHT])
def test_change_diffside(side):
change = Change(
lineNumber=1,
description="Change 1 to 2",
diffSide=side,
)
assert change.diffSide == side
assert change.model_dump()["diffSide"] == side
def test_change_invalid_line_number():
with pytest.raises(ValueError):
Change(lineNumber=0, description="Change 1 to 2")
def test_change_empty_description():
with pytest.raises(ValueError):
Change(lineNumber=1, description="")
def test_change_description_optional():
Change(lineNumber=1, description=None)
def test_write_codetf(tmpdir, mocker, codetf_schema):
path = tmpdir / "test.codetf.json"
assert not path.exists()
context = mocker.MagicMock(directory=Path("/foo/bar/whatever"))
codetf = CodeTF.build(context, 42, [], [])
retval = codetf.write_report(path)
assert retval == 0
assert path.exists()
data = path.read_text(encoding="utf-8")
CodeTF.model_validate_json(data)
jsonschema.validate(json.loads(data), codetf_schema)
def test_write_codetf_with_results(tmpdir, mocker, codetf_schema):
path = tmpdir / "test.codetf.json"
assert not path.exists()
context = mocker.MagicMock(directory=Path("/foo/bar/whatever"))
results = [
Result(
codemod="test",
summary="test",
description="test",
changeset=[
ChangeSet(
path="test",
diff="--- a/test\n+++ b/test\n@@ -1,1 +1,1 @@\n-1\n+2\n",
changes=[
Change(
lineNumber=1,
description="Change 1 to 2",
),
],
),
],
)
]
codetf = CodeTF.build(context, 42, [], results)
retval = codetf.write_report(path)
assert retval == 0
assert path.exists()
data = path.read_text(encoding="utf-8")
CodeTF.model_validate_json(data)
jsonschema.validate(json.loads(data), codetf_schema)
def test_reference_use_url_for_description():
ref = Reference(url="https://example.com")
assert ref.description == "https://example.com"
def test_case_insensitive_change_validation():
json = {
"lineNumber": 1,
"description": "Change 1 to 2",
"diffSide": "RIGHT",
"packageActions": [
{
"action": "ADD",
"package": "foo",
"result": "COMPLETED",
}
],
}
Change.model_validate(json)
@pytest.mark.parametrize("bad_value", ["MIDDLE", "middle"])
def test_still_invalidates_bad_value(bad_value):
json = {
"lineNumber": 1,
"description": "Change 1 to 2",
"diffSide": bad_value,
"packageActions": [
{
"action": "ADD",
"package": "foo",
"result": "COMPLETED",
}
],
}
with pytest.raises(ValidationError):
Change.model_validate(json)
def test_v2_finding_id_optional():
Finding(id=None, rule=Rule(id="foo", name="whatever"))
def test_v3_finding_id_not_optional():
with pytest.raises(ValidationError):
FindingV3(id=None, rule=Rule(id="foo", name="whatever")) # type: ignore[arg-type]