blob: 8338712da2b70328c45c961e6c959bad31ec7b7b [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
Ned Deilyacdc56d2012-05-10 17:45:49 -070010import errno
Serhiy Storchaka1a31cba2015-11-21 14:11:57 +020011import subprocess
12from distutils.spawn import find_executable
Serhiy Storchaka04861dc2015-09-06 18:31:23 +030013from shutil import (make_archive,
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000014 register_archive_format, unregister_archive_format,
15 get_archive_formats)
16import tarfile
17import warnings
18
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +030019from test import test_support as support
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000020from test.test_support import TESTFN, check_warnings, captured_stdout
21
Antoine Pitrou1fc02312009-05-01 20:55:35 +000022TESTFN2 = TESTFN + "2"
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000023
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000024try:
25 import grp
26 import pwd
27 UID_GID_SUPPORT = True
28except ImportError:
29 UID_GID_SUPPORT = False
30
31try:
32 import zlib
33except ImportError:
34 zlib = None
35
36try:
37 import zipfile
Serhiy Storchaka30ad6e22016-12-16 19:04:17 +020038 import zlib
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000039 ZIP_SUPPORT = True
40except ImportError:
41 ZIP_SUPPORT = find_executable('zip')
42
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000043class TestShutil(unittest.TestCase):
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +000044
45 def setUp(self):
46 super(TestShutil, self).setUp()
47 self.tempdirs = []
48
49 def tearDown(self):
50 super(TestShutil, self).tearDown()
51 while self.tempdirs:
52 d = self.tempdirs.pop()
53 shutil.rmtree(d, os.name in ('nt', 'cygwin'))
54
55 def write_file(self, path, content='xxx'):
56 """Writes a file in the given path.
57
58
59 path can be a string or a sequence.
60 """
61 if isinstance(path, (list, tuple)):
62 path = os.path.join(*path)
63 f = open(path, 'w')
64 try:
65 f.write(content)
66 finally:
67 f.close()
68
69 def mkdtemp(self):
70 """Create a temporary directory that will be cleaned up.
71
72 Returns the path of the directory.
73 """
74 d = tempfile.mkdtemp()
75 self.tempdirs.append(d)
76 return d
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000077 def test_rmtree_errors(self):
78 # filename is guaranteed not to exist
79 filename = tempfile.mktemp()
80 self.assertRaises(OSError, shutil.rmtree, filename)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000081
Serhiy Storchaka32e23e72013-11-03 23:15:46 +020082 @unittest.skipUnless(hasattr(os, 'chmod'), 'requires os.chmod()')
83 @unittest.skipIf(sys.platform[:6] == 'cygwin',
84 "This test can't be run on Cygwin (issue #1071513).")
85 @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
86 "This test can't be run reliably as root (issue #1076467).")
87 def test_on_error(self):
88 self.errorState = 0
89 os.mkdir(TESTFN)
90 self.childpath = os.path.join(TESTFN, 'a')
91 f = open(self.childpath, 'w')
92 f.close()
93 old_dir_mode = os.stat(TESTFN).st_mode
94 old_child_mode = os.stat(self.childpath).st_mode
95 # Make unwritable.
96 os.chmod(self.childpath, stat.S_IREAD)
97 os.chmod(TESTFN, stat.S_IREAD)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000098
Serhiy Storchaka32e23e72013-11-03 23:15:46 +020099 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
100 # Test whether onerror has actually been called.
101 self.assertEqual(self.errorState, 2,
102 "Expected call to onerror function did not happen.")
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000103
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200104 # Make writable again.
105 os.chmod(TESTFN, old_dir_mode)
106 os.chmod(self.childpath, old_child_mode)
Tim Peters4590c002004-11-01 02:40:52 +0000107
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200108 # Clean up.
109 shutil.rmtree(TESTFN)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000110
111 def check_args_to_onerror(self, func, arg, exc):
Benjamin Peterson9c6fc512009-04-29 22:43:35 +0000112 # test_rmtree_errors deliberately runs rmtree
113 # on a directory that is chmod 400, which will fail.
114 # This function is run when shutil.rmtree fails.
115 # 99.9% of the time it initially fails to remove
116 # a file in the directory, so the first time through
117 # func is os.remove.
118 # However, some Linux machines running ZFS on
119 # FUSE experienced a failure earlier in the process
120 # at os.listdir. The first failure may legally
121 # be either.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000122 if self.errorState == 0:
Benjamin Peterson9c6fc512009-04-29 22:43:35 +0000123 if func is os.remove:
124 self.assertEqual(arg, self.childpath)
125 else:
126 self.assertIs(func, os.listdir,
127 "func must be either os.remove or os.listdir")
128 self.assertEqual(arg, TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000129 self.assertTrue(issubclass(exc[0], OSError))
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000130 self.errorState = 1
131 else:
132 self.assertEqual(func, os.rmdir)
133 self.assertEqual(arg, TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000134 self.assertTrue(issubclass(exc[0], OSError))
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +0000135 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000136
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +0000137 def test_rmtree_dont_delete_file(self):
138 # When called on a file instead of a directory, don't delete it.
139 handle, path = tempfile.mkstemp()
140 os.fdopen(handle).close()
141 self.assertRaises(OSError, shutil.rmtree, path)
142 os.remove(path)
143
Martin v. Löwis4e678382006-07-30 13:00:31 +0000144 def test_copytree_simple(self):
Tim Petersb2dd1a32006-08-10 03:01:26 +0000145 def write_data(path, data):
146 f = open(path, "w")
147 f.write(data)
148 f.close()
149
150 def read_data(path):
151 f = open(path)
152 data = f.read()
153 f.close()
154 return data
155
Martin v. Löwis4e678382006-07-30 13:00:31 +0000156 src_dir = tempfile.mkdtemp()
157 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
Tim Petersb2dd1a32006-08-10 03:01:26 +0000158
159 write_data(os.path.join(src_dir, 'test.txt'), '123')
160
Martin v. Löwis4e678382006-07-30 13:00:31 +0000161 os.mkdir(os.path.join(src_dir, 'test_dir'))
Tim Petersb2dd1a32006-08-10 03:01:26 +0000162 write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
163
Martin v. Löwis4e678382006-07-30 13:00:31 +0000164 try:
165 shutil.copytree(src_dir, dst_dir)
166 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
167 self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
Tim Petersb2dd1a32006-08-10 03:01:26 +0000168 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
169 'test.txt')))
170 actual = read_data(os.path.join(dst_dir, 'test.txt'))
171 self.assertEqual(actual, '123')
172 actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
173 self.assertEqual(actual, '456')
Martin v. Löwis4e678382006-07-30 13:00:31 +0000174 finally:
Tim Petersb2dd1a32006-08-10 03:01:26 +0000175 for path in (
176 os.path.join(src_dir, 'test.txt'),
177 os.path.join(dst_dir, 'test.txt'),
178 os.path.join(src_dir, 'test_dir', 'test.txt'),
179 os.path.join(dst_dir, 'test_dir', 'test.txt'),
180 ):
181 if os.path.exists(path):
182 os.remove(path)
Christian Heimes547867e2007-11-20 03:21:02 +0000183 for path in (src_dir,
Antoine Pitrou4ac6b932009-11-04 00:50:26 +0000184 os.path.dirname(dst_dir)
Christian Heimes547867e2007-11-20 03:21:02 +0000185 ):
Tim Petersb2dd1a32006-08-10 03:01:26 +0000186 if os.path.exists(path):
Christian Heimes044d7092007-11-20 01:48:48 +0000187 shutil.rmtree(path)
Tim Peters64584522006-07-31 01:46:03 +0000188
Georg Brandle78fbcc2008-07-05 10:13:36 +0000189 def test_copytree_with_exclude(self):
190
191 def write_data(path, data):
192 f = open(path, "w")
193 f.write(data)
194 f.close()
195
196 def read_data(path):
197 f = open(path)
198 data = f.read()
199 f.close()
200 return data
201
202 # creating data
203 join = os.path.join
204 exists = os.path.exists
205 src_dir = tempfile.mkdtemp()
Georg Brandle78fbcc2008-07-05 10:13:36 +0000206 try:
Antoine Pitrou4ac6b932009-11-04 00:50:26 +0000207 dst_dir = join(tempfile.mkdtemp(), 'destination')
208 write_data(join(src_dir, 'test.txt'), '123')
209 write_data(join(src_dir, 'test.tmp'), '123')
210 os.mkdir(join(src_dir, 'test_dir'))
211 write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
212 os.mkdir(join(src_dir, 'test_dir2'))
213 write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
214 os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
215 os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
216 write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456')
217 write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456')
218
219
220 # testing glob-like patterns
221 try:
222 patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
223 shutil.copytree(src_dir, dst_dir, ignore=patterns)
224 # checking the result: some elements should not be copied
225 self.assertTrue(exists(join(dst_dir, 'test.txt')))
226 self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
227 self.assertTrue(not exists(join(dst_dir, 'test_dir2')))
228 finally:
229 if os.path.exists(dst_dir):
230 shutil.rmtree(dst_dir)
231 try:
232 patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
233 shutil.copytree(src_dir, dst_dir, ignore=patterns)
234 # checking the result: some elements should not be copied
235 self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
236 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
237 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
238 finally:
239 if os.path.exists(dst_dir):
240 shutil.rmtree(dst_dir)
241
242 # testing callable-style
243 try:
244 def _filter(src, names):
245 res = []
246 for name in names:
247 path = os.path.join(src, name)
248
249 if (os.path.isdir(path) and
250 path.split()[-1] == 'subdir'):
251 res.append(name)
252 elif os.path.splitext(path)[-1] in ('.py'):
253 res.append(name)
254 return res
255
256 shutil.copytree(src_dir, dst_dir, ignore=_filter)
257
258 # checking the result: some elements should not be copied
259 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2',
260 'test.py')))
261 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
262
263 finally:
264 if os.path.exists(dst_dir):
265 shutil.rmtree(dst_dir)
Georg Brandle78fbcc2008-07-05 10:13:36 +0000266 finally:
Antoine Pitrou4ac6b932009-11-04 00:50:26 +0000267 shutil.rmtree(src_dir)
268 shutil.rmtree(os.path.dirname(dst_dir))
Tim Peters64584522006-07-31 01:46:03 +0000269
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000270 if hasattr(os, "symlink"):
271 def test_dont_copy_file_onto_link_to_itself(self):
272 # bug 851123.
273 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000274 src = os.path.join(TESTFN, 'cheese')
275 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000276 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000277 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000278 f.write('cheddar')
279 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000280
281 os.link(src, dst)
282 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
Antoine Pitrou99d848b2010-10-14 22:22:30 +0000283 with open(src, 'r') as f:
284 self.assertEqual(f.read(), 'cheddar')
Johannes Gijsbers68128712004-08-14 13:57:08 +0000285 os.remove(dst)
286
287 # Using `src` here would mean we end up with a symlink pointing
288 # to TESTFN/TESTFN/cheese, while it should point at
289 # TESTFN/cheese.
290 os.symlink('cheese', dst)
291 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
Antoine Pitrou99d848b2010-10-14 22:22:30 +0000292 with open(src, 'r') as f:
293 self.assertEqual(f.read(), 'cheddar')
Johannes Gijsbers68128712004-08-14 13:57:08 +0000294 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000295 finally:
296 try:
297 shutil.rmtree(TESTFN)
298 except OSError:
299 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000300
Georg Brandl52353982008-01-20 14:17:42 +0000301 def test_rmtree_on_symlink(self):
302 # bug 1669.
303 os.mkdir(TESTFN)
304 try:
305 src = os.path.join(TESTFN, 'cheese')
306 dst = os.path.join(TESTFN, 'shop')
307 os.mkdir(src)
308 os.symlink(src, dst)
309 self.assertRaises(OSError, shutil.rmtree, dst)
310 finally:
311 shutil.rmtree(TESTFN, ignore_errors=True)
312
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200313 # Issue #3002: copyfile and copytree block indefinitely on named pipes
314 @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
315 def test_copyfile_named_pipe(self):
316 os.mkfifo(TESTFN)
317 try:
318 self.assertRaises(shutil.SpecialFileError,
319 shutil.copyfile, TESTFN, TESTFN2)
320 self.assertRaises(shutil.SpecialFileError,
321 shutil.copyfile, __file__, TESTFN)
322 finally:
323 os.remove(TESTFN)
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000324
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200325 @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
326 def test_copytree_named_pipe(self):
327 os.mkdir(TESTFN)
328 try:
329 subdir = os.path.join(TESTFN, "subdir")
330 os.mkdir(subdir)
331 pipe = os.path.join(subdir, "mypipe")
332 os.mkfifo(pipe)
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000333 try:
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200334 shutil.copytree(TESTFN, TESTFN2)
335 except shutil.Error as e:
336 errors = e.args[0]
337 self.assertEqual(len(errors), 1)
338 src, dst, error_msg = errors[0]
339 self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
340 else:
341 self.fail("shutil.Error should have been raised")
342 finally:
343 shutil.rmtree(TESTFN, ignore_errors=True)
344 shutil.rmtree(TESTFN2, ignore_errors=True)
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000345
Ned Deilyacdc56d2012-05-10 17:45:49 -0700346 @unittest.skipUnless(hasattr(os, 'chflags') and
347 hasattr(errno, 'EOPNOTSUPP') and
348 hasattr(errno, 'ENOTSUP'),
349 "requires os.chflags, EOPNOTSUPP & ENOTSUP")
350 def test_copystat_handles_harmless_chflags_errors(self):
351 tmpdir = self.mkdtemp()
352 file1 = os.path.join(tmpdir, 'file1')
353 file2 = os.path.join(tmpdir, 'file2')
354 self.write_file(file1, 'xxx')
355 self.write_file(file2, 'xxx')
356
357 def make_chflags_raiser(err):
358 ex = OSError()
359
360 def _chflags_raiser(path, flags):
361 ex.errno = err
362 raise ex
363 return _chflags_raiser
364 old_chflags = os.chflags
365 try:
366 for err in errno.EOPNOTSUPP, errno.ENOTSUP:
367 os.chflags = make_chflags_raiser(err)
368 shutil.copystat(file1, file2)
369 # assert others errors break it
370 os.chflags = make_chflags_raiser(errno.EOPNOTSUPP + errno.ENOTSUP)
371 self.assertRaises(OSError, shutil.copystat, file1, file2)
372 finally:
373 os.chflags = old_chflags
374
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000375 @unittest.skipUnless(zlib, "requires zlib")
376 def test_make_tarball(self):
377 # creating something to tar
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300378 root_dir, base_dir = self._create_files('')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000379
380 tmpdir2 = self.mkdtemp()
Éric Araujoe7329f42011-08-19 03:07:39 +0200381 # force shutil to create the directory
382 os.rmdir(tmpdir2)
Serhiy Storchakaad7b0cd2015-09-07 19:58:23 +0300383 # working with relative paths
384 work_dir = os.path.dirname(tmpdir2)
385 rel_base_name = os.path.join(os.path.basename(tmpdir2), 'archive')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000386
Serhiy Storchakaad7b0cd2015-09-07 19:58:23 +0300387 with support.change_cwd(work_dir):
Serhiy Storchaka672237e2015-09-08 09:59:02 +0300388 base_name = os.path.abspath(rel_base_name)
Serhiy Storchakaad7b0cd2015-09-07 19:58:23 +0300389 tarball = make_archive(rel_base_name, 'gztar', root_dir, '.')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000390
391 # check if the compressed tarball was created
Serhiy Storchaka0ecf4622015-09-07 13:55:25 +0300392 self.assertEqual(tarball, base_name + '.tar.gz')
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300393 self.assertTrue(os.path.isfile(tarball))
394 self.assertTrue(tarfile.is_tarfile(tarball))
395 with tarfile.open(tarball, 'r:gz') as tf:
396 self.assertEqual(sorted(tf.getnames()),
397 ['.', './file1', './file2',
398 './sub', './sub/file3', './sub2'])
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000399
400 # trying an uncompressed one
Serhiy Storchakaad7b0cd2015-09-07 19:58:23 +0300401 with support.change_cwd(work_dir):
402 tarball = make_archive(rel_base_name, 'tar', root_dir, '.')
Serhiy Storchaka0ecf4622015-09-07 13:55:25 +0300403 self.assertEqual(tarball, base_name + '.tar')
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300404 self.assertTrue(os.path.isfile(tarball))
405 self.assertTrue(tarfile.is_tarfile(tarball))
406 with tarfile.open(tarball, 'r') as tf:
407 self.assertEqual(sorted(tf.getnames()),
408 ['.', './file1', './file2',
409 './sub', './sub/file3', './sub2'])
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000410
411 def _tarinfo(self, path):
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300412 with tarfile.open(path) as tar:
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000413 names = tar.getnames()
414 names.sort()
415 return tuple(names)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000416
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300417 def _create_files(self, base_dir='dist'):
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000418 # creating something to tar
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300419 root_dir = self.mkdtemp()
420 dist = os.path.join(root_dir, base_dir)
421 if not os.path.isdir(dist):
422 os.makedirs(dist)
423 self.write_file((dist, 'file1'), 'xxx')
424 self.write_file((dist, 'file2'), 'xxx')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000425 os.mkdir(os.path.join(dist, 'sub'))
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300426 self.write_file((dist, 'sub', 'file3'), 'xxx')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000427 os.mkdir(os.path.join(dist, 'sub2'))
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300428 if base_dir:
429 self.write_file((root_dir, 'outer'), 'xxx')
430 return root_dir, base_dir
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000431
432 @unittest.skipUnless(zlib, "Requires zlib")
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300433 @unittest.skipUnless(find_executable('tar'),
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000434 'Need the tar command to run')
435 def test_tarfile_vs_tar(self):
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300436 root_dir, base_dir = self._create_files()
437 base_name = os.path.join(self.mkdtemp(), 'archive')
Serhiy Storchaka0ecf4622015-09-07 13:55:25 +0300438 tarball = make_archive(base_name, 'gztar', root_dir, base_dir)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000439
440 # check if the compressed tarball was created
Serhiy Storchaka0ecf4622015-09-07 13:55:25 +0300441 self.assertEqual(tarball, base_name + '.tar.gz')
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300442 self.assertTrue(os.path.isfile(tarball))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000443
444 # now create another tarball using `tar`
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300445 tarball2 = os.path.join(root_dir, 'archive2.tar')
446 tar_cmd = ['tar', '-cf', 'archive2.tar', base_dir]
Serhiy Storchaka1a31cba2015-11-21 14:11:57 +0200447 subprocess.check_call(tar_cmd, cwd=root_dir)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000448
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300449 self.assertTrue(os.path.isfile(tarball2))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000450 # let's compare both tarballs
Ezio Melotti2623a372010-11-21 13:34:58 +0000451 self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000452
453 # trying an uncompressed one
Serhiy Storchaka0ecf4622015-09-07 13:55:25 +0300454 tarball = make_archive(base_name, 'tar', root_dir, base_dir)
455 self.assertEqual(tarball, base_name + '.tar')
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300456 self.assertTrue(os.path.isfile(tarball))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000457
458 # now for a dry_run
Serhiy Storchaka0ecf4622015-09-07 13:55:25 +0300459 tarball = make_archive(base_name, 'tar', root_dir, base_dir,
460 dry_run=True)
461 self.assertEqual(tarball, base_name + '.tar')
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300462 self.assertTrue(os.path.isfile(tarball))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000463
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000464 @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
465 def test_make_zipfile(self):
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300466 # creating something to zip
467 root_dir, base_dir = self._create_files()
Serhiy Storchakaad7b0cd2015-09-07 19:58:23 +0300468
469 tmpdir2 = self.mkdtemp()
470 # force shutil to create the directory
471 os.rmdir(tmpdir2)
472 # working with relative paths
473 work_dir = os.path.dirname(tmpdir2)
474 rel_base_name = os.path.join(os.path.basename(tmpdir2), 'archive')
Serhiy Storchakaad7b0cd2015-09-07 19:58:23 +0300475
476 with support.change_cwd(work_dir):
Serhiy Storchaka672237e2015-09-08 09:59:02 +0300477 base_name = os.path.abspath(rel_base_name)
Serhiy Storchakaef5c24a2016-10-23 15:52:01 +0300478 res = make_archive(rel_base_name, 'zip', root_dir)
479
480 self.assertEqual(res, base_name + '.zip')
481 self.assertTrue(os.path.isfile(res))
482 self.assertTrue(zipfile.is_zipfile(res))
483 with zipfile.ZipFile(res) as zf:
484 self.assertEqual(sorted(zf.namelist()),
485 ['dist/', 'dist/file1', 'dist/file2',
486 'dist/sub/', 'dist/sub/file3', 'dist/sub2/',
487 'outer'])
Serhiy Storchaka30ad6e22016-12-16 19:04:17 +0200488 support.unlink(res)
Serhiy Storchakaef5c24a2016-10-23 15:52:01 +0300489
490 with support.change_cwd(work_dir):
491 base_name = os.path.abspath(rel_base_name)
Serhiy Storchakafe45f652015-09-08 05:47:01 +0300492 res = make_archive(rel_base_name, 'zip', root_dir, base_dir)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000493
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300494 self.assertEqual(res, base_name + '.zip')
495 self.assertTrue(os.path.isfile(res))
496 self.assertTrue(zipfile.is_zipfile(res))
497 with zipfile.ZipFile(res) as zf:
498 self.assertEqual(sorted(zf.namelist()),
Serhiy Storchakafe45f652015-09-08 05:47:01 +0300499 ['dist/', 'dist/file1', 'dist/file2',
500 'dist/sub/', 'dist/sub/file3', 'dist/sub2/'])
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000501
Serhiy Storchakafe45f652015-09-08 05:47:01 +0300502 @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
503 @unittest.skipUnless(find_executable('zip'),
504 'Need the zip command to run')
505 def test_zipfile_vs_zip(self):
506 root_dir, base_dir = self._create_files()
507 base_name = os.path.join(self.mkdtemp(), 'archive')
508 archive = make_archive(base_name, 'zip', root_dir, base_dir)
509
510 # check if ZIP file was created
511 self.assertEqual(archive, base_name + '.zip')
512 self.assertTrue(os.path.isfile(archive))
513
514 # now create another ZIP file using `zip`
515 archive2 = os.path.join(root_dir, 'archive2.zip')
516 zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir]
Serhiy Storchaka1a31cba2015-11-21 14:11:57 +0200517 subprocess.check_call(zip_cmd, cwd=root_dir)
Serhiy Storchakafe45f652015-09-08 05:47:01 +0300518
519 self.assertTrue(os.path.isfile(archive2))
520 # let's compare both ZIP files
521 with zipfile.ZipFile(archive) as zf:
522 names = zf.namelist()
523 with zipfile.ZipFile(archive2) as zf:
524 names2 = zf.namelist()
525 self.assertEqual(sorted(names), sorted(names2))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000526
Serhiy Storchaka37c02ac2015-11-22 14:56:22 +0200527 @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
528 @unittest.skipUnless(find_executable('unzip'),
529 'Need the unzip command to run')
530 def test_unzip_zipfile(self):
531 root_dir, base_dir = self._create_files()
532 base_name = os.path.join(self.mkdtemp(), 'archive')
533 archive = make_archive(base_name, 'zip', root_dir, base_dir)
534
535 # check if ZIP file was created
536 self.assertEqual(archive, base_name + '.zip')
537 self.assertTrue(os.path.isfile(archive))
538
539 # now check the ZIP file using `unzip -t`
540 zip_cmd = ['unzip', '-t', archive]
541 with support.change_cwd(root_dir):
542 try:
543 subprocess.check_output(zip_cmd, stderr=subprocess.STDOUT)
544 except subprocess.CalledProcessError as exc:
545 details = exc.output
546 msg = "{}\n\n**Unzip Output**\n{}"
547 self.fail(msg.format(exc, details))
548
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000549 def test_make_archive(self):
550 tmpdir = self.mkdtemp()
551 base_name = os.path.join(tmpdir, 'archive')
552 self.assertRaises(ValueError, make_archive, base_name, 'xxx')
553
554 @unittest.skipUnless(zlib, "Requires zlib")
555 def test_make_archive_owner_group(self):
556 # testing make_archive with owner and group, with various combinations
557 # this works even if there's not gid/uid support
558 if UID_GID_SUPPORT:
559 group = grp.getgrgid(0)[0]
560 owner = pwd.getpwuid(0)[0]
561 else:
562 group = owner = 'root'
563
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300564 root_dir, base_dir = self._create_files()
565 base_name = os.path.join(self.mkdtemp(), 'archive')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000566 res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
567 group=group)
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300568 self.assertTrue(os.path.isfile(res))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000569
570 res = make_archive(base_name, 'zip', root_dir, base_dir)
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300571 self.assertTrue(os.path.isfile(res))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000572
573 res = make_archive(base_name, 'tar', root_dir, base_dir,
574 owner=owner, group=group)
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300575 self.assertTrue(os.path.isfile(res))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000576
577 res = make_archive(base_name, 'tar', root_dir, base_dir,
578 owner='kjhkjhkjg', group='oihohoh')
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300579 self.assertTrue(os.path.isfile(res))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000580
581 @unittest.skipUnless(zlib, "Requires zlib")
582 @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
583 def test_tarfile_root_owner(self):
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300584 root_dir, base_dir = self._create_files()
585 base_name = os.path.join(self.mkdtemp(), 'archive')
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000586 group = grp.getgrgid(0)[0]
587 owner = pwd.getpwuid(0)[0]
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300588 with support.change_cwd(root_dir):
589 archive_name = make_archive(base_name, 'gztar', root_dir, 'dist',
590 owner=owner, group=group)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000591
592 # check if the compressed tarball was created
Serhiy Storchaka04861dc2015-09-06 18:31:23 +0300593 self.assertTrue(os.path.isfile(archive_name))
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000594
595 # now checks the rights
596 archive = tarfile.open(archive_name)
597 try:
598 for member in archive.getmembers():
Ezio Melotti2623a372010-11-21 13:34:58 +0000599 self.assertEqual(member.uid, 0)
600 self.assertEqual(member.gid, 0)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000601 finally:
602 archive.close()
603
604 def test_make_archive_cwd(self):
605 current_dir = os.getcwd()
606 def _breaks(*args, **kw):
607 raise RuntimeError()
608
609 register_archive_format('xxx', _breaks, [], 'xxx file')
610 try:
611 try:
612 make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
613 except Exception:
614 pass
Ezio Melotti2623a372010-11-21 13:34:58 +0000615 self.assertEqual(os.getcwd(), current_dir)
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000616 finally:
617 unregister_archive_format('xxx')
618
Serhiy Storchakac3542852014-11-28 00:50:06 +0200619 def test_make_tarfile_in_curdir(self):
620 # Issue #21280
621 root_dir = self.mkdtemp()
622 saved_dir = os.getcwd()
623 try:
624 os.chdir(root_dir)
625 self.assertEqual(make_archive('test', 'tar'), 'test.tar')
626 self.assertTrue(os.path.isfile('test.tar'))
627 finally:
628 os.chdir(saved_dir)
629
630 @unittest.skipUnless(zlib, "Requires zlib")
631 def test_make_zipfile_in_curdir(self):
632 # Issue #21280
633 root_dir = self.mkdtemp()
634 saved_dir = os.getcwd()
635 try:
636 os.chdir(root_dir)
637 self.assertEqual(make_archive('test', 'zip'), 'test.zip')
638 self.assertTrue(os.path.isfile('test.zip'))
639 finally:
640 os.chdir(saved_dir)
641
Tarek Ziadé48cc8dc2010-02-23 05:16:41 +0000642 def test_register_archive_format(self):
643
644 self.assertRaises(TypeError, register_archive_format, 'xxx', 1)
645 self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
646 1)
647 self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
648 [(1, 2), (1, 2, 3)])
649
650 register_archive_format('xxx', lambda: x, [(1, 2)], 'xxx file')
651 formats = [name for name, params in get_archive_formats()]
652 self.assertIn('xxx', formats)
653
654 unregister_archive_format('xxx')
655 formats = [name for name, params in get_archive_formats()]
656 self.assertNotIn('xxx', formats)
657
Georg Brandl52353982008-01-20 14:17:42 +0000658
Sean Reifscheider493894c2008-03-18 17:24:12 +0000659class TestMove(unittest.TestCase):
660
661 def setUp(self):
662 filename = "foo"
663 self.src_dir = tempfile.mkdtemp()
664 self.dst_dir = tempfile.mkdtemp()
665 self.src_file = os.path.join(self.src_dir, filename)
666 self.dst_file = os.path.join(self.dst_dir, filename)
667 # Try to create a dir in the current directory, hoping that it is
668 # not located on the same filesystem as the system tmp dir.
669 try:
670 self.dir_other_fs = tempfile.mkdtemp(
671 dir=os.path.dirname(__file__))
672 self.file_other_fs = os.path.join(self.dir_other_fs,
673 filename)
674 except OSError:
675 self.dir_other_fs = None
676 with open(self.src_file, "wb") as f:
677 f.write("spam")
678
679 def tearDown(self):
680 for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
681 try:
682 if d:
683 shutil.rmtree(d)
684 except:
685 pass
686
687 def _check_move_file(self, src, dst, real_dst):
Antoine Pitrou99d848b2010-10-14 22:22:30 +0000688 with open(src, "rb") as f:
689 contents = f.read()
Sean Reifscheider493894c2008-03-18 17:24:12 +0000690 shutil.move(src, dst)
Antoine Pitrou99d848b2010-10-14 22:22:30 +0000691 with open(real_dst, "rb") as f:
692 self.assertEqual(contents, f.read())
Sean Reifscheider493894c2008-03-18 17:24:12 +0000693 self.assertFalse(os.path.exists(src))
694
695 def _check_move_dir(self, src, dst, real_dst):
696 contents = sorted(os.listdir(src))
697 shutil.move(src, dst)
698 self.assertEqual(contents, sorted(os.listdir(real_dst)))
699 self.assertFalse(os.path.exists(src))
700
701 def test_move_file(self):
702 # Move a file to another location on the same filesystem.
703 self._check_move_file(self.src_file, self.dst_file, self.dst_file)
704
705 def test_move_file_to_dir(self):
706 # Move a file inside an existing dir on the same filesystem.
707 self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
708
709 def test_move_file_other_fs(self):
710 # Move a file to an existing dir on another filesystem.
711 if not self.dir_other_fs:
Zachary Ware1f702212013-12-10 14:09:20 -0600712 self.skipTest('dir on other filesystem not available')
Sean Reifscheider493894c2008-03-18 17:24:12 +0000713 self._check_move_file(self.src_file, self.file_other_fs,
714 self.file_other_fs)
715
716 def test_move_file_to_dir_other_fs(self):
717 # Move a file to another location on another filesystem.
718 if not self.dir_other_fs:
Zachary Ware1f702212013-12-10 14:09:20 -0600719 self.skipTest('dir on other filesystem not available')
Sean Reifscheider493894c2008-03-18 17:24:12 +0000720 self._check_move_file(self.src_file, self.dir_other_fs,
721 self.file_other_fs)
722
723 def test_move_dir(self):
724 # Move a dir to another location on the same filesystem.
725 dst_dir = tempfile.mktemp()
726 try:
727 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
728 finally:
729 try:
730 shutil.rmtree(dst_dir)
731 except:
732 pass
733
734 def test_move_dir_other_fs(self):
735 # Move a dir to another location on another filesystem.
736 if not self.dir_other_fs:
Zachary Ware1f702212013-12-10 14:09:20 -0600737 self.skipTest('dir on other filesystem not available')
Sean Reifscheider493894c2008-03-18 17:24:12 +0000738 dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
739 try:
740 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
741 finally:
742 try:
743 shutil.rmtree(dst_dir)
744 except:
745 pass
746
747 def test_move_dir_to_dir(self):
748 # Move a dir inside an existing dir on the same filesystem.
749 self._check_move_dir(self.src_dir, self.dst_dir,
750 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
751
752 def test_move_dir_to_dir_other_fs(self):
753 # Move a dir inside an existing dir on another filesystem.
754 if not self.dir_other_fs:
Zachary Ware1f702212013-12-10 14:09:20 -0600755 self.skipTest('dir on other filesystem not available')
Sean Reifscheider493894c2008-03-18 17:24:12 +0000756 self._check_move_dir(self.src_dir, self.dir_other_fs,
757 os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
758
Serhiy Storchakaa4b9c872014-02-11 10:30:06 +0200759 def test_move_dir_sep_to_dir(self):
760 self._check_move_dir(self.src_dir + os.path.sep, self.dst_dir,
761 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
762
763 @unittest.skipUnless(os.path.altsep, 'requires os.path.altsep')
764 def test_move_dir_altsep_to_dir(self):
765 self._check_move_dir(self.src_dir + os.path.altsep, self.dst_dir,
766 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
767
Sean Reifscheider493894c2008-03-18 17:24:12 +0000768 def test_existing_file_inside_dest_dir(self):
769 # A file with the same name inside the destination dir already exists.
770 with open(self.dst_file, "wb"):
771 pass
772 self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
773
774 def test_dont_move_dir_in_itself(self):
775 # Moving a dir inside itself raises an Error.
776 dst = os.path.join(self.src_dir, "bar")
777 self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
778
Antoine Pitrou707c5932009-01-29 20:19:34 +0000779 def test_destinsrc_false_negative(self):
780 os.mkdir(TESTFN)
781 try:
782 for src, dst in [('srcdir', 'srcdir/dest')]:
783 src = os.path.join(TESTFN, src)
784 dst = os.path.join(TESTFN, dst)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000785 self.assertTrue(shutil._destinsrc(src, dst),
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000786 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou707c5932009-01-29 20:19:34 +0000787 'dst (%s) is not in src (%s)' % (dst, src))
788 finally:
789 shutil.rmtree(TESTFN, ignore_errors=True)
Sean Reifscheider493894c2008-03-18 17:24:12 +0000790
Antoine Pitrou707c5932009-01-29 20:19:34 +0000791 def test_destinsrc_false_positive(self):
792 os.mkdir(TESTFN)
793 try:
794 for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
795 src = os.path.join(TESTFN, src)
796 dst = os.path.join(TESTFN, dst)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000797 self.assertFalse(shutil._destinsrc(src, dst),
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000798 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou707c5932009-01-29 20:19:34 +0000799 'dst (%s) is in src (%s)' % (dst, src))
800 finally:
801 shutil.rmtree(TESTFN, ignore_errors=True)
Georg Brandl52353982008-01-20 14:17:42 +0000802
Tarek Ziadé38f81222010-05-05 22:15:31 +0000803
804class TestCopyFile(unittest.TestCase):
805
806 _delete = False
807
808 class Faux(object):
809 _entered = False
810 _exited_with = None
811 _raised = False
812 def __init__(self, raise_in_exit=False, suppress_at_exit=True):
813 self._raise_in_exit = raise_in_exit
814 self._suppress_at_exit = suppress_at_exit
815 def read(self, *args):
816 return ''
817 def __enter__(self):
818 self._entered = True
819 def __exit__(self, exc_type, exc_val, exc_tb):
820 self._exited_with = exc_type, exc_val, exc_tb
821 if self._raise_in_exit:
822 self._raised = True
823 raise IOError("Cannot close")
824 return self._suppress_at_exit
825
826 def tearDown(self):
827 if self._delete:
828 del shutil.open
829
830 def _set_shutil_open(self, func):
831 shutil.open = func
832 self._delete = True
833
834 def test_w_source_open_fails(self):
835 def _open(filename, mode='r'):
836 if filename == 'srcfile':
837 raise IOError('Cannot open "srcfile"')
838 assert 0 # shouldn't reach here.
839
840 self._set_shutil_open(_open)
841
842 self.assertRaises(IOError, shutil.copyfile, 'srcfile', 'destfile')
843
844 def test_w_dest_open_fails(self):
845
846 srcfile = self.Faux()
847
848 def _open(filename, mode='r'):
849 if filename == 'srcfile':
850 return srcfile
851 if filename == 'destfile':
852 raise IOError('Cannot open "destfile"')
853 assert 0 # shouldn't reach here.
854
855 self._set_shutil_open(_open)
856
857 shutil.copyfile('srcfile', 'destfile')
Ezio Melotti62c3c792010-06-05 22:28:10 +0000858 self.assertTrue(srcfile._entered)
859 self.assertTrue(srcfile._exited_with[0] is IOError)
Tarek Ziadé38f81222010-05-05 22:15:31 +0000860 self.assertEqual(srcfile._exited_with[1].args,
861 ('Cannot open "destfile"',))
862
863 def test_w_dest_close_fails(self):
864
865 srcfile = self.Faux()
866 destfile = self.Faux(True)
867
868 def _open(filename, mode='r'):
869 if filename == 'srcfile':
870 return srcfile
871 if filename == 'destfile':
872 return destfile
873 assert 0 # shouldn't reach here.
874
875 self._set_shutil_open(_open)
876
877 shutil.copyfile('srcfile', 'destfile')
Ezio Melotti62c3c792010-06-05 22:28:10 +0000878 self.assertTrue(srcfile._entered)
879 self.assertTrue(destfile._entered)
880 self.assertTrue(destfile._raised)
881 self.assertTrue(srcfile._exited_with[0] is IOError)
Tarek Ziadé38f81222010-05-05 22:15:31 +0000882 self.assertEqual(srcfile._exited_with[1].args,
883 ('Cannot close',))
884
885 def test_w_source_close_fails(self):
886
887 srcfile = self.Faux(True)
888 destfile = self.Faux()
889
890 def _open(filename, mode='r'):
891 if filename == 'srcfile':
892 return srcfile
893 if filename == 'destfile':
894 return destfile
895 assert 0 # shouldn't reach here.
896
897 self._set_shutil_open(_open)
898
899 self.assertRaises(IOError,
900 shutil.copyfile, 'srcfile', 'destfile')
Ezio Melotti62c3c792010-06-05 22:28:10 +0000901 self.assertTrue(srcfile._entered)
902 self.assertTrue(destfile._entered)
903 self.assertFalse(destfile._raised)
904 self.assertTrue(srcfile._exited_with[0] is None)
905 self.assertTrue(srcfile._raised)
Tarek Ziadé38f81222010-05-05 22:15:31 +0000906
Ronald Oussoren58d6b1b2011-05-06 11:31:33 +0200907 def test_move_dir_caseinsensitive(self):
908 # Renames a folder to the same name
909 # but a different case.
910
911 self.src_dir = tempfile.mkdtemp()
912 dst_dir = os.path.join(
913 os.path.dirname(self.src_dir),
914 os.path.basename(self.src_dir).upper())
915 self.assertNotEqual(self.src_dir, dst_dir)
916
917 try:
918 shutil.move(self.src_dir, dst_dir)
919 self.assertTrue(os.path.isdir(dst_dir))
920 finally:
921 if os.path.exists(dst_dir):
922 os.rmdir(dst_dir)
923
924
Tarek Ziadé38f81222010-05-05 22:15:31 +0000925
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000926def test_main():
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300927 support.run_unittest(TestShutil, TestMove, TestCopyFile)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000928
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000929if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000930 test_main()