Пример #1
0
def pytest_ignore_collect(path: py.path.local,
                          config: Config) -> "Optional[Literal[True]]":
    ignore_paths = config._getconftest_pathlist("collect_ignore",
                                                path=path.dirpath())
    ignore_paths = ignore_paths or []
    excludeopt = config.getoption("ignore")
    if excludeopt:
        ignore_paths.extend([py.path.local(x) for x in excludeopt])

    if py.path.local(path) in ignore_paths:
        return True

    ignore_globs = config._getconftest_pathlist("collect_ignore_glob",
                                                path=path.dirpath())
    ignore_globs = ignore_globs or []
    excludeglobopt = config.getoption("ignore_glob")
    if excludeglobopt:
        ignore_globs.extend([py.path.local(x) for x in excludeglobopt])

    if any(fnmatch.fnmatch(str(path), str(glob)) for glob in ignore_globs):
        return True

    allow_in_venv = config.getoption("collect_in_virtualenv")
    if not allow_in_venv and _in_venv(path):
        return True
    return None
Пример #2
0
 def _recurse(self, dirpath: py.path.local) -> bool:
     if dirpath.basename == "__pycache__":
         return False
     ihook = self._gethookproxy(dirpath.dirpath())
     if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
         return False
     for pat in self._norecursepatterns:
         if dirpath.check(fnmatch=pat):
             return False
     ihook = self._gethookproxy(dirpath)
     ihook.pytest_collect_directory(path=dirpath, parent=self)
     return True
Пример #3
0
    def _importconftest(
        self, conftestpath: py.path.local, importmode: Union[str, ImportMode],
    ) -> types.ModuleType:
        # Use a resolved Path object as key to avoid loading the same conftest
        # twice with build systems that create build directories containing
        # symlinks to actual files.
        # Using Path().resolve() is better than py.path.realpath because
        # it resolves to the correct path/drive in case-insensitive file systems (#5792)
        key = Path(str(conftestpath)).resolve()

        with contextlib.suppress(KeyError):
            return self._conftestpath2mod[key]

        pkgpath = conftestpath.pypkgpath()
        if pkgpath is None:
            _ensure_removed_sysmodule(conftestpath.purebasename)

        try:
            mod = import_path(conftestpath, mode=importmode)
        except Exception as e:
            assert e.__traceback__ is not None
            exc_info = (type(e), e, e.__traceback__)
            raise ConftestImportFailure(conftestpath, exc_info) from e

        self._check_non_top_pytest_plugins(mod, conftestpath)

        self._conftest_plugins.add(mod)
        self._conftestpath2mod[key] = mod
        dirpath = conftestpath.dirpath()
        if dirpath in self._dirpath2confmods:
            for path, mods in self._dirpath2confmods.items():
                if path and path.relto(dirpath) or path == dirpath:
                    assert mod not in mods
                    mods.append(mod)
        self.trace("loading conftestmodule {!r}".format(mod))
        self.consider_conftest(mod)
        return mod
Пример #4
0
    def _getconftestmodules(
        self, path: py.path.local, importmode: Union[str, ImportMode],
    ) -> List[types.ModuleType]:
        if self._noconftest:
            return []

        if path.isfile():
            directory = path.dirpath()
        else:
            directory = path

        # XXX these days we may rather want to use config.rootdir
        # and allow users to opt into looking into the rootdir parent
        # directories instead of requiring to specify confcutdir.
        clist = []
        for parent in directory.parts():
            if self._confcutdir and self._confcutdir.relto(parent):
                continue
            conftestpath = parent.join("conftest.py")
            if conftestpath.isfile():
                mod = self._importconftest(conftestpath, importmode)
                clist.append(mod)
        self._dirpath2confmods[directory] = clist
        return clist