Skip to content

Commit a0b7a5f

Browse files
committed
TST: add tests for astropygh-17507
1 parent f5317b4 commit a0b7a5f

6 files changed

Lines changed: 148 additions & 44 deletions

File tree

‎astropy/config/tests/test_configs.py‎

Lines changed: 118 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,130 @@ 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, cls, func",
86+
[
87+
pytest.param(
88+
"ASTROPY_CACHE_DIR",
89+
"XDG_CACHE_HOME",
90+
paths.set_temp_cache,
91+
paths.get_cache_dir_path,
92+
id="cache",
93+
marks=pytest.mark.xfail,
94+
),
95+
pytest.param(
96+
"ASTROPY_CONFIG_DIR",
97+
"XDG_CONFIG_HOME",
98+
paths.set_temp_config,
99+
paths.get_config_dir_path,
100+
id="config",
101+
marks=pytest.mark.xfail,
102+
),
103+
],
104+
)
105+
@pytest.mark.usefixtures("isolated_setup")
106+
def test_env_variables_priority(
107+
monkeypatch, tmp_path, astropy_env_var, xdg_env_var, cls, func
108+
):
109+
# ASTROPY_* environment variables should have priority over XDG_*
110+
astropy_target_dir = tmp_path / "astropy" / "astropy"
111+
astropy_target_dir.mkdir(parents=True)
112+
monkeypatch.setenv(astropy_env_var, str(tmp_path / "astropy"))
113+
114+
xdg_target_dir = tmp_path / "xdg" / "astropy"
115+
xdg_target_dir.mkdir(parents=True)
116+
monkeypatch.setenv(xdg_env_var, str(tmp_path / "xdg"))
117+
118+
assert func() == astropy_target_dir
53119

54120

55-
def test_set_temp_config(tmp_path, monkeypatch):
121+
@pytest.mark.parametrize(
122+
"env_var_template",
123+
[
124+
pytest.param("XDG_{}_HOME", id="xdg"),
125+
pytest.param("ASTROPY_{}_DIR", id="astropy", marks=pytest.mark.xfail),
126+
],
127+
)
128+
@pytest.mark.parametrize("dir_type", ["cache", "config"])
129+
@pytest.mark.usefixtures("isolated_setup")
130+
def test_context_over_environment(
131+
monkeypatch,
132+
tmp_path,
133+
env_var_template,
134+
dir_type,
135+
):
136+
match dir_type:
137+
case "cache":
138+
cls = paths.set_temp_cache
139+
func = paths.get_cache_dir_path
140+
case "config":
141+
cls = paths.set_temp_config
142+
func = paths.get_config_dir_path
143+
case _:
144+
raise ValueError
145+
146+
# context managers should shadow environment variables
147+
env_target_dir = tmp_path / "astropy"
148+
env_target_dir.mkdir()
149+
monkeypatch.setenv(env_var_template.format(dir_type.upper()), str(tmp_path))
150+
151+
assert func() == env_target_dir
152+
153+
ctx_target_dir = tmp_path / "context"
154+
ctx_target_dir.mkdir()
155+
with cls(ctx_target_dir):
156+
assert func() == ctx_target_dir / "astropy"
157+
158+
assert func() == env_target_dir
159+
160+
161+
@pytest.mark.usefixtures("isolated_setup")
162+
def test_set_temp_config(tmp_path):
56163
# Check that we start in an understood state.
57164
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)
60165

61166
orig_config_dir = paths.get_config_dir(rootname="astropy")
62167
(temp_config_dir := tmp_path / "config").mkdir()
@@ -84,9 +189,8 @@ def test_func():
84189
assert configuration._cfgobjs == OLD_CONFIG
85190

86191

87-
def test_set_temp_cache(tmp_path, monkeypatch):
88-
monkeypatch.setattr(paths.set_temp_cache, "_temp_path", None)
89-
192+
@pytest.mark.usefixtures("isolated_setup")
193+
def test_set_temp_cache(tmp_path):
90194
orig_cache_dir = paths.get_cache_dir(rootname="astropy")
91195
(temp_cache_dir := tmp_path / "cache").mkdir()
92196
temp_astropy_cache = temp_cache_dir / "astropy"
@@ -414,17 +518,13 @@ def test_help_invalid_config_item():
414518
conf.help("bad_name")
415519

416520

521+
@pytest.mark.usefixtures("isolated_setup")
417522
def test_config_noastropy_fallback(monkeypatch):
418523
"""
419524
Tests to make sure configuration items fall back to their defaults when
420525
there's a problem accessing the astropy directory
421526
"""
422527

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-
428528
# make sure the _find_or_create_root_dir function fails as though the
429529
# astropy dir could not be accessed
430530
@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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,13 @@ 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("ASTROPY_CONFIG_DIR", "foo")
1103+
monkeypatch.delenv("ASTROPY_CONFIG_DIR")
1104+
monkeypatch.setenv("ASTROPY_CACHE_DIR", "foo")
1105+
monkeypatch.delenv("ASTROPY_CACHE_DIR")
11021106
monkeypatch.setenv("XDG_CONFIG_HOME", "foo")
11031107
monkeypatch.delenv("XDG_CONFIG_HOME")
1104-
monkeypatch.setenv("XDG_CACHE_HOME", "bar")
1108+
monkeypatch.setenv("XDG_CACHE_HOME", "foo")
11051109
monkeypatch.delenv("XDG_CACHE_HOME")
11061110

11071111
monkeypatch.setattr(paths.set_temp_config, "_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)