Show More
@@ -1,250 +1,204 b'' | |||
|
1 | from contextlib import contextmanager | |
|
2 | 1 |
|
|
3 | 2 | import os |
|
4 | 3 | import shutil |
|
5 | 4 | import sys |
|
6 | 5 | import tempfile |
|
7 | 6 | import warnings |
|
8 | 7 | |
|
9 | try: | |
|
10 | reload | |
|
11 |
except |
|
|
12 |
from |
|
|
8 | try: # Python 3 | |
|
9 | from unittest.mock import patch | |
|
10 | except ImportError: # Python 2 | |
|
11 | from mock import patch | |
|
13 | 12 | |
|
14 | from nose import with_setup | |
|
15 | 13 | import nose.tools as nt |
|
16 | 14 | from testpath import modified_env |
|
17 | 15 | |
|
18 | import IPython | |
|
19 | 16 | from IPython import paths |
|
20 | 17 | from IPython.testing.decorators import skip_win32 |
|
21 | 18 | from IPython.utils.tempdir import TemporaryDirectory |
|
22 | 19 | |
|
23 | env = os.environ | |
|
24 | 20 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
25 | 21 | HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir") |
|
26 | 22 | XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir") |
|
27 | 23 | XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir") |
|
28 | 24 | IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython') |
|
29 | 25 | |
|
30 | 26 | def setup(): |
|
31 | 27 | """Setup testenvironment for the module: |
|
32 | 28 | |
|
33 | 29 | - Adds dummy home dir tree |
|
34 | 30 | """ |
|
35 | 31 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
36 | 32 | # problem because that exception is only defined on Windows... |
|
37 | 33 | os.makedirs(IP_TEST_DIR) |
|
38 | 34 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
39 | 35 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) |
|
40 | 36 | |
|
41 | 37 | |
|
42 | 38 | def teardown(): |
|
43 | 39 | """Teardown testenvironment for the module: |
|
44 | 40 | |
|
45 | 41 | - Remove dummy home dir tree |
|
46 | 42 | """ |
|
47 | 43 | # Note: we remove the parent test dir, which is the root of all test |
|
48 | 44 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
49 | 45 | # that non-empty directories are all recursively removed. |
|
50 | 46 | shutil.rmtree(TMP_TEST_DIR) |
|
51 | 47 | |
|
52 | ||
|
53 | def setup_environment(): | |
|
54 | """Setup testenvironment for some functions that are tested | |
|
55 | in this module. In particular this functions stores attributes | |
|
56 | and other things that we need to stub in some test functions. | |
|
57 | This needs to be done on a function level and not module level because | |
|
58 | each testfunction needs a pristine environment. | |
|
59 | """ | |
|
60 | global oldstuff | |
|
61 | oldstuff = (os.name, sys.platform, paths.get_home_dir, IPython.__file__, os.getcwd()) | |
|
62 | ||
|
63 | def teardown_environment(): | |
|
64 | """Restore things that were remembered by the setup_environment function | |
|
65 | """ | |
|
66 | (os.name, sys.platform, paths.get_home_dir, IPython.__file__, old_wd) = oldstuff | |
|
67 | os.chdir(old_wd) | |
|
68 | reload(paths) | |
|
69 | ||
|
70 | if hasattr(sys, 'frozen'): | |
|
71 | del sys.frozen | |
|
72 | ||
|
73 | # Build decorator that uses the setup_environment/setup_environment | |
|
74 | with_environment = with_setup(setup_environment, teardown_environment) | |
|
75 | ||
|
76 | @contextmanager | |
|
77 | 48 | def patch_get_home_dir(dirpath): |
|
78 | orig_get_home_dir = paths.get_home_dir | |
|
79 | paths.get_home_dir = lambda : dirpath | |
|
80 | try: | |
|
81 | yield | |
|
82 | finally: | |
|
83 | paths.get_home_dir = orig_get_home_dir | |
|
49 | return patch.object(paths, 'get_home_dir', return_value=dirpath) | |
|
50 | ||
|
84 | 51 | |
|
85 | 52 | def test_get_ipython_dir_1(): |
|
86 | 53 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
87 | 54 | env_ipdir = os.path.join("someplace", ".ipython") |
|
88 | paths._writable_dir = lambda path: True | |
|
89 |
|
|
|
55 | with patch.object(paths, '_writable_dir', return_value=True), \ | |
|
56 | modified_env({'IPYTHONDIR': env_ipdir}): | |
|
90 | 57 | ipdir = paths.get_ipython_dir() |
|
91 | 58 | |
|
92 | 59 | nt.assert_equal(ipdir, env_ipdir) |
|
93 | 60 | |
|
94 | @with_environment | |
|
95 | 61 | def test_get_ipython_dir_2(): |
|
96 | 62 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
97 |
with patch_get_home_dir('someplace') |
|
|
98 | paths.get_xdg_dir = lambda : None | |
|
99 | paths._writable_dir = lambda path: True | |
|
100 |
os.name |
|
|
101 |
|
|
|
63 | with 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({'IPYTHON_DIR': None, | |
|
102 | 68 |
|
|
103 | 69 |
|
|
104 | 70 |
|
|
105 | 71 |
|
|
106 | 72 | |
|
107 | 73 |
|
|
108 | 74 | |
|
109 | @with_environment | |
|
110 | 75 | def test_get_ipython_dir_3(): |
|
111 | 76 | """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist.""" |
|
112 | 77 | tmphome = TemporaryDirectory() |
|
113 | 78 | try: |
|
114 |
with patch_get_home_dir(tmphome.name) |
|
|
115 |
os.name |
|
|
116 |
|
|
|
79 | with patch_get_home_dir(tmphome.name), \ | |
|
80 | patch('os.name', 'posix'), \ | |
|
81 | modified_env({ | |
|
117 | 82 | 'IPYTHON_DIR': None, |
|
118 | 83 | 'IPYTHONDIR': None, |
|
119 | 84 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
120 | 85 | }), warnings.catch_warnings(record=True) as w: |
|
121 | 86 |
|
|
122 | 87 | |
|
123 | 88 |
|
|
124 | 89 |
|
|
125 | 90 |
|
|
126 | 91 |
|
|
127 | 92 | finally: |
|
128 | 93 | tmphome.cleanup() |
|
129 | 94 | |
|
130 | @with_environment | |
|
131 | 95 | def test_get_ipython_dir_4(): |
|
132 | 96 | """test_get_ipython_dir_4, warn if XDG and home both exist.""" |
|
133 |
with patch_get_home_dir(HOME_TEST_DIR) |
|
|
134 |
os.name |
|
|
97 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
|
98 | patch('os.name', 'posix'): | |
|
135 | 99 | try: |
|
136 | 100 | os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
137 | 101 | except OSError as e: |
|
138 | 102 | if e.errno != errno.EEXIST: |
|
139 | 103 | raise |
|
140 | 104 | |
|
141 | 105 | |
|
142 | 106 | with modified_env({ |
|
143 | 107 | 'IPYTHON_DIR': None, |
|
144 | 108 | 'IPYTHONDIR': None, |
|
145 | 109 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
146 | 110 | }), warnings.catch_warnings(record=True) as w: |
|
147 | 111 | ipdir = paths.get_ipython_dir() |
|
148 | 112 | |
|
149 | 113 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython")) |
|
150 | 114 | if sys.platform != 'darwin': |
|
151 | 115 | nt.assert_equal(len(w), 1) |
|
152 | 116 | nt.assert_in('Ignoring', str(w[0])) |
|
153 | 117 | |
|
154 | @with_environment | |
|
155 | 118 | def test_get_ipython_dir_5(): |
|
156 | 119 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" |
|
157 |
with patch_get_home_dir(HOME_TEST_DIR) |
|
|
158 |
os.name |
|
|
120 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
|
121 | patch('os.name', 'posix'): | |
|
159 | 122 | try: |
|
160 | 123 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
161 | 124 | except OSError as e: |
|
162 | 125 | if e.errno != errno.ENOENT: |
|
163 | 126 | raise |
|
164 | 127 | |
|
165 | 128 | with modified_env({ |
|
166 | 129 | 'IPYTHON_DIR': None, |
|
167 | 130 | 'IPYTHONDIR': None, |
|
168 | 131 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
169 | 132 | }): |
|
170 | 133 | ipdir = paths.get_ipython_dir() |
|
171 | 134 | |
|
172 | 135 | nt.assert_equal(ipdir, IP_TEST_DIR) |
|
173 | 136 | |
|
174 | @with_environment | |
|
175 | 137 | def test_get_ipython_dir_6(): |
|
176 | 138 | """test_get_ipython_dir_6, use home over XDG if defined and neither exist.""" |
|
177 | 139 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') |
|
178 | 140 | os.mkdir(xdg) |
|
179 | 141 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) |
|
180 | with patch_get_home_dir(HOME_TEST_DIR): | |
|
181 | orig_get_xdg_dir = paths.get_xdg_dir | |
|
182 | paths.get_xdg_dir = lambda : xdg | |
|
183 | try: | |
|
184 | os.name = "posix" | |
|
185 | with modified_env({ | |
|
142 | print(paths._writable_dir) | |
|
143 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
|
144 | patch.object(paths, 'get_xdg_dir', return_value=xdg), \ | |
|
145 | patch('os.name', 'posix'), \ | |
|
146 | modified_env({ | |
|
186 | 147 | 'IPYTHON_DIR': None, |
|
187 | 148 | 'IPYTHONDIR': None, |
|
188 | 149 | 'XDG_CONFIG_HOME': None, |
|
189 | 150 | }), warnings.catch_warnings(record=True) as w: |
|
190 | 151 |
|
|
191 | 152 | |
|
192 | 153 |
|
|
193 | 154 |
|
|
194 | finally: | |
|
195 | paths.get_xdg_dir = orig_get_xdg_dir | |
|
196 | 155 | |
|
197 | 156 | def test_get_ipython_dir_7(): |
|
198 | 157 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" |
|
199 | paths._writable_dir = lambda path: True | |
|
200 | 158 | home_dir = os.path.normpath(os.path.expanduser('~')) |
|
201 |
with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}) |
|
|
159 | with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \ | |
|
160 | patch.object(paths, '_writable_dir', return_value=True): | |
|
202 | 161 | ipdir = paths.get_ipython_dir() |
|
203 | 162 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) |
|
204 | 163 | |
|
205 | 164 | @skip_win32 |
|
206 | 165 | def test_get_ipython_dir_8(): |
|
207 | 166 | """test_get_ipython_dir_8, test / home directory""" |
|
208 | old = paths._writable_dir, paths.get_xdg_dir | |
|
209 | try: | |
|
210 | paths._writable_dir = lambda path: bool(path) | |
|
211 | paths.get_xdg_dir = lambda: None | |
|
212 | with modified_env({ | |
|
167 | with patch.object(paths, '_writable_dir', lambda path: bool(path)), \ | |
|
168 | patch.object(paths, 'get_xdg_dir', return_value=None), \ | |
|
169 | modified_env({ | |
|
213 | 170 | 'IPYTHON_DIR': None, |
|
214 | 171 | 'IPYTHONDIR': None, |
|
215 | 172 | 'HOME': '/', |
|
216 | 173 | }): |
|
217 | 174 |
|
|
218 | finally: | |
|
219 | paths._writable_dir, paths.get_xdg_dir = old | |
|
220 | 175 | |
|
221 | 176 | |
|
222 | @with_environment | |
|
223 | 177 | def test_get_ipython_cache_dir(): |
|
224 | 178 | with modified_env({'HOME': HOME_TEST_DIR}): |
|
225 | 179 | if os.name == 'posix' and sys.platform != 'darwin': |
|
226 | 180 | # test default |
|
227 | 181 | os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) |
|
228 | 182 | with modified_env({'XDG_CACHE_HOME': None}): |
|
229 | 183 | ipdir = paths.get_ipython_cache_dir() |
|
230 | 184 | nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), |
|
231 | 185 | ipdir) |
|
232 | 186 | nt.assert_true(os.path.isdir(ipdir)) |
|
233 | 187 | |
|
234 | 188 | # test env override |
|
235 | 189 | with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}): |
|
236 | 190 | ipdir = paths.get_ipython_cache_dir() |
|
237 | 191 | nt.assert_true(os.path.isdir(ipdir)) |
|
238 | 192 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) |
|
239 | 193 | else: |
|
240 | 194 | nt.assert_equal(paths.get_ipython_cache_dir(), |
|
241 | 195 | paths.get_ipython_dir()) |
|
242 | 196 | |
|
243 | 197 | def test_get_ipython_package_dir(): |
|
244 | 198 | ipdir = paths.get_ipython_package_dir() |
|
245 | 199 | nt.assert_true(os.path.isdir(ipdir)) |
|
246 | 200 | |
|
247 | 201 | |
|
248 | 202 | def test_get_ipython_module_path(): |
|
249 | 203 | ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp') |
|
250 | 204 | nt.assert_true(os.path.isfile(ipapp_path)) |
General Comments 0
You need to be logged in to leave comments.
Login now