Skip to content

Commit a1a0560

Browse files
committed
TST: add tests for astropygh-17507
1 parent a34c310 commit a1a0560

1 file changed

Lines changed: 120 additions & 7 deletions

File tree

‎astropy/config/tests/test_configs.py‎

Lines changed: 120 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,131 @@ def test_paths():
3838

3939

4040
@pytest.mark.parametrize(
41-
"environment_variable,func",
41+
"environment_variable, cls, func",
4242
[
4343
# 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"),
44+
pytest.param(
45+
"XDG_CACHE_HOME",
46+
paths.set_temp_cache,
47+
paths.get_cache_dir_path,
48+
id="xdg-cache",
49+
),
50+
pytest.param(
51+
"ASTROPY_CACHE_DIR",
52+
paths.set_temp_cache,
53+
paths.get_cache_dir_path,
54+
id="astropy-cache",
55+
marks=pytest.mark.xfail,
56+
),
57+
pytest.param(
58+
"XDG_CONFIG_HOME",
59+
paths.set_temp_config,
60+
paths.get_config_dir_path,
61+
id="xdg-config",
62+
),
63+
pytest.param(
64+
"ASTROPY_CONFIG_DIR",
65+
paths.set_temp_config,
66+
paths.get_config_dir_path,
67+
id="astropy-config",
68+
marks=pytest.mark.xfail,
69+
),
70+
],
71+
)
72+
def test_env_variables(monkeypatch, tmp_path, environment_variable, cls, func):
73+
target_dir = tmp_path / "astropy"
74+
target_dir.mkdir()
75+
monkeypatch.setenv(environment_variable, str(tmp_path))
76+
77+
monkeypatch.setattr(cls, "_temp_path", None)
78+
assert func() == target_dir
79+
80+
81+
@pytest.mark.parametrize(
82+
"astropy_env_var, xdg_env_var, cls, func",
83+
[
84+
pytest.param(
85+
"ASTROPY_CACHE_DIR",
86+
"XDG_CACHE_HOME",
87+
paths.set_temp_cache,
88+
paths.get_cache_dir_path,
89+
id="cache",
90+
marks=pytest.mark.xfail,
91+
),
92+
pytest.param(
93+
"ASTROPY_CONFIG_DIR",
94+
"XDG_CONFIG_HOME",
95+
paths.set_temp_config,
96+
paths.get_config_dir_path,
97+
id="config",
98+
marks=pytest.mark.xfail,
99+
),
100+
],
101+
)
102+
def test_env_variables_priority(
103+
monkeypatch, tmp_path, astropy_env_var, xdg_env_var, cls, func
104+
):
105+
# ASTROPY_* environment variables should have priority over XDG_*
106+
astropy_target_dir = tmp_path / "astropy" / "astropy"
107+
astropy_target_dir.mkdir(parents=True)
108+
monkeypatch.setenv(astropy_env_var, str(tmp_path / "astropy"))
109+
110+
xdg_target_dir = tmp_path / "xdg" / "astropy"
111+
xdg_target_dir.mkdir(parents=True)
112+
monkeypatch.setenv(xdg_env_var, str(tmp_path / "xdg"))
113+
114+
monkeypatch.setattr(cls, "_temp_path", None)
115+
116+
assert func() == astropy_target_dir
117+
118+
119+
@pytest.mark.parametrize(
120+
"environment_variable, cls, func",
121+
[
122+
pytest.param(
123+
"ASTROPY_CACHE_DIR",
124+
paths.set_temp_cache,
125+
paths.get_cache_dir_path,
126+
id="astropy-cache",
127+
marks=pytest.mark.xfail,
128+
),
129+
pytest.param(
130+
"ASTROPY_CONFIG_DIR",
131+
paths.set_temp_config,
132+
paths.get_config_dir_path,
133+
id="astropy-config",
134+
marks=pytest.mark.xfail,
135+
),
136+
pytest.param(
137+
"XDG_CACHE_HOME",
138+
paths.set_temp_cache,
139+
paths.get_cache_dir_path,
140+
id="xdg-cache",
141+
),
142+
pytest.param(
143+
"XDG_CONFIG_HOME",
144+
paths.set_temp_config,
145+
paths.get_config_dir_path,
146+
id="xdg-config",
147+
),
46148
],
47149
)
48-
def test_xdg_variables(monkeypatch, tmp_path, environment_variable, func):
49-
config_dir = tmp_path / "astropy"
50-
config_dir.mkdir()
150+
def test_context_over_environment(
151+
monkeypatch, tmp_path, environment_variable, cls, func
152+
):
153+
# context managers should shadow environment variables
154+
env_target_dir = tmp_path / "astropy"
155+
env_target_dir.mkdir()
51156
monkeypatch.setenv(environment_variable, str(tmp_path))
52-
assert func() == config_dir
157+
158+
assert func() == env_target_dir
159+
160+
ctx_target_dir = tmp_path / "context"
161+
ctx_target_dir.mkdir()
162+
with cls(ctx_target_dir):
163+
assert func() == ctx_target_dir / "astropy"
164+
165+
assert func() == env_target_dir
53166

54167

55168
def test_set_temp_config(tmp_path, monkeypatch):

0 commit comments

Comments
 (0)