-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathconftest.py
More file actions
46 lines (37 loc) · 1.72 KB
/
conftest.py
File metadata and controls
46 lines (37 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# This file needs to be included here to make sure commands such
# as ``pytest docs/...`` works, since this
# will ignore the conftest.py file at the root of the repository
# and the one in astropy/conftest.py
import os
import tempfile
from pathlib import Path
import pytest
# Make sure we use temporary directories for the config and cache
# so that the tests are insensitive to local configuration.
os.environ["XDG_CONFIG_HOME"] = tempfile.mkdtemp("astropy_config")
os.environ["XDG_CACHE_HOME"] = tempfile.mkdtemp("astropy_cache")
Path(os.environ["XDG_CONFIG_HOME"]).joinpath("astropy").mkdir()
Path(os.environ["XDG_CACHE_HOME"]).joinpath("astropy").mkdir()
# Note that we don't need to change the environment variables back or remove
# them after testing, because they are only changed for the duration of the
# Python process, and this configuration only matters if running pytest
# directly, not from e.g. an IPython session.
@pytest.fixture(autouse=True)
def _docdir(request):
"""Run doctests in isolated tmp_path so outputs do not end up in repo."""
# Trigger ONLY for doctestplus
doctest_plugin = request.config.pluginmanager.getplugin("doctestplus")
if isinstance(request.node.parent, doctest_plugin._doctest_textfile_item_cls):
# Don't apply this fixture to io.rst. It reads files and doesn't write.
# Implementation from https://github.com/pytest-dev/pytest/discussions/10437
if "io.rst" not in request.node.name:
old_cwd = Path.cwd()
tmp_path = request.getfixturevalue("tmp_path")
os.chdir(tmp_path)
yield
os.chdir(old_cwd)
else:
yield
else:
yield