File: conftest.py

package info (click to toggle)
astropy 7.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 35,328 kB
  • sloc: python: 233,437; ansic: 55,264; javascript: 17,680; lex: 8,621; sh: 3,317; xml: 2,287; makefile: 191
file content (46 lines) | stat: -rw-r--r-- 1,766 bytes parent folder | download | duplicates (2)
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