Coverage for python/conftest.py: 87%

15 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-11 03:03 -0800

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 

7from typing import Optional 

8 

9import _pytest.pathlib 

10 

11resolve_pkg_path_orig = _pytest.pathlib.resolve_package_path 

12 

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

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

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

16 

17 

18# patched method 

19def resolve_package_path(path: pathlib.Path) -> Optional[pathlib.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