blob: c28e5a3a4833757ef75071d9a0920b7a944d4899 [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
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'
22 and os.getenv('USER') != 'root'):
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)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000051 self.assertEqual(exc[0], OSError)
52 self.errorState = 1
53 else:
54 self.assertEqual(func, os.rmdir)
55 self.assertEqual(arg, TESTFN)
56 self.assertEqual(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
Brett Cannon1c3fa182004-06-19 21:11:35 +000066 def test_dont_move_dir_in_itself(self):
67 src_dir = tempfile.mkdtemp()
68 try:
69 dst = os.path.join(src_dir, 'foo')
70 self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
71 finally:
72 try:
73 os.rmdir(src_dir)
74 except:
75 pass
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000076
Johannes Gijsbers46f14592004-08-14 13:30:02 +000077 if hasattr(os, "symlink"):
78 def test_dont_copy_file_onto_link_to_itself(self):
79 # bug 851123.
80 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +000081 src = os.path.join(TESTFN, 'cheese')
82 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000083 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +000084 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000085 f.write('cheddar')
86 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +000087
88 os.link(src, dst)
89 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
90 self.assertEqual(open(src,'r').read(), 'cheddar')
91 os.remove(dst)
92
93 # Using `src` here would mean we end up with a symlink pointing
94 # to TESTFN/TESTFN/cheese, while it should point at
95 # TESTFN/cheese.
96 os.symlink('cheese', dst)
97 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
98 self.assertEqual(open(src,'r').read(), 'cheddar')
99 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000100 finally:
101 try:
102 shutil.rmtree(TESTFN)
103 except OSError:
104 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000105
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000106def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000107 test_support.run_unittest(TestShutil)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000108
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000109if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000110 test_main()