File: util.py

package info (click to toggle)
python-pytest-unmagic 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: python: 1,199; makefile: 3
file content (33 lines) | stat: -rw-r--r-- 768 bytes parent folder | download
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
import inspect
import textwrap
from contextlib import contextmanager
from unittest.mock import patch

import _pytest.pytester as _pytester
from unmagic import fixture
from unmagic.scope import get_active, get_request, set_active


def get_source(func):
    src = inspect.getsource(func)
    while True:
        firstline, src = src.split("\n", 1)
        if f'def {func.__name__}(' in firstline:
            return textwrap.dedent(src)
        assert src


@fixture
def unmagic_tester():
    with patch.object(_pytester, "main", unmagic_inactive()(_pytester.main)):
        yield get_request().getfixturevalue("pytester")


@contextmanager
def unmagic_inactive():
    obj = get_active()
    set_active(None)
    try:
        yield
    finally:
        set_active(obj)