|
10 | 10 | import tempfile
|
11 | 11 | from unittest.mock import patch
|
12 | 12 | import pytest
|
| 13 | +import site |
13 | 14 | import subprocess
|
14 | 15 | import sys
|
15 | 16 | import warnings
|
|
47 | 48 |
|
48 | 49 | jupyter_config_env = '/jupyter-cfg'
|
49 | 50 | config_env = patch.dict('os.environ', {'JUPYTER_CONFIG_DIR': jupyter_config_env})
|
| 51 | +prefer_env = patch.dict('os.environ', {'JUPYTER_PREFER_ENV_PATH': 'True'}) |
| 52 | + |
| 53 | +resetenv = patch.dict(os.environ) |
| 54 | + |
| 55 | +def setup_module(): |
| 56 | + resetenv.start() |
| 57 | + os.environ.pop('JUPYTER_PREFER_ENV_PATH', None) |
| 58 | + |
| 59 | +def teardown_module(): |
| 60 | + resetenv.stop() |
| 61 | + |
50 | 62 |
|
51 | 63 |
|
52 | 64 | def realpath(path):
|
53 | 65 | return os.path.abspath(os.path.realpath(os.path.expanduser(path)))
|
54 | 66 |
|
55 | 67 | home_jupyter = realpath('~/.jupyter')
|
56 | 68 |
|
57 |
| - |
58 | 69 | def test_envset():
|
59 | 70 | true_values = ['', 'True', 'on', 'yes', 'Y', '1', 'anything']
|
60 | 71 | false_values = ['n', 'No', 'N', 'fAlSE', '0', '0.0', 'Off']
|
@@ -184,8 +195,27 @@ def test_jupyter_path():
|
184 | 195 | assert path[0] == jupyter_data_dir()
|
185 | 196 | assert path[-2:] == system_path
|
186 | 197 |
|
| 198 | +def test_jupyter_path_user_site(): |
| 199 | + with no_config_env, patch.object(site, 'ENABLE_USER_SITE', True): |
| 200 | + path = jupyter_path() |
| 201 | + |
| 202 | + # deduplicated expected values |
| 203 | + values = list(dict.fromkeys([ |
| 204 | + jupyter_data_dir(), |
| 205 | + os.path.join(site.getuserbase(), 'share', 'jupyter'), |
| 206 | + paths.ENV_JUPYTER_PATH[0] |
| 207 | + ])) |
| 208 | + for p,v in zip(path, values): |
| 209 | + assert p == v |
| 210 | + |
| 211 | +def test_jupyter_path_no_user_site(): |
| 212 | + with no_config_env, patch.object(site, 'ENABLE_USER_SITE', False): |
| 213 | + path = jupyter_path() |
| 214 | + assert path[0] == jupyter_data_dir() |
| 215 | + assert path[1] == paths.ENV_JUPYTER_PATH[0] |
| 216 | + |
187 | 217 | def test_jupyter_path_prefer_env():
|
188 |
| - with patch.dict('os.environ', {'JUPYTER_PREFER_ENV_PATH': 'true'}): |
| 218 | + with prefer_env: |
189 | 219 | path = jupyter_path()
|
190 | 220 | assert path[0] == paths.ENV_JUPYTER_PATH[0]
|
191 | 221 | assert path[1] == jupyter_data_dir()
|
@@ -213,15 +243,37 @@ def test_jupyter_path_subdir():
|
213 | 243 | assert p.endswith(pjoin('', 'sub1', 'sub2'))
|
214 | 244 |
|
215 | 245 | def test_jupyter_config_path():
|
216 |
| - path = jupyter_config_path() |
| 246 | + with patch.object(site, 'ENABLE_USER_SITE', True): |
| 247 | + path = jupyter_config_path() |
| 248 | + |
| 249 | + # deduplicated expected values |
| 250 | + values = list(dict.fromkeys([ |
| 251 | + jupyter_config_dir(), |
| 252 | + os.path.join(site.getuserbase(), 'etc', 'jupyter'), |
| 253 | + paths.ENV_CONFIG_PATH[0] |
| 254 | + ])) |
| 255 | + for p,v in zip(path, values): |
| 256 | + assert p == v |
| 257 | + |
| 258 | +def test_jupyter_config_path_no_user_site(): |
| 259 | + with patch.object(site, 'ENABLE_USER_SITE', False): |
| 260 | + path = jupyter_config_path() |
217 | 261 | assert path[0] == jupyter_config_dir()
|
218 | 262 | assert path[1] == paths.ENV_CONFIG_PATH[0]
|
219 | 263 |
|
| 264 | + |
220 | 265 | def test_jupyter_config_path_prefer_env():
|
221 |
| - with patch.dict('os.environ', {'JUPYTER_PREFER_ENV_PATH': 'true'}): |
| 266 | + with prefer_env, patch.object(site, 'ENABLE_USER_SITE', True): |
222 | 267 | path = jupyter_config_path()
|
223 |
| - assert path[0] == paths.ENV_CONFIG_PATH[0] |
224 |
| - assert path[1] == jupyter_config_dir() |
| 268 | + |
| 269 | + # deduplicated expected values |
| 270 | + values = list(dict.fromkeys([ |
| 271 | + paths.ENV_CONFIG_PATH[0], |
| 272 | + jupyter_config_dir(), |
| 273 | + os.path.join(site.getuserbase(), 'etc', 'jupyter') |
| 274 | + ])) |
| 275 | + for p,v in zip(path, values): |
| 276 | + assert p == v |
225 | 277 |
|
226 | 278 | def test_jupyter_config_path_env():
|
227 | 279 | path_env = os.pathsep.join([
|
|
0 commit comments