##// END OF EJS Templates
Small fixes to make test work.
Jonathan Frederic -
Show More
@@ -1,631 +1,635 b''
1 1 # encoding: utf-8
2 2 """Tests for IPython.utils.path.py"""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2008-2011 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 from __future__ import with_statement
16 16
17 17 import os
18 18 import shutil
19 19 import sys
20 20 import tempfile
21 21 from contextlib import contextmanager
22 22
23 23 from os.path import join, abspath, split
24 24
25 25 import nose.tools as nt
26 26
27 27 from nose import with_setup
28 28
29 29 import IPython
30 30 from IPython.testing import decorators as dec
31 31 from IPython.testing.decorators import skip_if_not_win32, skip_win32
32 32 from IPython.testing.tools import make_tempfile, AssertPrints
33 33 from IPython.utils import path
34 34 from IPython.utils import py3compat
35 35 from IPython.utils.tempdir import TemporaryDirectory
36 36
37 37 # Platform-dependent imports
38 38 try:
39 39 import _winreg as wreg
40 40 except ImportError:
41 41 #Fake _winreg module on none windows platforms
42 42 import types
43 43 wr_name = "winreg" if py3compat.PY3 else "_winreg"
44 44 sys.modules[wr_name] = types.ModuleType(wr_name)
45 45 import _winreg as wreg
46 46 #Add entries that needs to be stubbed by the testing code
47 47 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
48 48
49 49 try:
50 50 reload
51 51 except NameError: # Python 3
52 52 from imp import reload
53 53
54 54 #-----------------------------------------------------------------------------
55 55 # Globals
56 56 #-----------------------------------------------------------------------------
57 57 env = os.environ
58 58 TEST_FILE_PATH = split(abspath(__file__))[0]
59 59 TMP_TEST_DIR = tempfile.mkdtemp()
60 60 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
61 61 XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir")
62 62 XDG_CACHE_DIR = join(HOME_TEST_DIR, "xdg_cache_dir")
63 63 IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
64 64 #
65 65 # Setup/teardown functions/decorators
66 66 #
67 67
68 68 def setup():
69 69 """Setup testenvironment for the module:
70 70
71 71 - Adds dummy home dir tree
72 72 """
73 73 # Do not mask exceptions here. In particular, catching WindowsError is a
74 74 # problem because that exception is only defined on Windows...
75 75 os.makedirs(IP_TEST_DIR)
76 76 os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
77 77 os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython'))
78 78
79 79
80 80 def teardown():
81 81 """Teardown testenvironment for the module:
82 82
83 83 - Remove dummy home dir tree
84 84 """
85 85 # Note: we remove the parent test dir, which is the root of all test
86 86 # subdirs we may have created. Use shutil instead of os.removedirs, so
87 87 # that non-empty directories are all recursively removed.
88 88 shutil.rmtree(TMP_TEST_DIR)
89 89
90 90
91 91 def setup_environment():
92 92 """Setup testenvironment for some functions that are tested
93 93 in this module. In particular this functions stores attributes
94 94 and other things that we need to stub in some test functions.
95 95 This needs to be done on a function level and not module level because
96 96 each testfunction needs a pristine environment.
97 97 """
98 98 global oldstuff, platformstuff
99 99 oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())
100 100
101 101 if os.name == 'nt':
102 102 platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)
103 103
104 104
105 105 def teardown_environment():
106 106 """Restore things that were remembered by the setup_environment function
107 107 """
108 108 (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
109 109 os.chdir(old_wd)
110 110 reload(path)
111 111
112 112 for key in env.keys():
113 113 if key not in oldenv:
114 114 del env[key]
115 115 env.update(oldenv)
116 116 if hasattr(sys, 'frozen'):
117 117 del sys.frozen
118 118 if os.name == 'nt':
119 119 (wreg.OpenKey, wreg.QueryValueEx,) = platformstuff
120 120
121 121 # Build decorator that uses the setup_environment/setup_environment
122 122 with_environment = with_setup(setup_environment, teardown_environment)
123 123
124 124 @skip_if_not_win32
125 125 @with_environment
126 126 def test_get_home_dir_1():
127 127 """Testcase for py2exe logic, un-compressed lib
128 128 """
129 129 unfrozen = path.get_home_dir()
130 130 sys.frozen = True
131 131
132 132 #fake filename for IPython.__init__
133 133 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
134 134
135 135 home_dir = path.get_home_dir()
136 136 nt.assert_equal(home_dir, unfrozen)
137 137
138 138
139 139 @skip_if_not_win32
140 140 @with_environment
141 141 def test_get_home_dir_2():
142 142 """Testcase for py2exe logic, compressed lib
143 143 """
144 144 unfrozen = path.get_home_dir()
145 145 sys.frozen = True
146 146 #fake filename for IPython.__init__
147 147 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
148 148
149 149 home_dir = path.get_home_dir(True)
150 150 nt.assert_equal(home_dir, unfrozen)
151 151
152 152
153 153 @with_environment
154 154 def test_get_home_dir_3():
155 155 """get_home_dir() uses $HOME if set"""
156 156 env["HOME"] = HOME_TEST_DIR
157 157 home_dir = path.get_home_dir(True)
158 158 # get_home_dir expands symlinks
159 159 nt.assert_equal(home_dir, os.path.realpath(env["HOME"]))
160 160
161 161
162 162 @with_environment
163 163 def test_get_home_dir_4():
164 164 """get_home_dir() still works if $HOME is not set"""
165 165
166 166 if 'HOME' in env: del env['HOME']
167 167 # this should still succeed, but we don't care what the answer is
168 168 home = path.get_home_dir(False)
169 169
170 170 @with_environment
171 171 def test_get_home_dir_5():
172 172 """raise HomeDirError if $HOME is specified, but not a writable dir"""
173 173 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
174 174 # set os.name = posix, to prevent My Documents fallback on Windows
175 175 os.name = 'posix'
176 176 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
177 177
178 178
179 179 # Should we stub wreg fully so we can run the test on all platforms?
180 180 @skip_if_not_win32
181 181 @with_environment
182 182 def test_get_home_dir_8():
183 183 """Using registry hack for 'My Documents', os=='nt'
184 184
185 185 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
186 186 """
187 187 os.name = 'nt'
188 188 # Remove from stub environment all keys that may be set
189 189 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
190 190 env.pop(key, None)
191 191
192 192 #Stub windows registry functions
193 193 def OpenKey(x, y):
194 194 class key:
195 195 def Close(self):
196 196 pass
197 197 return key()
198 198 def QueryValueEx(x, y):
199 199 return [abspath(HOME_TEST_DIR)]
200 200
201 201 wreg.OpenKey = OpenKey
202 202 wreg.QueryValueEx = QueryValueEx
203 203
204 204 home_dir = path.get_home_dir()
205 205 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
206 206
207 207
208 208 @with_environment
209 209 def test_get_ipython_dir_1():
210 210 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
211 211 env_ipdir = os.path.join("someplace", ".ipython")
212 212 path._writable_dir = lambda path: True
213 213 env['IPYTHONDIR'] = env_ipdir
214 214 ipdir = path.get_ipython_dir()
215 215 nt.assert_equal(ipdir, env_ipdir)
216 216
217 217
218 218 @with_environment
219 219 def test_get_ipython_dir_2():
220 220 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
221 221 path.get_home_dir = lambda : "someplace"
222 222 path.get_xdg_dir = lambda : None
223 223 path._writable_dir = lambda path: True
224 224 os.name = "posix"
225 225 env.pop('IPYTHON_DIR', None)
226 226 env.pop('IPYTHONDIR', None)
227 227 env.pop('XDG_CONFIG_HOME', None)
228 228 ipdir = path.get_ipython_dir()
229 229 nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
230 230
231 231 @with_environment
232 232 def test_get_ipython_dir_3():
233 233 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
234 234 path.get_home_dir = lambda : "someplace"
235 235 path._writable_dir = lambda path: True
236 236 os.name = "posix"
237 237 env.pop('IPYTHON_DIR', None)
238 238 env.pop('IPYTHONDIR', None)
239 239 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
240 240 ipdir = path.get_ipython_dir()
241 241 if sys.platform == "darwin":
242 242 expected = os.path.join("someplace", ".ipython")
243 243 else:
244 244 expected = os.path.join(XDG_TEST_DIR, "ipython")
245 245 nt.assert_equal(ipdir, expected)
246 246
247 247 @with_environment
248 248 def test_get_ipython_dir_4():
249 249 """test_get_ipython_dir_4, use XDG if both exist."""
250 250 path.get_home_dir = lambda : HOME_TEST_DIR
251 251 os.name = "posix"
252 252 env.pop('IPYTHON_DIR', None)
253 253 env.pop('IPYTHONDIR', None)
254 254 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
255 255 ipdir = path.get_ipython_dir()
256 256 if sys.platform == "darwin":
257 257 expected = os.path.join(HOME_TEST_DIR, ".ipython")
258 258 else:
259 259 expected = os.path.join(XDG_TEST_DIR, "ipython")
260 260 nt.assert_equal(ipdir, expected)
261 261
262 262 @with_environment
263 263 def test_get_ipython_dir_5():
264 264 """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
265 265 path.get_home_dir = lambda : HOME_TEST_DIR
266 266 os.name = "posix"
267 267 env.pop('IPYTHON_DIR', None)
268 268 env.pop('IPYTHONDIR', None)
269 269 env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
270 270 os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
271 271 ipdir = path.get_ipython_dir()
272 272 nt.assert_equal(ipdir, IP_TEST_DIR)
273 273
274 274 @with_environment
275 275 def test_get_ipython_dir_6():
276 276 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
277 277 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
278 278 os.mkdir(xdg)
279 279 shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
280 280 path.get_home_dir = lambda : HOME_TEST_DIR
281 281 path.get_xdg_dir = lambda : xdg
282 282 os.name = "posix"
283 283 env.pop('IPYTHON_DIR', None)
284 284 env.pop('IPYTHONDIR', None)
285 285 env.pop('XDG_CONFIG_HOME', None)
286 286 xdg_ipdir = os.path.join(xdg, "ipython")
287 287 ipdir = path.get_ipython_dir()
288 288 nt.assert_equal(ipdir, xdg_ipdir)
289 289
290 290 @with_environment
291 291 def test_get_ipython_dir_7():
292 292 """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
293 293 path._writable_dir = lambda path: True
294 294 home_dir = os.path.normpath(os.path.expanduser('~'))
295 295 env['IPYTHONDIR'] = os.path.join('~', 'somewhere')
296 296 ipdir = path.get_ipython_dir()
297 297 nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
298 298
299 299 @skip_win32
300 300 @with_environment
301 301 def test_get_ipython_dir_8():
302 302 """test_get_ipython_dir_8, test / home directory"""
303 303 old = path._writable_dir, path.get_xdg_dir
304 304 try:
305 305 path._writable_dir = lambda path: bool(path)
306 306 path.get_xdg_dir = lambda: None
307 307 env.pop('IPYTHON_DIR', None)
308 308 env.pop('IPYTHONDIR', None)
309 309 env['HOME'] = '/'
310 310 nt.assert_equal(path.get_ipython_dir(), '/.ipython')
311 311 finally:
312 312 path._writable_dir, path.get_xdg_dir = old
313 313
314 314 @with_environment
315 315 def test_get_xdg_dir_0():
316 316 """test_get_xdg_dir_0, check xdg_dir"""
317 317 reload(path)
318 318 path._writable_dir = lambda path: True
319 319 path.get_home_dir = lambda : 'somewhere'
320 320 os.name = "posix"
321 321 sys.platform = "linux2"
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 sys.platform = "linux2"
336 336 env.pop('IPYTHON_DIR', None)
337 337 env.pop('IPYTHONDIR', None)
338 338 env.pop('XDG_CONFIG_HOME', None)
339 339 nt.assert_equal(path.get_xdg_dir(), None)
340 340
341 341 @with_environment
342 342 def test_get_xdg_dir_2():
343 343 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
344 344 reload(path)
345 345 path.get_home_dir = lambda : HOME_TEST_DIR
346 346 os.name = "posix"
347 347 sys.platform = "linux2"
348 348 env.pop('IPYTHON_DIR', None)
349 349 env.pop('IPYTHONDIR', None)
350 350 env.pop('XDG_CONFIG_HOME', None)
351 351 cfgdir=os.path.join(path.get_home_dir(), '.config')
352 352 if not os.path.exists(cfgdir):
353 353 os.makedirs(cfgdir)
354 354
355 355 nt.assert_equal(path.get_xdg_dir(), cfgdir)
356 356
357 357 @with_environment
358 358 def test_get_xdg_dir_3():
359 359 """test_get_xdg_dir_3, check xdg_dir not used on OS X"""
360 360 reload(path)
361 361 path.get_home_dir = lambda : HOME_TEST_DIR
362 362 os.name = "posix"
363 363 sys.platform = "darwin"
364 364 env.pop('IPYTHON_DIR', None)
365 365 env.pop('IPYTHONDIR', None)
366 366 env.pop('XDG_CONFIG_HOME', None)
367 367 cfgdir=os.path.join(path.get_home_dir(), '.config')
368 368 if not os.path.exists(cfgdir):
369 369 os.makedirs(cfgdir)
370 370
371 371 nt.assert_equal(path.get_xdg_dir(), None)
372 372
373 373 def test_filefind():
374 374 """Various tests for filefind"""
375 375 f = tempfile.NamedTemporaryFile()
376 376 # print 'fname:',f.name
377 377 alt_dirs = path.get_ipython_dir()
378 378 t = path.filefind(f.name, alt_dirs)
379 379 # print 'found:',t
380 380
381 381 @with_environment
382 382 def test_get_ipython_cache_dir():
383 383 os.environ["HOME"] = HOME_TEST_DIR
384 384 if os.name == 'posix' and sys.platform != 'darwin':
385 385 # test default
386 386 os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
387 387 os.environ.pop("XDG_CACHE_HOME", None)
388 388 ipdir = path.get_ipython_cache_dir()
389 389 nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"),
390 390 ipdir)
391 391 nt.assert_true(os.path.isdir(ipdir))
392 392
393 393 # test env override
394 394 os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR
395 395 ipdir = path.get_ipython_cache_dir()
396 396 nt.assert_true(os.path.isdir(ipdir))
397 397 nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython"))
398 398 else:
399 399 nt.assert_equal(path.get_ipython_cache_dir(),
400 400 path.get_ipython_dir())
401 401
402 402 def test_get_ipython_package_dir():
403 403 ipdir = path.get_ipython_package_dir()
404 404 nt.assert_true(os.path.isdir(ipdir))
405 405
406 406
407 407 def test_get_ipython_module_path():
408 408 ipapp_path = path.get_ipython_module_path('IPython.terminal.ipapp')
409 409 nt.assert_true(os.path.isfile(ipapp_path))
410 410
411 411
412 412 @dec.skip_if_not_win32
413 413 def test_get_long_path_name_win32():
414 414 with TemporaryDirectory() as tmpdir:
415
416 # Make a long path.
415 417 long_path = os.path.join(tmpdir, u'this is my long path name')
416 short_path = os.path.join(tmpdir, u'THISIS~1')
418 os.makedirs(long_path)
417 419
418 path = path.get_long_path_name(short_path)
419 nt.assert_equal(path, long_path)
420 # Test to see if the short path evaluates correctly.
421 short_path = os.path.join(tmpdir, u'THISIS~1')
422 evaluated_path = path.get_long_path_name(short_path)
423 nt.assert_equal(evaluated_path.lower(), long_path.lower())
420 424
421 425
422 426 @dec.skip_win32
423 427 def test_get_long_path_name():
424 428 p = path.get_long_path_name('/usr/local')
425 429 nt.assert_equal(p,'/usr/local')
426 430
427 431 @dec.skip_win32 # can't create not-user-writable dir on win
428 432 @with_environment
429 433 def test_not_writable_ipdir():
430 434 tmpdir = tempfile.mkdtemp()
431 435 os.name = "posix"
432 436 env.pop('IPYTHON_DIR', None)
433 437 env.pop('IPYTHONDIR', None)
434 438 env.pop('XDG_CONFIG_HOME', None)
435 439 env['HOME'] = tmpdir
436 440 ipdir = os.path.join(tmpdir, '.ipython')
437 441 os.mkdir(ipdir)
438 442 os.chmod(ipdir, 600)
439 443 with AssertPrints('is not a writable location', channel='stderr'):
440 444 ipdir = path.get_ipython_dir()
441 445 env.pop('IPYTHON_DIR', None)
442 446
443 447 def test_unquote_filename():
444 448 for win32 in (True, False):
445 449 nt.assert_equal(path.unquote_filename('foo.py', win32=win32), 'foo.py')
446 450 nt.assert_equal(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
447 451 nt.assert_equal(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
448 452 nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
449 453 nt.assert_equal(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
450 454 nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
451 455 nt.assert_equal(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
452 456 nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
453 457 nt.assert_equal(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
454 458 nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
455 459
456 460 @with_environment
457 461 def test_get_py_filename():
458 462 os.chdir(TMP_TEST_DIR)
459 463 for win32 in (True, False):
460 464 with make_tempfile('foo.py'):
461 465 nt.assert_equal(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
462 466 nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo.py')
463 467 with make_tempfile('foo'):
464 468 nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo')
465 469 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
466 470 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
467 471 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
468 472 true_fn = 'foo with spaces.py'
469 473 with make_tempfile(true_fn):
470 474 nt.assert_equal(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
471 475 nt.assert_equal(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
472 476 if win32:
473 477 nt.assert_equal(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
474 478 nt.assert_equal(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
475 479 else:
476 480 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
477 481 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
478 482
479 483 def test_unicode_in_filename():
480 484 """When a file doesn't exist, the exception raised should be safe to call
481 485 str() on - i.e. in Python 2 it must only have ASCII characters.
482 486
483 487 https://github.com/ipython/ipython/issues/875
484 488 """
485 489 try:
486 490 # these calls should not throw unicode encode exceptions
487 491 path.get_py_filename(u'fooéè.py', force_win32=False)
488 492 except IOError as ex:
489 493 str(ex)
490 494
491 495
492 496 class TestShellGlob(object):
493 497
494 498 @classmethod
495 499 def setUpClass(cls):
496 500 cls.filenames_start_with_a = map('a{0}'.format, range(3))
497 501 cls.filenames_end_with_b = map('{0}b'.format, range(3))
498 502 cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b
499 503 cls.tempdir = TemporaryDirectory()
500 504 td = cls.tempdir.name
501 505
502 506 with cls.in_tempdir():
503 507 # Create empty files
504 508 for fname in cls.filenames:
505 509 open(os.path.join(td, fname), 'w').close()
506 510
507 511 @classmethod
508 512 def tearDownClass(cls):
509 513 cls.tempdir.cleanup()
510 514
511 515 @classmethod
512 516 @contextmanager
513 517 def in_tempdir(cls):
514 518 save = os.getcwdu()
515 519 try:
516 520 os.chdir(cls.tempdir.name)
517 521 yield
518 522 finally:
519 523 os.chdir(save)
520 524
521 525 def check_match(self, patterns, matches):
522 526 with self.in_tempdir():
523 527 # glob returns unordered list. that's why sorted is required.
524 528 nt.assert_equals(sorted(path.shellglob(patterns)),
525 529 sorted(matches))
526 530
527 531 def common_cases(self):
528 532 return [
529 533 (['*'], self.filenames),
530 534 (['a*'], self.filenames_start_with_a),
531 535 (['*c'], ['*c']),
532 536 (['*', 'a*', '*b', '*c'], self.filenames
533 537 + self.filenames_start_with_a
534 538 + self.filenames_end_with_b
535 539 + ['*c']),
536 540 (['a[012]'], self.filenames_start_with_a),
537 541 ]
538 542
539 543 @skip_win32
540 544 def test_match_posix(self):
541 545 for (patterns, matches) in self.common_cases() + [
542 546 ([r'\*'], ['*']),
543 547 ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a),
544 548 ([r'a\[012]'], ['a[012]']),
545 549 ]:
546 550 yield (self.check_match, patterns, matches)
547 551
548 552 @skip_if_not_win32
549 553 def test_match_windows(self):
550 554 for (patterns, matches) in self.common_cases() + [
551 555 # In windows, backslash is interpreted as path
552 556 # separator. Therefore, you can't escape glob
553 557 # using it.
554 558 ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a),
555 559 ([r'a\[012]'], [r'a\[012]']),
556 560 ]:
557 561 yield (self.check_match, patterns, matches)
558 562
559 563
560 564 def test_unescape_glob():
561 565 nt.assert_equals(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?')
562 566 nt.assert_equals(path.unescape_glob(r'\\*'), r'\*')
563 567 nt.assert_equals(path.unescape_glob(r'\\\*'), r'\*')
564 568 nt.assert_equals(path.unescape_glob(r'\\a'), r'\a')
565 569 nt.assert_equals(path.unescape_glob(r'\a'), r'\a')
566 570
567 571
568 572 class TestLinkOrCopy(object):
569 573 def setUp(self):
570 574 self.tempdir = TemporaryDirectory()
571 575 self.src = self.dst("src")
572 576 with open(self.src, "w") as f:
573 577 f.write("Hello, world!")
574 578
575 579 def tearDown(self):
576 580 self.tempdir.cleanup()
577 581
578 582 def dst(self, *args):
579 583 return os.path.join(self.tempdir.name, *args)
580 584
581 585 def assert_inode_not_equal(self, a, b):
582 586 nt.assert_not_equals(os.stat(a).st_ino, os.stat(b).st_ino,
583 587 "%r and %r do reference the same indoes" %(a, b))
584 588
585 589 def assert_inode_equal(self, a, b):
586 590 nt.assert_equals(os.stat(a).st_ino, os.stat(b).st_ino,
587 591 "%r and %r do not reference the same indoes" %(a, b))
588 592
589 593 def assert_content_equal(self, a, b):
590 594 with open(a) as a_f:
591 595 with open(b) as b_f:
592 596 nt.assert_equals(a_f.read(), b_f.read())
593 597
594 598 @skip_win32
595 599 def test_link_successful(self):
596 600 dst = self.dst("target")
597 601 path.link_or_copy(self.src, dst)
598 602 self.assert_inode_equal(self.src, dst)
599 603
600 604 @skip_win32
601 605 def test_link_into_dir(self):
602 606 dst = self.dst("some_dir")
603 607 os.mkdir(dst)
604 608 path.link_or_copy(self.src, dst)
605 609 expected_dst = self.dst("some_dir", os.path.basename(self.src))
606 610 self.assert_inode_equal(self.src, expected_dst)
607 611
608 612 @skip_win32
609 613 def test_target_exists(self):
610 614 dst = self.dst("target")
611 615 open(dst, "w").close()
612 616 path.link_or_copy(self.src, dst)
613 617 self.assert_inode_equal(self.src, dst)
614 618
615 619 @skip_win32
616 620 def test_no_link(self):
617 621 real_link = os.link
618 622 try:
619 623 del os.link
620 624 dst = self.dst("target")
621 625 path.link_or_copy(self.src, dst)
622 626 self.assert_content_equal(self.src, dst)
623 627 self.assert_inode_not_equal(self.src, dst)
624 628 finally:
625 629 os.link = real_link
626 630
627 631 @skip_if_not_win32
628 632 def test_windows(self):
629 633 dst = self.dst("target")
630 634 path.link_or_copy(self.src, dst)
631 635 self.assert_content_equal(self.src, dst)
General Comments 0
You need to be logged in to leave comments. Login now