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