blob: cdb039d4d07f2ecb3da659dbf12f033c61b01347 [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +000010from test import support
11from test.support import TESTFN
Antoine Pitrou7fff0962009-05-01 21:09:44 +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):
49 if self.errorState == 0:
50 self.assertEqual(func, os.remove)
Tim Peters4590c002004-11-01 02:40:52 +000051 self.assertEqual(arg, self.childpath)
Thomas Wouters477c8d52006-05-27 19:21:47 +000052 self.failUnless(issubclass(exc[0], OSError))
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000053 self.errorState = 1
54 else:
55 self.assertEqual(func, os.rmdir)
56 self.assertEqual(arg, TESTFN)
Thomas Wouters477c8d52006-05-27 19:21:47 +000057 self.failUnless(issubclass(exc[0], OSError))
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000058 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000059
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000060 def test_rmtree_dont_delete_file(self):
61 # When called on a file instead of a directory, don't delete it.
62 handle, path = tempfile.mkstemp()
63 os.fdopen(handle).close()
64 self.assertRaises(OSError, shutil.rmtree, path)
65 os.remove(path)
66
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 def test_copytree_simple(self):
68 def write_data(path, data):
69 f = open(path, "w")
70 f.write(data)
71 f.close()
72
73 def read_data(path):
74 f = open(path)
75 data = f.read()
76 f.close()
77 return data
78
79 src_dir = tempfile.mkdtemp()
80 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
81
82 write_data(os.path.join(src_dir, 'test.txt'), '123')
83
84 os.mkdir(os.path.join(src_dir, 'test_dir'))
85 write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
86
87 try:
88 shutil.copytree(src_dir, dst_dir)
89 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
90 self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
91 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
92 'test.txt')))
93 actual = read_data(os.path.join(dst_dir, 'test.txt'))
94 self.assertEqual(actual, '123')
95 actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
96 self.assertEqual(actual, '456')
97 finally:
98 for path in (
99 os.path.join(src_dir, 'test.txt'),
100 os.path.join(dst_dir, 'test.txt'),
101 os.path.join(src_dir, 'test_dir', 'test.txt'),
102 os.path.join(dst_dir, 'test_dir', 'test.txt'),
103 ):
104 if os.path.exists(path):
105 os.remove(path)
Christian Heimese052dd82007-11-20 03:20:04 +0000106 for path in (src_dir,
107 os.path.abspath(os.path.join(dst_dir, os.path.pardir))
108 ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000109 if os.path.exists(path):
Christian Heimes94140152007-11-20 01:45:17 +0000110 shutil.rmtree(path)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111
Georg Brandl2ee470f2008-07-16 12:55:28 +0000112 def test_copytree_with_exclude(self):
113
114 def write_data(path, data):
115 f = open(path, "w")
116 f.write(data)
117 f.close()
118
119 def read_data(path):
120 f = open(path)
121 data = f.read()
122 f.close()
123 return data
124
125 # creating data
126 join = os.path.join
127 exists = os.path.exists
128 src_dir = tempfile.mkdtemp()
129 dst_dir = join(tempfile.mkdtemp(), 'destination')
130 write_data(join(src_dir, 'test.txt'), '123')
131 write_data(join(src_dir, 'test.tmp'), '123')
132 os.mkdir(join(src_dir, 'test_dir'))
133 write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
134 os.mkdir(join(src_dir, 'test_dir2'))
135 write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
136 os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
137 os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
138 write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456')
139 write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456')
140
141
142 # testing glob-like patterns
143 try:
144 patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
145 shutil.copytree(src_dir, dst_dir, ignore=patterns)
146 # checking the result: some elements should not be copied
147 self.assert_(exists(join(dst_dir, 'test.txt')))
148 self.assert_(not exists(join(dst_dir, 'test.tmp')))
149 self.assert_(not exists(join(dst_dir, 'test_dir2')))
150 finally:
151 if os.path.exists(dst_dir):
152 shutil.rmtree(dst_dir)
153 try:
154 patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
155 shutil.copytree(src_dir, dst_dir, ignore=patterns)
156 # checking the result: some elements should not be copied
157 self.assert_(not exists(join(dst_dir, 'test.tmp')))
158 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
159 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir')))
160 finally:
161 if os.path.exists(dst_dir):
162 shutil.rmtree(dst_dir)
163
164 # testing callable-style
165 try:
166 def _filter(src, names):
167 res = []
168 for name in names:
169 path = os.path.join(src, name)
170
171 if (os.path.isdir(path) and
172 path.split()[-1] == 'subdir'):
173 res.append(name)
174 elif os.path.splitext(path)[-1] in ('.py'):
175 res.append(name)
176 return res
177
178 shutil.copytree(src_dir, dst_dir, ignore=_filter)
179
180 # checking the result: some elements should not be copied
181 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2',
182 'test.py')))
183 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir')))
184
185 finally:
186 if os.path.exists(dst_dir):
187 shutil.rmtree(dst_dir)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000188
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000189 if hasattr(os, "symlink"):
190 def test_dont_copy_file_onto_link_to_itself(self):
191 # bug 851123.
192 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000193 src = os.path.join(TESTFN, 'cheese')
194 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000195 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000196 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000197 f.write('cheddar')
198 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000199
200 os.link(src, dst)
201 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
202 self.assertEqual(open(src,'r').read(), 'cheddar')
203 os.remove(dst)
204
205 # Using `src` here would mean we end up with a symlink pointing
206 # to TESTFN/TESTFN/cheese, while it should point at
207 # TESTFN/cheese.
208 os.symlink('cheese', dst)
209 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
210 self.assertEqual(open(src,'r').read(), 'cheddar')
211 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000212 finally:
213 try:
214 shutil.rmtree(TESTFN)
215 except OSError:
216 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000217
Christian Heimes9bd667a2008-01-20 15:14:11 +0000218 def test_rmtree_on_symlink(self):
219 # bug 1669.
220 os.mkdir(TESTFN)
221 try:
222 src = os.path.join(TESTFN, 'cheese')
223 dst = os.path.join(TESTFN, 'shop')
224 os.mkdir(src)
225 os.symlink(src, dst)
226 self.assertRaises(OSError, shutil.rmtree, dst)
227 finally:
228 shutil.rmtree(TESTFN, ignore_errors=True)
229
Antoine Pitrou7fff0962009-05-01 21:09:44 +0000230 if hasattr(os, "mkfifo"):
231 # Issue #3002: copyfile and copytree block indefinitely on named pipes
232 def test_copyfile_named_pipe(self):
233 os.mkfifo(TESTFN)
234 try:
235 self.assertRaises(shutil.SpecialFileError,
236 shutil.copyfile, TESTFN, TESTFN2)
237 self.assertRaises(shutil.SpecialFileError,
238 shutil.copyfile, __file__, TESTFN)
239 finally:
240 os.remove(TESTFN)
241
242 def test_copytree_named_pipe(self):
243 os.mkdir(TESTFN)
244 try:
245 subdir = os.path.join(TESTFN, "subdir")
246 os.mkdir(subdir)
247 pipe = os.path.join(subdir, "mypipe")
248 os.mkfifo(pipe)
249 try:
250 shutil.copytree(TESTFN, TESTFN2)
251 except shutil.Error as e:
252 errors = e.args[0]
253 self.assertEqual(len(errors), 1)
254 src, dst, error_msg = errors[0]
255 self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
256 else:
257 self.fail("shutil.Error should have been raised")
258 finally:
259 shutil.rmtree(TESTFN, ignore_errors=True)
260 shutil.rmtree(TESTFN2, ignore_errors=True)
261
Christian Heimes9bd667a2008-01-20 15:14:11 +0000262
Christian Heimesada8c3b2008-03-18 18:26:33 +0000263class TestMove(unittest.TestCase):
264
265 def setUp(self):
266 filename = "foo"
267 self.src_dir = tempfile.mkdtemp()
268 self.dst_dir = tempfile.mkdtemp()
269 self.src_file = os.path.join(self.src_dir, filename)
270 self.dst_file = os.path.join(self.dst_dir, filename)
271 # Try to create a dir in the current directory, hoping that it is
272 # not located on the same filesystem as the system tmp dir.
273 try:
274 self.dir_other_fs = tempfile.mkdtemp(
275 dir=os.path.dirname(__file__))
276 self.file_other_fs = os.path.join(self.dir_other_fs,
277 filename)
278 except OSError:
279 self.dir_other_fs = None
280 with open(self.src_file, "wb") as f:
281 f.write(b"spam")
282
283 def tearDown(self):
284 for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
285 try:
286 if d:
287 shutil.rmtree(d)
288 except:
289 pass
290
291 def _check_move_file(self, src, dst, real_dst):
292 contents = open(src, "rb").read()
293 shutil.move(src, dst)
294 self.assertEqual(contents, open(real_dst, "rb").read())
295 self.assertFalse(os.path.exists(src))
296
297 def _check_move_dir(self, src, dst, real_dst):
298 contents = sorted(os.listdir(src))
299 shutil.move(src, dst)
300 self.assertEqual(contents, sorted(os.listdir(real_dst)))
301 self.assertFalse(os.path.exists(src))
302
303 def test_move_file(self):
304 # Move a file to another location on the same filesystem.
305 self._check_move_file(self.src_file, self.dst_file, self.dst_file)
306
307 def test_move_file_to_dir(self):
308 # Move a file inside an existing dir on the same filesystem.
309 self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
310
311 def test_move_file_other_fs(self):
312 # Move a file to an existing dir on another filesystem.
313 if not self.dir_other_fs:
314 # skip
315 return
316 self._check_move_file(self.src_file, self.file_other_fs,
317 self.file_other_fs)
318
319 def test_move_file_to_dir_other_fs(self):
320 # Move a file to another location on another filesystem.
321 if not self.dir_other_fs:
322 # skip
323 return
324 self._check_move_file(self.src_file, self.dir_other_fs,
325 self.file_other_fs)
326
327 def test_move_dir(self):
328 # Move a dir to another location on the same filesystem.
329 dst_dir = tempfile.mktemp()
330 try:
331 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
332 finally:
333 try:
334 shutil.rmtree(dst_dir)
335 except:
336 pass
337
338 def test_move_dir_other_fs(self):
339 # Move a dir to another location on another filesystem.
340 if not self.dir_other_fs:
341 # skip
342 return
343 dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
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_to_dir(self):
353 # Move a dir inside an existing dir on the same filesystem.
354 self._check_move_dir(self.src_dir, self.dst_dir,
355 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
356
357 def test_move_dir_to_dir_other_fs(self):
358 # Move a dir inside an existing dir on another filesystem.
359 if not self.dir_other_fs:
360 # skip
361 return
362 self._check_move_dir(self.src_dir, self.dir_other_fs,
363 os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
364
365 def test_existing_file_inside_dest_dir(self):
366 # A file with the same name inside the destination dir already exists.
367 with open(self.dst_file, "wb"):
368 pass
369 self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
370
371 def test_dont_move_dir_in_itself(self):
372 # Moving a dir inside itself raises an Error.
373 dst = os.path.join(self.src_dir, "bar")
374 self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
375
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000376 def test_destinsrc_false_negative(self):
377 os.mkdir(TESTFN)
378 try:
379 for src, dst in [('srcdir', 'srcdir/dest')]:
380 src = os.path.join(TESTFN, src)
381 dst = os.path.join(TESTFN, dst)
Benjamin Peterson247a9b82009-02-20 04:09:19 +0000382 self.assert_(shutil._destinsrc(src, dst),
383 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000384 'dst (%s) is not in src (%s)' % (dst, src))
385 finally:
386 shutil.rmtree(TESTFN, ignore_errors=True)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000387
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000388 def test_destinsrc_false_positive(self):
389 os.mkdir(TESTFN)
390 try:
391 for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
392 src = os.path.join(TESTFN, src)
393 dst = os.path.join(TESTFN, dst)
Benjamin Peterson247a9b82009-02-20 04:09:19 +0000394 self.failIf(shutil._destinsrc(src, dst),
395 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000396 'dst (%s) is in src (%s)' % (dst, src))
397 finally:
398 shutil.rmtree(TESTFN, ignore_errors=True)
Christian Heimes9bd667a2008-01-20 15:14:11 +0000399
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000400def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000401 support.run_unittest(TestShutil, TestMove)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000402
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000403if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000404 test_main()