aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/pytest/plugins/conftests.py
blob: 2ea36ae4c2149f60f3d6d6529688e5a121110f7f (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import importlib
import sys
import inspect

import yatest.common as yc

from pytest import hookimpl
from yatest_lib.ya import Ya

from .fixtures import metrics, links  # noqa

orig_getfile = inspect.getfile


def getfile(object):
    if inspect.ismodule(object) and getattr(object, '__orig_file__', None):
        res = object.__orig_file__
    else:
        res = orig_getfile(object)
    return res

inspect.getfile = getfile
conftest_modules = []


@hookimpl(trylast=True)
def pytest_load_initial_conftests(early_config, parser, args):
    yc.runtime._set_ya_config(ya=Ya())
    conftests = filter(lambda name: name.endswith(".conftest"), sys.extra_modules)

    def conftest_key(name):
        if not name.startswith("__tests__."):
            # Make __tests__ come last
            return "_." + name
        return name

    for name in sorted(conftests, key=conftest_key):
        mod = importlib.import_module(name)
        if os.getenv("CONFTEST_LOAD_POLICY") != "LOCAL":
            mod.__orig_file__ = mod.__file__
            mod.__file__ = ""
        conftest_modules.append(mod)
        early_config.pluginmanager.consider_conftest(mod)


def getconftestmodules(*args, **kwargs):
    return conftest_modules


def pytest_sessionstart(session):
    # Override filesystem based relevant conftest discovery on the call path
    assert session.config.pluginmanager
    session.config.pluginmanager._getconftestmodules = getconftestmodules