|
5 | 5 | import subprocess |
6 | 6 | import sys |
7 | 7 | from inspect import cleandoc |
| 8 | +from threading import Lock |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 |
|
@@ -37,26 +38,130 @@ def test_paths(): |
37 | 38 | assert "testpkg" in paths.get_cache_dir(rootname="testpkg") |
38 | 39 |
|
39 | 40 |
|
| 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 | + |
40 | 60 | @pytest.mark.parametrize( |
41 | | - "environment_variable,func", |
| 61 | + "env_var_template", |
42 | 62 | [ |
43 | 63 | # 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), |
46 | 66 | ], |
47 | 67 | ) |
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() |
51 | 80 | 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 |
53 | 119 |
|
54 | 120 |
|
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): |
56 | 163 | # Check that we start in an understood state. |
57 | 164 | 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) |
60 | 165 |
|
61 | 166 | orig_config_dir = paths.get_config_dir(rootname="astropy") |
62 | 167 | (temp_config_dir := tmp_path / "config").mkdir() |
@@ -84,9 +189,8 @@ def test_func(): |
84 | 189 | assert configuration._cfgobjs == OLD_CONFIG |
85 | 190 |
|
86 | 191 |
|
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): |
90 | 194 | orig_cache_dir = paths.get_cache_dir(rootname="astropy") |
91 | 195 | (temp_cache_dir := tmp_path / "cache").mkdir() |
92 | 196 | temp_astropy_cache = temp_cache_dir / "astropy" |
@@ -414,17 +518,13 @@ def test_help_invalid_config_item(): |
414 | 518 | conf.help("bad_name") |
415 | 519 |
|
416 | 520 |
|
| 521 | +@pytest.mark.usefixtures("isolated_setup") |
417 | 522 | def test_config_noastropy_fallback(monkeypatch): |
418 | 523 | """ |
419 | 524 | Tests to make sure configuration items fall back to their defaults when |
420 | 525 | there's a problem accessing the astropy directory |
421 | 526 | """ |
422 | 527 |
|
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 | | - |
428 | 528 | # make sure the _find_or_create_root_dir function fails as though the |
429 | 529 | # astropy dir could not be accessed |
430 | 530 | @classmethod |
|
0 commit comments