Rewrite integration test suite in Python
[tinc] / test / integration / testlib / path.py
1 """Paths to compiled binaries, and a few other important environment variables."""
2
3 import os
4 import pathlib
5 import sys
6
7 env = {
8     "TEST_NAME": os.getenv("TEST_NAME"),
9     "TINC_PATH": os.getenv("TINC_PATH"),
10     "TINCD_PATH": os.getenv("TINCD_PATH"),
11     "SPLICE_PATH": os.getenv("SPLICE_PATH"),
12     "PYTHON_PATH": os.getenv("PYTHON_PATH"),
13     "SPTPS_TEST_PATH": os.getenv("SPTPS_TEST_PATH"),
14     "SPTPS_KEYPAIR_PATH": os.getenv("SPTPS_KEYPAIR_PATH"),
15 }
16
17 # Not strictly necessary, used for better autocompletion and search by reference.
18 TEST_NAME = str(env["TEST_NAME"])
19 TINC_PATH = str(env["TINC_PATH"])
20 TINCD_PATH = str(env["TINCD_PATH"])
21 SPLICE_PATH = str(env["SPLICE_PATH"])
22 PYTHON_PATH = str(env["PYTHON_PATH"])
23 SPTPS_TEST_PATH = str(env["SPTPS_TEST_PATH"])
24 SPTPS_KEYPAIR_PATH = str(env["SPTPS_KEYPAIR_PATH"])
25
26
27 def _check() -> bool:
28     """Basic sanity checks on passed environment variables."""
29     for key, val in env.items():
30         if not val or (key != "TEST_NAME" and not os.path.isfile(val)):
31             return False
32     return True
33
34
35 if not _check():
36     MSG = """
37 Please run tests using
38     $ meson test -C build
39 or
40     $ ninja -C build test
41 """
42     print(MSG, file=sys.stderr)
43     sys.exit(1)
44
45 # Current working directory
46 CWD = os.getcwd()
47
48 # Path to the testing library
49 TESTLIB_ROOT = pathlib.Path(__file__).parent
50
51 # Source root for the integration test suite
52 TEST_SRC_ROOT = TESTLIB_ROOT.parent.resolve()
53
54 _wd = os.path.join(CWD, "wd")
55 os.makedirs(_wd, exist_ok=True)
56
57 # Useful when running tests manually
58 _gitignore = os.path.join(_wd, ".gitignore")
59 if not os.path.exists(_gitignore):
60     with open(_gitignore, "w", encoding="utf-8") as f:
61         f.write("*")
62
63 # Working directory for this test
64 TEST_WD = os.path.join(_wd, TEST_NAME)