Ejemplo n.º 1
0
def _is_doctest(config: Config, path: Path, parent: Collector) -> bool:
    if path.suffix in (".txt", ".rst") and parent.session.isinitpath(path):
        return True
    globs = config.getoption("doctestglob") or ["test*.txt"]
    for glob in globs:
        if fnmatch_ex(glob, path):
            return True
    return False
Ejemplo n.º 2
0
 def _recurse(self, direntry: "os.DirEntry[str]") -> bool:
     if direntry.name == "__pycache__":
         return False
     fspath = Path(direntry.path)
     ihook = self.gethookproxy(fspath.parent)
     if ihook.pytest_ignore_collect(collection_path=fspath, config=self.config):
         return False
     norecursepatterns = self.config.getini("norecursedirs")
     if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns):
         return False
     return True
Ejemplo n.º 3
0
    def _should_rewrite(self, name: str, fn: str, state: "AssertionState") -> bool:
        # always rewrite conftest files
        if os.path.basename(fn) == "conftest.py":
            state.trace(f"rewriting conftest file: {fn!r}")
            return True

        if self.session is not None:
            if self.session.isinitpath(py.path.local(fn)):
                state.trace(f"matched test file (was specified on cmdline): {fn!r}")
                return True

        # modules not passed explicitly on the command line are only
        # rewritten if they match the naming convention for test files
        fn_path = PurePath(fn)
        for pat in self.fnpats:
            if fnmatch_ex(pat, fn_path):
                state.trace(f"matched test file {fn!r}")
                return True

        return self._is_marked_for_rewrite(name, state)
Ejemplo n.º 4
0
    def _early_rewrite_bailout(self, name: str,
                               state: "AssertionState") -> bool:
        """A fast way to get out of rewriting modules.

        Profiling has shown that the call to PathFinder.find_spec (inside of
        the find_spec from this class) is a major slowdown, so, this method
        tries to filter what we're sure won't be rewritten before getting to
        it.
        """
        if self.session is not None and not self._session_paths_checked:
            self._session_paths_checked = True
            for initial_path in self.session._initialpaths:
                # Make something as c:/projects/my_project/path.py ->
                #     ['c:', 'projects', 'my_project', 'path.py']
                parts = str(initial_path).split(os.path.sep)
                # add 'path' to basenames to be checked.
                self._basenames_to_check_rewrite.add(
                    os.path.splitext(parts[-1])[0])

        # Note: conftest already by default in _basenames_to_check_rewrite.
        parts = name.split(".")
        if parts[-1] in self._basenames_to_check_rewrite:
            return False

        # For matching the name it must be as if it was a filename.
        path = PurePath(os.path.sep.join(parts) + ".py")

        for pat in self.fnpats:
            # if the pattern contains subdirectories ("tests/**.py" for example) we can't bail out based
            # on the name alone because we need to match against the full path
            if os.path.dirname(pat):
                return False
            if fnmatch_ex(pat, path):
                return False

        if self._is_marked_for_rewrite(name, state):
            return False

        state.trace("early skip of rewriting module: {}".format(name))
        return True
Ejemplo n.º 5
0
    def _early_rewrite_bailout(self, name, state):
        """
        This is a fast way to get out of rewriting modules. Profiling has
        shown that the call to imp.find_module (inside of the find_module
        from this class) is a major slowdown, so, this method tries to
        filter what we're sure won't be rewritten before getting to it.
        """
        if self.session is not None and not self._session_paths_checked:
            self._session_paths_checked = True
            for path in self.session._initialpaths:
                # Make something as c:/projects/my_project/path.py ->
                #     ['c:', 'projects', 'my_project', 'path.py']
                parts = str(path).split(os.path.sep)
                # add 'path' to basenames to be checked.
                self._basenames_to_check_rewrite.add(os.path.splitext(parts[-1])[0])

        # Note: conftest already by default in _basenames_to_check_rewrite.
        parts = name.split(".")
        if parts[-1] in self._basenames_to_check_rewrite:
            return False

        # For matching the name it must be as if it was a filename.
        path = PurePath(os.path.sep.join(parts) + ".py")

        for pat in self.fnpats:
            # if the pattern contains subdirectories ("tests/**.py" for example) we can't bail out based
            # on the name alone because we need to match against the full path
            if os.path.dirname(pat):
                return False
            if fnmatch_ex(pat, path):
                return False

        if self._is_marked_for_rewrite(name, state):
            return False

        state.trace("early skip of rewriting module: %s" % (name,))
        return True
Ejemplo n.º 6
0
 def test_not_matching(self, pattern: str, path: str) -> None:
     assert not fnmatch_ex(pattern, path)
Ejemplo n.º 7
0
 def test_matching_abspath(self) -> None:
     abspath = os.path.abspath(os.path.join("tests/foo.py"))
     assert fnmatch_ex("tests/foo.py", abspath)
Ejemplo n.º 8
0
 def match_(pattern, path):
     return fnmatch_ex(pattern, path)
Ejemplo n.º 9
0
 def match_(pattern, path):
     return fnmatch_ex(pattern, path)