-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_suite.py
More file actions
63 lines (49 loc) · 1.71 KB
/
test_suite.py
File metadata and controls
63 lines (49 loc) · 1.71 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
from collections import defaultdict
from glob import glob
import os.path
import textwrap
import git
import pytest
TEST_ROOT = os.path.join(os.path.dirname(__file__), "tests")
def get_test_files() -> list[str]:
tests = glob(os.path.join(TEST_ROOT, "**", "test_*.sh"), recursive=True)
return tests
def simplify_test_name(test_file: str) -> str:
name = os.path.relpath(test_file, start=TEST_ROOT)
_, name = os.path.split(name)
name, _ = os.path.splitext(name)
name = name.replace(os.path.sep, "-")
name = name.replace("_", "-")
name = name.replace("test-", "")
return name
def get_test_folder(test_file: str) -> str:
name = os.path.relpath(test_file, start=TEST_ROOT)
folder, *_ = name.split(os.path.sep)
folder = folder.replace("-", "_")
return folder
TEST_FILES: dict[str, dict[str, str]] = defaultdict(dict)
for test_file in get_test_files():
TEST_FILES[get_test_folder(test_file)][simplify_test_name(test_file)] = test_file
@pytest.fixture(autouse=True)
def git_setup():
repository = git.Repo()
diff = repository.index.diff(None, paths=TEST_ROOT)
if diff:
raise Exception(
"Git repository has changes to the worktree."
"Please, `git restore` before using the test script."
)
yield
repository.git.restore(TEST_ROOT)
for folder, params in TEST_FILES.items():
keys = list(params.keys())
code = textwrap.dedent(
f"""
import subprocess
@pytest.mark.parametrize("test_name", {keys})
def test_{folder}(test_name: str) -> None:
test_script = TEST_FILES["{folder}"][test_name]
subprocess.run(test_script, shell=True, check=True)
"""
)
exec(code)