doctest: use Path instead of py.path where possible

This commit is contained in:
Ran Benita 2020-12-19 14:02:24 +02:00
parent 2c05a7babb
commit 042d12fae6
2 changed files with 19 additions and 19 deletions

View File

@ -36,6 +36,7 @@ from _pytest.config.argparsing import Parser
from _pytest.fixtures import FixtureRequest
from _pytest.nodes import Collector
from _pytest.outcomes import OutcomeException
from _pytest.pathlib import fnmatch_ex
from _pytest.pathlib import import_path
from _pytest.python_api import approx
from _pytest.warning_types import PytestWarning
@ -120,32 +121,32 @@ def pytest_unconfigure() -> None:
def pytest_collect_file(
path: py.path.local, parent: Collector,
fspath: Path, path: py.path.local, parent: Collector,
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
config = parent.config
if path.ext == ".py":
if config.option.doctestmodules and not _is_setup_py(path):
if fspath.suffix == ".py":
if config.option.doctestmodules and not _is_setup_py(fspath):
mod: DoctestModule = DoctestModule.from_parent(parent, fspath=path)
return mod
elif _is_doctest(config, path, parent):
elif _is_doctest(config, fspath, parent):
txt: DoctestTextfile = DoctestTextfile.from_parent(parent, fspath=path)
return txt
return None
def _is_setup_py(path: py.path.local) -> bool:
if path.basename != "setup.py":
def _is_setup_py(path: Path) -> bool:
if path.name != "setup.py":
return False
contents = path.read_binary()
contents = path.read_bytes()
return b"setuptools" in contents or b"distutils" in contents
def _is_doctest(config: Config, path: py.path.local, parent) -> bool:
if path.ext in (".txt", ".rst") and parent.session.isinitpath(path):
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 path.check(fnmatch=glob):
if fnmatch_ex(glob, path):
return True
return False

View File

@ -1,10 +1,9 @@
import inspect
import textwrap
from pathlib import Path
from typing import Callable
from typing import Optional
import py
import pytest
from _pytest.doctest import _get_checker
from _pytest.doctest import _is_mocked
@ -1496,25 +1495,25 @@ def test_warning_on_unwrap_of_broken_object(
assert inspect.unwrap.__module__ == "inspect"
def test_is_setup_py_not_named_setup_py(tmp_path):
def test_is_setup_py_not_named_setup_py(tmp_path: Path) -> None:
not_setup_py = tmp_path.joinpath("not_setup.py")
not_setup_py.write_text('from setuptools import setup; setup(name="foo")')
assert not _is_setup_py(py.path.local(str(not_setup_py)))
assert not _is_setup_py(not_setup_py)
@pytest.mark.parametrize("mod", ("setuptools", "distutils.core"))
def test_is_setup_py_is_a_setup_py(tmpdir, mod):
setup_py = tmpdir.join("setup.py")
setup_py.write(f'from {mod} import setup; setup(name="foo")')
def test_is_setup_py_is_a_setup_py(tmp_path: Path, mod: str) -> None:
setup_py = tmp_path.joinpath("setup.py")
setup_py.write_text(f'from {mod} import setup; setup(name="foo")', "utf-8")
assert _is_setup_py(setup_py)
@pytest.mark.parametrize("mod", ("setuptools", "distutils.core"))
def test_is_setup_py_different_encoding(tmp_path, mod):
def test_is_setup_py_different_encoding(tmp_path: Path, mod: str) -> None:
setup_py = tmp_path.joinpath("setup.py")
contents = (
"# -*- coding: cp1252 -*-\n"
'from {} import setup; setup(name="foo", description="")\n'.format(mod)
)
setup_py.write_bytes(contents.encode("cp1252"))
assert _is_setup_py(py.path.local(str(setup_py)))
assert _is_setup_py(setup_py)