Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Merge pull request #7186 from asottile/is_setup_py_encoding_agnostic #7189

Merged
merged 1 commit into from May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7180.bugfix.rst
@@ -0,0 +1 @@
Fix ``_is_setup_py`` for files encoded differently than locale.
10 changes: 5 additions & 5 deletions src/_pytest/doctest.py
Expand Up @@ -108,20 +108,20 @@ def pytest_unconfigure():
RUNNER_CLASS = None


def pytest_collect_file(path, parent):
def pytest_collect_file(path: py.path.local, parent):
config = parent.config
if path.ext == ".py":
if config.option.doctestmodules and not _is_setup_py(config, path, parent):
if config.option.doctestmodules and not _is_setup_py(path):
return DoctestModule.from_parent(parent, fspath=path)
elif _is_doctest(config, path, parent):
return DoctestTextfile.from_parent(parent, fspath=path)


def _is_setup_py(config, path, parent):
def _is_setup_py(path: py.path.local) -> bool:
if path.basename != "setup.py":
return False
contents = path.read()
return "setuptools" in contents or "distutils" in contents
contents = path.read_binary()
return b"setuptools" in contents or b"distutils" in contents


def _is_doctest(config, path, parent):
Expand Down
25 changes: 25 additions & 0 deletions testing/test_doctest.py
Expand Up @@ -5,6 +5,7 @@
from _pytest.compat import MODULE_NOT_FOUND_ERROR
from _pytest.doctest import _get_checker
from _pytest.doctest import _is_mocked
from _pytest.doctest import _is_setup_py
from _pytest.doctest import _patch_unwrap_mock_aware
from _pytest.doctest import DoctestItem
from _pytest.doctest import DoctestModule
Expand Down Expand Up @@ -1487,3 +1488,27 @@ def test_warning_on_unwrap_of_broken_object(stop):
with pytest.raises(KeyError):
inspect.unwrap(bad_instance, stop=stop)
assert inspect.unwrap.__module__ == "inspect"


def test_is_setup_py_not_named_setup_py(tmpdir):
not_setup_py = tmpdir.join("not_setup.py")
not_setup_py.write('from setuptools import setup; setup(name="foo")')
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('from {} import setup; setup(name="foo")'.format(mod))
assert _is_setup_py(setup_py)


@pytest.mark.parametrize("mod", ("setuptools", "distutils.core"))
def test_is_setup_py_different_encoding(tmpdir, mod):
setup_py = tmpdir.join("setup.py")
contents = (
"# -*- coding: cp1252 -*-\n"
'from {} import setup; setup(name="foo", description="€")\n'.format(mod)
)
setup_py.write_binary(contents.encode("cp1252"))
assert _is_setup_py(setup_py)