forked from open-atmos/devops_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_todos_annotated.py
72 lines (61 loc) · 2.13 KB
/
test_todos_annotated.py
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
""" utilities to ensure all TO-DO comments in the code are annotated
with an id of an open GitHub issue """
import os
import re
import pytest
from binaryornot.check import is_binary
from ghapi.all import GhApi, paged
from git.cmd import Git
from .utils import find_files
def _grep(filepath, regex):
reg_obj = re.compile(regex)
res = []
with open(filepath, encoding="utf8") as file_lines:
for line in file_lines:
if reg_obj.match(line):
res.append(line)
return res
@pytest.fixture(
params=find_files(),
name="git_tracked_file",
)
def _git_tracked_file(request):
return request.param
@pytest.fixture(scope="session", name="gh_issues")
def _gh_issues():
res = {}
repo = os.path.basename(Git(".").rev_parse("--show-toplevel"))
api = GhApi(owner="open-atmos", repo=repo)
pages = paged(
api.issues.list_for_repo,
owner="open-atmos",
repo=repo,
state="all",
per_page=100,
)
for page in pages:
for item in page.items:
res[item.number] = item.state
return res
def test_todos_annotated(git_tracked_file, gh_issues):
"""raises assertion errors if a (TODO|FIXME) is not annotated or if the annotation
does not point to an open issue"""
if is_binary(git_tracked_file):
pytest.skip("binary file")
for line in _grep(git_tracked_file, r".*(TODO|FIXME).*"):
if "(TODO|FIXME)" in line:
continue
match = re.search(r"(TODO|FIXME) #(\d+)", line)
if match is None:
raise AssertionError(f"(TODO|FIXME) not annotated with issue id ({line})")
giving_up_with_hope_other_builds_did_it = len(gh_issues) == 0
if not giving_up_with_hope_other_builds_did_it:
number = int(match.group(2))
if number not in gh_issues.keys():
raise AssertionError(
f"(TODO|FIXME) annotated with non-existent id ({line})"
)
if gh_issues[number] != "open":
raise AssertionError(
f"(TODO|FIXME) remains for a non-open issue ({line})"
)