forked from talonhub/community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
55 lines (45 loc) · 1.8 KB
/
conftest.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
"""
Configuration file for pytest
See also https://docs.pytest.org/en/6.2.x/writing_plugins.html#writing-hook-functions
"""
import importlib
import os
import sys
class UnitTestPathFinder(importlib.machinery.PathFinder):
"""
Makes the knausj_talon repo root directory available under
knausj_talon_pkg and tests/stubs/talon/ available
under talon. Activated by the code in pytest_sessionstart()
A loader is needed since the 'code' folder in knausj conflicts
with the built in python 'code' module. Renaming the folder
could cause merge conflicts.
"""
@classmethod
def find_spec(cls, fullname, path=None, target=None):
curr_dir = os.path.dirname(__file__)
knausj_prefix = "knausj_talon_pkg"
if fullname == "talon" or fullname.startswith("talon."):
# Load talon stubs as talon module
filepath = os.path.join(curr_dir, "tests", "stubs")
return super().find_spec(fullname, [filepath])
elif fullname == knausj_prefix:
# Load knausj_talon root module
return importlib.machinery.ModuleSpec(
name=fullname,
loader=importlib.machinery.SourceFileLoader(
fullname, os.path.join(curr_dir, "tests", "repo_root_init.py")
),
is_package=True,
)
elif fullname.startswith(knausj_prefix + "."):
# Load knausj_talon submodules
return super().find_spec(fullname, [curr_dir])
else:
# Allow normal sys.path stuff to handle everything else
return None
def pytest_sessionstart():
"""
Set up test environment. Only invoked when we're in the pytest
environment so as not to mess with the Talon runtime.
"""
sys.meta_path.append(UnitTestPathFinder)