Skip to content

Commit

Permalink
pytest-dev#7124: Fix __main__.py files causing import errors with `…
Browse files Browse the repository at this point in the history
…--doctest-modules`
  • Loading branch information
symonk committed Jul 27, 2021
1 parent 6a1ba80 commit 9ead96e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/7124.bugfix.rst
@@ -0,0 +1 @@
Fixed an issue where ``__main__.py`` would raise an ``ImportError`` when ``--doctest-modules`` was provided.
8 changes: 7 additions & 1 deletion src/_pytest/doctest.py
Expand Up @@ -125,7 +125,9 @@ def pytest_collect_file(
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
config = parent.config
if fspath.suffix == ".py":
if config.option.doctestmodules and not _is_setup_py(fspath):
if config.option.doctestmodules and not any(
(_is_setup_py(fspath), _is_main_py(fspath))
):
mod: DoctestModule = DoctestModule.from_parent(parent, path=fspath)
return mod
elif _is_doctest(config, fspath, parent):
Expand All @@ -148,6 +150,10 @@ def _is_doctest(config: Config, path: Path, parent: Collector) -> bool:
return any(fnmatch_ex(glob, path) for glob in globs)


def _is_main_py(path: Path) -> bool:
return path.name == "__main__.py"


class ReprFailDoctest(TerminalRepr):
def __init__(
self, reprlocation_lines: Sequence[Tuple[ReprFileLocation, Sequence[str]]]
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions testing/example_scripts/doctest/main_py/__main__.py
@@ -0,0 +1,2 @@
def test_this_is_ignored():
assert True
6 changes: 6 additions & 0 deletions testing/example_scripts/doctest/main_py/test_normal_module.py
@@ -0,0 +1,6 @@
def test_doc():
"""
>>> 10 > 5
True
"""
assert False
14 changes: 14 additions & 0 deletions testing/test_doctest.py
Expand Up @@ -6,6 +6,7 @@

import pytest
from _pytest.doctest import _get_checker
from _pytest.doctest import _is_main_py
from _pytest.doctest import _is_mocked
from _pytest.doctest import _is_setup_py
from _pytest.doctest import _patch_unwrap_mock_aware
Expand Down Expand Up @@ -811,6 +812,11 @@ def test_valid_setup_py(self, pytester: Pytester):
result = pytester.runpytest(p, "--doctest-modules")
result.stdout.fnmatch_lines(["*collected 0 items*"])

def test_main_py_does_not_cause_import_errors(self, pytester: Pytester):
p = pytester.copy_example("doctest//main_py")
result = pytester.runpytest(p, "--doctest-modules")
result.stdout.fnmatch_lines(["*collected 2 items*", "*1 failed, 1 passed*"])

def test_invalid_setup_py(self, pytester: Pytester):
"""
Test to make sure that pytest reads setup.py files that are not used
Expand Down Expand Up @@ -1518,3 +1524,11 @@ def test_is_setup_py_different_encoding(tmp_path: Path, mod: str) -> None:
)
setup_py.write_bytes(contents.encode("cp1252"))
assert _is_setup_py(setup_py)


@pytest.mark.parametrize(
"name, expected", [("__main__.py", True), ("__init__.py", False)]
)
def test_is_main_py(tmp_path: Path, name: str, expected: bool) -> None:
dunder_main = tmp_path.joinpath(name)
assert _is_main_py(dunder_main) == expected

0 comments on commit 9ead96e

Please sign in to comment.