Skip to content

Commit b40a14d

Browse files
test: add a meta test to make sure that v4/objects/ files are imported
Add a test to make sure that all of the `gitlab/v4/objects/` files are imported in `gitlab/v4/objects/__init__.py`
1 parent 8af403c commit b40a14d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Ensure objects defined in gitlab.v4.objects are imported in
3+
`gitlab/v4/objects/__init__.py`
4+
5+
"""
6+
import os
7+
import pprint
8+
from typing import Set
9+
10+
RELATIVE_V4_OBJECTS_DIR = os.path.join("gitlab", "v4", "objects")
11+
V4_OBJECTS_DIR = os.path.join(os.getcwd(), RELATIVE_V4_OBJECTS_DIR)
12+
V4_OBJECTS_INIT = os.path.join(V4_OBJECTS_DIR, "__init__.py")
13+
14+
15+
def parse_import_line(line: str) -> str:
16+
line = line.rstrip()
17+
assert line.startswith("from .")
18+
line = line[len("from .") :]
19+
return line.split()[0]
20+
21+
22+
def test_verify_v4_objects_imported() -> None:
23+
print(os.getcwd())
24+
print(V4_OBJECTS_DIR)
25+
object_files: Set[str] = set()
26+
for filename in sorted(os.listdir(V4_OBJECTS_DIR)):
27+
if not filename.endswith(".py"):
28+
continue
29+
if filename in ("__init__.py",):
30+
continue
31+
root, _ = os.path.splitext(filename)
32+
object_files.add(root)
33+
34+
init_files: Set[str] = set()
35+
with open(V4_OBJECTS_INIT, "r") as in_file:
36+
for line in in_file.readlines():
37+
if not line.startswith("from ."):
38+
continue
39+
init_files.add(parse_import_line(line))
40+
41+
if object_files == init_files:
42+
return
43+
44+
missing_in_init = object_files - init_files
45+
assert (
46+
missing_in_init == set()
47+
), f"Missing imports in {V4_OBJECTS_INIT} for {pprint.pformat(missing_in_init)}"

0 commit comments

Comments
 (0)