-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.py
65 lines (53 loc) · 1.93 KB
/
run_test.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
# Build the package
# And run pytest
import os
import shutil
import subprocess
import sys
import tempfile
import tomllib
import re
from virtualenv import cli_run
venv_name = "test-venv"
test_requirements = "requirements.txt"
def in_virtualenv():
def get_base_prefix_compat():
"""Get base/real prefix, or sys.prefix if there is none."""
return (
getattr(sys, "base_prefix", None)
or getattr(sys, "real_prefix", None)
or sys.prefix
)
return sys.prefix != get_base_prefix_compat()
def main():
# build python
subprocess.run(["pip", "install", "build"])
subprocess.run(["python", "-m", "build"])
# create a test environment
with open("pyproject.toml", "rb") as f:
parsed_toml = tomllib.load(f)
version = parsed_toml["project"]["version"][1:]
sdist = f"dist/TMDB-Py-{version}.tar.gz"
with tempfile.TemporaryDirectory(ignore_cleanup_errors=False) as test_dir:
print("Copy files ...")
shutil.copy(sdist, test_dir)
shutil.copy(test_requirements, test_dir)
shutil.copy("test.credential", test_dir)
shutil.copy("pytest.ini", test_dir)
print("Copy finished")
os.chdir(test_dir)
with open("pytest.ini", 'r+') as f:
new = re.sub("tmdbapi/tests", f"{venv_name}/lib/python3.11/site-packages/tmdbapi/tests", f.read())
f.seek(0)
f.write(new)
print("Modify pytest.ini")
cli_run([venv_name, "-p", "python3.11"])
activate_file = f"{venv_name}/bin/activate_this.py"
exec(open(activate_file).read(), {"__file__": activate_file})
# build environment and test
if in_virtualenv():
subprocess.run(["pip", "install", f"TMDB-Py-{version}.tar.gz"])
subprocess.run(["pip", "install", "-r", test_requirements])
subprocess.run(["pytest", "--pyargs", "tmdbapi"])
if __name__ == "__main__":
main()