pathlib: replace py.path.local.visit() with our own function

Part of reducing dependency on `py`. Also enables upcoming improvements.

In cases where there are simpler alternatives (in tests), I used those.

What's left are a couple of uses in `_pytest.main` and `_pytest.python`
and they only have modest requirements, so all of the featureful code
from py is not needed.
This commit is contained in:
Ran Benita
2020-07-05 22:58:47 +03:00
parent 3a060b77e8
commit c15bb5d3de
6 changed files with 30 additions and 15 deletions

View File

@@ -16,6 +16,7 @@ from os.path import isabs
from os.path import sep
from posixpath import sep as posix_sep
from types import ModuleType
from typing import Callable
from typing import Iterable
from typing import Iterator
from typing import Optional
@@ -556,3 +557,17 @@ def resolve_package_path(path: Path) -> Optional[Path]:
break
result = parent
return result
def visit(
path: py.path.local, recurse: Callable[[py.path.local], bool],
) -> Iterator[py.path.local]:
"""Walk path recursively, in breadth-first order.
Entries at each directory level are sorted.
"""
entries = sorted(path.listdir())
yield from entries
for entry in entries:
if entry.check(dir=1) and recurse(entry):
yield from visit(entry, recurse)