Skip to content

Commit a147ac5

Browse files
committed
TST: add tests for astropygh-17507
1 parent 0c0557c commit a147ac5

6 files changed

Lines changed: 145 additions & 47 deletions

File tree

‎astropy/config/tests/test_configs.py‎

Lines changed: 116 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import sys
77
from inspect import cleandoc
8+
from threading import Lock
89

910
import pytest
1011

@@ -37,26 +38,128 @@ def test_paths():
3738
assert "testpkg" in paths.get_cache_dir(rootname="testpkg")
3839

3940

41+
_ISOLATED_SETUP_LOCK = Lock()
42+
43+
44+
@pytest.fixture
45+
def isolated_setup(monkeypatch):
46+
# ignore global state of the test session
47+
# and preserve thread safety across all users of this fixture
48+
with _ISOLATED_SETUP_LOCK:
49+
monkeypatch.delenv("ASTROPY_CACHE_DIR", raising=False)
50+
monkeypatch.delenv("ASTROPY_CONFIG_DIR", raising=False)
51+
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
52+
monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)
53+
54+
monkeypatch.setattr(paths.set_temp_cache, "_temp_path", None)
55+
monkeypatch.setattr(paths.set_temp_config, "_temp_path", None)
56+
57+
yield
58+
59+
4060
@pytest.mark.parametrize(
41-
"environment_variable,func",
61+
"env_var_template",
4262
[
4363
# Regression test for #17514 - XDG_CACHE_HOME had no effect
44-
pytest.param("XDG_CACHE_HOME", paths.get_cache_dir_path, id="cache"),
45-
pytest.param("XDG_CONFIG_HOME", paths.get_config_dir_path, id="config"),
64+
pytest.param("XDG_{}_HOME", id="xdg"),
65+
pytest.param("ASTROPY_{}_DIR", id="astropy", marks=pytest.mark.xfail),
4666
],
4767
)
48-
def test_xdg_variables(monkeypatch, tmp_path, environment_variable, func):
49-
config_dir = tmp_path / "astropy"
50-
config_dir.mkdir()
68+
@pytest.mark.parametrize(
69+
"dir_type, func",
70+
[
71+
pytest.param("CACHE", paths.get_cache_dir_path, id="cache"),
72+
pytest.param("CONFIG", paths.get_config_dir_path, id="config"),
73+
],
74+
)
75+
@pytest.mark.usefixtures("isolated_setup")
76+
def test_env_variables(monkeypatch, tmp_path, env_var_template, dir_type, func):
77+
environment_variable = env_var_template.format(dir_type)
78+
target_dir = tmp_path / "astropy"
79+
target_dir.mkdir()
5180
monkeypatch.setenv(environment_variable, str(tmp_path))
52-
assert func() == config_dir
81+
assert func() == target_dir
82+
83+
84+
@pytest.mark.parametrize(
85+
"astropy_env_var, xdg_env_var, func",
86+
[
87+
pytest.param(
88+
"ASTROPY_CACHE_DIR",
89+
"XDG_CACHE_HOME",
90+
paths.get_cache_dir_path,
91+
id="cache",
92+
marks=pytest.mark.xfail,
93+
),
94+
pytest.param(
95+
"ASTROPY_CONFIG_DIR",
96+
"XDG_CONFIG_HOME",
97+
paths.get_config_dir_path,
98+
id="config",
99+
marks=pytest.mark.xfail,
100+
),
101+
],
102+
)
103+
@pytest.mark.usefixtures("isolated_setup")
104+
def test_env_variables_priority(
105+
monkeypatch, tmp_path, astropy_env_var, xdg_env_var, func
106+
):
107+
# ASTROPY_* environment variables should have priority over XDG_*
108+
astropy_target_dir = tmp_path / "astropy" / "astropy"
109+
astropy_target_dir.mkdir(parents=True)
110+
monkeypatch.setenv(astropy_env_var, str(tmp_path / "astropy"))
111+
112+
xdg_target_dir = tmp_path / "xdg" / "astropy"
113+
xdg_target_dir.mkdir(parents=True)
114+
monkeypatch.setenv(xdg_env_var, str(tmp_path / "xdg"))
115+
116+
assert func() == astropy_target_dir
53117

54118

