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