Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
[python.git] / Lib / test / test_shutil.py
blob6226f9b61c1e1785e9f12e0295290f1b3b0ec405
1 # Copyright (C) 2003 Python Software Foundation
3 import unittest
4 import shutil
5 import tempfile
6 import sys
7 import stat
8 import os
9 import os.path
10 from test import test_support
11 from test.test_support import TESTFN
12 TESTFN2 = TESTFN + "2"
14 class 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)
20 # 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'
23 and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
24 def test_on_error(self):
25 self.errorState = 0
26 os.mkdir(TESTFN)
27 self.childpath = os.path.join(TESTFN, 'a')
28 f = open(self.childpath, 'w')
29 f.close()
30 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)
36 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
37 # Test whether onerror has actually been called.
38 self.assertEqual(self.errorState, 2,
39 "Expected call to onerror function did not happen.")
41 # Make writable again.
42 os.chmod(TESTFN, old_dir_mode)
43 os.chmod(self.childpath, old_child_mode)
45 # Clean up.
46 shutil.rmtree(TESTFN)
48 def check_args_to_onerror(self, func, arg, exc):
49 # 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.
59 if self.errorState == 0:
60 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)
66 self.failUnless(issubclass(exc[0], OSError))
67 self.errorState = 1
68 else:
69 self.assertEqual(func, os.rmdir)
70 self.assertEqual(arg, TESTFN)
71 self.failUnless(issubclass(exc[0], OSError))
72 self.errorState = 2
74 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)
81 def test_copytree_simple(self):
82 def write_data(path, data):
83 f = open(path, "w")
84 f.write(data)
85 f.close()
87 def read_data(path):
88 f = open(path)
89 data = f.read()
90 f.close()
91 return data
93 src_dir = tempfile.mkdtemp()
94 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
96 write_data(os.path.join(src_dir, 'test.txt'), '123')
98 os.mkdir(os.path.join(src_dir, 'test_dir'))
99 write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
101 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')))
105 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')
111 finally:
112 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'),
118 if os.path.exists(path):
119 os.remove(path)
120 for path in (src_dir,
121 os.path.abspath(os.path.join(dst_dir, os.path.pardir))
123 if os.path.exists(path):
124 shutil.rmtree(path)
126 def test_copytree_with_exclude(self):
128 def write_data(path, data):
129 f = open(path, "w")
130 f.write(data)
131 f.close()
133 def read_data(path):
134 f = open(path)
135 data = f.read()
136 f.close()
137 return data
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')
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
161 self.assert_(exists(join(dst_dir, 'test.txt')))
162 self.assert_(not exists(join(dst_dir, 'test.tmp')))
163 self.assert_(not exists(join(dst_dir, 'test_dir2')))
164 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
171 self.assert_(not exists(join(dst_dir, 'test.tmp')))
172 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
173 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir')))
174 finally:
175 if os.path.exists(dst_dir):
176 shutil.rmtree(dst_dir)
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)
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
192 shutil.copytree(src_dir, dst_dir, ignore=_filter)
194 # checking the result: some elements should not be copied
195 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2',
196 'test.py')))
197 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir')))
199 finally:
200 if os.path.exists(dst_dir):
201 shutil.rmtree(dst_dir)
203 if hasattr(os, "symlink"):
204 def test_dont_copy_file_onto_link_to_itself(self):
205 # bug 851123.
206 os.mkdir(TESTFN)
207 src = os.path.join(TESTFN, 'cheese')
208 dst = os.path.join(TESTFN, 'shop')
209 try:
210 f = open(src, 'w')
211 f.write('cheddar')
212 f.close()
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)
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)
226 finally:
227 try:
228 shutil.rmtree(TESTFN)
229 except OSError:
230 pass
232 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)
244 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)
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)
277 class TestMove(unittest.TestCase):
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")
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
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))
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))
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)
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)
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)
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)
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
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
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)))
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)))
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)
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)
390 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)
396 self.assert_(shutil._destinsrc(src, dst),
397 msg='_destinsrc() wrongly concluded that '
398 'dst (%s) is not in src (%s)' % (dst, src))
399 finally:
400 shutil.rmtree(TESTFN, ignore_errors=True)
402 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)
408 self.failIf(shutil._destinsrc(src, dst),
409 msg='_destinsrc() wrongly concluded that '
410 'dst (%s) is in src (%s)' % (dst, src))
411 finally:
412 shutil.rmtree(TESTFN, ignore_errors=True)
414 def test_main():
415 test_support.run_unittest(TestShutil, TestMove)
417 if __name__ == '__main__':
418 test_main()