-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_get_callsite_path.py
More file actions
39 lines (27 loc) · 1.09 KB
/
test_get_callsite_path.py
File metadata and controls
39 lines (27 loc) · 1.09 KB
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
import inspect
import re
from types import SimpleNamespace
import pytest
from alternative import _get_caller_path
MODULE = __name__
FALLBACK = "<unknown module>.<unknown> (<unknown location>)"
def test_callsite_normal(request: pytest.FixtureRequest):
"""A reasonable level of depth information is returned, as well as the line number."""
def level1():
def level2():
return _get_caller_path()
return level2()
path = level1()
assert path is not None
func_name = request.node.originalname
# Should start with MODULE.level1.<locals>.level2
prefix = f"{MODULE}.{func_name}.<locals>.level1 ({__file__}"
assert re.fullmatch(re.escape(prefix) + r":\d+\)", path)
def test_callsite_no_two_up(monkeypatch):
"""If the caller is not python, a default is returned."""
# Create a fake frame with no .f_back or only one level
fake_frame_top = SimpleNamespace(f_back=None)
# Monkeypatch currentframe to return our fake
monkeypatch.setattr(inspect, "currentframe", lambda: fake_frame_top)
path = _get_caller_path()
assert path == FALLBACK