55-
def test_set_temp_config(tmp_path, monkeypatch):
119+
@pytest.mark.parametrize(
120+
"env_var_template",
121+
[
122+
pytest.param("XDG_{}_HOME", id="xdg"),
123+
pytest.param("ASTROPY_{}_DIR", id="astropy", marks=pytest.mark.xfail),
124+
],
125+
)
126+
@pytest.mark.parametrize("dir_type", ["cache", "config"])
127+
@pytest.mark.usefixtures("isolated_setup")
128+
def test_context_over_environment(
129+
monkeypatch,
130+
tmp_path,
131+
env_var_template,
132+
dir_type,
133+
):
134+
match dir_type:
135+
case "cache":
136+
cls = paths.set_temp_cache
137+
func = paths.get_cache_dir_path
138+
case "config":
139+
cls = paths.set_temp_config
140+
func = paths.get_config_dir_path
141+
case _:
142+
raise ValueError
143+
144+
# context managers should shadow environment variables
145+
env_target_dir = tmp_path / "astropy"
146+
env_target_dir.mkdir()
147+
monkeypatch.setenv(env_var_template.format(dir_type.upper()), str(tmp_path))
148+
149+
assert func() == env_target_dir
150+
151+
ctx_target_dir = tmp_path / "context"
152+
ctx_target_dir.mkdir()
153+
with cls(ctx_target_dir):
154+
assert func() == ctx_target_dir / "astropy"
155+
156+
assert func() == env_target_dir
157+
158+
159+
@pytest.mark.usefixtures("isolated_setup")
160+
def test_set_temp_config(tmp_path):
56161
# Check that we start in an understood state.
57162
assert configuration._cfgobjs == OLD_CONFIG
58-
# Temporarily remove any temporary overrides of the configuration dir.
59-
monkeypatch.setattr(paths.set_temp_config, "_temp_path", None)
60163

61164
orig_config_dir = paths.get_config_dir(rootname="astropy")
62165
(temp_config_dir := tmp_path / "config").mkdir()
@@ -84,9 +187,8 @@ def test_func():
84187
assert configuration._cfgobjs == OLD_CONFIG
85188

86189

87-
def test_set_temp_cache(tmp_path, monkeypatch):
88-
monkeypatch.setattr(paths.set_temp_cache, "_temp_path", None)
89-
190+
@pytest.mark.usefixtures("isolated_setup")
191+
def test_set_temp_cache(tmp_path):
90192
orig_cache_dir = paths.get_cache_dir(rootname="astropy")
91193
(temp_cache_dir := tmp_path / "cache").mkdir()
92194
temp_astropy_cache = temp_cache_dir / "astropy"
@@ -414,17 +516,13 @@ def test_help_invalid_config_item():
414516
conf.help("bad_name")
415517

416518

519+
@pytest.mark.usefixtures("isolated_setup")
417520
def test_config_noastropy_fallback(monkeypatch):
418521
"""
419522
Tests to make sure configuration items fall back to their defaults when
420523
there's a problem accessing the astropy directory
421524
"""
422525

423-
# make sure the config directory is not searched
424-
monkeypatch.setenv("XDG_CONFIG_HOME", "foo")
425-
monkeypatch.delenv("XDG_CONFIG_HOME")
426-
monkeypatch.setattr(paths.set_temp_config, "_temp_path", None)
427-
428526
# make sure the _find_or_create_root_dir function fails as though the
429527
# astropy dir could not be accessed
430528
@classmethod

‎astropy/conftest.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ def pytest_configure(config):
7777
# is also set in the test runner, but we need to also set it here for
7878
# things to work properly in parallel mode
7979

80-
builtins._xdg_config_home_orig = os.environ.get("XDG_CONFIG_HOME")
81-
builtins._xdg_cache_home_orig = os.environ.get("XDG_CACHE_HOME")
80+
builtins._astropy_config_dir_orig = os.environ.get("ASTROPY_CONFIG_HOME")
81+
builtins._astropy_cache_dir_orig = os.environ.get("ASTROPY_CACHE_HOME")
8282

83-
os.environ["XDG_CONFIG_HOME"] = tempfile.mkdtemp("astropy_config")
84-
os.environ["XDG_CACHE_HOME"] = tempfile.mkdtemp("astropy_cache")
83+
os.environ["ASTROPY_CONFIG_DIR"] = tempfile.mkdtemp("astropy_config")
84+
os.environ["ASTROPY_CACHE_DIR"] = tempfile.mkdtemp("astropy_cache")
8585

86-
Path(os.environ["XDG_CONFIG_HOME"]).joinpath("astropy").mkdir()
87-
Path(os.environ["XDG_CACHE_HOME"]).joinpath("astropy").mkdir()
86+
Path(os.environ["ASTROPY_CONFIG_DIR"]).joinpath("astropy").mkdir()
87+
Path(os.environ["ASTROPY_CACHE_DIR"]).joinpath("astropy").mkdir()
8888

8989
config.option.astropy_header = True
9090
PYTEST_HEADER_MODULES["PyERFA"] = "erfa"
@@ -129,15 +129,15 @@ def pytest_unconfigure(config):
129129
mpl.rcParams.update(matplotlibrc_cache)
130130
matplotlibrc_cache.clear()
131131

132-
if builtins._xdg_config_home_orig is None:
133-
os.environ.pop("XDG_CONFIG_HOME")
132+
if builtins._astropy_config_dir_orig is None:
133+
os.environ.pop("ASTROPY_CONFIG_DIR")
134134
else:
135-
os.environ["XDG_CONFIG_HOME"] = builtins._xdg_config_home_orig
135+
os.environ["ASTROPY_CONFIG_DIR"] = builtins._astropy_config_dir_orig
136136

