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

Update config._getconftest_pathlist for pytest-dev #157

Merged
merged 6 commits into from
Jun 16, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- os: ubuntu-latest
python-version: 3.9
toxenv: py39-test-pytest61
- os: ubuntu-latest
python-version: 3.9
toxenv: py39-test-pytest62
- os: macos-latest
python-version: 3.8
toxenv: py38-test-pytestdev
Expand Down
55 changes: 44 additions & 11 deletions pytest_doctestplus/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
_pytest_version = Version(pytest.__version__)
PYTEST_GT_5 = _pytest_version > Version('5.9.9')
PYTEST_GE_6_3 = _pytest_version.is_devrelease or _pytest_version >= Version('6.3')
PYTEST_GT_6_3 = _pytest_version.is_devrelease or _pytest_version > Version('6.3')

comment_characters = {
'.txt': '#',
Expand Down Expand Up @@ -189,12 +190,23 @@ class DocTestModulePlus(doctest_plugin.DoctestModule):
def collect(self):
# When running directly from pytest we need to make sure that we
# don't accidentally import setup.py!
if self.fspath.basename == "setup.py":
if PYTEST_GE_6_3:
fspath = self.path
filepath = self.path.name
else:
fspath = self.fspath
filepath = self.fspath.basename

if filepath == "setup.py":
return
elif self.fspath.basename == "conftest.py":
if PYTEST_GE_6_3:
elif filepath == "conftest.py":
if PYTEST_GT_6_3:
module = self.config.pluginmanager._importconftest(
Path(self.fspath), self.config.getoption("importmode"))
self.path, self.config.getoption("importmode"),
rootpath=self.config.rootpath)
elif PYTEST_GE_6_3:
module = self.config.pluginmanager._importconftest(
self.path, self.config.getoption("importmode"))
elif PYTEST_GT_5:
module = self.config.pluginmanager._importconftest(
self.fspath, self.config.getoption("importmode"))
Expand All @@ -203,10 +215,18 @@ def collect(self):
self.fspath)
else:
try:
module = self.fspath.pyimport()
if PYTEST_GT_5:
from _pytest.pathlib import import_path

if PYTEST_GE_6_3:
module = import_path(fspath, root=self.config.rootpath)
elif PYTEST_GT_5:
module = import_path(fspath)
else:
module = fspath.pyimport()
except ImportError:
if self.config.getvalue("doctest_ignore_import_errors"):
pytest.skip("unable to import module %r" % self.fspath)
pytest.skip("unable to import module %r" % fspath)
else:
raise

Expand Down Expand Up @@ -262,10 +282,16 @@ def collect(self):
class DocTestTextfilePlus(pytest.Module):

def collect(self):
if PYTEST_GE_6_3:
fspath = self.path
filepath = self.path.name
else:
fspath = self.fspath
filepath = self.fspath.basename

encoding = self.config.getini("doctest_encoding")
text = self.fspath.read_text(encoding)
filename = str(self.fspath)
name = self.fspath.basename
text = fspath.read_text(encoding)
filename = str(fspath)
globs = {"__name__": "__main__"}

optionflags = get_optionflags(self) | FIX
Expand All @@ -274,7 +300,7 @@ def collect(self):
verbose=False, optionflags=optionflags, checker=OutputChecker())

parser = DocTestParserPlus()
test = parser.get_doctest(text, globs, name, filename, 0)
test = parser.get_doctest(text, globs, filepath, filename, 0)
if test.examples:
try:
yield doctest_plugin.DoctestItem.from_parent(
Expand Down Expand Up @@ -438,7 +464,14 @@ def pytest_ignore_collect(self, path, config):
dirpath = Path(path).parent
else:
dirpath = path.dirpath()
collect_ignore = config._getconftest_pathlist("collect_ignore", path=dirpath)

try:
collect_ignore = config._getconftest_pathlist("collect_ignore", path=dirpath)
except TypeError:
# Pytest 7.0+
Comment on lines +470 to +471
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not doing version check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about it ;). And this is a bit of a mess, some changes are in 6.3 which is not released yet, and some will be in 7.0. And dev is currently 6.3.devXXX though it includes the 7.0 changes apparently.
So I added PYTEST_GT_6_3 after but I'm still not sure if that's a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean try/except is easy to do, distinguishing 6.3 and 7.0 is currently not obvious.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, maybe we can merge as is but open issue to revisit in the future because you don't want the stable pytest to keep hitting the except block.

collect_ignore = config._getconftest_pathlist("collect_ignore",
path=dirpath,
rootpath=config.rootpath)

# The collect_ignore conftest.py variable should cause all test
# runners to ignore this file and all subfiles and subdirectories
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ deps =
pytest53: pytest==5.3.*
pytest60: pytest==6.0.*
pytest61: pytest==6.1.*
pytest62: pytest==6.2.*
pytestdev: git+https:/pytest-dev/pytest#egg=pytest

commands =
Expand Down