blob: b4e5415c0a8f4663fc7bb0db0694ad67ac6a36ab [file] [log] [blame]
Barry Warsaw7fc2cca2003-01-24 17:34:13 +00001# Copyright (C) 2003 Python Software Foundation
2
3import unittest
4import shutil
5import tempfile
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +00006import sys
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +00007import stat
Brett Cannon1c3fa182004-06-19 21:11:35 +00008import os
9import os.path
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000010from os.path import splitdrive
11from distutils.spawn import find_executable, spawn
12from shutil import (_make_tarball, _make_zipfile, make_archive,
13 register_archive_format, unregister_archive_format,
14 get_archive_formats)
15import tarfile
16import warnings
17
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000018from test import test_support
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000019from test.test_support import TESTFN, check_warnings, captured_stdout
20
Antoine Pitrou1fc02312009-05-01 20:55:35 +000021TESTFN2 = TESTFN + "2"
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000022
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000023try:
24 import grp
25 import pwd
26 UID_GID_SUPPORT = True
27except ImportError:
28 UID_GID_SUPPORT = False
29
30try:
31 import zlib
32except ImportError:
33 zlib = None
34
35try:
36 import zipfile
37 ZIP_SUPPORT = True
38except ImportError:
39 ZIP_SUPPORT = find_executable('zip')
40
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000041class TestShutil(unittest.TestCase):
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000042
43 def setUp(self):
44 super(TestShutil, self).setUp()
45 self.tempdirs = []
46
47 def tearDown(self):
48 super(TestShutil, self).tearDown()
49 while self.tempdirs:
50 d = self.tempdirs.pop()
51 shutil.rmtree(d, os.name in ('nt', 'cygwin'))
52
53 def write_file(self, path, content='xxx'):
54 """Writes a file in the given path.
55
56
57 path can be a string or a sequence.
58 """
59 if isinstance(path, (list, tuple)):
60 path = os.path.join(*path)
61 f = open(path, 'w')
62 try:
63 f.write(content)
64 finally:
65 f.close()
66
67 def mkdtemp(self):
68 """Create a temporary directory that will be cleaned up.
69
70 Returns the path of the directory.
71 """
72 d = tempfile.mkdtemp()
73 self.tempdirs.append(d)
74 return d
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000075 def test_rmtree_errors(self):
76 # filename is guaranteed not to exist
77 filename = tempfile.mktemp()
78 self.assertRaises(OSError, shutil.rmtree, filename)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000079
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000080 # See bug #1071513 for why we don't run this on cygwin
81 # and bug #1076467 for why we don't run this as root.
82 if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin'
Johannes Gijsbers6b220b02004-12-12 15:52:57 +000083 and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000084 def test_on_error(self):
85 self.errorState = 0
86 os.mkdir(TESTFN)
Tim Peters4590c002004-11-01 02:40:52 +000087 self.childpath = os.path.join(TESTFN, 'a')
88 f = open(self.childpath, 'w')
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000089 f.close()
Tim Peters4590c002004-11-01 02:40:52 +000090 old_dir_mode = os.stat(TESTFN).st_mode
91 old_child_mode = os.stat(self.childpath).st_mode
92 # Make unwritable.
93 os.chmod(self.childpath, stat.S_IREAD)
94 os.chmod(TESTFN, stat.S_IREAD)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000095
96 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000097 # Test whether onerror has actually been called.
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000098 self.assertEqual(self.errorState, 2,
99 "Expected call to onerror function did not happen.")
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000100
Tim Peters4590c002004-11-01 02:40:52 +0000101 # Make writable again.
102 os.chmod(TESTFN, old_dir_mode)
103 os.chmod(self.childpath, old_child_mode)
104
105 # Clean up.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000106 shutil.rmtree(TESTFN)
107
108 def check_args_to_onerror(self, func, arg, exc):
Benjamin Peterson9c6fc512009-04-29 22:43:35 +0000109 # test_rmtree_errors deliberately runs rmtree
110 # on a directory that is chmod 400, which will fail.
111 # This function is run when shutil.rmtree fails.
112 # 99.9% of the time it initially fails to remove
113 # a file in the directory, so the first time through
114 # func is os.remove.
115 # However, some Linux machines running ZFS on
116 # FUSE experienced a failure earlier in the process
117 # at os.listdir. The first failure may legally
118 # be either.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000119 if self.errorState == 0:
Benjamin Peterson9c6fc512009-04-29 22:43:35 +0000120 if func is os.remove:
121 self.assertEqual(arg, self.childpath)
122 else:
123 self.assertIs(func, os.listdir,
124 "func must be either os.remove or os.listdir")
125 self.assertEqual(arg, TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000126 self.assertTrue(issubclass(exc[0], OSError))
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000127 self.errorState = 1
128 else:
129 self.assertEqual(func, os.rmdir)
130 self.assertEqual(arg, TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000131 self.assertTrue(issubclass(exc[0], OSError))
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +0000132 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000133
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +0000134 def test_rmtree_dont_delete_file(self):
135 # When called on a file instead of a directory, don't delete it.
136 handle, path = tempfile.mkstemp()
137 os.fdopen(handle).close()
138 self.assertRaises(OSError, shutil.rmtree, path)
139 os.remove(path)
140
Martin v. Löwis4e678382006-07-30 13:00:31 +0000141 def test_copytree_simple(self):
Tim Petersb2dd1a32006-08-10 03:01:26 +0000142 def write_data(path, data):
143 f = open(path, "w")
144 f.write(data)
145 f.close()
146
147 def read_data(path):
148 f = open(path)
149 data = f.read()
150 f.close()
151 return data
152
Martin v. Löwis4e678382006-07-30 13:00:31 +0000153 src_dir = tempfile.mkdtemp()
154 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
Tim Petersb2dd1a32006-08-10 03:01:26 +0000155
156 write_data(os.path.join(src_dir, 'test.txt'), '123')
157
Martin v. Löwis4e678382006-07-30 13:00:31 +0000158 os.mkdir(os.path.join(src_dir, 'test_dir'))
Tim Petersb2dd1a32006-08-10 03:01:26 +0000159 write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
160
Martin v. Löwis4e678382006-07-30 13:00:31 +0000161 try:
162 shutil.copytree(src_dir, dst_dir)
163 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
164 self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
Tim Petersb2dd1a32006-08-10 03:01:26 +0000165 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
166 'test.txt')))
167 actual = read_data(os.path.join(dst_dir, 'test.txt'))
168 self.assertEqual(actual, '123')
169 actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
170 self.assertEqual(actual, '456')
Martin v. Löwis4e678382006-07-30 13:00:31 +0000171 finally:
Tim Petersb2dd1a32006-08-10 03:01:26 +0000172 for path in (
173 os.path.join(src_dir, 'test.txt'),
174 os.path.join(dst_dir, 'test.txt'),
175 os.path.join(src_dir, 'test_dir', 'test.txt'),
176 os.path.join(dst_dir, 'test_dir', 'test.txt'),
177 ):
178 if os.path.exists(path):
179 os.remove(path)
Christian Heimes547867e2007-11-20 03:21:02 +0000180 for path in (src_dir,
Antoine Pitrou4ac6b932009-11-04 00:50:26 +0000181 os.path.dirname(dst_dir)
Christian Heimes547867e2007-11-20 03:21:02 +0000182 ):
Tim Petersb2dd1a32006-08-10 03:01:26 +0000183 if os.path.exists(path):
Christian Heimes044d7092007-11-20 01:48:48 +0000184 shutil.rmtree(path)
Tim Peters64584522006-07-31 01:46:03 +0000185
Georg Brandle78fbcc2008-07-05 10:13:36 +0000186 def test_copytree_with_exclude(self):
187
188 def write_data(path, data):
189 f = open(path, "w")
190 f.write(data)
191 f.close()
192
193 def read_data(path):
194 f = open(path)
195 data = f.read()
196 f.close()
197 return data
198
199 # creating data
200 join = os.path.join
201 exists = os.path.exists
202 src_dir = tempfile.mkdtemp()
Georg Brandle78fbcc2008-07-05 10:13:36 +0000203 try:
Antoine Pitrou4ac6b932009-11-04 00:50:26 +0000204 dst_dir = join(tempfile.mkdtemp(), 'destination')
205 write_data(join(src_dir, 'test.txt'), '123')
206 write_data(join(src_dir, 'test.tmp'), '123')
207 os.mkdir(join(src_dir, 'test_dir'))
208 write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
209 os.mkdir(join(src_dir, 'test_dir2'))
210 write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
211 os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
212 os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
213 write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456')
214 write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456')
215
216
217 # testing glob-like patterns
218 try:
219 patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
220 shutil.copytree(src_dir, dst_dir, ignore=patterns)
221 # checking the result: some elements should not be copied
222 self.assertTrue(exists(join(dst_dir, 'test.txt')))
223 self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
224 self.assertTrue(not exists(join(dst_dir, 'test_dir2')))
225 finally:
226 if os.path.exists(dst_dir):
227 shutil.rmtree(dst_dir)
228 try:
229 patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
230 shutil.copytree(src_dir, dst_dir, ignore=patterns)
231 # checking the result: some elements should not be copied
232 self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
233 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
234 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
235 finally:
236 if os.path.exists(dst_dir):
237 shutil.rmtree(dst_dir)
238
239 # testing callable-style
240 try:
241 def _filter(src, names):
242 res = []
243 for name in names:
244 path = os.path.join(src, name)
245
246 if (os.path.isdir(path) and
247 path.split()[-1] == 'subdir'):
248 res.append(name)
249 elif os.path.splitext(path)[-1] in ('.py'):
250 res.append(name)
251 return res
252
253 shutil.copytree(src_dir, dst_dir, ignore=_filter)
254
255 # checking the result: some elements should not be copied
256 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2',
257 'test.py')))
258 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
259
260 finally:
261 if os.path.exists(dst_dir):
262 shutil.rmtree(dst_dir)
Georg Brandle78fbcc2008-07-05 10:13:36 +0000263 finally:
Antoine Pitrou4ac6b932009-11-04 00:50:26 +0000264 shutil.rmtree(src_dir)
265 shutil.rmtree(os.path.dirname(dst_dir))
Tim Peters64584522006-07-31 01:46:03 +0000266
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000267 if hasattr(os, "symlink"):
268 def test_dont_copy_file_onto_link_to_itself(self):
269 # bug 851123.
270 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000271 src = os.path.join(TESTFN, 'cheese')
272 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000273 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000274 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000275 f.write('cheddar')
276 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000277
278 os.link(src, dst)
279 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
Antoine Pitrou99d848b2010-10-14 22:22:30 +0000280 with open(src, 'r') as f:
281 self.assertEqual(f.read(), 'cheddar')
Johannes Gijsbers68128712004-08-14 13:57:08 +0000282 os.remove(dst)
283
284 # Using `src` here would mean we end up with a symlink pointing
285 # to TESTFN/TESTFN/cheese, while it should point at
286 # TESTFN/cheese.
287 os.symlink('cheese', dst)
288 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
Antoine Pitrou99d848b2010-10-14 22:22:30 +0000289 with open(src, 'r') as f:
290 self.assertEqual(f.read(), 'cheddar')
Johannes Gijsbers68128712004-08-14 13:57:08 +0000291 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000292 finally:
293 try:
294 shutil.rmtree(TESTFN)
295 except OSError:
296 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000297
Georg Brandl52353982008-01-20 14:17:42 +0000298 def test_rmtree_on_symlink(self):
299 # bug 1669.
300 os.mkdir(TESTFN)
301 try:
302 src = os.path.join(TESTFN, 'cheese')
303 dst = os.path.join(TESTFN, 'shop')
304 os.mkdir(src)
305 os.symlink(src, dst)
306 self.assertRaises(OSError, shutil.rmtree, dst)
307 finally:
308 shutil.rmtree(TESTFN, ignore_errors=True)
309
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000310 if hasattr(os, "mkfifo"):
311 # Issue #3002: copyfile and copytree block indefinitely on named pipes
312 def test_copyfile_named_pipe(self):
313 os.mkfifo(TESTFN)
314 try:
315 self.assertRaises(shutil.SpecialFileError,
316 shutil.copyfile, TESTFN, TESTFN2)
317 self.assertRaises(shutil.SpecialFileError,
318 shutil.copyfile, __file__, TESTFN)
319 finally:
320 os.remove(TESTFN)
321
322 def test_copytree_named_pipe(self):
323 os.mkdir(TESTFN)
324 try:
325 subdir = os.path.join(TESTFN, "subdir")
326 os.mkdir(subdir)
327 pipe = os.path.join(subdir, "mypipe")
328 os.mkfifo(pipe)
329 try:
330 shutil.copytree(TESTFN, TESTFN2)
331 except shutil.Error as e:
332 errors = e.args[0]
333 self.assertEqual(len(errors), 1)
334 src, dst, error_msg = errors[0]
335 self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
336 else:
337 self.fail("shutil.Error should have been raised")
338 finally:
339 shutil.rmtree(TESTFN, ignore_errors=True)
340 shutil.rmtree(TESTFN2, ignore_errors=True)
341
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000342 @unittest.skipUnless(zlib, "requires zlib")
343 def test_make_tarball(self):
344 # creating something to tar
345 tmpdir = self.mkdtemp()
346 self.write_file([tmpdir, 'file1'], 'xxx')
347 self.write_file([tmpdir, 'file2'], 'xxx')
348 os.mkdir(os.path.join(tmpdir, 'sub'))
349 self.write_file([tmpdir, 'sub', 'file3'], 'xxx')
350
351 tmpdir2 = self.mkdtemp()
Éric Araujoe7329f42011-08-19 03:07:39 +0200352 # force shutil to create the directory
353 os.rmdir(tmpdir2)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000354 unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
355 "source and target should be on same drive")
356
357 base_name = os.path.join(tmpdir2, 'archive')
358
359 # working with relative paths to avoid tar warnings
360 old_dir = os.getcwd()
361 os.chdir(tmpdir)
362 try:
363 _make_tarball(splitdrive(base_name)[1], '.')
364 finally:
365 os.chdir(old_dir)
366
367 # check if the compressed tarball was created
368 tarball = base_name + '.tar.gz'
369 self.assertTrue(os.path.exists(tarball))
370
371 # trying an uncompressed one
372 base_name = os.path.join(tmpdir2, 'archive')
373 old_dir = os.getcwd()
374 os.chdir(tmpdir)
375 try:
376 _make_tarball(splitdrive(base_name)[1], '.', compress=None)
377 finally:
378 os.chdir(old_dir)
379 tarball = base_name + '.tar'
380 self.assertTrue(os.path.exists(tarball))
381
382 def _tarinfo(self, path):
383 tar = tarfile.open(path)
384 try:
385 names = tar.getnames()
386 names.sort()
387 return tuple(names)
388 finally:
389 tar.close()
390
391 def _create_files(self):
392 # creating something to tar
393 tmpdir = self.mkdtemp()
394 dist = os.path.join(tmpdir, 'dist')
395 os.mkdir(dist)
396 self.write_file([dist, 'file1'], 'xxx')
397 self.write_file([dist, 'file2'], 'xxx')
398 os.mkdir(os.path.join(dist, 'sub'))
399 self.write_file([dist, 'sub', 'file3'], 'xxx')
400 os.mkdir(os.path.join(dist, 'sub2'))
401 tmpdir2 = self.mkdtemp()
402 base_name = os.path.join(tmpdir2, 'archive')
403 return tmpdir, tmpdir2, base_name
404
405 @unittest.skipUnless(zlib, "Requires zlib")
406 @unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
407 'Need the tar command to run')
408 def test_tarfile_vs_tar(self):
409 tmpdir, tmpdir2, base_name = self._create_files()
410 old_dir = os.getcwd()
411 os.chdir(tmpdir)
412 try:
413 _make_tarball(base_name, 'dist')
414 finally:
415 os.chdir(old_dir)
416
417 # check if the compressed tarball was created
418 tarball = base_name + '.tar.gz'
419 self.assertTrue(os.path.exists(tarball))
420
421 # now create another tarball using `tar`
422 tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
423 tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
424 gzip_cmd = ['gzip', '-f9', 'archive2.tar']
425 old_dir = os.getcwd()
426 os.chdir(tmpdir)
427 try:
428 with captured_stdout() as s:
429 spawn(tar_cmd)
430 spawn(gzip_cmd)
431 finally:
432 os.chdir(old_dir)
433
434 self.assertTrue(os.path.exists(tarball2))
435 # let's compare both tarballs
Ezio Melotti2623a372010-11-21 13:34:58 +0000436 self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000437
438 # trying an uncompressed one
439 base_name = os.path.join(tmpdir2, 'archive')
440 old_dir = os.getcwd()
441 os.chdir(tmpdir)
442 try:
443 _make_tarball(base_name, 'dist', compress=None)
444 finally:
445 os.chdir(old_dir)
446 tarball = base_name + '.tar'
447 self.assertTrue(os.path.exists(tarball))
448
449 # now for a dry_run
450 base_name = os.path.join(tmpdir2, 'archive')
451 old_dir = os.getcwd()
452 os.chdir(tmpdir)
453 try:
454 _make_tarball(base_name, 'dist', compress=None, dry_run=True)
455 finally:
456 os.chdir(old_dir)
457 tarball = base_name + '.tar'
458 self.assertTrue(os.path.exists(tarball))
459
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000460 @unittest.skipUnless(zlib, "Requires zlib")
461 @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
462 def test_make_zipfile(self):
463 # creating something to tar
464 tmpdir = self.mkdtemp()
465 self.write_file([tmpdir, 'file1'], 'xxx')
466 self.write_file([tmpdir, 'file2'], 'xxx')
467
468 tmpdir2 = self.mkdtemp()
Éric Araujoe7329f42011-08-19 03:07:39 +0200469 # force shutil to create the directory
470 os.rmdir(tmpdir2)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000471 base_name = os.path.join(tmpdir2, 'archive')
472 _make_zipfile(base_name, tmpdir)
473
474 # check if the compressed tarball was created
475 tarball = base_name + '.zip'
Éric Araujo1c4253d2010-11-17 23:11:08 +0000476 self.assertTrue(os.path.exists(tarball))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000477
478
479 def test_make_archive(self):
480 tmpdir = self.mkdtemp()
481 base_name = os.path.join(tmpdir, 'archive')
482 self.assertRaises(ValueError, make_archive, base_name, 'xxx')
483
484 @unittest.skipUnless(zlib, "Requires zlib")
485 def test_make_archive_owner_group(self):
486 # testing make_archive with owner and group, with various combinations
487 # this works even if there's not gid/uid support
488 if UID_GID_SUPPORT:
489 group = grp.getgrgid(0)[0]
490 owner = pwd.getpwuid(0)[0]
491 else:
492 group = owner = 'root'
493
494 base_dir, root_dir, base_name = self._create_files()
495 base_name = os.path.join(self.mkdtemp() , 'archive')
496 res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
497 group=group)
498 self.assertTrue(os.path.exists(res))
499
500 res = make_archive(base_name, 'zip', root_dir, base_dir)
501 self.assertTrue(os.path.exists(res))
502
503 res = make_archive(base_name, 'tar', root_dir, base_dir,
504 owner=owner, group=group)
505 self.assertTrue(os.path.exists(res))
506
507 res = make_archive(base_name, 'tar', root_dir, base_dir,
508 owner='kjhkjhkjg', group='oihohoh')
509 self.assertTrue(os.path.exists(res))
510
511 @unittest.skipUnless(zlib, "Requires zlib")
512 @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
513 def test_tarfile_root_owner(self):
514 tmpdir, tmpdir2, base_name = self._create_files()
515 old_dir = os.getcwd()
516 os.chdir(tmpdir)
517 group = grp.getgrgid(0)[0]
518 owner = pwd.getpwuid(0)[0]
519 try:
520 archive_name = _make_tarball(base_name, 'dist', compress=None,
521 owner=owner, group=group)
522 finally:
523 os.chdir(old_dir)
524
525 # check if the compressed tarball was created
526 self.assertTrue(os.path.exists(archive_name))
527
528 # now checks the rights
529 archive = tarfile.open(archive_name)
530 try:
531 for member in archive.getmembers():
Ezio Melotti2623a372010-11-21 13:34:58 +0000532 self.assertEqual(member.uid, 0)
533 self.assertEqual(member.gid, 0)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000534 finally:
535 archive.close()
536
537 def test_make_archive_cwd(self):
538 current_dir = os.getcwd()
539 def _breaks(*args, **kw):
540 raise RuntimeError()
541
542 register_archive_format('xxx', _breaks, [], 'xxx file')
543 try:
544 try:
545 make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
546 except Exception:
547 pass
Ezio Melotti2623a372010-11-21 13:34:58 +0000548 self.assertEqual(os.getcwd(), current_dir)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000549 finally:
550 unregister_archive_format('xxx')
551
552 def test_register_archive_format(self):
553
554 self.assertRaises(TypeError, register_archive_format, 'xxx', 1)
555 self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
556 1)
557 self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
558 [(1, 2), (1, 2, 3)])
559
560 register_archive_format('xxx', lambda: x, [(1, 2)], 'xxx file')
561 formats = [name for name, params in get_archive_formats()]
562 self.assertIn('xxx', formats)
563
564 unregister_archive_format('xxx')
565 formats = [name for name, params in get_archive_formats()]
566 self.assertNotIn('xxx', formats)
567
Georg Brandl52353982008-01-20 14:17:42 +0000568
Sean Reifscheider493894c2008-03-18 17:24:12 +0000569class TestMove(unittest.TestCase):
570
571 def setUp(self):
572 filename = "foo"
573 self.src_dir = tempfile.mkdtemp()
574 self.dst_dir = tempfile.mkdtemp()
575 self.src_file = os.path.join(self.src_dir, filename)
576 self.dst_file = os.path.join(self.dst_dir, filename)
577 # Try to create a dir in the current directory, hoping that it is
578 # not located on the same filesystem as the system tmp dir.
579 try:
580 self.dir_other_fs = tempfile.mkdtemp(
581 dir=os.path.dirname(__file__))
582 self.file_other_fs = os.path.join(self.dir_other_fs,
583 filename)
584 except OSError:
585 self.dir_other_fs = None
586 with open(self.src_file, "wb") as f:
587 f.write("spam")
588
589 def tearDown(self):
590 for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
591 try:
592 if d:
593 shutil.rmtree(d)
594 except:
595 pass
596
597 def _check_move_file(self, src, dst, real_dst):
Antoine Pitrou99d848b2010-10-14 22:22:30 +0000598 with open(src, "rb") as f:
599 contents = f.read()
Sean Reifscheider493894c2008-03-18 17:24:12 +0000600 shutil.move(src, dst)
Antoine Pitrou99d848b2010-10-14 22:22:30 +0000601 with open(real_dst, "rb") as f:
602 self.assertEqual(contents, f.read())
Sean Reifscheider493894c2008-03-18 17:24:12 +0000603 self.assertFalse(os.path.exists(src))
604
605 def _check_move_dir(self, src, dst, real_dst):
606 contents = sorted(os.listdir(src))
607 shutil.move(src, dst)
608 self.assertEqual(contents, sorted(os.listdir(real_dst)))
609 self.assertFalse(os.path.exists(src))
610
611 def test_move_file(self):
612 # Move a file to another location on the same filesystem.
613 self._check_move_file(self.src_file, self.dst_file, self.dst_file)
614
615 def test_move_file_to_dir(self):
616 # Move a file inside an existing dir on the same filesystem.
617 self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
618
619 def test_move_file_other_fs(self):
620 # Move a file to an existing dir on another filesystem.
621 if not self.dir_other_fs:
622 # skip
623 return
624 self._check_move_file(self.src_file, self.file_other_fs,
625 self.file_other_fs)
626
627 def test_move_file_to_dir_other_fs(self):
628 # Move a file to another location on another filesystem.
629 if not self.dir_other_fs:
630 # skip
631 return
632 self._check_move_file(self.src_file, self.dir_other_fs,
633 self.file_other_fs)
634
635 def test_move_dir(self):
636 # Move a dir to another location on the same filesystem.
637 dst_dir = tempfile.mktemp()
638 try:
639 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
640 finally:
641 try:
642 shutil.rmtree(dst_dir)
643 except:
644 pass
645
646 def test_move_dir_other_fs(self):
647 # Move a dir to another location on another filesystem.
648 if not self.dir_other_fs:
649 # skip
650 return
651 dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
652 try:
653 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
654 finally:
655 try:
656 shutil.rmtree(dst_dir)
657 except:
658 pass
659
660 def test_move_dir_to_dir(self):
661 # Move a dir inside an existing dir on the same filesystem.
662 self._check_move_dir(self.src_dir, self.dst_dir,
663 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
664
665 def test_move_dir_to_dir_other_fs(self):
666 # Move a dir inside an existing dir on another filesystem.
667 if not self.dir_other_fs:
668 # skip
669 return
670 self._check_move_dir(self.src_dir, self.dir_other_fs,
671 os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
672
673 def test_existing_file_inside_dest_dir(self):
674 # A file with the same name inside the destination dir already exists.
675 with open(self.dst_file, "wb"):
676 pass
677 self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
678
679 def test_dont_move_dir_in_itself(self):
680 # Moving a dir inside itself raises an Error.
681 dst = os.path.join(self.src_dir, "bar")
682 self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
683
Antoine Pitrou707c5932009-01-29 20:19:34 +0000684 def test_destinsrc_false_negative(self):
685 os.mkdir(TESTFN)
686 try:
687 for src, dst in [('srcdir', 'srcdir/dest')]:
688 src = os.path.join(TESTFN, src)
689 dst = os.path.join(TESTFN, dst)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000690 self.assertTrue(shutil._destinsrc(src, dst),
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000691 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou707c5932009-01-29 20:19:34 +0000692 'dst (%s) is not in src (%s)' % (dst, src))
693 finally:
694 shutil.rmtree(TESTFN, ignore_errors=True)
Sean Reifscheider493894c2008-03-18 17:24:12 +0000695
Antoine Pitrou707c5932009-01-29 20:19:34 +0000696 def test_destinsrc_false_positive(self):
697 os.mkdir(TESTFN)
698 try:
699 for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
700 src = os.path.join(TESTFN, src)
701 dst = os.path.join(TESTFN, dst)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000702 self.assertFalse(shutil._destinsrc(src, dst),
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000703 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou707c5932009-01-29 20:19:34 +0000704 'dst (%s) is in src (%s)' % (dst, src))
705 finally:
706 shutil.rmtree(TESTFN, ignore_errors=True)
Georg Brandl52353982008-01-20 14:17:42 +0000707
Tarek Ziadé38f81222010-05-05 22:15:31 +0000708
709class TestCopyFile(unittest.TestCase):
710
711 _delete = False
712
713 class Faux(object):
714 _entered = False
715 _exited_with = None
716 _raised = False
717 def __init__(self, raise_in_exit=False, suppress_at_exit=True):
718 self._raise_in_exit = raise_in_exit
719 self._suppress_at_exit = suppress_at_exit
720 def read(self, *args):
721 return ''
722 def __enter__(self):
723 self._entered = True
724 def __exit__(self, exc_type, exc_val, exc_tb):
725 self._exited_with = exc_type, exc_val, exc_tb
726 if self._raise_in_exit:
727 self._raised = True
728 raise IOError("Cannot close")
729 return self._suppress_at_exit
730
731 def tearDown(self):
732 if self._delete:
733 del shutil.open
734
735 def _set_shutil_open(self, func):
736 shutil.open = func
737 self._delete = True
738
739 def test_w_source_open_fails(self):
740 def _open(filename, mode='r'):
741 if filename == 'srcfile':
742 raise IOError('Cannot open "srcfile"')
743 assert 0 # shouldn't reach here.
744
745 self._set_shutil_open(_open)
746
747 self.assertRaises(IOError, shutil.copyfile, 'srcfile', 'destfile')
748
749 def test_w_dest_open_fails(self):
750
751 srcfile = self.Faux()
752
753 def _open(filename, mode='r'):
754 if filename == 'srcfile':
755 return srcfile
756 if filename == 'destfile':
757 raise IOError('Cannot open "destfile"')
758 assert 0 # shouldn't reach here.
759
760 self._set_shutil_open(_open)
761
762 shutil.copyfile('srcfile', 'destfile')
Ezio Melotti62c3c792010-06-05 22:28:10 +0000763 self.assertTrue(srcfile._entered)
764 self.assertTrue(srcfile._exited_with[0] is IOError)
Tarek Ziadé38f81222010-05-05 22:15:31 +0000765 self.assertEqual(srcfile._exited_with[1].args,
766 ('Cannot open "destfile"',))
767
768 def test_w_dest_close_fails(self):
769
770 srcfile = self.Faux()
771 destfile = self.Faux(True)
772
773 def _open(filename, mode='r'):
774 if filename == 'srcfile':
775 return srcfile
776 if filename == 'destfile':
777 return destfile
778 assert 0 # shouldn't reach here.
779
780 self._set_shutil_open(_open)
781
782 shutil.copyfile('srcfile', 'destfile')
Ezio Melotti62c3c792010-06-05 22:28:10 +0000783 self.assertTrue(srcfile._entered)
784 self.assertTrue(destfile._entered)
785 self.assertTrue(destfile._raised)
786 self.assertTrue(srcfile._exited_with[0] is IOError)
Tarek Ziadé38f81222010-05-05 22:15:31 +0000787 self.assertEqual(srcfile._exited_with[1].args,
788 ('Cannot close',))
789
790 def test_w_source_close_fails(self):
791
792 srcfile = self.Faux(True)
793 destfile = self.Faux()
794
795 def _open(filename, mode='r'):
796 if filename == 'srcfile':
797 return srcfile
798 if filename == 'destfile':
799 return destfile
800 assert 0 # shouldn't reach here.
801
802 self._set_shutil_open(_open)
803
804 self.assertRaises(IOError,
805 shutil.copyfile, 'srcfile', 'destfile')
Ezio Melotti62c3c792010-06-05 22:28:10 +0000806 self.assertTrue(srcfile._entered)
807 self.assertTrue(destfile._entered)
808 self.assertFalse(destfile._raised)
809 self.assertTrue(srcfile._exited_with[0] is None)
810 self.assertTrue(srcfile._raised)
Tarek Ziadé38f81222010-05-05 22:15:31 +0000811
Ronald Oussoren58d6b1b2011-05-06 11:31:33 +0200812 def test_move_dir_caseinsensitive(self):
813 # Renames a folder to the same name
814 # but a different case.
815
816 self.src_dir = tempfile.mkdtemp()
817 dst_dir = os.path.join(
818 os.path.dirname(self.src_dir),
819 os.path.basename(self.src_dir).upper())
820 self.assertNotEqual(self.src_dir, dst_dir)
821
822 try:
823 shutil.move(self.src_dir, dst_dir)
824 self.assertTrue(os.path.isdir(dst_dir))
825 finally:
826 if os.path.exists(dst_dir):
827 os.rmdir(dst_dir)
828
829
Tarek Ziadé38f81222010-05-05 22:15:31 +0000830
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000831def test_main():
Tarek Ziadé38f81222010-05-05 22:15:31 +0000832 test_support.run_unittest(TestShutil, TestMove, TestCopyFile)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000833
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000834if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000835 test_main()