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

Jail doctestplus outputs in tmpdir #98

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 17 additions & 0 deletions pytest_doctestplus/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ def __exit__(self, *args, **kwargs):
""".lstrip()


@pytest.fixture(autouse=True)
def _docdir(request):
"""Run doctests in isolated tmpdir so outputs do not end up in repo"""
# Trigger ONLY for doctestplus
doctest_plugin = request.config.pluginmanager.getplugin("pytest_doctestplus")
if isinstance(request.node, doctest_plugin.DocTestTextfilePlus):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure how to access the class name when this fixture lives in the plugin itself and not over at core lib.

AttributeError: module 'pytest_doctestplus.plugin' has no attribute 'DocTestTextfilePlus'

# Don't apply this fixture to io.rst. It reads files and doesn't write
if "io.rst" not in request.node.name:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

io.rst sounds awfully specific to astropy core library. How do we generalize this?

Copy link
Member

Choose a reason for hiding this comment

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

Only based on the narrative, I don't get why would it hurt to still apply when it reads files but not writes?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this is specific to the astropy core library. The problem with that doctest is that it looks for an input file in the working directory. If one makes the working dir a tmpdir, then the file is no longer in the working directory, so one needs a general way of getting the file. Using importlib.resources might be the solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It is available as an external package for python 3.6 and earlier. importlib_resources One can do something like.

https://github.com/spacetelescope/asdf/blob/522f25b60715612dd1369a644c756fb7572b89d1/asdf/tests/helpers.py#L45-L51

tmpdir = request.getfixturevalue('tmpdir')
with tmpdir.as_cwd():
yield
else:
yield
else:
yield


# these pytest hooks allow us to mark tests and run the marked tests with
# specific command line options.
def pytest_addoption(parser):
Expand Down