Skip to content

Commit 433a534

Browse files
committed
Change setting up directories for our test runner
Testing the effect of the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables using the `astropy` test runner (like the double test CI job does) did not work because the test runner used `set_temp_cache` and `set_temp_config` context managers, which have priority over the environment variables. The test runner has no need to configure the cache directory because `pytest` is configured to do that anyways. Setting up a temporary configuration directory is required to guarantee that `pytest` uses the temporary `astropy` configuration before its own configuration is read, but `set_temp_cache` should nonetheless be avoided.
1 parent 49765f4 commit 433a534

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

‎astropy/tests/runner.py‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import warnings
1111
from functools import wraps
1212
from importlib.util import find_spec
13+
from pathlib import Path
1314

14-
from astropy.config.paths import set_temp_cache, set_temp_config
1515
from astropy.utils import find_current_module
1616
from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyWarning
1717

@@ -235,23 +235,25 @@ def run_tests(self, **kwargs):
235235
else:
236236
plugins = []
237237

238-
# Override the config locations to not make a new directory nor use
239-
# existing cache or config. Note that we need to do this here in
238+
# Avoid the existing config. Note that we need to do this here in
240239
# addition to in conftest.py - for users running tests interactively
241240
# in e.g. IPython, conftest.py would get read in too late, so we need
242241
# to do it here - but at the same time the code here doesn't work when
243242
# running tests in parallel mode because this uses subprocesses which
244243
# don't know about the temporary config/cache.
245-
astropy_config = tempfile.mkdtemp("astropy_config")
246-
astropy_cache = tempfile.mkdtemp("astropy_cache")
247-
248-
# Have to use nested with statements for cross-Python support
249-
# Note, using these context managers here is superfluous if the
250-
# config_dir or cache_dir options to pytest are in use, but it's
251-
# also harmless to nest the contexts
252-
with set_temp_config(astropy_config, delete=True):
253-
with set_temp_cache(astropy_cache, delete=True):
244+
# Note, this is superfluous if the config_dir option to pytest is in use,
245+
# but it's also harmless
246+
orig_xdg_config = os.environ.get("XDG_CONFIG_HOME")
247+
with tempfile.TemporaryDirectory("astropy_config") as astropy_config:
248+
Path(astropy_config, "astropy").mkdir()
249+
os.environ["XDG_CONFIG_HOME"] = astropy_config
250+
try:
254251
return pytest.main(args=args, plugins=plugins)
252+
finally:
253+
if orig_xdg_config is None:
254+
os.environ.pop("XDG_CONFIG_HOME", None)
255+
else:
256+
os.environ["XDG_CONFIG_HOME"] = orig_xdg_config
255257

256258
@classmethod
257259
def make_test_runner_in(cls, path):

0 commit comments

Comments
 (0)