Coverage for python/conftest.py: 86%

14 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-29 03:09 -0700

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 

21 Parameters 

22 ---------- 

23 path : `pathlib.Path` 

24 Path to resolve. 

25 

26 Returns 

27 ------- 

28 `pathlib.Path` or `None` 

29 Resolved path if it can be resolved. 

30 """ 

31 # call original lookup 

32 result = resolve_pkg_path_orig(path) 

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

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

35 for parent in result.parents: 

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

37 return parent 

38 return None 

39 

40 

41# apply patch 

42_pytest.pathlib.resolve_package_path = resolve_package_path