##// END OF EJS Templates
Skip writable_dir test on Windows...
MinRK -
Show More
@@ -1,403 +1,404
1 1 # encoding: utf-8
2 2 """Tests for IPython.utils.path.py"""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2008 The IPython Development Team
6 6 #
7 7 # Distributed under the terms of the BSD License. The full license is in
8 8 # the file COPYING, distributed as part of this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 import os
16 16 import shutil
17 17 import sys
18 18 import tempfile
19 19 import StringIO
20 20
21 21 from os.path import join, abspath, split
22 22
23 23 import nose.tools as nt
24 24
25 25 from nose import with_setup
26 26
27 27 import IPython
28 28 from IPython.testing import decorators as dec
29 29 from IPython.testing.decorators import skip_if_not_win32, skip_win32
30 30 from IPython.utils import path, io
31 31
32 32 # Platform-dependent imports
33 33 try:
34 34 import _winreg as wreg
35 35 except ImportError:
36 36 #Fake _winreg module on none windows platforms
37 37 import new
38 38 sys.modules["_winreg"] = new.module("_winreg")
39 39 import _winreg as wreg
40 40 #Add entries that needs to be stubbed by the testing code
41 41 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Globals
45 45 #-----------------------------------------------------------------------------
46 46 env = os.environ
47 47 TEST_FILE_PATH = split(abspath(__file__))[0]
48 48 TMP_TEST_DIR = tempfile.mkdtemp()
49 49 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
50 50 XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir")
51 51 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
52 52 #
53 53 # Setup/teardown functions/decorators
54 54 #
55 55
56 56 def setup():
57 57 """Setup testenvironment for the module:
58 58
59 59 - Adds dummy home dir tree
60 60 """
61 61 # Do not mask exceptions here. In particular, catching WindowsError is a
62 62 # problem because that exception is only defined on Windows...
63 63 os.makedirs(IP_TEST_DIR)
64 64 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
65 65
66 66
67 67 def teardown():
68 68 """Teardown testenvironment for the module:
69 69
70 70 - Remove dummy home dir tree
71 71 """
72 72 # Note: we remove the parent test dir, which is the root of all test
73 73 # subdirs we may have created. Use shutil instead of os.removedirs, so
74 74 # that non-empty directories are all recursively removed.
75 75 shutil.rmtree(TMP_TEST_DIR)
76 76
77 77
78 78 def setup_environment():
79 79 """Setup testenvironment for some functions that are tested
80 80 in this module. In particular this functions stores attributes
81 81 and other things that we need to stub in some test functions.
82 82 This needs to be done on a function level and not module level because
83 83 each testfunction needs a pristine environment.
84 84 """
85 85 global oldstuff, platformstuff
86 86 oldstuff = (env.copy(), os.name, path.get_home_dir, IPython.__file__)
87 87
88 88 if os.name == 'nt':
89 89 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
90 90
91 91
92 92 def teardown_environment():
93 93 """Restore things that were remebered by the setup_environment function
94 94 """
95 95 (oldenv, os.name, path.get_home_dir, IPython.__file__,) = oldstuff
96 96 reload(path)
97 97
98 98 for key in env.keys():
99 99 if key not in oldenv:
100 100 del env[key]
101 101 env.update(oldenv)
102 102 if hasattr(sys, 'frozen'):
103 103 del sys.frozen
104 104 if os.name == 'nt':
105 105 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
106 106
107 107 # Build decorator that uses the setup_environment/setup_environment
108 108 with_environment = with_setup(setup_environment, teardown_environment)
109 109
110 110
111 111 @skip_if_not_win32
112 112 @with_environment
113 113 def test_get_home_dir_1():
114 114 """Testcase for py2exe logic, un-compressed lib
115 115 """
116 116 sys.frozen = True
117 117
118 118 #fake filename for IPython.__init__
119 119 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
120 120
121 121 home_dir = path.get_home_dir()
122 122 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
123 123
124 124
125 125 @skip_if_not_win32
126 126 @with_environment
127 127 def test_get_home_dir_2():
128 128 """Testcase for py2exe logic, compressed lib
129 129 """
130 130 sys.frozen = True
131 131 #fake filename for IPython.__init__
132 132 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
133 133
134 134 home_dir = path.get_home_dir()
135 135 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
136 136
137 137
138 138 @with_environment
139 139 @skip_win32
140 140 def test_get_home_dir_3():
141 141 """Testcase $HOME is set, then use its value as home directory."""
142 142 env["HOME"] = HOME_TEST_DIR
143 143 home_dir = path.get_home_dir()
144 144 nt.assert_equal(home_dir, env["HOME"])
145 145
146 146
147 147 @with_environment
148 148 @skip_win32
149 149 def test_get_home_dir_4():
150 150 """Testcase $HOME is not set, os=='posix'.
151 151 This should fail with HomeDirError"""
152 152
153 153 os.name = 'posix'
154 154 if 'HOME' in env: del env['HOME']
155 155 nt.assert_raises(path.HomeDirError, path.get_home_dir)
156 156
157 157
158 158 @skip_if_not_win32
159 159 @with_environment
160 160 def test_get_home_dir_5():
161 161 """Using HOMEDRIVE + HOMEPATH, os=='nt'.
162 162
163 163 HOMESHARE is missing.
164 164 """
165 165
166 166 os.name = 'nt'
167 167 env.pop('HOMESHARE', None)
168 168 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR)
169 169 home_dir = path.get_home_dir()
170 170 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
171 171
172 172
173 173 @skip_if_not_win32
174 174 @with_environment
175 175 def test_get_home_dir_6():
176 176 """Using USERPROFILE, os=='nt'.
177 177
178 178 HOMESHARE, HOMEDRIVE, HOMEPATH are missing.
179 179 """
180 180
181 181 os.name = 'nt'
182 182 env.pop('HOMESHARE', None)
183 183 env.pop('HOMEDRIVE', None)
184 184 env.pop('HOMEPATH', None)
185 185 env["USERPROFILE"] = abspath(HOME_TEST_DIR)
186 186 home_dir = path.get_home_dir()
187 187 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
188 188
189 189
190 190 @skip_if_not_win32
191 191 @with_environment
192 192 def test_get_home_dir_7():
193 193 """Using HOMESHARE, os=='nt'."""
194 194
195 195 os.name = 'nt'
196 196 env["HOMESHARE"] = abspath(HOME_TEST_DIR)
197 197 home_dir = path.get_home_dir()
198 198 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
199 199
200 200
201 201 # Should we stub wreg fully so we can run the test on all platforms?
202 202 @skip_if_not_win32
203 203 @with_environment
204 204 def test_get_home_dir_8():
205 205 """Using registry hack for 'My Documents', os=='nt'
206 206
207 207 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
208 208 """
209 209 os.name = 'nt'
210 210 # Remove from stub environment all keys that may be set
211 211 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
212 212 env.pop(key, None)
213 213
214 214 #Stub windows registry functions
215 215 def OpenKey(x, y):
216 216 class key:
217 217 def Close(self):
218 218 pass
219 219 return key()
220 220 def QueryValueEx(x, y):
221 221 return [abspath(HOME_TEST_DIR)]
222 222
223 223 wreg.OpenKey = OpenKey
224 224 wreg.QueryValueEx = QueryValueEx
225 225
226 226 home_dir = path.get_home_dir()
227 227 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
228 228
229 229
230 230 @with_environment
231 231 def test_get_ipython_dir_1():
232 232 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
233 233 env_ipdir = os.path.join("someplace", ".ipython")
234 234 path._writable_dir = lambda path: True
235 235 env['IPYTHON_DIR'] = env_ipdir
236 236 ipdir = path.get_ipython_dir()
237 237 nt.assert_equal(ipdir, env_ipdir)
238 238
239 239
240 240 @with_environment
241 241 def test_get_ipython_dir_2():
242 242 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
243 243 path.get_home_dir = lambda : "someplace"
244 244 path.get_xdg_dir = lambda : None
245 245 path._writable_dir = lambda path: True
246 246 os.name = "posix"
247 247 env.pop('IPYTHON_DIR', None)
248 248 env.pop('IPYTHONDIR', None)
249 249 env.pop('XDG_CONFIG_HOME', None)
250 250 ipdir = path.get_ipython_dir()
251 251 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
252 252
253 253 @with_environment
254 254 def test_get_ipython_dir_3():
255 255 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
256 256 path.get_home_dir = lambda : "someplace"
257 257 path._writable_dir = lambda path: True
258 258 os.name = "posix"
259 259 env.pop('IPYTHON_DIR', None)
260 260 env.pop('IPYTHONDIR', None)
261 261 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
262 262 ipdir = path.get_ipython_dir()
263 263 nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython"))
264 264
265 265 @with_environment
266 266 def test_get_ipython_dir_4():
267 267 """test_get_ipython_dir_4, use XDG if both exist."""
268 268 path.get_home_dir = lambda : HOME_TEST_DIR
269 269 os.name = "posix"
270 270 env.pop('IPYTHON_DIR', None)
271 271 env.pop('IPYTHONDIR', None)
272 272 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
273 273 xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython")
274 274 ipdir = path.get_ipython_dir()
275 275 nt.assert_equal(ipdir, xdg_ipdir)
276 276
277 277 @with_environment
278 278 def test_get_ipython_dir_5():
279 279 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
280 280 path.get_home_dir = lambda : HOME_TEST_DIR
281 281 os.name = "posix"
282 282 env.pop('IPYTHON_DIR', None)
283 283 env.pop('IPYTHONDIR', None)
284 284 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
285 285 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
286 286 ipdir = path.get_ipython_dir()
287 287 nt.assert_equal(ipdir, IP_TEST_DIR)
288 288
289 289 @with_environment
290 290 def test_get_ipython_dir_6():
291 291 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
292 292 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
293 293 os.mkdir(xdg)
294 294 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
295 295 path.get_home_dir = lambda : HOME_TEST_DIR
296 296 path.get_xdg_dir = lambda : xdg
297 297 os.name = "posix"
298 298 env.pop('IPYTHON_DIR', None)
299 299 env.pop('IPYTHONDIR', None)
300 300 env.pop('XDG_CONFIG_HOME', None)
301 301 xdg_ipdir = os.path.join(xdg, "ipython")
302 302 ipdir = path.get_ipython_dir()
303 303 nt.assert_equal(ipdir, xdg_ipdir)
304 304
305 305 @with_environment
306 306 def test_get_ipython_dir_7():
307 307 """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR"""
308 308 path._writable_dir = lambda path: True
309 309 home_dir = os.path.expanduser('~')
310 310 env['IPYTHON_DIR'] = os.path.join('~', 'somewhere')
311 311 ipdir = path.get_ipython_dir()
312 312 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
313 313
314 314
315 315 @with_environment
316 316 def test_get_xdg_dir_1():
317 317 """test_get_xdg_dir_1, check xdg_dir"""
318 318 reload(path)
319 319 path._writable_dir = lambda path: True
320 320 path.get_home_dir = lambda : 'somewhere'
321 321 os.name = "posix"
322 322 env.pop('IPYTHON_DIR', None)
323 323 env.pop('IPYTHONDIR', None)
324 324 env.pop('XDG_CONFIG_HOME', None)
325 325
326 326 nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config'))
327 327
328 328
329 329 @with_environment
330 330 def test_get_xdg_dir_1():
331 331 """test_get_xdg_dir_1, check nonexistant xdg_dir"""
332 332 reload(path)
333 333 path.get_home_dir = lambda : HOME_TEST_DIR
334 334 os.name = "posix"
335 335 env.pop('IPYTHON_DIR', None)
336 336 env.pop('IPYTHONDIR', None)
337 337 env.pop('XDG_CONFIG_HOME', None)
338 338 nt.assert_equal(path.get_xdg_dir(), None)
339 339
340 340 @with_environment
341 341 def test_get_xdg_dir_2():
342 342 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
343 343 reload(path)
344 344 path.get_home_dir = lambda : HOME_TEST_DIR
345 345 os.name = "posix"
346 346 env.pop('IPYTHON_DIR', None)
347 347 env.pop('IPYTHONDIR', None)
348 348 env.pop('XDG_CONFIG_HOME', None)
349 349 cfgdir=os.path.join(path.get_home_dir(), '.config')
350 350 os.makedirs(cfgdir)
351 351
352 352 nt.assert_equal(path.get_xdg_dir(), cfgdir)
353 353
354 354 def test_filefind():
355 355 """Various tests for filefind"""
356 356 f = tempfile.NamedTemporaryFile()
357 357 # print 'fname:',f.name
358 358 alt_dirs = path.get_ipython_dir()
359 359 t = path.filefind(f.name, alt_dirs)
360 360 # print 'found:',t
361 361
362 362
363 363 def test_get_ipython_package_dir():
364 364 ipdir = path.get_ipython_package_dir()
365 365 nt.assert_true(os.path.isdir(ipdir))
366 366
367 367
368 368 def test_get_ipython_module_path():
369 369 ipapp_path = path.get_ipython_module_path('IPython.frontend.terminal.ipapp')
370 370 nt.assert_true(os.path.isfile(ipapp_path))
371 371
372 372
373 373 @dec.skip_if_not_win32
374 374 def test_get_long_path_name_win32():
375 375 p = path.get_long_path_name('c:\\docume~1')
376 376 nt.assert_equals(p,u'c:\\Documents and Settings')
377 377
378 378
379 379 @dec.skip_win32
380 380 def test_get_long_path_name():
381 381 p = path.get_long_path_name('/usr/local')
382 382 nt.assert_equals(p,'/usr/local')
383 383
384 @dec.skip_win32 # can't create not-user-writable dir on win
384 385 @with_environment
385 386 def test_not_writable_ipdir():
386 387 tmpdir = tempfile.mkdtemp()
387 388 os.name = "posix"
388 389 env.pop('IPYTHON_DIR', None)
389 390 env.pop('IPYTHONDIR', None)
390 391 env.pop('XDG_CONFIG_HOME', None)
391 392 env['HOME'] = tmpdir
392 393 ipdir = os.path.join(tmpdir, '.ipython')
393 394 os.mkdir(ipdir)
394 395 os.chmod(ipdir, 600)
395 396 stderr = io.stderr
396 397 pipe = StringIO.StringIO()
397 398 io.stderr = pipe
398 399 ipdir = path.get_ipython_dir()
399 400 io.stderr.flush()
400 401 io.stderr = stderr
401 402 nt.assert_true('WARNING' in pipe.getvalue())
402 403 env.pop('IPYTHON_DIR', None)
403 404 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now