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