Debian Codegraph @

tests/test_paths.py

raw
1 import errno
2 import os
3 import shutil
4 import tempfile
5 import warnings
6 from unittest.mock import patch
7
8 from tempfile import TemporaryDirectory
9 from testpath import assert_isdir, assert_isfile, modified_env
10
11 from IPython import paths
12 from IPython.testing.decorators import skip_win32
13
14 TMP_TEST_DIR = os.path.realpath(tempfile.mkdtemp())
15 HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir")
16 XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir")
17 XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir")
18 IP_TEST_DIR = os.path.join(HOME_TEST_DIR, ".ipython")
19
20
21 def setup_module():
22 """Setup testenvironment for the module:
23
24 - Adds dummy home dir tree
25 """
26 # Do not mask exceptions here. In particular, catching WindowsError is a
27 # problem because that exception is only defined on Windows...
28 os.makedirs(IP_TEST_DIR)
29 os.makedirs(os.path.join(XDG_TEST_DIR, "ipython"))
30 os.makedirs(os.path.join(XDG_CACHE_DIR, "ipython"))
31
32
33 def teardown_module():
34 """Teardown testenvironment for the module:
35
36 - Remove dummy home dir tree
37 """
38 # Note: we remove the parent test dir, which is the root of all test
39 # subdirs we may have created. Use shutil instead of os.removedirs, so
40 # that non-empty directories are all recursively removed.
41 shutil.rmtree(TMP_TEST_DIR)
42
43
44 def patch_get_home_dir(dirpath):
45 return patch.object(paths, "get_home_dir", return_value=dirpath)
46
47
48 def test_get_ipython_dir_1():
49 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
50 env_ipdir = os.path.join("someplace", ".ipython")
51 with (
52 patch.object(paths, "_writable_dir", return_value=True),
53 modified_env({"IPYTHONDIR": env_ipdir}),
54 ):
55 ipdir = paths.get_ipython_dir()
56
57 assert ipdir == env_ipdir
58
59
60 def test_get_ipython_dir_2():
61 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
62 with (
63 patch_get_home_dir("someplace"),
64 patch.object(paths, "get_xdg_dir", return_value=None),
65 patch.object(paths, "_writable_dir", return_value=True),
66 patch("os.name", "posix"),
67 modified_env(
68 {"IPYTHON_DIR": None, "IPYTHONDIR": None, "XDG_CONFIG_HOME": None}
69 ),
70 ):
71 ipdir = paths.get_ipython_dir()
72
73 assert ipdir == os.path.join("someplace", ".ipython")
74
75
76 def test_get_ipython_dir_3():
77 """test_get_ipython_dir_3, use XDG if defined and exists, and .ipython doesn't exist."""
78 tmphome = TemporaryDirectory()
79 try:
80 with (
81 patch_get_home_dir(tmphome.name),
82 patch("os.name", "posix"),
83 modified_env(
84 {
85 "IPYTHON_DIR": None,
86 "IPYTHONDIR": None,
87 "XDG_CONFIG_HOME": XDG_TEST_DIR,
88 }
89 ),
90 warnings.catch_warnings(record=True) as w,
91 ):
92 ipdir = paths.get_ipython_dir()
93
94 assert ipdir == os.path.join(tmphome.name, XDG_TEST_DIR, "ipython")
95 assert len(w) == 0
96 finally:
97 tmphome.cleanup()
98
99
100 def test_get_ipython_dir_4():
101 """test_get_ipython_dir_4, warn if XDG and home both exist."""
102 with patch_get_home_dir(HOME_TEST_DIR), patch("os.name", "posix"):
103 try:
104 os.mkdir(os.path.join(XDG_TEST_DIR, "ipython"))
105 except OSError as e:
106 if e.errno != errno.EEXIST:
107 raise
108
109 with (
110 modified_env(
111 {
112 "IPYTHON_DIR": None,
113 "IPYTHONDIR": None,
114 "XDG_CONFIG_HOME": XDG_TEST_DIR,
115 }
116 ),
117 warnings.catch_warnings(record=True) as w,
118 ):
119 ipdir = paths.get_ipython_dir()
120
121 assert len(w) == 1
122 assert "Ignoring" in str(w[0])
123
124
125 def test_get_ipython_dir_5():
126 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
127 with patch_get_home_dir(HOME_TEST_DIR), patch("os.name", "posix"):
128 try:
129 os.rmdir(os.path.join(XDG_TEST_DIR, "ipython"))
130 except OSError as e:
131 if e.errno != errno.ENOENT:
132 raise
133
134 with modified_env(
135 {
136 "IPYTHON_DIR": None,
137 "IPYTHONDIR": None,
138 "XDG_CONFIG_HOME": XDG_TEST_DIR,
139 }
140 ):
141 ipdir = paths.get_ipython_dir()
142
143 assert ipdir == IP_TEST_DIR
144
145
146 def test_get_ipython_dir_6():
147 """test_get_ipython_dir_6, use home over XDG if defined and neither exist."""
148 xdg = os.path.join(HOME_TEST_DIR, "somexdg")
149 os.mkdir(xdg)
150 shutil.rmtree(os.path.join(HOME_TEST_DIR, ".ipython"))
151 print(paths._writable_dir)
152 with (
153 patch_get_home_dir(HOME_TEST_DIR),
154 patch.object(paths, "get_xdg_dir", return_value=xdg),
155 patch("os.name", "posix"),
156 modified_env(
157 {
158 "IPYTHON_DIR": None,
159 "IPYTHONDIR": None,
160 "XDG_CONFIG_HOME": None,
161 }
162 ),
163 warnings.catch_warnings(record=True) as w,
164 ):
165 ipdir = paths.get_ipython_dir()
166
167 assert ipdir == os.path.join(HOME_TEST_DIR, ".ipython")
168 assert len(w) == 0
169
170
171 def test_get_ipython_dir_7():
172 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
173 home_dir = os.path.normpath(os.path.expanduser("~"))
174 with (
175 modified_env({"IPYTHONDIR": os.path.join("~", "somewhere")}),
176 patch.object(paths, "_writable_dir", return_value=True),
177 ):
178 ipdir = paths.get_ipython_dir()
179 assert ipdir == os.path.join(home_dir, "somewhere")
180
181
182 @skip_win32
183 def test_get_ipython_dir_8():
184 """test_get_ipython_dir_8, test / home directory"""
185 if not os.access("/", os.W_OK):
186 # test only when HOME directory actually writable
187 return
188
189 with (
190 patch.object(paths, "_writable_dir", lambda path: bool(path)),
191 patch.object(paths, "get_xdg_dir", return_value=None),
192 modified_env(
193 {
194 "IPYTHON_DIR": None,
195 "IPYTHONDIR": None,
196 "HOME": "/",
197 }
198 ),
199 ):
200 assert paths.get_ipython_dir() == "/.ipython"
201
202
203 def test_get_ipython_cache_dir():
204 with modified_env({"HOME": HOME_TEST_DIR}):
205 if os.name == "posix":
206 # test default
207 os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
208 with modified_env({"XDG_CACHE_HOME": None}):
209 ipdir = paths.get_ipython_cache_dir()
210 assert os.path.join(HOME_TEST_DIR, ".cache", "ipython") == ipdir
211 assert_isdir(ipdir)
212
213 # test env override
214 with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}):
215 ipdir = paths.get_ipython_cache_dir()
216 assert_isdir(ipdir)
217 assert ipdir == os.path.join(XDG_CACHE_DIR, "ipython")
218 else:
219 assert paths.get_ipython_cache_dir() == paths.get_ipython_dir()
220
221
222 def test_get_ipython_package_dir():
223 ipdir = paths.get_ipython_package_dir()
224 assert_isdir(ipdir)
225
226
227 def test_get_ipython_module_path():
228 ipapp_path = paths.get_ipython_module_path("IPython.terminal.ipapp")
229 assert_isfile(ipapp_path)