Skip to content

Commit

Permalink
Merge pull request #233 from flying-sheep/fix-import-mode
Browse files Browse the repository at this point in the history
Support import-mode
  • Loading branch information
bsipocz authored Nov 6, 2023
2 parents f61f87d + 839abb0 commit b002adc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

- Versions of Python <3.8 are no longer supported. [#217]

- Fix erroneous attempt to import ``__main__.py`` by skipping it [#232]
- Fix erroneous attempt to import ``__main__.py`` by skipping it. [#232]

- Respect pytest ``--import-mode``. [#233]


1.0.0 (2023-08-11)
Expand Down
5 changes: 3 additions & 2 deletions pytest_doctestplus/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,12 @@ def collect(self):
try:
if PYTEST_GT_5:
from _pytest.pathlib import import_path
mode = self.config.getoption("importmode")

if PYTEST_GE_7_0:
module = import_path(fspath, root=self.config.rootpath)
module = import_path(fspath, mode=mode, root=self.config.rootpath)
elif PYTEST_GT_5:
module = import_path(fspath)
module = import_path(fspath, mode=mode)
else:
module = fspath.pyimport()
except ImportError:
Expand Down
25 changes: 25 additions & 0 deletions tests/test_doctestplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,31 @@ def test_doctest_subpackage_requires(subpackage_requires_testdir, caplog):
assert caplog.text == ''


@pytest.mark.parametrize(('import_mode', 'expected'), [
pytest.param('importlib', dict(passed=2), marks=pytest.mark.skipif(PYTEST_LT_6, reason="importlib import mode not supported on Pytest <6"), id="importlib"),
pytest.param('append', dict(failed=1), id="append"),
pytest.param('prepend', dict(failed=1), id="prepend"),
])
def test_import_mode(testdir, import_mode, expected):
"""Test that two files with the same name but in different folders work with --import-mode=importlib."""
a = testdir.mkdir('a')
b = testdir.mkdir('b')

pyfile = dedent("""
def f():
'''
>>> 1
1
'''
""")

a.join('testcode.py').write(pyfile)
b.join('testcode.py').write(pyfile)

reprec = testdir.inline_run(str(testdir), "--doctest-plus", f"--import-mode={import_mode}")
reprec.assertoutcome(**expected)


def test_doctest_skip(testdir):
testdir.makeini(
"""
Expand Down

0 comments on commit b002adc

Please sign in to comment.