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