137-
if builtins._xdg_cache_home_orig is None:
138-
os.environ.pop("XDG_CACHE_HOME")
137+
if builtins._astropy_cache_dir_orig is None:
138+
os.environ.pop("ASTROPY_CACHE_DIR")
139139
else:
140-
os.environ["XDG_CACHE_HOME"] = builtins._xdg_cache_home_orig
140+
os.environ["ASTROPY_CACHE_DIR"] = builtins._astropy_cache_dir_orig
141141

142142

143143
def pytest_terminal_summary(terminalreporter):

‎astropy/tests/runner.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,17 @@ def run_tests(self, **kwargs):
243243
# don't know about the temporary config/cache.
244244
# Note, this is superfluous if the config_dir option to pytest is in use,
245245
# but it's also harmless
246-
orig_xdg_config = os.environ.get("XDG_CONFIG_HOME")
246+
astropy_config_dir_orig = os.environ.get("ASTROPY_CONFIG_DI")
247247
with tempfile.TemporaryDirectory("astropy_config") as astropy_config:
248248
Path(astropy_config, "astropy").mkdir()
249-
os.environ["XDG_CONFIG_HOME"] = astropy_config
249+
os.environ["ASTROPY_CONFIG_DIR"] = astropy_config
250250
try:
251251
return pytest.main(args=args, plugins=plugins)
252252
finally:
253-
if orig_xdg_config is None:
254-
os.environ.pop("XDG_CONFIG_HOME", None)
253+
if astropy_config_dir_orig is None:
254+
os.environ.pop("ASTROPY_CONFIG_DIR", None)
255255
else:
256-
os.environ["XDG_CONFIG_HOME"] = orig_xdg_config
256+
os.environ["ASTROPY_CONFIG_DIR"] = astropy_config_dir_orig
257257

258258
@classmethod
259259
def make_test_runner_in(cls, path):

‎astropy/utils/tests/test_data.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,10 +1099,10 @@ def test_data_noastropy_fallback(monkeypatch):
10991099
conf.delete_temporary_downloads_at_exit = True
11001100

11011101
# make sure the config and cache directories are not searched
1102-
monkeypatch.setenv("XDG_CONFIG_HOME", "foo")
1103-
monkeypatch.delenv("XDG_CONFIG_HOME")
1104-
monkeypatch.setenv("XDG_CACHE_HOME", "bar")
1105-
monkeypatch.delenv("XDG_CACHE_HOME")
1102+
monkeypatch.delenv("ASTROPY_CONFIG_DIR", raising=False)
1103+
monkeypatch.delenv("ASTROPY_CACHE_DIR", raising=False)
1104+
monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)
1105+
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
11061106

11071107
monkeypatch.setattr(paths.set_temp_config, "_temp_path", None)
11081108
monkeypatch.setattr(paths.set_temp_cache, "_temp_path", None)

‎conftest.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ def pytest_report_header(config):
6969
# Make sure we use temporary directories for the config and cache
7070
# so that the tests are insensitive to local configuration.
7171

72-
os.environ["XDG_CONFIG_HOME"] = tempfile.mkdtemp("astropy_config")
73-
os.environ["XDG_CACHE_HOME"] = tempfile.mkdtemp("astropy_cache")
72+
os.environ["ASTROPY_CONFIG_DIR"] = tempfile.mkdtemp("astropy_config")
73+
os.environ["ASTROPY_CACHE_DIR"] = tempfile.mkdtemp("astropy_cache")
7474

75-
Path(os.environ["XDG_CONFIG_HOME"]).joinpath("astropy").mkdir()
76-
Path(os.environ["XDG_CACHE_HOME"]).joinpath("astropy").mkdir()
75+
Path(os.environ["ASTROPY_CONFIG_DIR"]).joinpath("astropy").mkdir()
76+
Path(os.environ["ASTROPY_CACHE_DIR"]).joinpath("astropy").mkdir()
7777

7878
# Note that we don't need to change the environment variables back or remove
7979
# them after testing, because they are only changed for the duration of the

‎docs/conftest.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
# Make sure we use temporary directories for the config and cache
1515
# so that the tests are insensitive to local configuration.
1616

17-
os.environ["XDG_CONFIG_HOME"] = tempfile.mkdtemp("astropy_config")
18-
os.environ["XDG_CACHE_HOME"] = tempfile.mkdtemp("astropy_cache")
17+
os.environ["ASTROPY_CONFIG_DIR"] = tempfile.mkdtemp("astropy_config")
18+
os.environ["ASTROPY_CACHE_DIR"] = tempfile.mkdtemp("astropy_cache")
1919

20-
Path(os.environ["XDG_CONFIG_HOME"]).joinpath("astropy").mkdir()
21-
Path(os.environ["XDG_CACHE_HOME"]).joinpath("astropy").mkdir()
20+
Path(os.environ["ASTROPY_CONFIG_DIR"]).joinpath("astropy").mkdir()
21+
Path(os.environ["ASTROPY_CACHE_DIR"]).joinpath("astropy").mkdir()
2222

2323
# Note that we don't need to change the environment variables back or remove
2424
# them after testing, because they are only changed for the duration of the

0 commit comments

Comments
 (0)