blob: 17d3cabcb630247ecc5f6b928c276f5116ae911c [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
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000010from test import test_support
Johannes Gijsbers46f14592004-08-14 13:30:02 +000011from test.test_support import TESTFN
Antoine Pitrou1fc02312009-05-01 20:55:35 +000012TESTFN2 = TESTFN + "2"
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000013
14class TestShutil(unittest.TestCase):
15 def test_rmtree_errors(self):
16 # filename is guaranteed not to exist
17 filename = tempfile.mktemp()
18 self.assertRaises(OSError, shutil.rmtree, filename)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000019
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000020 # See bug #1071513 for why we don't run this on cygwin
21 # and bug #1076467 for why we don't run this as root.
22 if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin'
Johannes Gijsbers6b220b02004-12-12 15:52:57 +000023 and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000024 def test_on_error(self):
25 self.errorState = 0
26 os.mkdir(TESTFN)
Tim Peters4590c002004-11-01 02:40:52 +000027 self.childpath = os.path.join(TESTFN, 'a')
28 f = open(self.childpath, 'w')
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000029 f.close()
Tim Peters4590c002004-11-01 02:40:52 +000030 old_dir_mode = os.stat(TESTFN).st_mode
31 old_child_mode = os.stat(self.childpath).st_mode
32 # Make unwritable.
33 os.chmod(self.childpath, stat.S_IREAD)
34 os.chmod(TESTFN, stat.S_IREAD)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000035
36 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000037 # Test whether onerror has actually been called.
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000038 self.assertEqual(self.errorState, 2,
39 "Expected call to onerror function did not happen.")
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000040
Tim Peters4590c002004-11-01 02:40:52 +000041 # Make writable again.
42 os.chmod(TESTFN, old_dir_mode)
43 os.chmod(self.childpath, old_child_mode)
44
45 # Clean up.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000046 shutil.rmtree(TESTFN)
47
48 def check_args_to_onerror(self, func, arg, exc):
Benjamin Peterson9c6fc512009-04-29 22:43:35 +000049 # test_rmtree_errors deliberately runs rmtree
50 # on a directory that is chmod 400, which will fail.
51 # This function is run when shutil.rmtree fails.
52 # 99.9% of the time it initially fails to remove
53 # a file in the directory, so the first time through
54 # func is os.remove.
55 # However, some Linux machines running ZFS on
56 # FUSE experienced a failure earlier in the process
57 # at os.listdir. The first failure may legally
58 # be either.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000059 if self.errorState == 0:
Benjamin Peterson9c6fc512009-04-29 22:43:35 +000060 if func is os.remove:
61 self.assertEqual(arg, self.childpath)
62 else:
63 self.assertIs(func, os.listdir,
64 "func must be either os.remove or os.listdir")
65 self.assertEqual(arg, TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000066 self.assertTrue(issubclass(exc[0], OSError))
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000067 self.errorState = 1
68 else:
69 self.assertEqual(func, os.rmdir)
70 self.assertEqual(arg, TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000071 self.assertTrue(issubclass(exc[0], OSError))
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000072 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000073
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000074 def test_rmtree_dont_delete_file(self):
75 # When called on a file instead of a directory, don't delete it.
76 handle, path = tempfile.mkstemp()
77 os.fdopen(handle).close()
78 self.assertRaises(OSError, shutil.rmtree, path)
79 os.remove(path)
80
Martin v. Löwis4e678382006-07-30 13:00:31 +000081 def test_copytree_simple(self):
Tim Petersb2dd1a32006-08-10 03:01:26 +000082 def write_data(path, data):
83 f = open(path, "w")
84 f.write(data)
85 f.close()
86
87 def read_data(path):
88 f = open(path)
89 data = f.read()
90 f.close()
91 return data
92
Martin v. Löwis4e678382006-07-30 13:00:31 +000093 src_dir = tempfile.mkdtemp()
94 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
Tim Petersb2dd1a32006-08-10 03:01:26 +000095
96 write_data(os.path.join(src_dir, 'test.txt'), '123')
97
Martin v. Löwis4e678382006-07-30 13:00:31 +000098 os.mkdir(os.path.join(src_dir, 'test_dir'))
Tim Petersb2dd1a32006-08-10 03:01:26 +000099 write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
100
Martin v. Löwis4e678382006-07-30 13:00:31 +0000101 try:
102 shutil.copytree(src_dir, dst_dir)
103 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
104 self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
Tim Petersb2dd1a32006-08-10 03:01:26 +0000105 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
106 'test.txt')))
107 actual = read_data(os.path.join(dst_dir, 'test.txt'))
108 self.assertEqual(actual, '123')
109 actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
110 self.assertEqual(actual, '456')
Martin v. Löwis4e678382006-07-30 13:00:31 +0000111 finally:
Tim Petersb2dd1a32006-08-10 03:01:26 +0000112 for path in (
113 os.path.join(src_dir, 'test.txt'),
114 os.path.join(dst_dir, 'test.txt'),
115 os.path.join(src_dir, 'test_dir', 'test.txt'),
116 os.path.join(dst_dir, 'test_dir', 'test.txt'),
117 ):
118 if os.path.exists(path):
119 os.remove(path)
Christian Heimes547867e2007-11-20 03:21:02 +0000120 for path in (src_dir,
121 os.path.abspath(os.path.join(dst_dir, os.path.pardir))
122 ):
Tim Petersb2dd1a32006-08-10 03:01:26 +0000123 if os.path.exists(path):
Christian Heimes044d7092007-11-20 01:48:48 +0000124 shutil.rmtree(path)
Tim Peters64584522006-07-31 01:46:03 +0000125
Georg Brandle78fbcc2008-07-05 10:13:36 +0000126 def test_copytree_with_exclude(self):
127
128 def write_data(path, data):
129 f = open(path, "w")
130 f.write(data)
131 f.close()
132
133 def read_data(path):
134 f = open(path)
135 data = f.read()
136 f.close()
137 return data
138
139 # creating data
140 join = os.path.join
141 exists = os.path.exists
142 src_dir = tempfile.mkdtemp()
143 dst_dir = join(tempfile.mkdtemp(), 'destination')
144 write_data(join(src_dir, 'test.txt'), '123')
145 write_data(join(src_dir, 'test.tmp'), '123')
146 os.mkdir(join(src_dir, 'test_dir'))
147 write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
148 os.mkdir(join(src_dir, 'test_dir2'))
149 write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
150 os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
151 os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
152 write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456')
153 write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456')
154
155
156 # testing glob-like patterns
157 try:
158 patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
159 shutil.copytree(src_dir, dst_dir, ignore=patterns)
160 # checking the result: some elements should not be copied
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000161 self.assertTrue(exists(join(dst_dir, 'test.txt')))
162 self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
163 self.assertTrue(not exists(join(dst_dir, 'test_dir2')))
Georg Brandle78fbcc2008-07-05 10:13:36 +0000164 finally:
165 if os.path.exists(dst_dir):
166 shutil.rmtree(dst_dir)
167 try:
168 patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
169 shutil.copytree(src_dir, dst_dir, ignore=patterns)
170 # checking the result: some elements should not be copied
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000171 self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
172 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
173 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
Georg Brandle78fbcc2008-07-05 10:13:36 +0000174 finally:
175 if os.path.exists(dst_dir):
176 shutil.rmtree(dst_dir)
177
178 # testing callable-style
179 try:
180 def _filter(src, names):
181 res = []
182 for name in names:
183 path = os.path.join(src, name)
184
185 if (os.path.isdir(path) and
186 path.split()[-1] == 'subdir'):
187 res.append(name)
188 elif os.path.splitext(path)[-1] in ('.py'):
189 res.append(name)
190 return res
191
192 shutil.copytree(src_dir, dst_dir, ignore=_filter)
193
194 # checking the result: some elements should not be copied
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000195 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2',
Georg Brandle78fbcc2008-07-05 10:13:36 +0000196 'test.py')))
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000197 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
Georg Brandle78fbcc2008-07-05 10:13:36 +0000198
199 finally:
200 if os.path.exists(dst_dir):
201 shutil.rmtree(dst_dir)
Tim Peters64584522006-07-31 01:46:03 +0000202
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000203 if hasattr(os, "symlink"):
204 def test_dont_copy_file_onto_link_to_itself(self):
205 # bug 851123.
206 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000207 src = os.path.join(TESTFN, 'cheese')
208 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000209 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000210 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000211 f.write('cheddar')
212 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000213
214 os.link(src, dst)
215 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
216 self.assertEqual(open(src,'r').read(), 'cheddar')
217 os.remove(dst)
218
219 # Using `src` here would mean we end up with a symlink pointing
220 # to TESTFN/TESTFN/cheese, while it should point at
221 # TESTFN/cheese.
222 os.symlink('cheese', dst)
223 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
224 self.assertEqual(open(src,'r').read(), 'cheddar')
225 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000226 finally:
227 try:
228 shutil.rmtree(TESTFN)
229 except OSError:
230 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000231
Georg Brandl52353982008-01-20 14:17:42 +0000232 def test_rmtree_on_symlink(self):
233 # bug 1669.
234 os.mkdir(TESTFN)
235 try:
236 src = os.path.join(TESTFN, 'cheese')
237 dst = os.path.join(TESTFN, 'shop')
238 os.mkdir(src)
239 os.symlink(src, dst)
240 self.assertRaises(OSError, shutil.rmtree, dst)
241 finally:
242 shutil.rmtree(TESTFN, ignore_errors=True)
243
Antoine Pitrou1fc02312009-05-01 20:55:35 +0000244 if hasattr(os, "mkfifo"):
245 # Issue #3002: copyfile and copytree block indefinitely on named pipes
246 def test_copyfile_named_pipe(self):
247 os.mkfifo(TESTFN)
248 try:
249 self.assertRaises(shutil.SpecialFileError,
250 shutil.copyfile, TESTFN, TESTFN2)
251 self.assertRaises(shutil.SpecialFileError,
252 shutil.copyfile, __file__, TESTFN)
253 finally:
254 os.remove(TESTFN)
255
256 def test_copytree_named_pipe(self):
257 os.mkdir(TESTFN)
258 try:
259 subdir = os.path.join(TESTFN, "subdir")
260 os.mkdir(subdir)
261 pipe = os.path.join(subdir, "mypipe")
262 os.mkfifo(pipe)
263 try:
264 shutil.copytree(TESTFN, TESTFN2)
265 except shutil.Error as e:
266 errors = e.args[0]
267 self.assertEqual(len(errors), 1)
268 src, dst, error_msg = errors[0]
269 self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
270 else:
271 self.fail("shutil.Error should have been raised")
272 finally:
273 shutil.rmtree(TESTFN, ignore_errors=True)
274 shutil.rmtree(TESTFN2, ignore_errors=True)
275
Georg Brandl52353982008-01-20 14:17:42 +0000276
Sean Reifscheider493894c2008-03-18 17:24:12 +0000277class TestMove(unittest.TestCase):
278
279 def setUp(self):
280 filename = "foo"
281 self.src_dir = tempfile.mkdtemp()
282 self.dst_dir = tempfile.mkdtemp()
283 self.src_file = os.path.join(self.src_dir, filename)
284 self.dst_file = os.path.join(self.dst_dir, filename)
285 # Try to create a dir in the current directory, hoping that it is
286 # not located on the same filesystem as the system tmp dir.
287 try:
288 self.dir_other_fs = tempfile.mkdtemp(
289 dir=os.path.dirname(__file__))
290 self.file_other_fs = os.path.join(self.dir_other_fs,
291 filename)
292 except OSError:
293 self.dir_other_fs = None
294 with open(self.src_file, "wb") as f:
295 f.write("spam")
296
297 def tearDown(self):
298 for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
299 try:
300 if d:
301 shutil.rmtree(d)
302 except:
303 pass
304
305 def _check_move_file(self, src, dst, real_dst):
306 contents = open(src, "rb").read()
307 shutil.move(src, dst)
308 self.assertEqual(contents, open(real_dst, "rb").read())
309 self.assertFalse(os.path.exists(src))
310
311 def _check_move_dir(self, src, dst, real_dst):
312 contents = sorted(os.listdir(src))
313 shutil.move(src, dst)
314 self.assertEqual(contents, sorted(os.listdir(real_dst)))
315 self.assertFalse(os.path.exists(src))
316
317 def test_move_file(self):
318 # Move a file to another location on the same filesystem.
319 self._check_move_file(self.src_file, self.dst_file, self.dst_file)
320
321 def test_move_file_to_dir(self):
322 # Move a file inside an existing dir on the same filesystem.
323 self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
324
325 def test_move_file_other_fs(self):
326 # Move a file to an existing dir on another filesystem.
327 if not self.dir_other_fs:
328 # skip
329 return
330 self._check_move_file(self.src_file, self.file_other_fs,
331 self.file_other_fs)
332
333 def test_move_file_to_dir_other_fs(self):
334 # Move a file to another location on another filesystem.
335 if not self.dir_other_fs:
336 # skip
337 return
338 self._check_move_file(self.src_file, self.dir_other_fs,
339 self.file_other_fs)
340
341 def test_move_dir(self):
342 # Move a dir to another location on the same filesystem.
343 dst_dir = tempfile.mktemp()
344 try:
345 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
346 finally:
347 try:
348 shutil.rmtree(dst_dir)
349 except:
350 pass
351
352 def test_move_dir_other_fs(self):
353 # Move a dir to another location on another filesystem.
354 if not self.dir_other_fs:
355 # skip
356 return
357 dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
358 try:
359 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
360 finally:
361 try:
362 shutil.rmtree(dst_dir)
363 except:
364 pass
365
366 def test_move_dir_to_dir(self):
367 # Move a dir inside an existing dir on the same filesystem.
368 self._check_move_dir(self.src_dir, self.dst_dir,
369 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
370
371 def test_move_dir_to_dir_other_fs(self):
372 # Move a dir inside an existing dir on another filesystem.
373 if not self.dir_other_fs:
374 # skip
375 return
376 self._check_move_dir(self.src_dir, self.dir_other_fs,
377 os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
378
379 def test_existing_file_inside_dest_dir(self):
380 # A file with the same name inside the destination dir already exists.
381 with open(self.dst_file, "wb"):
382 pass
383 self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
384
385 def test_dont_move_dir_in_itself(self):
386 # Moving a dir inside itself raises an Error.
387 dst = os.path.join(self.src_dir, "bar")
388 self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
389
Antoine Pitrou707c5932009-01-29 20:19:34 +0000390 def test_destinsrc_false_negative(self):
391 os.mkdir(TESTFN)
392 try:
393 for src, dst in [('srcdir', 'srcdir/dest')]:
394 src = os.path.join(TESTFN, src)
395 dst = os.path.join(TESTFN, dst)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000396 self.assertTrue(shutil._destinsrc(src, dst),
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000397 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou707c5932009-01-29 20:19:34 +0000398 'dst (%s) is not in src (%s)' % (dst, src))
399 finally:
400 shutil.rmtree(TESTFN, ignore_errors=True)
Sean Reifscheider493894c2008-03-18 17:24:12 +0000401
Antoine Pitrou707c5932009-01-29 20:19:34 +0000402 def test_destinsrc_false_positive(self):
403 os.mkdir(TESTFN)
404 try:
405 for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
406 src = os.path.join(TESTFN, src)
407 dst = os.path.join(TESTFN, dst)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000408 self.assertFalse(shutil._destinsrc(src, dst),
Benjamin Peterson096c3ad2009-02-07 19:08:22 +0000409 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou707c5932009-01-29 20:19:34 +0000410 'dst (%s) is in src (%s)' % (dst, src))
411 finally:
412 shutil.rmtree(TESTFN, ignore_errors=True)
Georg Brandl52353982008-01-20 14:17:42 +0000413
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000414def test_main():
Sean Reifscheider493894c2008-03-18 17:24:12 +0000415 test_support.run_unittest(TestShutil, TestMove)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000416
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000417if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000418 test_main()