Coverage for python/conftest.py: 86%

14 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-07 09:51 +0000

1# This file exists to fix import paths during pytest runs 

2# See discussion https://lsstc.slack.com/archives/C01FBUGM2CV/p1663207441828139 

3# 

4# The following code is from https://stackoverflow.com/a/72366347/834250 

5 

6import pathlib 

7 

8import _pytest.pathlib 

9 

10resolve_pkg_path_orig = _pytest.pathlib.resolve_package_path 

11 

12# we consider all dirs in repo/ to be namespace packages 

13root_dir = pathlib.Path(__file__).parent.resolve() 

14namespace_pkg_dirs = [str(d) for d in root_dir.iterdir() if d.is_dir()] 

15 

16 

17# patched method 

18def resolve_package_path(path: pathlib.Path) -> pathlib.Path | None: 

19 """Resolve the supplied path.""" 

20 # call original lookup 

21 result = resolve_pkg_path_orig(path) 

22 if result is None: 22 ↛ 24line 22 didn't jump to line 24, because the condition on line 22 was never false

23 result = path # let's search from the current directory upwards 

24 for parent in result.parents: 

25 if str(parent) in namespace_pkg_dirs: 25 ↛ 26line 25 didn't jump to line 26, because the condition on line 25 was never true

26 return parent 

27 return None 

28 

29 

30# apply patch 

31_pytest.pathlib.resolve_package_path = resolve_package_path