blob: 86c7b649e7aa766f5b7d8a52d5b19ecd4e53d0a4 [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
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000012
13class TestShutil(unittest.TestCase):
14 def test_rmtree_errors(self):
15 # filename is guaranteed not to exist
16 filename = tempfile.mktemp()
17 self.assertRaises(OSError, shutil.rmtree, filename)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000018
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000019 # See bug #1071513 for why we don't run this on cygwin
20 # and bug #1076467 for why we don't run this as root.
21 if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin'
Johannes Gijsbers6b220b02004-12-12 15:52:57 +000022 and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000023 def test_on_error(self):
24 self.errorState = 0
25 os.mkdir(TESTFN)
Tim Peters4590c002004-11-01 02:40:52 +000026 self.childpath = os.path.join(TESTFN, 'a')
27 f = open(self.childpath, 'w')
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000028 f.close()
Tim Peters4590c002004-11-01 02:40:52 +000029 old_dir_mode = os.stat(TESTFN).st_mode
30 old_child_mode = os.stat(self.childpath).st_mode
31 # Make unwritable.
32 os.chmod(self.childpath, stat.S_IREAD)
33 os.chmod(TESTFN, stat.S_IREAD)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000034
35 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000036 # Test whether onerror has actually been called.
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000037 self.assertEqual(self.errorState, 2,
38 "Expected call to onerror function did not happen.")
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000039
Tim Peters4590c002004-11-01 02:40:52 +000040 # Make writable again.
41 os.chmod(TESTFN, old_dir_mode)
42 os.chmod(self.childpath, old_child_mode)
43
44 # Clean up.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000045 shutil.rmtree(TESTFN)
46
47 def check_args_to_onerror(self, func, arg, exc):
48 if self.errorState == 0:
49 self.assertEqual(func, os.remove)
Tim Peters4590c002004-11-01 02:40:52 +000050 self.assertEqual(arg, self.childpath)
Thomas Wouters477c8d52006-05-27 19:21:47 +000051 self.failUnless(issubclass(exc[0], OSError))
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000052 self.errorState = 1
53 else:
54 self.assertEqual(func, os.rmdir)
55 self.assertEqual(arg, TESTFN)
Thomas Wouters477c8d52006-05-27 19:21:47 +000056 self.failUnless(issubclass(exc[0], OSError))
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000057 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000058
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000059 def test_rmtree_dont_delete_file(self):
60 # When called on a file instead of a directory, don't delete it.
61 handle, path = tempfile.mkstemp()
62 os.fdopen(handle).close()
63 self.assertRaises(OSError, shutil.rmtree, path)
64 os.remove(path)
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066 def test_copytree_simple(self):
67 def write_data(path, data):
68 f = open(path, "w")
69 f.write(data)
70 f.close()
71
72 def read_data(path):
73 f = open(path)
74 data = f.read()
75 f.close()
76 return data
77
78 src_dir = tempfile.mkdtemp()
79 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
80
81 write_data(os.path.join(src_dir, 'test.txt'), '123')
82
83 os.mkdir(os.path.join(src_dir, 'test_dir'))
84 write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
85
86 try:
87 shutil.copytree(src_dir, dst_dir)
88 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
89 self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
90 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
91 'test.txt')))
92 actual = read_data(os.path.join(dst_dir, 'test.txt'))
93 self.assertEqual(actual, '123')
94 actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
95 self.assertEqual(actual, '456')
96 finally:
97 for path in (
98 os.path.join(src_dir, 'test.txt'),
99 os.path.join(dst_dir, 'test.txt'),
100 os.path.join(src_dir, 'test_dir', 'test.txt'),
101 os.path.join(dst_dir, 'test_dir', 'test.txt'),
102 ):
103 if os.path.exists(path):
104 os.remove(path)
Christian Heimese052dd82007-11-20 03:20:04 +0000105 for path in (src_dir,
106 os.path.abspath(os.path.join(dst_dir, os.path.pardir))
107 ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000108 if os.path.exists(path):
Christian Heimes94140152007-11-20 01:45:17 +0000109 shutil.rmtree(path)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110
Georg Brandl2ee470f2008-07-16 12:55:28 +0000111 def test_copytree_with_exclude(self):
112
113 def write_data(path, data):
114 f = open(path, "w")
115 f.write(data)
116 f.close()
117
118 def read_data(path):
119 f = open(path)
120 data = f.read()
121 f.close()
122 return data
123
124 # creating data
125 join = os.path.join
126 exists = os.path.exists
127 src_dir = tempfile.mkdtemp()
128 dst_dir = join(tempfile.mkdtemp(), 'destination')
129 write_data(join(src_dir, 'test.txt'), '123')
130 write_data(join(src_dir, 'test.tmp'), '123')
131 os.mkdir(join(src_dir, 'test_dir'))
132 write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
133 os.mkdir(join(src_dir, 'test_dir2'))
134 write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
135 os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
136 os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
137 write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456')
138 write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456')
139
140
141 # testing glob-like patterns
142 try:
143 patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
144 shutil.copytree(src_dir, dst_dir, ignore=patterns)
145 # checking the result: some elements should not be copied
146 self.assert_(exists(join(dst_dir, 'test.txt')))
147 self.assert_(not exists(join(dst_dir, 'test.tmp')))
148 self.assert_(not exists(join(dst_dir, 'test_dir2')))
149 finally:
150 if os.path.exists(dst_dir):
151 shutil.rmtree(dst_dir)
152 try:
153 patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
154 shutil.copytree(src_dir, dst_dir, ignore=patterns)
155 # checking the result: some elements should not be copied
156 self.assert_(not exists(join(dst_dir, 'test.tmp')))
157 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
158 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir')))
159 finally:
160 if os.path.exists(dst_dir):
161 shutil.rmtree(dst_dir)
162
163 # testing callable-style
164 try:
165 def _filter(src, names):
166 res = []
167 for name in names:
168 path = os.path.join(src, name)
169
170 if (os.path.isdir(path) and
171 path.split()[-1] == 'subdir'):
172 res.append(name)
173 elif os.path.splitext(path)[-1] in ('.py'):
174 res.append(name)
175 return res
176
177 shutil.copytree(src_dir, dst_dir, ignore=_filter)
178
179 # checking the result: some elements should not be copied
180 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2',
181 'test.py')))
182 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir')))
183
184 finally:
185 if os.path.exists(dst_dir):
186 shutil.rmtree(dst_dir)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000187
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000188 if hasattr(os, "symlink"):
189 def test_dont_copy_file_onto_link_to_itself(self):
190 # bug 851123.
191 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000192 src = os.path.join(TESTFN, 'cheese')
193 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000194 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000195 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000196 f.write('cheddar')
197 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000198
199 os.link(src, dst)
200 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
201 self.assertEqual(open(src,'r').read(), 'cheddar')
202 os.remove(dst)
203
204 # Using `src` here would mean we end up with a symlink pointing
205 # to TESTFN/TESTFN/cheese, while it should point at
206 # TESTFN/cheese.
207 os.symlink('cheese', dst)
208 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
209 self.assertEqual(open(src,'r').read(), 'cheddar')
210 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000211 finally:
212 try:
213 shutil.rmtree(TESTFN)
214 except OSError:
215 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000216
Christian Heimes9bd667a2008-01-20 15:14:11 +0000217 def test_rmtree_on_symlink(self):
218 # bug 1669.
219 os.mkdir(TESTFN)
220 try:
221 src = os.path.join(TESTFN, 'cheese')
222 dst = os.path.join(TESTFN, 'shop')
223 os.mkdir(src)
224 os.symlink(src, dst)
225 self.assertRaises(OSError, shutil.rmtree, dst)
226 finally:
227 shutil.rmtree(TESTFN, ignore_errors=True)
228
229
Christian Heimesada8c3b2008-03-18 18:26:33 +0000230class TestMove(unittest.TestCase):
231
232 def setUp(self):
233 filename = "foo"
234 self.src_dir = tempfile.mkdtemp()
235 self.dst_dir = tempfile.mkdtemp()
236 self.src_file = os.path.join(self.src_dir, filename)
237 self.dst_file = os.path.join(self.dst_dir, filename)
238 # Try to create a dir in the current directory, hoping that it is
239 # not located on the same filesystem as the system tmp dir.
240 try:
241 self.dir_other_fs = tempfile.mkdtemp(
242 dir=os.path.dirname(__file__))
243 self.file_other_fs = os.path.join(self.dir_other_fs,
244 filename)
245 except OSError:
246 self.dir_other_fs = None
247 with open(self.src_file, "wb") as f:
248 f.write(b"spam")
249
250 def tearDown(self):
251 for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
252 try:
253 if d:
254 shutil.rmtree(d)
255 except:
256 pass
257
258 def _check_move_file(self, src, dst, real_dst):
259 contents = open(src, "rb").read()
260 shutil.move(src, dst)
261 self.assertEqual(contents, open(real_dst, "rb").read())
262 self.assertFalse(os.path.exists(src))
263
264 def _check_move_dir(self, src, dst, real_dst):
265 contents = sorted(os.listdir(src))
266 shutil.move(src, dst)
267 self.assertEqual(contents, sorted(os.listdir(real_dst)))
268 self.assertFalse(os.path.exists(src))
269
270 def test_move_file(self):
271 # Move a file to another location on the same filesystem.
272 self._check_move_file(self.src_file, self.dst_file, self.dst_file)
273
274 def test_move_file_to_dir(self):
275 # Move a file inside an existing dir on the same filesystem.
276 self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
277
278 def test_move_file_other_fs(self):
279 # Move a file to an existing dir on another filesystem.
280 if not self.dir_other_fs:
281 # skip
282 return
283 self._check_move_file(self.src_file, self.file_other_fs,
284 self.file_other_fs)
285
286 def test_move_file_to_dir_other_fs(self):
287 # Move a file to another location on another filesystem.
288 if not self.dir_other_fs:
289 # skip
290 return
291 self._check_move_file(self.src_file, self.dir_other_fs,
292 self.file_other_fs)
293
294 def test_move_dir(self):
295 # Move a dir to another location on the same filesystem.
296 dst_dir = tempfile.mktemp()
297 try:
298 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
299 finally:
300 try:
301 shutil.rmtree(dst_dir)
302 except:
303 pass
304
305 def test_move_dir_other_fs(self):
306 # Move a dir to another location on another filesystem.
307 if not self.dir_other_fs:
308 # skip
309 return
310 dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
311 try:
312 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
313 finally:
314 try:
315 shutil.rmtree(dst_dir)
316 except:
317 pass
318
319 def test_move_dir_to_dir(self):
320 # Move a dir inside an existing dir on the same filesystem.
321 self._check_move_dir(self.src_dir, self.dst_dir,
322 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
323
324 def test_move_dir_to_dir_other_fs(self):
325 # Move a dir inside an existing dir on another filesystem.
326 if not self.dir_other_fs:
327 # skip
328 return
329 self._check_move_dir(self.src_dir, self.dir_other_fs,
330 os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
331
332 def test_existing_file_inside_dest_dir(self):
333 # A file with the same name inside the destination dir already exists.
334 with open(self.dst_file, "wb"):
335 pass
336 self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
337
338 def test_dont_move_dir_in_itself(self):
339 # Moving a dir inside itself raises an Error.
340 dst = os.path.join(self.src_dir, "bar")
341 self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
342
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000343 def test_destinsrc_false_negative(self):
344 os.mkdir(TESTFN)
345 try:
346 for src, dst in [('srcdir', 'srcdir/dest')]:
347 src = os.path.join(TESTFN, src)
348 dst = os.path.join(TESTFN, dst)
Benjamin Peterson247a9b82009-02-20 04:09:19 +0000349 self.assert_(shutil._destinsrc(src, dst),
350 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000351 'dst (%s) is not in src (%s)' % (dst, src))
352 finally:
353 shutil.rmtree(TESTFN, ignore_errors=True)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000354
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000355 def test_destinsrc_false_positive(self):
356 os.mkdir(TESTFN)
357 try:
358 for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
359 src = os.path.join(TESTFN, src)
360 dst = os.path.join(TESTFN, dst)
Benjamin Peterson247a9b82009-02-20 04:09:19 +0000361 self.failIf(shutil._destinsrc(src, dst),
362 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000363 'dst (%s) is in src (%s)' % (dst, src))
364 finally:
365 shutil.rmtree(TESTFN, ignore_errors=True)
Christian Heimes9bd667a2008-01-20 15:14:11 +0000366
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000367def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000368 support.run_unittest(TestShutil, TestMove)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000369
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000370if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000371 test_main()