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